]>
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 // modifier B3/4,F4/5,E4 always input
92 //PORTA &= 0b00000001;
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;
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;
108 matrix_prev
= _matrix1
;
111 uint8_t matrix_scan(void)
114 uint8_t *tmp
= matrix_prev
;
115 matrix_prev
= matrix
;
119 for (uint8_t i
= 0; i
< MATRIX_ROWS
; 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
130 } else { // CAPS LOCK is OFF on HOST
131 matrix
[i
] = ~read_col(i
);
134 if (matrix
[i
] != (uint8_t)~read_col(i
)) {
135 matrix
[i
] = (uint8_t)~read_col(i
);
139 debug("bounce!: "); debug_hex(debouncing
); print("\n");
141 debouncing
= DEBOUNCE
;
152 bool matrix_is_modified(void)
154 if (debouncing
) return false;
155 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
156 if (matrix
[i
] != matrix_prev
[i
]) {
164 bool matrix_has_ghost(void)
166 #ifdef MATRIX_HAS_GHOST
167 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
168 if (matrix_has_ghost_in_row(i
))
176 bool matrix_is_on(uint8_t row
, uint8_t col
)
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");
188 return (matrix
[row
] & (1<<col
));
192 #if (MATRIX_COLS <= 8)
193 uint8_t matrix_get_row(uint8_t row
)
195 uint16_t matrix_get_row(uint8_t row
)
201 void matrix_print(void)
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
));
209 pbin_reverse16(matrix_get_row(row
));
211 #ifdef MATRIX_HAS_GHOST
212 if (matrix_has_ghost_in_row(row
)) {
220 uint8_t matrix_key_count(void)
223 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
224 #if (MATRIX_COLS <= 8)
225 count
+= bitpop(matrix
[i
]);
227 count
+= bitpop16(matrix
[i
]);
233 #ifdef MATRIX_HAS_GHOST
235 static bool matrix_has_ghost_in_row(uint8_t row
)
237 // no ghost exists in case less than 2 keys on
238 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
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
])
251 static uint8_t read_col(uint8_t row
)
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)
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
267 tmp
= (PINE
>> 1)&0b00000001;
274 static void unselect_rows(void)
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;
296 static void select_row(uint8_t row
)
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
344 // modifier has no row enable
345 // to select virtual row, set port as input