]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/IIgs/matrix.c
Remove MCU dependent code from common/keyboard.c
[tmk_firmware.git] / keyboard / IIgs / 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                         if (matrix[i] != (uint8_t)~read_col(i)) {
134                                 matrix[i] = (uint8_t)~read_col(i);
135                                 if (debouncing) {
136                                         debug("bounce!: "); debug_hex(debouncing); print("\n");
137                                         }
138                                 debouncing = DEBOUNCE;
139                                 }
140                         }
141                 } else {
142                 if (matrix[i] != (uint8_t)~read_col(i)) {
143                         matrix[i] = (uint8_t)~read_col(i);
144                         if (debouncing) {
145                                 debug("bounce!: "); debug_hex(debouncing); print("\n");
146                                 }
147                         debouncing = DEBOUNCE;
148                         }
149                 }
150         }
151     unselect_rows();
152
153     if (debouncing) {
154         debouncing--;
155     }
156
157     return 1;
158 }
159
160 bool matrix_is_modified(void)
161 {
162     if (debouncing) return false;
163     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
164         if (matrix[i] != matrix_prev[i]) {
165             return true;
166         }
167     }
168     return false;
169 }
170
171 inline
172 bool matrix_has_ghost(void)
173 {
174 #ifdef MATRIX_HAS_GHOST
175     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
176         if (matrix_has_ghost_in_row(i))
177             return true;
178     }
179 #endif
180     return false;
181 }
182
183 inline
184 bool matrix_is_on(uint8_t row, uint8_t col)
185 {
186 //      if ( row == ( MATRIX_ROWS - 1 ) && col == 4) {                                                  // CHECK CAPS LOCK
187 //      if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) {            // CAPS LOCK is ON on HOST
188 //                      if ((matrix_prev[row] & 0b00010000) && (~matrix[row] & 0b00010000)) {
189 //                              debug("CapsLock Reverse:");debug_hex(matrix[row]);
190 //                              matrix[row] |= 0b00010000;
191 //                              matrix_prev[row] &= ~0b00010000;
192 //                              debug("->");debug_hex(matrix[row]);debug("\n");
193 //                      }
194 //              } 
195 //      }
196         return (matrix[row] & (1<<col));
197 }
198
199 inline
200 #if (MATRIX_COLS <= 8)
201 uint8_t matrix_get_row(uint8_t row)
202 #else
203 uint16_t matrix_get_row(uint8_t row)
204 #endif
205 {
206     return matrix[row];
207 }
208
209 void matrix_print(void)
210 {
211     print("\nr/c 01234567\n");
212     for (uint8_t row = 0; row < matrix_rows(); row++) {
213         phex(row); print(": ");
214 #if (MATRIX_COLS <= 8)
215         pbin_reverse(matrix_get_row(row));
216 #else
217         pbin_reverse16(matrix_get_row(row));
218 #endif
219 #ifdef MATRIX_HAS_GHOST
220         if (matrix_has_ghost_in_row(row)) {
221             print(" <ghost");
222         }
223 #endif
224         print("\n");
225     }
226 }
227
228 uint8_t matrix_key_count(void)
229 {
230     uint8_t count = 0;
231     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
232 #if (MATRIX_COLS <= 8)
233         count += bitpop(matrix[i]);
234 #else
235         count += bitpop16(matrix[i]);
236 #endif
237     }
238     return count;
239 }
240
241 #ifdef MATRIX_HAS_GHOST
242 inline
243 static bool matrix_has_ghost_in_row(uint8_t row)
244 {
245     // no ghost exists in case less than 2 keys on
246     if (((matrix[row] - 1) & matrix[row]) == 0)
247         return false;
248
249     // ghost exists in case same state as other row
250     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
251         if (i != row && (matrix[i] & matrix[row]) == matrix[row])
252             return true;
253     }
254     return false;
255 }
256 #endif
257
258 inline
259 static uint8_t read_col(uint8_t row)
260 {
261         // For normal   : Column C1 ~ C7 (PortC0-6), C0(Port E1)
262         // For modifier : B3(CNTRL)/4(SHIFT),F4(CMD/GUI)/5(OPTION,ALT)
263         // Modifier would be copied to report->mods except E4(CAPSLOCK)
264         uint8_t tmp;
265         if ( row == 10 ) {
266                 tmp = 0xC0;
267                 tmp |= (PINB >> 3 ) & 0b00000011;       // LEFT CTRL  is 0bit in modifier (HID Spec)
268                                                                                         // LEFT SHIFT is 1bit in modifier (HID Spec)
269                 tmp |= (PINF >> 3 ) & 0b00000100;       // LEFT ALT   is 2bit in modifier (HID Spec)
270                 tmp |= (PINF >> 1 ) & 0b00001000;       // LEFT GUI   is 3bit in modifier (HID Spec)
271                 tmp |= (PINA << 4 ) & 0b00010000;       // CAPSLOCK
272                 tmp |= (PINB << 3 ) & 0b00100000;       // POWER         
273         } else {
274                 tmp = 0x00;
275                 tmp = (PINE >> 1)&0b00000001; 
276                 tmp |= PINC << 1 ;
277         }
278         return tmp;
279 }
280
281 inline
282 static void unselect_rows(void)
283 {
284     // Hi-Z(DDR:0, PORT:0) to unselect
285         // DDR : 1, output      0, input
286     DDRB  &= ~0b00000011; // PB: 1,0  
287     PORTB &= ~0b00000011;
288     DDRD  &= ~0b00010000; // PD: 4
289     PORTD &= ~0b00010000;
290     DDRE  &= ~0b11000000; // PE: 7,6
291     PORTE &= ~0b11000000;
292     DDRF  &= ~0b11000111; // PF: 7,6,2,1,0 
293     PORTF &= ~0b11000111;
294         // to unselect virtual row(modifier), set port to output with low
295     DDRA  |=  0b00000001; // PA: 0 for CAPSLOCK
296     PORTA &= ~0b00000001;
297     DDRB  |=  0b00011100; // PB: 3,4 for modifier(row10)
298     PORTB &= ~0b00011100; // PB: 2 for power
299     DDRF  |=  0b00110000; // PF: 4,5 for modifier
300     PORTF &= ~0b00110000;
301 }
302
303 inline
304 static void select_row(uint8_t row)
305 {
306     // Output low(DDR:1, PORT:0) to select
307         // with row enable, column could send low to AVR when pressed
308     // row: 0    1    2    3    4    5    6    7    8    9
309     // pin: PB1, PB0, PE7, PE6, PD4, PF2, PF0, PF1, PF6  PF7
310     switch (row) {
311         case 0:
312             DDRB  |= (1<<1);
313             PORTB &= ~(1<<1);
314             break;
315         case 1:
316             DDRB  |= (1<<0);
317             PORTB &= ~(1<<0);
318             break;
319         case 2:
320             DDRE  |= (1<<7);
321             PORTE &= ~(1<<7);
322             break;
323         case 3:
324             DDRE  |= (1<<6);
325             PORTE &= ~(1<<6);
326             break;
327         case 4:
328             DDRD  |= (1<<4);
329             PORTD &= ~(1<<4);
330             break;
331         case 5:
332             DDRF  |= (1<<2);
333             PORTF &= ~(1<<2);
334             break;
335         case 6:
336             DDRF  |= (1<<0);
337             PORTF &= ~(1<<0);
338             break;
339         case 7:
340             DDRF  |= (1<<1);
341             PORTF &= ~(1<<1);
342             break;
343         case 8:
344             DDRF  |= (1<<6);
345             PORTF &= ~(1<<6);
346             break;
347         case 9:
348             DDRF  |= (1<<7);
349             PORTF &= ~(1<<7);
350             break;
351                 case 10:
352                         // modifier has no row enable
353                         // to select virtual row, set port as input
354                     DDRA &= ~0b00000001;
355                     PORTA |= 0b00000001;
356                     DDRB &= ~0b00011100;
357                     PORTB |= 0b00011100;
358                     DDRF &= ~0b00110000;
359                     PORTF |= 0b00110000;
360             break;
361
362     }
363 }