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