From d926950e1783e136d5744f8a7fab7b043d9ff42c Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Thu, 21 Nov 2013 13:00:45 +0900 Subject: [PATCH] New feature to support external keymap in eeprom --- common.mk | 11 ++++- common/keyboard.c | 5 +++ common/keymap_ex.c | 102 +++++++++++++++++++++++++++++++++++++++++++++ common/keymap_ex.h | 62 +++++++++++++++++++++++++++ 4 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 common/keymap_ex.c create mode 100644 common/keymap_ex.h diff --git a/common.mk b/common.mk index 5b70db94..350558f8 100644 --- a/common.mk +++ b/common.mk @@ -53,7 +53,11 @@ ifdef PS2_MOUSE_ENABLE OPT_DEFS += -DPS2_MOUSE_ENABLE endif -ifdef $(or MOUSEKEY_ENABLE, PS2_MOUSE_ENABLE) +ifdef MOUSEKEY_ENABLE + OPT_DEFS += -DMOUSE_ENABLE +endif + +ifdef PS4_MOUSE_ENABLE OPT_DEFS += -DMOUSE_ENABLE endif @@ -73,6 +77,11 @@ ifdef KEYMAP_SECTION_ENABLE EXTRALDFLAGS = -Wl,-L$(TOP_DIR),-Tldscript_keymap_avr5.x endif +ifdef KEYMAP_EX_ENABLE + SRC += $(COMMON_DIR)/keymap_ex.c + OPT_DEFS += -DKEYMAP_EX_ENABLE +endif + # Version string OPT_DEFS += -DVERSION=$(shell (git describe --always --dirty || echo 'unknown') 2> /dev/null) diff --git a/common/keyboard.c b/common/keyboard.c index 601e3abe..4ad92eb6 100644 --- a/common/keyboard.c +++ b/common/keyboard.c @@ -32,6 +32,7 @@ along with this program. If not, see . #include "eeconfig.h" #include "mousekey.h" #include "backlight.h" +#include "keymap_ex.h" #ifdef MATRIX_HAS_GHOST @@ -67,6 +68,10 @@ void keyboard_init(void) #ifdef BACKLIGHT_ENABLE backlight_init(); #endif + +#ifdef KEYMAP_EX_ENABLE + keymap_init(); +#endif } /* diff --git a/common/keymap_ex.c b/common/keymap_ex.c new file mode 100644 index 00000000..082e3207 --- /dev/null +++ b/common/keymap_ex.c @@ -0,0 +1,102 @@ +/* +Copyright 2013 Kai Ryu + +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 . +*/ + +#include +#include +#include "eeconfig.h" +#include "keymap_ex.h" +#include "debug.h" + +#ifdef KEYMAP_EX_ENABLE + +void keymap_init(void) { + if (!check_keymap_in_eeprom()) { + write_keymap_to_eeprom(); + } +} + +bool check_keymap_in_eeprom(void) { + return false; +} + +void write_keymap_to_eeprom(void) { + uint16_t checksum = EECONFIG_MAGIC_NUMBER; + const uint16_t *fn_actions = fn_actions_pointer(); + const uint8_t *keymaps = keymaps_pointer(); + // write fn_actions + if (fn_actions != NULL) { + uint16_t size_of_fn_actions = fn_actions_size(); + for (uint16_t i = 0; i < FN_ACTIONS_SIZE_EX; i++) { + uint16_t fn_action = 0; + if (i < size_of_fn_actions) { + fn_action = pgm_read_word(fn_actions + i); + } + eeconfig_write_keymap_fn_action(i, fn_action); + checksum ^= fn_action; + } + } + // write keymaps + if (keymaps != NULL) { + uint16_t size_of_keymaps = keymaps_size(); + for (uint16_t i = 0; i < KEYMAPS_SIZE_EX; i++) { + uint8_t keymap = 0; + if (i < size_of_keymaps) { + keymap = pgm_read_byte(keymaps + i); + } + eeconfig_write_keymap_key_by_index(i, keymap); + uint16_t keymap_word = keymap; + if (i & 1) { + keymap_word = keymap << 8; + } + checksum ^= keymap_word; + } + } + // write checksum + eeprom_write_word(&((keymap_ex_t*)EECONFIG_KEYMAP_EX)->checksum, checksum); +} + +uint8_t eeconfig_read_keymap_key(uint8_t layer, uint8_t row, uint8_t col) { + //return eeprom_read_byte(&((keymap_ex_t*)(EECONFIG_KEYMAP_EX))->keymaps[layer][row][col]); + return eeprom_read_byte((void*)(EECONFIG_KEYMAP_KEYMAPS + KEY_OFFSET(layer, row, col))); +} + +void eeconfig_write_keymap_key(uint8_t layer, uint8_t row, uint8_t col, uint8_t key) { + //return eeprom_write_byte(&((keymap_ex_t*)(EECONFIG_KEYMAP_EX))->keymaps[layer][row][col], key); + return eeprom_write_byte((void*)(EECONFIG_KEYMAP_KEYMAPS + KEY_OFFSET(layer, row, col)), key); +} + +uint8_t eeconfig_read_keymap_key_by_index(uint16_t index) { + //return eeprom_read_byte(((keymap_ex_t*)(EECONFIG_KEYMAP_EX)) + index); + return eeprom_read_byte((void*)(EECONFIG_KEYMAP_KEYMAPS + index)); +} + +void eeconfig_write_keymap_key_by_index(uint16_t index, uint8_t key) { + //return eeprom_write_byte(((keymap_ex_t*)(EECONFIG_KEYMAP_EX)) + index, key); + return eeprom_write_byte((void*)(EECONFIG_KEYMAP_KEYMAPS + index), key); +} + +uint16_t eeconfig_read_keymap_fn_action(uint8_t index) { + //return eeprom_read_word(&((keymap_ex_t*)(EECONFIG_KEYMAP_EX))->fn_actions[index]); + return eeprom_read_word((void*)(EECONFIG_KEYMAP_FN_ACTIONS + FN_ACTION_OFFSET(index))); +} + +void eeconfig_write_keymap_fn_action(uint8_t index, uint16_t fn_action) { + //return eeprom_write_word(&((keymap_ex_t*)(EECONFIG_KEYMAP_EX))->fn_actions[index], fn_action); + return eeprom_write_word((void*)(EECONFIG_KEYMAP_FN_ACTIONS + FN_ACTION_OFFSET(index)), fn_action); +} + +#endif diff --git a/common/keymap_ex.h b/common/keymap_ex.h new file mode 100644 index 00000000..b15c3fe1 --- /dev/null +++ b/common/keymap_ex.h @@ -0,0 +1,62 @@ +/* +Copyright 2013 Kai Ryu + +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 . +*/ + +#ifndef KEYMAP_EX_H +#define KEYMAP_EX_H + +#ifdef KEYMAP_EX_ENABLE + +#include +#include + +#define EECONFIG_KEYMAP_EX 0x10 +#define FN_ACTIONS_COUNT 8 +#define KEYMAPS_COUNT 8 + +typedef struct { + uint16_t checksum; + uint16_t fn_actions[FN_ACTIONS_COUNT]; + uint8_t keymaps[KEYMAPS_COUNT][MATRIX_ROWS][MATRIX_COLS]; +} keymap_ex_t; + +#define EECONFIG_KEYMAP_CHECKSUM (EECONFIG_KEYMAP_EX) +#define EECONFIG_KEYMAP_FN_ACTIONS (EECONFIG_KEYMAP_EX + sizeof(uint16_t)) +#define EECONFIG_KEYMAP_KEYMAPS (EECONFIG_KEYMAP_FN_ACTIONS + sizeof(uint16_t) * FN_ACTIONS_COUNT) + +#define FN_ACTIONS_SIZE_EX (sizeof(uint16_t) * FN_ACTIONS_COUNT) +#define KEYMAPS_SIZE_EX (sizeof(uint8_t) * KEYMAPS_COUNT * MATRIX_ROWS * MATRIX_COLS) +#define FN_ACTION_OFFSET(index) (sizeof(uint16_t) * index) +#define KEY_OFFSET(layer, row, col) (sizeof(uint8_t) * (layer * MATRIX_ROWS * MATRIX_COLS + row * MATRIX_COLS + col)) + +void keymap_init(void); +bool check_keymap_in_eeprom(void); +void write_keymap_to_eeprom(void); +uint8_t eeconfig_read_keymap_key(uint8_t layer, uint8_t row, uint8_t col); +void eeconfig_write_keymap_key(uint8_t layer, uint8_t row, uint8_t col, uint8_t key); +uint8_t eeconfig_read_keymap_key_by_index(uint16_t index); +void eeconfig_write_keymap_key_by_index(uint16_t index, uint8_t key); +uint16_t eeconfig_read_keymap_fn_action(uint8_t index); +void eeconfig_write_keymap_fn_action(uint8_t index, uint16_t fn_action); + +const uint8_t* keymaps_pointer(void); +const uint16_t* fn_actions_pointer(void); +uint16_t keymaps_size(void); +uint16_t fn_actions_size(void); + +#endif + +#endif