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>
31 #if (MATRIX_COLS > 16)
32 # error "MATRIX_COLS must not exceed 16"
34 #if (MATRIX_ROWS > 255)
35 # error "MATRIX_ROWS must not exceed 255"
42 static uint8_t debouncing = DEBOUNCE;
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];
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];
57 #ifdef MATRIX_HAS_GHOST
58 static bool matrix_has_ghost_in_row(uint8_t row);
60 static uint8_t read_col(uint8_t row);
61 static void unselect_rows(void);
62 static void select_row(uint8_t row);
66 uint8_t matrix_rows(void)
72 uint8_t matrix_cols(void)
77 void matrix_init(void)
79 // initialize row and col
81 // Input with pull-up(DDR:0, PORT:1)
82 // Column C1 ~ C7 (PortC0-6)
88 // modifier B3/4,F4/5,E4 always input
93 //DDRB &= ~0b00011000;
94 //PORTB |= 0b00011000;
95 //DDRF &= ~0b00110000;
96 //PORTF |= 0b00110000;
97 //DDRE &= ~0b00010000;
98 //PORTE |= 0b00010000;
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;
104 matrix_prev = _matrix1;
107 uint8_t matrix_scan(void)
110 uint8_t *tmp = matrix_prev;
111 matrix_prev = matrix;
115 for (uint8_t i = 0; i < MATRIX_ROWS; 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);
122 debug("bounce!: "); debug_hex(debouncing); print("\n");
124 debouncing = DEBOUNCE;
136 bool matrix_is_modified(void)
138 if (debouncing) return false;
139 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
140 if (matrix[i] != matrix_prev[i]) {
148 bool matrix_has_ghost(void)
150 #ifdef MATRIX_HAS_GHOST
151 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
152 if (matrix_has_ghost_in_row(i))
160 bool matrix_is_on(uint8_t row, uint8_t col)
162 return (matrix[row] & (1<<col));
166 #if (MATRIX_COLS <= 8)
167 uint8_t matrix_get_row(uint8_t row)
169 uint16_t matrix_get_row(uint8_t row)
175 void matrix_print(void)
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));
183 pbin_reverse16(matrix_get_row(row));
185 #ifdef MATRIX_HAS_GHOST
186 if (matrix_has_ghost_in_row(row)) {
194 uint8_t matrix_key_count(void)
197 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
198 #if (MATRIX_COLS <= 8)
199 count += bitpop(matrix[i]);
201 count += bitpop16(matrix[i]);
207 #ifdef MATRIX_HAS_GHOST
209 static bool matrix_has_ghost_in_row(uint8_t row)
211 // no ghost exists in case less than 2 keys on
212 if (((matrix[row] - 1) & matrix[row]) == 0)
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])
225 static uint8_t read_col(uint8_t row)
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)
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
240 tmp = (PINE >> 1)&0b00000001;
247 static void unselect_rows(void)
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;
267 static void select_row(uint8_t row)
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
315 // modifier has no row enable
316 // to select virtual row, set port as input