From 82309deefc21f66d92df08b8eecae8466939e04d Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 13 Sep 2010 00:00:58 +0900 Subject: [PATCH] add anti-ghost logic --- README | 4 +++ matrix.c | 78 +++++++++++++++++++++++++++++++++++++++-------------- matrix.h | 6 ++++- mykey.c | 81 ++++++++++++++++++++++++++++++-------------------------- 4 files changed, 110 insertions(+), 59 deletions(-) diff --git a/README b/README index 228120cc..4b10d663 100644 --- a/README +++ b/README @@ -16,6 +16,9 @@ modulization clean source debouncing anti-ghost +sleep&wakeup +boot keyboard support +mouse key keymap layer key combination switch toggle siwtch @@ -24,6 +27,7 @@ setting menu(wizard) debug console keymap setting matrix display +PS/2 keyboard mode HHKB support Trackpoint(PS/2) support Thinkpad keyboard support diff --git a/matrix.c b/matrix.c index fc975a8f..6129f52c 100644 --- a/matrix.c +++ b/matrix.c @@ -7,38 +7,36 @@ #include "matrix.h" #include "print.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 *prev_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) { - // Column: input w/pullup + // initialize row and col + unselect_rows(); DDRB = 0x00; PORTB = 0xFF; - // Row: Hi-Z(unselected) - // PD:0,1,2,3,6,7 - // PC:6,7 - // PF:7 - DDRD = 0x00; - PORTD = 0x00; - DDRC = 0x00; - PORTC = 0x00; - DDRF = 0x00; - PORTF = 0x00; - - for (int i=0; i < MATRIX_ROWS; i++) { - _matrix0[i] = 0xFF; - _matrix1[i] = 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; - prev_matrix = _matrix1; + matrix_prev = _matrix1; } uint8_t matrix_scan(void) @@ -46,25 +44,65 @@ uint8_t matrix_scan(void) uint8_t row, state; uint8_t *tmp; - tmp = prev_matrix; - prev_matrix = matrix; + 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 extern uint8_t *matrix; -extern uint8_t *prev_matrix; +extern uint8_t *matrix_prev; void matrix_init(void); uint8_t matrix_scan(void); +bool matrix_is_modified(void); +bool matrix_has_ghost(void); +bool matrix_has_ghost_in_row(uint8_t row); diff --git a/mykey.c b/mykey.c index 93f753f0..b22c68fb 100644 --- a/mykey.c +++ b/mykey.c @@ -24,10 +24,12 @@ * THE SOFTWARE. */ +#include #include #include #include #include + #include "usb_keyboard_debug.h" #include "print.h" #include "matrix.h" @@ -45,7 +47,8 @@ uint16_t idle_count=0; int main(void) { - uint8_t modified = 0; + bool modified = false; + bool has_ghost = false; uint8_t key_index = 0; // set for 16 MHz clock @@ -77,61 +80,63 @@ int main(void) while (1) { uint8_t row, col, code; - modified = 0; - matrix_scan(); - keyboard_modifier_keys = 0; - for (int i = 0; i < 6; i++) - keyboard_keys[i] = KB_NO; - key_index = 0; + modified = matrix_is_modified(); + has_ghost = matrix_has_ghost(); - for (row = 0; row < MATRIX_ROWS; row++) { - if (matrix[row] != prev_matrix[row]) { - modified = 1; - } + // doesnt send keys during ghost occurs + if (modified && !has_ghost) { + key_index = 0; + keyboard_modifier_keys = 0; + for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO; - for (col = 0; col < MATRIX_COLS; col++) { - if (matrix[row] & 1< 6) { + //Rollover } - } - if (key_index > 6) { - //Rollover - } - + usb_keyboard_send(); - // if any keypresses were detected, reset the idle counter + // variables shared with interrupt routines must be + // accessed carefully so the interrupt routine doesn't + // try to use the variable in the middle of our access + cli(); + idle_count = 0; + sei(); + } + + // print matrix state for debug if (modified) { - print(" 01234567\n"); + print("r/c 01234567\n"); for (row = 0; row < MATRIX_ROWS; row++) { - phex(row); print(": "); pbin_reverse(matrix[row]); print("\n"); + phex(row); print(": "); + pbin_reverse(matrix[row]); + if (matrix_has_ghost_in_row(row)) { + print("