]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - keyboard/gh60/matrix.c
core: Clean up code of Locking key support
[max/tmk_keyboard.git] / keyboard / gh60 / matrix.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19  * scan matrix
20  */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include <util/delay.h>
25 #include "print.h"
26 #include "debug.h"
27 #include "util.h"
28 #include "timer.h"
29 #include "matrix.h"
30
31
32 #ifndef DEBOUNCE
33 #   define DEBOUNCE     5
34 #endif
35 static bool debouncing = false;
36 static uint16_t debouncing_time = 0;
37
38
39 /* matrix state(1:on, 0:off) */
40 static matrix_row_t matrix[MATRIX_ROWS];
41 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
42
43 static matrix_row_t read_cols(void);
44 static void init_cols(void);
45 static void unselect_rows(void);
46 static void select_row(uint8_t row);
47
48
49 void matrix_init(void)
50 {
51     // initialize row and col
52     unselect_rows();
53     init_cols();
54
55     // initialize matrix state: all keys off
56     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
57         matrix[i] = 0;
58         matrix_debouncing[i] = 0;
59     }
60 }
61
62 uint8_t matrix_scan(void)
63 {
64     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
65         select_row(i);
66         _delay_us(1);  // delay for settling
67         matrix_row_t cols = read_cols();
68         if (matrix_debouncing[i] != cols) {
69             if (debouncing) {
70                 dprintf("bounce: %d %d@%02X\n", timer_elapsed(debouncing_time), i, matrix_debouncing[i]^cols);
71             }
72             matrix_debouncing[i] = cols;
73             debouncing = true;
74             debouncing_time = timer_read();
75         }
76         unselect_rows();
77     }
78
79     if (debouncing && timer_elapsed(debouncing_time) >= DEBOUNCE) {
80         for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
81             matrix[i] = matrix_debouncing[i];
82         }
83         debouncing = false;
84     }
85
86     return 1;
87 }
88
89 inline
90 matrix_row_t matrix_get_row(uint8_t row)
91 {
92     return matrix[row];
93 }
94
95 /* Column pin configuration
96  * col: 0   1   2   3   4   5   6   7   8   9   10  11  12  13
97  * pin: F0  F1  E6  C7  C6  B6  D4  B1  B0  B5  B4  D7  D6  B3  (Rev.A)
98  * pin:                                 B7                      (Rev.B)
99  */
100 static void  init_cols(void)
101 {
102     // Input with pull-up(DDR:0, PORT:1)
103     DDRF  &= ~(1<<0 | 1<<1);
104     PORTF |=  (1<<0 | 1<<1);
105     DDRE  &= ~(1<<6);
106     PORTE |=  (1<<6);
107     DDRD  &= ~(1<<7 | 1<<6 | 1<<4);
108     PORTD |=  (1<<7 | 1<<6 | 1<<4);
109     DDRC  &= ~(1<<7 | 1<<6);
110     PORTC |=  (1<<7 | 1<<6);
111     DDRB  &= ~(1<<7 | 1<<6 | 1<< 5 | 1<<4 | 1<<3 | 1<<1 | 1<<0);
112     PORTB |=  (1<<7 | 1<<6 | 1<< 5 | 1<<4 | 1<<3 | 1<<1 | 1<<0);
113 }
114
115 static matrix_row_t read_cols(void)
116 {
117     return (PINF&(1<<0) ? 0 : (1<<0)) |
118            (PINF&(1<<1) ? 0 : (1<<1)) |
119            (PINE&(1<<6) ? 0 : (1<<2)) |
120            (PINC&(1<<7) ? 0 : (1<<3)) |
121            (PINC&(1<<6) ? 0 : (1<<4)) |
122            (PINB&(1<<6) ? 0 : (1<<5)) |
123            (PIND&(1<<4) ? 0 : (1<<6)) |
124            (PINB&(1<<1) ? 0 : (1<<7)) |
125            ((PINB&(1<<0) && PINB&(1<<7)) ? 0 : (1<<8)) |     // Rev.A and B
126            (PINB&(1<<5) ? 0 : (1<<9)) |
127            (PINB&(1<<4) ? 0 : (1<<10)) |
128            (PIND&(1<<7) ? 0 : (1<<11)) |
129            (PIND&(1<<6) ? 0 : (1<<12)) |
130            (PINB&(1<<3) ? 0 : (1<<13));
131 }
132
133 /* Row pin configuration
134  * row: 0   1   2   3   4
135  * pin: D0  D1  D2  D3  D5
136  */
137 static void unselect_rows(void)
138 {
139     // Hi-Z(DDR:0, PORT:0) to unselect
140     DDRD  &= ~0b00101111;
141     PORTD &= ~0b00101111;
142 }
143
144 static void select_row(uint8_t row)
145 {
146     // Output low(DDR:1, PORT:0) to select
147     switch (row) {
148         case 0:
149             DDRD  |= (1<<0);
150             PORTD &= ~(1<<0);
151             break;
152         case 1:
153             DDRD  |= (1<<1);
154             PORTD &= ~(1<<1);
155             break;
156         case 2:
157             DDRD  |= (1<<2);
158             PORTD &= ~(1<<2);
159             break;
160         case 3:
161             DDRD  |= (1<<3);
162             PORTD &= ~(1<<3);
163             break;
164         case 4:
165             DDRD  |= (1<<5);
166             PORTD &= ~(1<<5);
167             break;
168     }
169 }