7 #include <avr/interrupt.h>
8 #include <util/delay.h>
14 #if (MATRIX_COLS > 16)
15 # error "MATRIX_COLS must not exceed 16"
17 #if (MATRIX_ROWS > 255)
18 # error "MATRIX_ROWS must not exceed 255"
22 // matrix state buffer(1:on, 0:off)
23 #if (MATRIX_COLS <= 8)
24 static uint8_t *matrix;
25 static uint8_t *matrix_prev;
26 static uint8_t _matrix0[MATRIX_ROWS];
27 static uint8_t _matrix1[MATRIX_ROWS];
29 static uint16_t *matrix;
30 static uint16_t *matrix_prev;
31 static uint16_t _matrix0[MATRIX_ROWS];
32 static uint16_t _matrix1[MATRIX_ROWS];
35 // HHKB has no ghost and no bounce.
36 #ifdef MATRIX_HAS_GHOST
37 static bool matrix_has_ghost_in_row(uint8_t row);
43 // row: HC4051[A,B,C] selects scan row0-7
44 // col: LS145[A,B,C,D] selects scan col0-7 and enable(D)
46 // prev: unknown: output previous key state(negated)?
52 // key: PE6(pull-uped)
54 #define KEY_INIT() do { \
60 #define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \
61 (((COL) & 0x07)<<3) | \
63 #define KEY_ENABLE() (PORTB &= ~(1<<6))
64 #define KEY_UNABLE() (PORTB |= (1<<6))
65 #define KEY_STATE() (PINE & (1<<6))
66 #define KEY_PREV_ON() (PORTE |= (1<<7))
67 #define KEY_PREV_OFF() (PORTE &= ~(1<<7))
71 // key: PB0(pull-uped)
75 #define KEY_INIT() do { \
81 #define KEY_SELECT(ROW, COL) do { \
82 PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \
83 PORTC = (PORTC & 0xF8) | ((COL) & 0x07); \
85 #define KEY_ENABLE() (PORTC &= ~(1<<3))
86 #define KEY_UNABLE() (PORTC |= (1<<3))
87 #define KEY_STATE() (PINB & (1<<0))
88 #define KEY_PREV_ON() (PORTB |= (1<<1))
89 #define KEY_PREV_OFF() (PORTB &= ~(1<<1))
94 uint8_t matrix_rows(void)
100 uint8_t matrix_cols(void)
105 void matrix_init(void)
109 // initialize matrix state: all keys off
110 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
111 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
113 matrix_prev = _matrix1;
116 uint8_t matrix_scan(void)
121 matrix_prev = matrix;
124 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
125 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
126 KEY_SELECT(row, col);
127 _delay_us(40); // from logic analyzer chart
128 if (matrix_prev[row] & (1<<col)) {
131 _delay_us(7); // from logic analyzer chart
134 // to avoid V-USB interrupt during read key state
139 _delay_us(10); // from logic analyzer chart
141 matrix[row] &= ~(1<<col);
143 matrix[row] |= (1<<col);
151 _delay_us(150); // from logic analyzer chart
157 bool matrix_is_modified(void)
159 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
160 if (matrix[i] != matrix_prev[i])
167 bool matrix_has_ghost(void)
169 #ifdef MATRIX_HAS_GHOST
170 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
171 if (matrix_has_ghost_in_row(i))
179 bool matrix_is_on(uint8_t row, uint8_t col)
181 return (matrix[row] & (1<<col));
185 #if (MATRIX_COLS <= 8)
186 uint8_t matrix_get_row(uint8_t row)
188 uint16_t matrix_get_row(uint8_t row)
194 void matrix_print(void)
196 #if (MATRIX_COLS <= 8)
197 print("\nr/c 01234567\n");
199 print("\nr/c 0123456789ABCDEF\n");
201 for (uint8_t row = 0; row < matrix_rows(); row++) {
202 phex(row); print(": ");
203 #if (MATRIX_COLS <= 8)
204 pbin_reverse(matrix_get_row(row));
206 pbin_reverse16(matrix_get_row(row));
208 #ifdef MATRIX_HAS_GHOST
209 if (matrix_has_ghost_in_row(row)) {
217 uint8_t matrix_key_count(void)
220 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
221 #if (MATRIX_COLS <= 8)
222 count += bitpop(matrix[i]);
224 count += bitpop16(matrix[i]);
230 #ifdef MATRIX_HAS_GHOST
232 static bool matrix_has_ghost_in_row(uint8_t row)
234 // no ghost exists in case less than 2 keys on
235 if (((matrix[row] - 1) & matrix[row]) == 0)
238 // ghost exists in case same state as other row
239 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
240 if (i != row && (matrix[i] & matrix[row]) == matrix[row])