X-Git-Url: https://git.friedersdorff.com/?a=blobdiff_plain;f=macway%2Fmatrix.c;h=20cfd9dd60f610131976eabfaf5ccad494e36290;hb=e67c988824f5ec0c965beb412f8ee5953dfd3c8c;hp=cb52d79c30249d779f89e308d9eb48fd2de113a5;hpb=54b5bafaacf0d7863b7bdb84dd69cbc80db77956;p=max%2Ftmk_keyboard.git diff --git a/macway/matrix.c b/macway/matrix.c index cb52d79c..20cfd9dd 100644 --- a/macway/matrix.c +++ b/macway/matrix.c @@ -1,181 +1,252 @@ +/* +Copyright 2011 Jun Wako + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + /* * scan matrix */ +#include +#include #include #include -#include "keymap.h" +#include "print.h" +#include "util.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; +#if (MATRIX_COLS > 16) +# error "MATRIX_COLS must not exceed 16" +#endif +#if (MATRIX_ROWS > 255) +# error "MATRIX_ROWS must not exceed 255" +#endif + + +// matrix state buffer(1:on, 0:off) +#if (MATRIX_COLS <= 8) +static uint8_t *matrix; +static uint8_t *matrix_prev; static uint8_t _matrix0[MATRIX_ROWS]; static uint8_t _matrix1[MATRIX_ROWS]; - +#else +static uint16_t *matrix; +static uint16_t *matrix_prev; +static uint16_t _matrix0[MATRIX_ROWS]; +static uint16_t _matrix1[MATRIX_ROWS]; +#endif + +#ifdef MATRIX_HAS_GHOST +static bool matrix_has_ghost_in_row(uint8_t row); +#endif 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. +inline +uint8_t matrix_rows(void) +{ + return MATRIX_ROWS; +} + +inline +uint8_t matrix_cols(void) +{ + return MATRIX_COLS; +} + void matrix_init(void) { // initialize row and col unselect_rows(); + // Input with pull-up(DDR:0, PORT:1) 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; + for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00; + for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00; 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(); + for (uint8_t i = 0; i < MATRIX_ROWS; i++) { unselect_rows(); - - matrix[row] = state; + select_row(i); + _delay_us(30); // without this wait read unstable value. + matrix[i] = ~read_col(); } + unselect_rows(); return 1; } -bool matrix_is_modified(void) { - for (int i=0; i