]> git.gir.st - tmk_keyboard.git/blob - keyboard/IIgs_Standard/matrix.c
Power key support
[tmk_keyboard.git] / keyboard / IIgs_Standard / 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 #include "led.h"
30
31
32 #if (MATRIX_COLS > 16)
33 # error "MATRIX_COLS must not exceed 16"
34 #endif
35 #if (MATRIX_ROWS > 255)
36 # error "MATRIX_ROWS must not exceed 255"
37 #endif
38
39
40 #ifndef DEBOUNCE
41 # define DEBOUNCE 0
42 #endif
43 static uint8_t debouncing = DEBOUNCE;
44
45 // matrix state buffer(1:on, 0:off)
46 #if (MATRIX_COLS <= 8)
47 static uint8_t *matrix;
48 static uint8_t *matrix_prev;
49 static uint8_t _matrix0[MATRIX_ROWS];
50 static uint8_t _matrix1[MATRIX_ROWS];
51 #else
52 static uint16_t *matrix;
53 static uint16_t *matrix_prev;
54 static uint16_t _matrix0[MATRIX_ROWS];
55 static uint16_t _matrix1[MATRIX_ROWS];
56 #endif
57
58 #ifdef MATRIX_HAS_GHOST
59 static bool matrix_has_ghost_in_row(uint8_t row);
60 #endif
61 static uint8_t read_col(uint8_t row);
62 static void unselect_rows(void);
63 static void select_row(uint8_t row);
64
65
66 inline
67 uint8_t matrix_rows(void)
68 {
69 return MATRIX_ROWS;
70 }
71
72 inline
73 uint8_t matrix_cols(void)
74 {
75 return MATRIX_COLS;
76 }
77
78 void matrix_init(void)
79 {
80 // initialize row and col
81 unselect_rows();
82 // Input with pull-up(DDR:0, PORT:1)
83 // Column C1 ~ C7 (PortC0-6)
84 // Column C0(Port E1)
85 DDRC &= ~0b01111111;
86 PORTC |= 0b01111111;
87 DDRE &= ~0b00000010;
88 PORTE |= 0b00000010;
89 //DDRB &= ~0b00000100;
90 //PORTB |= 0b00000100;
91 // modifier B3/4,F4/5,E4 always input
92 // A0
93 //DDRA |= 0b00000001;
94 //PORTA &= 0b00000001;
95 //DDRB |= 0b00011000;
96 //PORTB &= 0b00011000;
97 //DDRF |= ~0b00110000;
98 //PORTF &= 0b00110000;
99 //DDRB &= ~0b00011000;
100 //PORTB |= 0b00011000;
101 //DDRF &= ~0b00110000;
102 //PORTF |= 0b00110000;
103 //DDRE &= ~0b00010000;
104 //PORTE |= 0b00010000;
105
106 // initialize matrix state: all keys off
107 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
108 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
109 matrix = _matrix0;
110 matrix_prev = _matrix1;
111 }
112
113 uint8_t matrix_scan(void)
114 {
115 if (!debouncing) {
116 uint8_t *tmp = matrix_prev;
117 matrix_prev = matrix;
118 matrix = tmp;
119 }
120
121 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
122 unselect_rows();
123 select_row(i);
124 _delay_us(30); // without this wait read unstable value.
125 if ( i == ( MATRIX_ROWS - 1 ) ) { // CHECK CAPS LOCK
126 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) { // CAPS LOCK is ON on HOST
127 if ( ~read_col(i) & (1<< 4) ) { // CAPS LOCK is still DOWN ( 0bXXX1_XXXX)
128 matrix[i] = ~read_col(i) & 0b11101111; // change CAPS LOCK as released
129 } else { // CAPS LOCK in UP
130 matrix[i] = ~read_col(i) | 0b00010000; // send fake caps lock down
131 }
132 } else { // CAPS LOCK is OFF on HOST
133 matrix[i] = ~read_col(i);
134 }
135 } else {
136 if (matrix[i] != (uint8_t)~read_col(i)) {
137 matrix[i] = (uint8_t)~read_col(i);
138 }
139 }
140 if (debouncing) {
141 debug("bounce!: "); debug_hex(debouncing); print("\n");
142 }
143 debouncing = DEBOUNCE;
144 }
145 unselect_rows();
146
147 if (debouncing) {
148 debouncing--;
149 }
150
151 return 1;
152 }
153
154 bool matrix_is_modified(void)
155 {
156 if (debouncing) return false;
157 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
158 if (matrix[i] != matrix_prev[i]) {
159 return true;
160 }
161 }
162 return false;
163 }
164
165 inline
166 bool matrix_has_ghost(void)
167 {
168 #ifdef MATRIX_HAS_GHOST
169 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
170 if (matrix_has_ghost_in_row(i))
171 return true;
172 }
173 #endif
174 return false;
175 }
176
177 inline
178 bool matrix_is_on(uint8_t row, uint8_t col)
179 {
180 // if ( row == ( MATRIX_ROWS - 1 ) && col == 4) { // CHECK CAPS LOCK
181 // if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) { // CAPS LOCK is ON on HOST
182 // if ((matrix_prev[row] & 0b00010000) && (~matrix[row] & 0b00010000)) {
183 // debug("CapsLock Reverse:");debug_hex(matrix[row]);
184 // matrix[row] |= 0b00010000;
185 // matrix_prev[row] &= ~0b00010000;
186 // debug("->");debug_hex(matrix[row]);debug("\n");
187 // }
188 // }
189 // }
190 return (matrix[row] & (1<<col));
191 }
192
193 inline
194 #if (MATRIX_COLS <= 8)
195 uint8_t matrix_get_row(uint8_t row)
196 #else
197 uint16_t matrix_get_row(uint8_t row)
198 #endif
199 {
200 return matrix[row];
201 }
202
203 void matrix_print(void)
204 {
205 print("\nr/c 01234567\n");
206 for (uint8_t row = 0; row < matrix_rows(); row++) {
207 phex(row); print(": ");
208 #if (MATRIX_COLS <= 8)
209 pbin_reverse(matrix_get_row(row));
210 #else
211 pbin_reverse16(matrix_get_row(row));
212 #endif
213 #ifdef MATRIX_HAS_GHOST
214 if (matrix_has_ghost_in_row(row)) {
215 print(" <ghost");
216 }
217 #endif
218 print("\n");
219 }
220 }
221
222 uint8_t matrix_key_count(void)
223 {
224 uint8_t count = 0;
225 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
226 #if (MATRIX_COLS <= 8)
227 count += bitpop(matrix[i]);
228 #else
229 count += bitpop16(matrix[i]);
230 #endif
231 }
232 return count;
233 }
234
235 #ifdef MATRIX_HAS_GHOST
236 inline
237 static bool matrix_has_ghost_in_row(uint8_t row)
238 {
239 // no ghost exists in case less than 2 keys on
240 if (((matrix[row] - 1) & matrix[row]) == 0)
241 return false;
242
243 // ghost exists in case same state as other row
244 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
245 if (i != row && (matrix[i] & matrix[row]) == matrix[row])
246 return true;
247 }
248 return false;
249 }
250 #endif
251
252 inline
253 static uint8_t read_col(uint8_t row)
254 {
255 // For normal : Column C1 ~ C7 (PortC0-6), C0(Port E1)
256 // For modifier : B3(CNTRL)/4(SHIFT),F4(CMD/GUI)/5(OPTION,ALT)
257 // Modifier would be copied to report->mods except E4(CAPSLOCK)
258 uint8_t tmp;
259 if ( row == 10 ) {
260 tmp = 0xC0;
261 tmp |= (PINB >> 3 ) & 0b00000011; // LEFT CTRL is 0bit in modifier (HID Spec)
262 // LEFT SHIFT is 1bit in modifier (HID Spec)
263 tmp |= (PINF >> 3 ) & 0b00000100; // LEFT ALT is 2bit in modifier (HID Spec)
264 tmp |= (PINF >> 1 ) & 0b00001000; // LEFT GUI is 3bit in modifier (HID Spec)
265 tmp |= (PINA << 4 ) & 0b00010000; // CAPSLOCK
266 tmp |= (PINB << 3 ) & 0b00100000; // POWER
267 //tmp |= (PINE << 1 ) & 0b00010000; // Caps Lock(Should not be in modifier
268 } else {
269 tmp = 0x00;
270 tmp = (PINE >> 1)&0b00000001;
271 tmp |= PINC << 1 ;
272 }
273 return tmp;
274 }
275
276 inline
277 static void unselect_rows(void)
278 {
279 // Hi-Z(DDR:0, PORT:0) to unselect
280 // DDR : 1, output 0, input
281 DDRB &= ~0b00000011; // PB: 1,0
282 PORTB &= ~0b00000011;
283 DDRD &= ~0b00010000; // PD: 4
284 PORTD &= ~0b00010000;
285 DDRE &= ~0b11000000; // PE: 7,6
286 PORTE &= ~0b11000000;
287 DDRF &= ~0b11000111; // PF: 7,6,2,1,0
288 PORTF &= ~0b11000111;
289 // to unselect virtual row(modifier), set port to output with low
290 DDRA |= 0b00000001; // PA: 0 for CAPSLOCK
291 PORTA &= ~0b00000001;
292 DDRB |= 0b00011100; // PB: 3,4 for modifier(row10)
293 PORTB &= ~0b00011100; // PB: 2 for power
294 DDRF |= 0b00110000; // PF: 4,5 for modifier
295 PORTF &= ~0b00110000;
296 }
297
298 inline
299 static void select_row(uint8_t row)
300 {
301 // Output low(DDR:1, PORT:0) to select
302 // with row enable, column could send low to AVR when pressed
303 // row: 0 1 2 3 4 5 6 7 8 9
304 // pin: PB1, PB0, PE7, PE6, PD4, PF2, PF0, PF1, PF6 PF7
305 switch (row) {
306 case 0:
307 DDRB |= (1<<1);
308 PORTB &= ~(1<<1);
309 break;
310 case 1:
311 DDRB |= (1<<0);
312 PORTB &= ~(1<<0);
313 break;
314 case 2:
315 DDRE |= (1<<7);
316 PORTE &= ~(1<<7);
317 break;
318 case 3:
319 DDRE |= (1<<6);
320 PORTE &= ~(1<<6);
321 break;
322 case 4:
323 DDRD |= (1<<4);
324 PORTD &= ~(1<<4);
325 break;
326 case 5:
327 DDRF |= (1<<2);
328 PORTF &= ~(1<<2);
329 break;
330 case 6:
331 DDRF |= (1<<0);
332 PORTF &= ~(1<<0);
333 break;
334 case 7:
335 DDRF |= (1<<1);
336 PORTF &= ~(1<<1);
337 break;
338 case 8:
339 DDRF |= (1<<6);
340 PORTF &= ~(1<<6);
341 break;
342 case 9:
343 DDRF |= (1<<7);
344 PORTF &= ~(1<<7);
345 break;
346 case 10:
347 // modifier has no row enable
348 // to select virtual row, set port as input
349 DDRA &= ~0b00000001;
350 PORTA |= 0b00000001;
351 DDRB &= ~0b00011100;
352 PORTB |= 0b00011100;
353 DDRF &= ~0b00110000;
354 PORTF |= 0b00110000;
355 break;
356
357 }
358 }
Imprint / Impressum