From 00c9ef9216387f5bea952754c36f1ce0b398a2a5 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Tue, 29 Sep 2020 10:54:24 -0600 Subject: [PATCH] Enable dynamic keymap --- keyboards/system76/launch_test/config.h | 24 +++++++ .../launch_test/keymaps/default/keymap.c | 2 +- keyboards/system76/launch_test/launch_test.c | 63 +++++++++++++++---- keyboards/system76/launch_test/launch_test.h | 3 - keyboards/system76/launch_test/rules.mk | 17 ++--- 5 files changed, 86 insertions(+), 23 deletions(-) diff --git a/keyboards/system76/launch_test/config.h b/keyboards/system76/launch_test/config.h index e431ad187e7..48f3c1cb92a 100644 --- a/keyboards/system76/launch_test/config.h +++ b/keyboards/system76/launch_test/config.h @@ -36,4 +36,28 @@ /* Locking resynchronize hack */ #define LOCKING_RESYNC_ENABLE +// Dynamic keyboard support { +#define DYNAMIC_KEYMAP_LAYER_COUNT 4 + +// EEPROM usage +#define EEPROM_SIZE 1024 + +// TODO: refactor with new user EEPROM code (coming soon) +#define EEPROM_MAGIC 0x76EC +#define EEPROM_MAGIC_ADDR 64 +// Bump this every time we change what we store +// This will automatically reset the EEPROM with defaults +// and avoid loading invalid data from the EEPROM +#define EEPROM_VERSION 0x01 +#define EEPROM_VERSION_ADDR (EEPROM_MAGIC_ADDR + 2) + +// Dynamic keymap starts after EEPROM version +#define DYNAMIC_KEYMAP_EEPROM_ADDR (EEPROM_VERSION_ADDR + 1) +#define DYNAMIC_KEYMAP_EEPROM_SIZE (DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2) +// Dynamic macro starts after dynamic keymaps +#define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_EEPROM_ADDR + DYNAMIC_KEYMAP_EEPROM_SIZE) +#define DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE (EEPROM_SIZE - DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR) +#define DYNAMIC_KEYMAP_MACRO_COUNT 16 +// } Dynamic keyboard support + #endif // CONFIG_H diff --git a/keyboards/system76/launch_test/keymaps/default/keymap.c b/keyboards/system76/launch_test/keymaps/default/keymap.c index 7426688899e..7fd122cbc3a 100644 --- a/keyboards/system76/launch_test/keymaps/default/keymap.c +++ b/keyboards/system76/launch_test/keymaps/default/keymap.c @@ -1,6 +1,6 @@ #include QMK_KEYBOARD_H -const uint16_t PROGMEM keymaps[MATRIX_LAYERS][MATRIX_ROWS][MATRIX_COLS] = { +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { [0] = LAYOUT( RGB_TOG, RGB_RMOD, RGB_MOD, RGB_HUD, RGB_HUI, RGB_SAD, RGB_SAI, RGB_VAD, RGB_VAI, RGB_M_P, RESET, KC_ESC, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0 diff --git a/keyboards/system76/launch_test/launch_test.c b/keyboards/system76/launch_test/launch_test.c index 4eb56fe9a68..402d70677c9 100644 --- a/keyboards/system76/launch_test/launch_test.c +++ b/keyboards/system76/launch_test/launch_test.c @@ -1,19 +1,33 @@ -#include "launch_test.h" +#include "dynamic_keymap.h" #include "raw_hid.h" +#include "tmk_core/common/eeprom.h" -void keyboard_post_init_user(void) { - // Customise these values to desired behaviour - debug_enable=true; - debug_matrix=true; - debug_keyboard=true; - //debug_mouse=true; -} +#include "launch_test.h" + +enum Command { + // Get keyboard map index + CMD_KEYMAP_GET = 9, + // Set keyboard map index + CMD_KEYMAP_SET = 10, +}; static bool keymap_get(uint8_t layer, uint8_t output, uint8_t input, uint16_t *value) { - if (layer < MATRIX_LAYERS) { + if (layer < dynamic_keymap_get_layer_count()) { if (output < MATRIX_ROWS) { if (input < MATRIX_COLS) { - *value = keymap_key_to_keycode(layer, (keypos_t){.row = output, .col = input}); + *value = dynamic_keymap_get_keycode(layer, output, input); + return true; + } + } + } + return false; +} + +static bool keymap_set(uint8_t layer, uint8_t output, uint8_t input, uint16_t value) { + if (layer < dynamic_keymap_get_layer_count()) { + if (output < MATRIX_ROWS) { + if (input < MATRIX_COLS) { + dynamic_keymap_set_keycode(layer, output, input, value); return true; } } @@ -26,7 +40,7 @@ void raw_hid_receive(uint8_t *data, uint8_t length) { data[1] = 1; switch (data[0]) { - case 9: // KEYMAP_GET + case CMD_KEYMAP_GET: { uint16_t value = 0; if (keymap_get(data[2], data[3], data[4], &value)) { @@ -36,7 +50,34 @@ void raw_hid_receive(uint8_t *data, uint8_t length) { } } break; + case CMD_KEYMAP_SET: + { + uint16_t value = + ((uint16_t)data[5]) | + (((uint16_t)data[6]) << 8); + if (keymap_set(data[2], data[3], data[4], value)) { + data[1] = 0; + } + } } raw_hid_send(data, length); } + +bool eeprom_is_valid(void) { + return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC && + eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION); +} + +void eeprom_set_valid(bool valid) { + eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF); + eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF); +} + +void matrix_init_kb(void) { + if (!eeprom_is_valid()) { + dynamic_keymap_reset(); + dynamic_keymap_macro_reset(); + eeprom_set_valid(true); + } +} diff --git a/keyboards/system76/launch_test/launch_test.h b/keyboards/system76/launch_test/launch_test.h index 945e112ead0..bf57051630e 100644 --- a/keyboards/system76/launch_test/launch_test.h +++ b/keyboards/system76/launch_test/launch_test.h @@ -3,9 +3,6 @@ #include "quantum.h" -//TODO: determine this automatically -#define MATRIX_LAYERS 1 - #define ___ KC_NO #define LAYOUT( \ diff --git a/keyboards/system76/launch_test/rules.mk b/keyboards/system76/launch_test/rules.mk index 6d58b38cc15..b80acb3790e 100644 --- a/keyboards/system76/launch_test/rules.mk +++ b/keyboards/system76/launch_test/rules.mk @@ -17,11 +17,12 @@ BOOTLOADER = atmel-dfu # Build Options # comment out to disable the options. # -BOOTMAGIC_ENABLE = no # Virtual DIP switch configuration(+1000) -MOUSEKEY_ENABLE = no # Mouse keys(+4700) -EXTRAKEY_ENABLE = no # Audio control and System control(+450) -CONSOLE_ENABLE = no # Console for debug(+400) -COMMAND_ENABLE = no # Commands for debug and configuration -NKRO_ENABLE = yes # USB Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work -RAW_ENABLE = yes # Enable RAW HID commands (used by keyboard configurator) -RGBLIGHT_ENABLE = yes +BOOTMAGIC_ENABLE = no # Virtual DIP switch configuration(+1000) +MOUSEKEY_ENABLE = no # Mouse keys(+4700) +EXTRAKEY_ENABLE = no # Audio control and System control(+450) +CONSOLE_ENABLE = no # Console for debug(+400) +COMMAND_ENABLE = no # Commands for debug and configuration +DYNAMIC_KEYMAP_ENABLE = yes # Reconfigurable keyboard without flashing firmware +NKRO_ENABLE = yes # USB Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work +RAW_ENABLE = yes # Enable RAW HID commands (used by keyboard configurator) +RGBLIGHT_ENABLE = yes # Support for RGB backlight