5 #include <util/delay.h>
10 // matrix is active low. (key on: 0/key off: 1)
11 // row: Hi-Z(unselected)/low output(selected)
12 // PD:0,1,2,3,6,7/PC:6,7/PF:7
13 // col: input w/pullup
16 // matrix state buffer
19 static uint8_t _matrix0[MATRIX_ROWS];
20 static uint8_t _matrix1[MATRIX_ROWS];
22 static uint8_t read_col(void);
23 static void unselect_rows(void);
24 static void select_row(uint8_t row);
27 // this must be called once before matrix_scan.
28 void matrix_init(void)
30 // initialize row and col
35 // initialize matrix state: all keys off
36 for (int i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0xFF;
37 for (int i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0xFF;
39 matrix_prev = _matrix1;
42 uint8_t matrix_scan(void)
51 for (row = 0; row < MATRIX_ROWS; row++) {
53 _delay_us(30); // without this wait read unstable value.
62 bool matrix_is_modified(void) {
63 for (int i=0; i <MATRIX_ROWS; i++) {
64 if (matrix[i] != matrix_prev[i])
70 bool matrix_has_ghost(void) {
71 for (int i=0; i <MATRIX_ROWS; i++) {
72 if (matrix_has_ghost_in_row(i))
78 bool matrix_has_ghost_in_row(uint8_t row) {
79 uint8_t state = ~matrix[row];
80 // no ghost exists in case less than 2 keys on
81 if (((state - 1) & state) == 0)
84 // ghost exists in case same state as other row
85 for (int i=0; i < MATRIX_ROWS; i++) {
86 if (i == row) continue;
87 if ((~matrix[i] & state) == state) return true;
92 static uint8_t read_col(void)
97 static void unselect_rows(void) {
106 static void select_row(uint8_t row)