diff options
Diffstat (limited to 'src/matrix.c')
| -rw-r--r-- | src/matrix.c | 65 |
1 files changed, 49 insertions, 16 deletions
diff --git a/src/matrix.c b/src/matrix.c index 1da8289..9bae95a 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -1,6 +1,7 @@ #include "keymap.h" #include "matrix.h" +#include "split.h" #include "pico/types.h" #include "hardware/gpio.h" #include "hardware/timer.h" @@ -12,23 +13,23 @@ static const uint matrix_col_pins[] = { 29, 28, 27, 26, 22, 20 }; static_assert(ARRLEN(matrix_row_pins) == KEY_ROWS); static_assert(ARRLEN(matrix_col_pins) == KEY_COLS); -bool prev_state_matrix[KEY_COUNT]; -bool state_matrix[KEY_COUNT]; +bool prev_state_matrix[KEY_COUNT * 2]; +bool state_matrix[KEY_COUNT * 2]; void matrix_init(void) { uint x, y; - for (y = 0; y < KEY_ROWS; y++) { - gpio_init(matrix_row_pins[y]); - gpio_set_dir(matrix_row_pins[y], GPIO_IN); - gpio_pull_up(matrix_row_pins[y]); - } - for (x = 0; x < KEY_COLS; x++) { gpio_init(matrix_col_pins[x]); - gpio_set_dir(matrix_col_pins[x], GPIO_OUT); + gpio_set_dir(matrix_col_pins[x], GPIO_IN); + gpio_pull_up(matrix_col_pins[x]); + } + + for (y = 0; y < KEY_ROWS; y++) { + gpio_init(matrix_row_pins[y]); + gpio_set_dir(matrix_row_pins[y], GPIO_OUT); } } @@ -36,18 +37,50 @@ void scan_matrix(void) { bool pressed; - uint x, y; + uint x, y, p; memcpy(prev_state_matrix, state_matrix, sizeof(state_matrix)); for (y = 0; y < KEY_ROWS; y++) { + gpio_put(matrix_row_pins[y], 0); + busy_wait_us(5); + for (x = 0; x < KEY_COLS; x++) { + pressed = !gpio_get(matrix_col_pins[x]); + p = MAT_OFFSET(SPLIT_SIDE) + y * KEY_COLS + x; + state_matrix[p] = pressed; + } + gpio_put(matrix_row_pins[y], 1); + busy_wait_us(5); + } +} + +uint32_t +matrix_encode_half(int side) +{ + uint32_t mask; + uint x, y, p; + + mask = 0; + for (y = 0; y < KEY_ROWS; y++) { + for (x = 0; x < KEY_COLS; x++) { + p = MAT_OFFSET(side) + y * KEY_COLS + x; + if (state_matrix[p]) + mask |= 1 << (y * KEY_COLS + x); + } + } + + return mask; +} + +void +matrix_decode_half(int side, uint32_t mask) +{ + uint x, y, p; + + for (y = 0; y < KEY_ROWS; y++) { for (x = 0; x < KEY_COLS; x++) { - gpio_put(matrix_col_pins[x], 0); - busy_wait_us(5); - pressed = !gpio_get(matrix_row_pins[y]); - state_matrix[y * KEY_COLS + x] = pressed; - gpio_put(matrix_col_pins[x], 1); - busy_wait_us(5); + p = MAT_OFFSET(side) + y * KEY_COLS + x; + state_matrix[p] = (mask >> (y * KEY_COLS + x)) & 1; } } } |
