From 06eb50be07ff16e4bfb046e4773185d9bcf048e9 Mon Sep 17 00:00:00 2001 From: tmk Date: Sun, 24 Oct 2010 01:17:26 +0900 Subject: hhkb: refactored --- hhkb/keymap.c | 130 +++++++++++++++++++++++++++++++++++----------------------- hhkb/keymap.h | 13 +----- hhkb/matrix.c | 36 +++++++++++++++- hhkb/matrix.h | 15 +++++++ 4 files changed, 129 insertions(+), 65 deletions(-) create mode 100644 hhkb/matrix.h (limited to 'hhkb') diff --git a/hhkb/keymap.c b/hhkb/keymap.c index 572f530b93..6838a08ac0 100644 --- a/hhkb/keymap.c +++ b/hhkb/keymap.c @@ -3,12 +3,18 @@ */ #include #include +#include "usb_keyboard.h" #include "matrix.h" #include "keymap.h" -#include "usb_keyboard.h" +#include "print.h" -int current_layer = 0; -bool key_sent = false; +#define FN_KEYCODE(fn) (pgm_read_byte(&fn_keycode[(fn)])) +#define FN_LAYER(fn) (pgm_read_byte(&fn_layer[(fn)])) +#define KEYMAPS(layer, row, col) (pgm_read_byte(&keymaps[(layer)][(row)][(col)])) + +static int current_layer = 0; +static bool layer_used = false; +static int onbit(uint8_t bits); /* * Layer0(Default Layer) @@ -66,15 +72,21 @@ bool key_sent = false; * Mc: Mouse Cursor / Mb: Mouse Button / Mw: Mouse Wheel */ -/* keycode sent when Fn key released without using layer keys. */ -static const uint8_t PROGMEM FnKey[] = { - KB_NO, // this must be KB_NO. (not used) +/* keycode to sent when Fn key released without using layer keys. */ +static const uint8_t PROGMEM fn_keycode[] = { + KB_NO, // FN_0 KB_NO, // FN_1 KB_RALT, // FN_2 KB_SCOLON, // FN_3 + KB_NO, // FN_4 + KB_NO, // FN_5 + KB_NO, // FN_6 + KB_NO, // FN_7 }; +/* layer to change into while Fn key pressed */ +static const int PROGMEM fn_layer[] = { 0, 1, 2, 3, 0, 0, 0, 0 }; -static const uint8_t PROGMEM Keymap[][MATRIX_ROWS][MATRIX_COLS] = { +static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { /* plain keymap { { KB_2, KB_Q, KB_W, KB_S, KB_A, KB_Z, KB_X, KB_C }, @@ -134,61 +146,77 @@ static const uint8_t PROGMEM Keymap[][MATRIX_ROWS][MATRIX_COLS] = { }; -uint8_t get_keycode(int layer, int row, int col) +uint8_t keymap_get_keycode(int row, int col) { - if (row >= MATRIX_ROWS) - return KB_NO; - if (col >= MATRIX_COLS) - return KB_NO; - return pgm_read_byte(&Keymap[layer][row][col]); + return keymap_get_keycodel(current_layer, row, col); } -int get_layer(void) { - // keep modifier state when Fn key pressed - static uint8_t preserved_modifiers = 0; - int layer = 0; - uint8_t modifiers = 0; - for (int row = 0; row < MATRIX_ROWS; row++) { - for (int col = 0; col < MATRIX_ROWS; col++) { - if (matrix[row] & 1<> 4) { bits >>= 4; n += 4;} + if (bits >> 2) { bits >>= 2; n += 2;} + if (bits >> 1) { bits >>= 1; n += 1;} + return n; } diff --git a/hhkb/keymap.h b/hhkb/keymap.h index be78609e61..a577c79b9b 100644 --- a/hhkb/keymap.h +++ b/hhkb/keymap.h @@ -4,17 +4,6 @@ #include #include #include "usb_keycodes.h" - - -#define MATRIX_ROWS 8 -#define MATRIX_COLS 8 - - -extern int current_layer; -extern bool key_sent; - - -int get_layer(void); -uint8_t get_keycode(int layer, int row, int col); +#include "keymap_skel.h" #endif diff --git a/hhkb/matrix.c b/hhkb/matrix.c index 3034a63612..a1917793e7 100644 --- a/hhkb/matrix.c +++ b/hhkb/matrix.c @@ -31,6 +31,19 @@ static uint8_t _matrix0[MATRIX_ROWS]; static uint8_t _matrix1[MATRIX_ROWS]; +static bool matrix_has_ghost_in_row(int row); + + +inline +int matrix_rows(void) { + return MATRIX_ROWS; +} + +inline +int matrix_cols(void) { + return MATRIX_COLS; +} + // this must be called once before matrix_scan. void matrix_init(void) { @@ -48,7 +61,7 @@ void matrix_init(void) matrix_prev = _matrix1; } -uint8_t matrix_scan(void) +int matrix_scan(void) { uint8_t *tmp; @@ -82,10 +95,29 @@ bool matrix_is_modified(void) { return false; } +inline bool matrix_has_ghost(void) { return false; } -bool matrix_has_ghost_in_row(uint8_t row) { +inline +uint16_t matrix_get_row(int row) { + return matrix[row]; +} + +void matrix_print(void) { + print("\nr/c 01234567\n"); + for (int row = 0; row < matrix_rows(); row++) { + phex(row); print(": "); + pbin_reverse(matrix_get_row(row)); + if (matrix_has_ghost_in_row(row)) { + print(" +#include "matrix_skel.h" + + +#define MATRIX_ROWS 8 +#define MATRIX_COLS 8 + + +extern uint8_t *matrix; +extern uint8_t *matrix_prev; + +#endif -- cgit v1.2.1