]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/lufa/matrix.c
initial attempt for LUFA.
[tmk_firmware.git] / keyboard / lufa / matrix.c
1 /*
2 Copyright 2011 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 "matrix.h"
29
30
31 #if (MATRIX_COLS > 16)
32 #   error "MATRIX_COLS must not exceed 16"
33 #endif
34 #if (MATRIX_ROWS > 255)
35 #   error "MATRIX_ROWS must not exceed 255"
36 #endif
37
38
39 #ifndef DEBOUNCE
40 #   define DEBOUNCE     0
41 #endif
42 static uint8_t debouncing = DEBOUNCE;
43
44 // matrix state buffer(1:on, 0:off)
45 #if (MATRIX_COLS <= 8)
46 static uint8_t *matrix;
47 static uint8_t *matrix_prev;
48 static uint8_t _matrix0[MATRIX_ROWS];
49 static uint8_t _matrix1[MATRIX_ROWS];
50 #else
51 static uint16_t *matrix;
52 static uint16_t *matrix_prev;
53 static uint16_t _matrix0[MATRIX_ROWS];
54 static uint16_t _matrix1[MATRIX_ROWS];
55 #endif
56
57 #ifdef MATRIX_HAS_GHOST
58 static bool matrix_has_ghost_in_row(uint8_t row);
59 #endif
60 static uint8_t read_col(void);
61 static void unselect_rows(void);
62 static void select_row(uint8_t row);
63
64
65 inline
66 uint8_t matrix_rows(void)
67 {
68     return MATRIX_ROWS;
69 }
70
71 inline
72 uint8_t matrix_cols(void)
73 {
74     return MATRIX_COLS;
75 }
76
77 void matrix_init(void)
78 {
79     // initialize row and col
80     unselect_rows();
81     // Input with pull-up(DDR:0, PORT:1)
82     DDRB = 0x00;
83     PORTB = 0xFF;
84
85     // initialize matrix state: all keys off
86     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
87     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
88     matrix = _matrix0;
89     matrix_prev = _matrix1;
90 }
91
92 uint8_t matrix_scan(void)
93 {
94     if (!debouncing) {
95         uint8_t *tmp = matrix_prev;
96         matrix_prev = matrix;
97         matrix = tmp;
98     }
99
100     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
101         unselect_rows();
102         select_row(i);
103         _delay_us(30);  // without this wait read unstable value.
104         if (matrix[i] != (uint8_t)~read_col()) {
105             matrix[i] = (uint8_t)~read_col();
106             if (debouncing) {
107                 debug("bounce!: "); debug_hex(debouncing); print("\n");
108             }
109             debouncing = DEBOUNCE;
110         }
111     }
112     unselect_rows();
113
114     if (debouncing) {
115         debouncing--;
116     }
117
118     return 1;
119 }
120
121 bool matrix_is_modified(void)
122 {
123     if (debouncing) return false;
124     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
125         if (matrix[i] != matrix_prev[i]) {
126             return true;
127         }
128     }
129     return false;
130 }
131
132 inline
133 bool matrix_has_ghost(void)
134 {
135 #ifdef MATRIX_HAS_GHOST
136     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
137         if (matrix_has_ghost_in_row(i))
138             return true;
139     }
140 #endif
141     return false;
142 }
143
144 inline
145 bool matrix_is_on(uint8_t row, uint8_t col)
146 {
147     return (matrix[row] & (1<<col));
148 }
149
150 inline
151 #if (MATRIX_COLS <= 8)
152 uint8_t matrix_get_row(uint8_t row)
153 #else
154 uint16_t matrix_get_row(uint8_t row)
155 #endif
156 {
157     return matrix[row];
158 }
159
160 void matrix_print(void)
161 {
162     print("\nr/c 01234567\n");
163     for (uint8_t row = 0; row < matrix_rows(); row++) {
164         phex(row); print(": ");
165 #if (MATRIX_COLS <= 8)
166         pbin_reverse(matrix_get_row(row));
167 #else
168         pbin_reverse16(matrix_get_row(row));
169 #endif
170 #ifdef MATRIX_HAS_GHOST
171         if (matrix_has_ghost_in_row(row)) {
172             print(" <ghost");
173         }
174 #endif
175         print("\n");
176     }
177 }
178
179 uint8_t matrix_key_count(void)
180 {
181     uint8_t count = 0;
182     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
183 #if (MATRIX_COLS <= 8)
184         count += bitpop(matrix[i]);
185 #else
186         count += bitpop16(matrix[i]);
187 #endif
188     }
189     return count;
190 }
191
192 #ifdef MATRIX_HAS_GHOST
193 inline
194 static bool matrix_has_ghost_in_row(uint8_t row)
195 {
196     // no ghost exists in case less than 2 keys on
197     if (((matrix[row] - 1) & matrix[row]) == 0)
198         return false;
199
200     // ghost exists in case same state as other row
201     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
202         if (i != row && (matrix[i] & matrix[row]) == matrix[row])
203             return true;
204     }
205     return false;
206 }
207 #endif
208
209 inline
210 static uint8_t read_col(void)
211 {
212     return PINB;
213 }
214
215 inline
216 static void unselect_rows(void)
217 {
218     // Hi-Z(DDR:0, PORT:0) to unselect
219     DDRC  &= ~0b01000000; // PC: 6
220     PORTC &= ~0b11000000;
221     DDRD  &= ~0b11100111; // PD: 7,6,5,2,1,0
222     PORTD &= ~0b11000111;
223     DDRF  &= ~0b11000000; // PF: 7,6
224     PORTF &= ~0b11000000;
225 }
226
227 inline
228 static void select_row(uint8_t row)
229 {
230     // Output low(DDR:1, PORT:0) to select
231     // row: 0    1    2    3    4    5    6    7    8
232     // pin: PD0, PD5, PD7, PF6, PD6, PD1, PD2, PC6, PF7
233     switch (row) {
234         case 0:
235             DDRD  |= (1<<0);
236             PORTD &= ~(1<<0);
237             break;
238         case 1:
239             DDRD  |= (1<<5);
240             PORTD &= ~(1<<5);
241             break;
242         case 2:
243             DDRD  |= (1<<7);
244             PORTD &= ~(1<<7);
245             break;
246         case 3:
247             DDRF  |= (1<<6);
248             PORTF &= ~(1<<6);
249             break;
250         case 4:
251             DDRD  |= (1<<6);
252             PORTD &= ~(1<<6);
253             break;
254         case 5:
255             DDRD  |= (1<<1);
256             PORTD &= ~(1<<1);
257             break;
258         case 6:
259             DDRD  |= (1<<2);
260             PORTD &= ~(1<<2);
261             break;
262         case 7:
263             DDRC  |= (1<<6);
264             PORTC &= ~(1<<6);
265             break;
266         case 8:
267             DDRF  |= (1<<7);
268             PORTF &= ~(1<<7);
269             break;
270     }
271 }