]>
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 matrix
[i
] = ~read_col(i
);
136 if (matrix
[i
] != (uint8_t)~read_col(i
)) {
137 matrix
[i
] = (uint8_t)~read_col(i
);
141 debug("bounce!: "); debug_hex(debouncing
); print("\n");
143 debouncing
= DEBOUNCE
;
154 bool matrix_is_modified(void)
156 if (debouncing
) return false;
157 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
158 if (matrix
[i
] != matrix_prev
[i
]) {
166 bool matrix_has_ghost(void)
168 #ifdef MATRIX_HAS_GHOST
169 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
170 if (matrix_has_ghost_in_row(i
))
178 bool matrix_is_on(uint8_t row
, uint8_t col
)
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");
190 return (matrix
[row
] & (1<<col
));
194 #if (MATRIX_COLS <= 8)
195 uint8_t matrix_get_row(uint8_t row
)
197 uint16_t matrix_get_row(uint8_t row
)
203 void matrix_print(void)
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
));
211 pbin_reverse16(matrix_get_row(row
));
213 #ifdef MATRIX_HAS_GHOST
214 if (matrix_has_ghost_in_row(row
)) {
222 uint8_t matrix_key_count(void)
225 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
226 #if (MATRIX_COLS <= 8)
227 count
+= bitpop(matrix
[i
]);
229 count
+= bitpop16(matrix
[i
]);
235 #ifdef MATRIX_HAS_GHOST
237 static bool matrix_has_ghost_in_row(uint8_t row
)
239 // no ghost exists in case less than 2 keys on
240 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
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
])
253 static uint8_t read_col(uint8_t row
)
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)
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
270 tmp
= (PINE
>> 1)&0b00000001;
277 static void unselect_rows(void)
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;
299 static void select_row(uint8_t row
)
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
347 // modifier has no row enable
348 // to select virtual row, set port as input