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