]>
git.gir.st - tmk_keyboard.git/blob - keyboard/IIgs_Standard/matrix.c
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
24 #include <util/delay.h>
32 #if (MATRIX_COLS > 16)
33 # error "MATRIX_COLS must not exceed 16"
35 #if (MATRIX_ROWS > 255)
36 # error "MATRIX_ROWS must not exceed 255"
43 static uint8_t debouncing
= DEBOUNCE
;
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
];
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
];
58 #ifdef MATRIX_HAS_GHOST
59 static bool matrix_has_ghost_in_row(uint8_t row
);
61 static uint8_t read_col(uint8_t row
);
62 static void unselect_rows(void);
63 static void select_row(uint8_t row
);
67 uint8_t matrix_rows(void)
73 uint8_t matrix_cols(void)
78 void matrix_init(void)
80 // initialize row and col
82 // Input with pull-up(DDR:0, PORT:1)
83 // Column C1 ~ C7 (PortC0-6)
89 //DDRB &= ~0b00000100;
90 //PORTB |= 0b00000100;
91 // modifier B3/4,F4/5,E4 always input
94 //PORTA &= 0b00000001;
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;
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;
110 matrix_prev
= _matrix1
;
113 uint8_t matrix_scan(void)
116 uint8_t *tmp
= matrix_prev
;
117 matrix_prev
= matrix
;
121 for (uint8_t i
= 0; i
< MATRIX_ROWS
; 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
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
);
136 debug("bounce!: "); debug_hex(debouncing
); print("\n");
138 debouncing
= DEBOUNCE
;
142 if (matrix
[i
] != (uint8_t)~read_col(i
)) {
143 matrix
[i
] = (uint8_t)~read_col(i
);
145 debug("bounce!: "); debug_hex(debouncing
); print("\n");
147 debouncing
= DEBOUNCE
;
160 bool matrix_is_modified(void)
162 if (debouncing
) return false;
163 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
164 if (matrix
[i
] != matrix_prev
[i
]) {
172 bool matrix_has_ghost(void)
174 #ifdef MATRIX_HAS_GHOST
175 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
176 if (matrix_has_ghost_in_row(i
))
184 bool matrix_is_on(uint8_t row
, uint8_t col
)
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");
196 return (matrix
[row
] & (1<<col
));
200 #if (MATRIX_COLS <= 8)
201 uint8_t matrix_get_row(uint8_t row
)
203 uint16_t matrix_get_row(uint8_t row
)
209 void matrix_print(void)
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
));
217 pbin_reverse16(matrix_get_row(row
));
219 #ifdef MATRIX_HAS_GHOST
220 if (matrix_has_ghost_in_row(row
)) {
228 uint8_t matrix_key_count(void)
231 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
232 #if (MATRIX_COLS <= 8)
233 count
+= bitpop(matrix
[i
]);
235 count
+= bitpop16(matrix
[i
]);
241 #ifdef MATRIX_HAS_GHOST
243 static bool matrix_has_ghost_in_row(uint8_t row
)
245 // no ghost exists in case less than 2 keys on
246 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
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
])
259 static uint8_t read_col(uint8_t row
)
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)
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
275 tmp
= (PINE
>> 1)&0b00000001;
282 static void unselect_rows(void)
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;
304 static void select_row(uint8_t row
)
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
352 // modifier has no row enable
353 // to select virtual row, set port as input