From c5003d5104ff625a6909c2e8529d87a10218196f Mon Sep 17 00:00:00 2001 From: Mark Date: Thu, 21 Jul 2022 14:09:50 -0700 Subject: [PATCH] Added HID layer state and keymap. Closes #3 --- keyboards/betalupi_ergodox/betalupi_ergodox.c | 8 +++++ .../betalupi_ergodox/keymaps/default/keymap.c | 9 +++++ .../betalupi_ergodox/keymaps/default/keymap.h | 10 +++++- .../default/layers/definitions/arrows.h | 5 +++ .../default/layers/definitions/desktop.h | 5 +++ .../default/layers/definitions/fkeys.h | 5 +++ .../default/layers/definitions/keyboard.h | 5 +++ .../keymaps/default/layers/definitions/main.h | 5 +++ .../default/layers/definitions/numpad.h | 5 +++ .../default/layers/definitions/russian.h | 5 +++ .../default/layers/definitions/symbols.h | 11 ++++++- .../default/layers/definitions/symbols_ru.h | 5 +++ .../keymaps/default/layers/layers.c | 10 +++++- .../keymaps/default/layers/layers.h | 3 +- keyboards/betalupi_ergodox/rawhid.c | 26 +++++++++++++-- keyboards/betalupi_ergodox/rawhid.h | 33 +++++++++++++++++-- 16 files changed, 141 insertions(+), 9 deletions(-) diff --git a/keyboards/betalupi_ergodox/betalupi_ergodox.c b/keyboards/betalupi_ergodox/betalupi_ergodox.c index 40d6802..075cc07 100644 --- a/keyboards/betalupi_ergodox/betalupi_ergodox.c +++ b/keyboards/betalupi_ergodox/betalupi_ergodox.c @@ -476,6 +476,14 @@ bool process_record_kb(uint16_t keycode, keyrecord_t *record) { return process_record_user(keycode, record); } +uint32_t layer_state_set_kb(uint32_t state) { + //uint8_t layer = biton32(state); + + hid_send_state( + .state = state + ); + return layer_state_set_user(state); +}; // EEPROM is getting reset! void eeconfig_init_kb(void) { diff --git a/keyboards/betalupi_ergodox/keymaps/default/keymap.c b/keyboards/betalupi_ergodox/keymaps/default/keymap.c index 0201b92..ffda408 100644 --- a/keyboards/betalupi_ergodox/keymaps/default/keymap.c +++ b/keyboards/betalupi_ergodox/keymaps/default/keymap.c @@ -2,6 +2,7 @@ #include "layers/layers.h" #include "spellcheck.h" +#include "rawhid.h" // Values that should not be saved to git. // Create a `secrets.h` in the keymap directory. @@ -37,6 +38,14 @@ void matrix_scan_user(void) { } +uint32_t layer_state_set_user(uint32_t state) { + set_layer_colors(state); + + //uint8_t layer = biton32(state); + return state; +}; + + // Return FALSE to halt key processing, // Return TRUE to allow QMK to handle keypress. diff --git a/keyboards/betalupi_ergodox/keymaps/default/keymap.h b/keyboards/betalupi_ergodox/keymaps/default/keymap.h index 781b7e3..0a3d5cc 100644 --- a/keyboards/betalupi_ergodox/keymaps/default/keymap.h +++ b/keyboards/betalupi_ergodox/keymaps/default/keymap.h @@ -22,8 +22,16 @@ extern rgb_config_t rgb_matrix_config; #define LC_RU_G LC_HSV(153, 255, 153) #define LC_RU_K LC_HSV( 0, 255, 145) +// Language keymap ids for layers. +// See layers/definitions/symbols.h. +enum layer_layout_ids { + LAYOUT_NULL, // This layer doesn't care what keymap the OS is using + LAYOUT_EN, // This layer is designed for the standard keymapping + LAYOUT_RU, +}; + // Define custom keys -// (Must be done BEFORE keymaps) +// (Must be done before keymaps are loaded) enum custom_keycodes { M_SHUTDOWN = BETA_SAFE_RANGE, M_RESETWM, diff --git a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/arrows.h b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/arrows.h index 98fca86..ed83cd7 100644 --- a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/arrows.h +++ b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/arrows.h @@ -2,6 +2,11 @@ #define BETA_LAYER_MAGIC_MACRO BETA_LAYER(ARROWS) +#ifdef BETA_LAYER_LAYOUTS + #define BETA_LAYER_CONTEXT_DATA LAYOUT_NULL + BETA_LAYER_MAGIC_MACRO +#endif + #ifdef BETA_LAYER_KEYS #define BETA_LAYER_CONTEXT_DATA LAYOUT_ergodox(\ KC_TRANSPARENT, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO,\ diff --git a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/desktop.h b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/desktop.h index dcb8c34..fa29807 100644 --- a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/desktop.h +++ b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/desktop.h @@ -2,6 +2,11 @@ #define BETA_LAYER_MAGIC_MACRO BETA_LAYER(DESKTOP) +#ifdef BETA_LAYER_LAYOUTS + #define BETA_LAYER_CONTEXT_DATA LAYOUT_EN + BETA_LAYER_MAGIC_MACRO +#endif + #ifdef BETA_LAYER_KEYS #define BETA_LAYER_CONTEXT_DATA LAYOUT_ergodox(\ KC_TRANSPARENT, LGUI(KC_1), LGUI(KC_2), LGUI(KC_3), LGUI(KC_4), LGUI(KC_5), TO(LAYER_MAIN),\ diff --git a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/fkeys.h b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/fkeys.h index 19b5049..214b4fc 100644 --- a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/fkeys.h +++ b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/fkeys.h @@ -2,6 +2,11 @@ #define BETA_LAYER_MAGIC_MACRO BETA_LAYER(FKEYS) +#ifdef BETA_LAYER_LAYOUTS + #define BETA_LAYER_CONTEXT_DATA LAYOUT_NULL + BETA_LAYER_MAGIC_MACRO +#endif + #ifdef BETA_LAYER_KEYS #define BETA_LAYER_CONTEXT_DATA LAYOUT_ergodox(\ KC_NO, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_NO,\ diff --git a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/keyboard.h b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/keyboard.h index c28072a..c4a4e7b 100644 --- a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/keyboard.h +++ b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/keyboard.h @@ -2,6 +2,11 @@ #define BETA_LAYER_MAGIC_MACRO BETA_LAYER(KEYBOARD) +#ifdef BETA_LAYER_LAYOUTS + #define BETA_LAYER_CONTEXT_DATA LAYOUT_EN + BETA_LAYER_MAGIC_MACRO +#endif + #ifdef BETA_LAYER_KEYS #define BETA_LAYER_CONTEXT_DATA LAYOUT_ergodox(\ RGB_TOG, RGB_HUD, RGB_HUI, KC_NO, KC_NO, KC_NO, KC_NO,\ diff --git a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/main.h b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/main.h index 4f45084..d99a230 100644 --- a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/main.h +++ b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/main.h @@ -2,6 +2,11 @@ #define BETA_LAYER_MAGIC_MACRO BETA_LAYER(MAIN) +#ifdef BETA_LAYER_LAYOUTS + #define BETA_LAYER_CONTEXT_DATA LAYOUT_EN + BETA_LAYER_MAGIC_MACRO +#endif + #ifdef BETA_LAYER_KEYS #define BETA_LAYER_CONTEXT_DATA LAYOUT_ergodox(\ KC_ESCAPE, KC_1, KC_2, KC_3, KC_4, KC_5, OSL(LAYER_DESKTOP),\ diff --git a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/numpad.h b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/numpad.h index 9369cb7..ee6b326 100644 --- a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/numpad.h +++ b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/numpad.h @@ -2,6 +2,11 @@ #define BETA_LAYER_MAGIC_MACRO BETA_LAYER(NUMPAD) +#ifdef BETA_LAYER_LAYOUTS + #define BETA_LAYER_CONTEXT_DATA LAYOUT_EN + BETA_LAYER_MAGIC_MACRO +#endif + #ifdef BETA_LAYER_KEYS #define BETA_LAYER_CONTEXT_DATA LAYOUT_ergodox(\ KC_TRANSPARENT, KC_CIRC, KC_NO, KC_HASH, KC_DLR, KC_PERC, KC_NO,\ diff --git a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/russian.h b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/russian.h index 9c044bb..75fd67c 100644 --- a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/russian.h +++ b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/russian.h @@ -2,6 +2,11 @@ #define BETA_LAYER_MAGIC_MACRO BETA_LAYER(RUSSIAN) +#ifdef BETA_LAYER_LAYOUTS + #define BETA_LAYER_CONTEXT_DATA LAYOUT_RU + BETA_LAYER_MAGIC_MACRO +#endif + #ifdef BETA_LAYER_KEYS #define BETA_LAYER_CONTEXT_DATA LAYOUT_ergodox(\ KC_TRANSPARENT, RU_1, RU_2, RU_3, RU_4, RU_5, KC_TRANSPARENT,\ diff --git a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/symbols.h b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/symbols.h index 15c82f7..c4d2574 100644 --- a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/symbols.h +++ b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/symbols.h @@ -13,6 +13,15 @@ #define BETA_LAYER_MAGIC_MACRO BETA_LAYER(SYMBOLS) +// What OS keyboard layout this layer is designed for. +// See keymap.h for possible values. +// +// Every layer must have this section. +#ifdef BETA_LAYER_LAYOUTS + #define BETA_LAYER_CONTEXT_DATA LAYOUT_EN + BETA_LAYER_MAGIC_MACRO +#endif + // If BETA_LAYER_NAME is defined, the preprocessor is making // enums for this layer. See keymap.h. @@ -28,7 +37,7 @@ #ifdef BETA_LAYER_KEYS #define BETA_LAYER_CONTEXT_DATA LAYOUT_ergodox(\ KC_NO, KC_EXLM, KC_AT, KC_HASH, KC_DLR, KC_PERC, KC_NO,\ - KC_NO, KC_GRAVE, KC_LPRN, KC_RPRN, KC_QUES, KC_LCBR, KC_NO,\ + KC_NO, KC_GRAVE, KC_LPRN, KC_RPRN, KC_QUES, KC_LCBR, KC_TRNS,\ KC_NO, KC_TILD, KC_QUOTE, KC_COMMA, KC_DOT, KC_DQUO,\ KC_NO, KC_NO, KC_LBRACKET, KC_RBRACKET, KC_EXLM, KC_RCBR, KC_NO,\ KC_NO, KC_NO, KC_NO, KC_NO, KC_NO,\ diff --git a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/symbols_ru.h b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/symbols_ru.h index 1a9dba4..7b097cb 100644 --- a/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/symbols_ru.h +++ b/keyboards/betalupi_ergodox/keymaps/default/layers/definitions/symbols_ru.h @@ -2,6 +2,11 @@ #define BETA_LAYER_MAGIC_MACRO BETA_LAYER(SYMBOLS_RU) +#ifdef BETA_LAYER_LAYOUTS + #define BETA_LAYER_CONTEXT_DATA LAYOUT_RU + BETA_LAYER_MAGIC_MACRO +#endif + #ifdef BETA_LAYER_KEYS #define BETA_LAYER_CONTEXT_DATA LAYOUT_ergodox(\ KC_NO, RU_EXLM, RU_DQUO, RU_NUM, RU_SCLN, RU_PERC, KC_NO,\ diff --git a/keyboards/betalupi_ergodox/keymaps/default/layers/layers.c b/keyboards/betalupi_ergodox/keymaps/default/layers/layers.c index c36634a..66a0cd7 100644 --- a/keyboards/betalupi_ergodox/keymaps/default/layers/layers.c +++ b/keyboards/betalupi_ergodox/keymaps/default/layers/layers.c @@ -20,6 +20,14 @@ const uint8_t PROGMEM ledmaps[][DRIVER_LED_TOTAL][3] = { #undef BETA_LAYER #undef BETA_LAYER_LEDS +// Create os layout array +#define BETA_LAYER_LAYOUTS +#define BETA_LAYER(name) [LAYER_##name] = BETA_LAYER_CONTEXT_DATA, +uint8_t layer_layouts[] = { + #include LAYER_INCLUDE_FILE +}; +#undef BETA_LAYER +#undef BETA_LAYER_LAYOUTS void set_layer_color(int layer) { @@ -70,7 +78,7 @@ void rgb_matrix_indicators_user(void) { -uint32_t layer_state_set_user(uint32_t state) { +uint32_t set_layer_colors(uint32_t state) { uint8_t layer = biton32(state); diff --git a/keyboards/betalupi_ergodox/keymaps/default/layers/layers.h b/keyboards/betalupi_ergodox/keymaps/default/layers/layers.h index d9956fd..70a23d1 100644 --- a/keyboards/betalupi_ergodox/keymaps/default/layers/layers.h +++ b/keyboards/betalupi_ergodox/keymaps/default/layers/layers.h @@ -33,8 +33,9 @@ enum led_indices { //const uint16_t PROGMEM keymaps[X_LAYER_MAX][MATRIX_ROWS][MATRIX_COLS]; //const uint8_t PROGMEM ledmaps[X_LAYER_LEDS_MAX][DRIVER_LED_TOTAL][3]; +extern uint8_t layer_layouts[]; void set_layer_color(int layer); void rgb_matrix_indicators_user(void); -uint32_t layer_state_set_user(uint32_t state); +uint32_t set_layer_colors(uint32_t state); diff --git a/keyboards/betalupi_ergodox/rawhid.c b/keyboards/betalupi_ergodox/rawhid.c index 1b6f59f..9b877f8 100644 --- a/keyboards/betalupi_ergodox/rawhid.c +++ b/keyboards/betalupi_ergodox/rawhid.c @@ -1,6 +1,7 @@ #include "rawhid.h" #include "extra_mappings.h" +extern uint8_t layer_layouts[]; #include "spellcheck.h" // See rawhid.h for prococol documentation @@ -36,14 +37,35 @@ void raw_hid_receive(uint8_t *data, uint8_t length) { } +uint8_t nth_byte(size_t n, uint32_t data) { + return (data >> (8 * n)) & 0xFF; +} -void hid_send_state() { +// state: layer state. +// If this is 0, we'll use global layer_state instead. +void _hid_send_state(_hid_send_state_args args) { + + uint32_t state = args.state ? args.state : layer_state; + uint8_t packet[RAW_EPSIZE] = { - CMD_SEND_STATE + CMD_SEND_STATE, + + // Animation state, set later + 0x00, + + // Layer state + nth_byte(0, state), + nth_byte(1, state), + nth_byte(2, state), + nth_byte(3, state), + + // Keymap for active layer + layer_layouts[biton32(state)] }; + // Set second byte if (rgb_matrix_get_flags() != LED_FLAG_ALL) { // RGB matrix is disabled packet[1] = 0x00; diff --git a/keyboards/betalupi_ergodox/rawhid.h b/keyboards/betalupi_ergodox/rawhid.h index 650aee1..3a1aa8d 100644 --- a/keyboards/betalupi_ergodox/rawhid.h +++ b/keyboards/betalupi_ergodox/rawhid.h @@ -4,12 +4,26 @@ void raw_hid_receive(uint8_t *data, uint8_t length); -void hid_send_state(void); void hid_send_word(void); +// hid_send_state with advanced arguments +typedef struct { + uint32_t state; +} _hid_send_state_args; +void _hid_send_state(_hid_send_state_args args); + +#define hid_send_state(...) _hid_send_state( \ + (_hid_send_state_args) { \ + .state = 0, \ + __VA_ARGS__ \ + } \ +) + + void cmd_animation(uint8_t *data, uint8_t length); + #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS extern uint8_t g_rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS]; #endif @@ -35,15 +49,28 @@ extern uint8_t g_rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS]; // Send keyboard state to host. // // Packet structure: -// Data: | cmd | anim state | -// # of Bytes: | 1 | 1 | +// Data: | cmd | anim state | layer state | layer layout | +// # of Bytes: | 1 | 1 | 4 | 1 | // // anim state: // 0x00: RGBMatrix disabled // 0x01: normal animation, no HID data. // 0x02: FFT Animation +// +// layer state: layer state right now. +// This is a uint32_t, where each bit corresponds to a layer index. +// Lowest-order bit is base layer, highest bit is layer 31. +// Layer indices are defined by the LAYER_* enum in layer.h, +// host interface should have a matching enum. +// Make sure to update it when you change your layers! +// +// layer layout: +// The layout this layer was designed for. +// 0x00: en_us +// 0x01: russian #define CMD_SEND_STATE 0x02 + // Sent by keyboard to host when a complete word is typed. // Host checks if this is a known word. // If it is not, host responds with the same CMD (see below).