/* * scan matrix */ #include #include #include "keymap.h" #include "matrix.h" // matrix is active low. (key on: 0/key off: 1) // row: Hi-Z(unselected)/low output(selected) // PD:0,1,2,3,6,7/PC:6,7/PF:7 // col: input w/pullup // PB:0-8 // matrix state buffer uint8_t *matrix; uint8_t *matrix_prev; static uint8_t _matrix0[MATRIX_ROWS]; static uint8_t _matrix1[MATRIX_ROWS]; static uint8_t read_col(void); static void unselect_rows(void); static void select_row(uint8_t row); // this must be called once before matrix_scan. void matrix_init(void) { // initialize row and col unselect_rows(); DDRB = 0x00; PORTB = 0xFF; // initialize matrix state: all keys off for (int i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0xFF; for (int i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0xFF; matrix = _matrix0; matrix_prev = _matrix1; } uint8_t matrix_scan(void) { uint8_t row, state; uint8_t *tmp; tmp = matrix_prev; matrix_prev = matrix; matrix = tmp; for (row = 0; row < MATRIX_ROWS; row++) { select_row(row); _delay_us(30); // without this wait read unstable value. state = read_col(); unselect_rows(); matrix[row] = state; } return 1; } bool matrix_is_modified(void) { for (int i=0; i