From 54b5bafaacf0d7863b7bdb84dd69cbc80db77956 Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 14 Oct 2010 17:36:00 +0900 Subject: add directories for each keyboard: hhkb, macway --- macway/matrix.c | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 macway/matrix.c (limited to 'macway/matrix.c') diff --git a/macway/matrix.c b/macway/matrix.c new file mode 100644 index 0000000000..cb52d79c30 --- /dev/null +++ b/macway/matrix.c @@ -0,0 +1,181 @@ +/* + * 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) +// PD0, PC7, PD7, PF6, PD6, PD1, PD2, PC6, PF7 +// col: input w/pullup +// PB0-PB7 + +// 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