From f3f1748acd6f6f09a916826d21cbf008d57a247a Mon Sep 17 00:00:00 2001 From: David Hoelscher Date: Tue, 24 Oct 2023 23:56:58 -0500 Subject: [PATCH 1/2] fix default layer persistence to support up to 32 single layer or 8 bitmasked layers --- docs/feature_layers.md | 2 ++ keyboards/satt/comet46/keymaps/default-rgbled/keymap.c | 6 +++++- keyboards/satt/comet46/keymaps/default/keymap.c | 4 ++++ quantum/eeconfig.c | 4 ++++ quantum/quantum.c | 4 ++++ 5 files changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/feature_layers.md b/docs/feature_layers.md index e57642071f4a..551962eb6d74 100644 --- a/docs/feature_layers.md +++ b/docs/feature_layers.md @@ -53,6 +53,8 @@ Layers stack on top of each other in numerical order. When determining what a ke Sometimes, you might want to switch between layers in a macro or as part of a tap dance routine. `layer_on` activates a layer, and `layer_off` deactivates it. More layer-related functions can be found in [action_layer.h](https://github.com/qmk/qmk_firmware/blob/master/quantum/action_layer.h). +If you want to save multiple persistent default layers, you will want to avoid using the `set_single_persistent_default_layer` function which only supports one default persistent layer at a time. Instead, you can `#define DEFAULT_LAYER_BITMASK_ENABLE` and call the `eeconfig_update_default_layer` function directly, passing it an 8-bit wide bitfield representing the least significant byte of a layer_state_t variable. Note that this approach is limited to storing persistent default layers only between layers 0 and 7. + ## Functions :id=functions There are a number of functions (and variables) related to how you can use or manipulate the layers. diff --git a/keyboards/satt/comet46/keymaps/default-rgbled/keymap.c b/keyboards/satt/comet46/keymaps/default-rgbled/keymap.c index 1e96bd7fbcea..569db8caa480 100644 --- a/keyboards/satt/comet46/keymaps/default-rgbled/keymap.c +++ b/keyboards/satt/comet46/keymaps/default-rgbled/keymap.c @@ -174,7 +174,11 @@ void matrix_init_user(void) { void matrix_scan_user(void) { uint8_t layer = get_highest_layer(layer_state); - uint8_t default_layer = biton32(eeconfig_read_default_layer()); +#ifdef DEFAULT_LAYER_BITMASK_ENABLE + uint8_t default_layer = get_highest_layer(eeconfig_read_default_layer()); +#else + uint8_t default_layer = eeconfig_read_default_layer(); +#endif switch (layer) { case _LOWER: set_led_red; diff --git a/keyboards/satt/comet46/keymaps/default/keymap.c b/keyboards/satt/comet46/keymaps/default/keymap.c index 3e6f9045ffe7..3e0d6b5c06f5 100644 --- a/keyboards/satt/comet46/keymaps/default/keymap.c +++ b/keyboards/satt/comet46/keymaps/default/keymap.c @@ -158,7 +158,11 @@ bool oled_task_user(void) { char layer_str[22]; oled_write_P(PSTR("Layer: "), false); uint8_t layer = get_highest_layer(layer_state); +#ifdef DEFAULT_LAYER_BITMASK_ENABLE uint8_t default_layer = get_highest_layer(eeconfig_read_default_layer()); +#else + uint8_t default_layer = eeconfig_read_default_layer(); +#endif switch (layer) { case _QWERTY: switch (default_layer) { diff --git a/quantum/eeconfig.c b/quantum/eeconfig.c index 40690d6a97a6..4ed221d4d01f 100644 --- a/quantum/eeconfig.c +++ b/quantum/eeconfig.c @@ -51,7 +51,11 @@ void eeconfig_init_quantum(void) { eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER); eeprom_update_byte(EECONFIG_DEBUG, 0); +#if defined(DEFAULT_LAYER_BITMASK_ENABLE) default_layer_state = (layer_state_t)1 << 0; +#else + default_layer_state = 0; +#endif eeprom_update_byte(EECONFIG_DEFAULT_LAYER, default_layer_state); // Enable oneshot and autocorrect by default: 0b0001 0100 0000 0000 eeprom_update_word(EECONFIG_KEYMAP, 0x1400); diff --git a/quantum/quantum.c b/quantum/quantum.c index 6639dc229109..daef4010f2b9 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -483,7 +483,11 @@ void set_single_persistent_default_layer(uint8_t default_layer) { #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS) PLAY_SONG(default_layer_songs[default_layer]); #endif +#if defined(DEFAULT_LAYER_BITMASK_ENABLE) eeconfig_update_default_layer((layer_state_t)1 << default_layer); +#else + eeconfig_update_default_layer(default_layer); +#endif default_layer_set((layer_state_t)1 << default_layer); } From 96c8ec2703eeaa016b8d69fa300bc8381fa4428f Mon Sep 17 00:00:00 2001 From: David Hoelscher Date: Mon, 8 Apr 2024 02:28:30 -0500 Subject: [PATCH 2/2] adding back in initialization of default layer --- quantum/keyboard.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/quantum/keyboard.c b/quantum/keyboard.c index df1dc1c3ee09..71190a8efbdb 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -394,7 +394,13 @@ void quantum_init(void) { #endif /* read here just incase bootmagic process changed its value */ +#if defined(DEFAULT_LAYER_BITMASK_ENABLE) + /* stored as 8-bit-wide bitmask, so write the value directly to default_layer */ layer_state_t default_layer = (layer_state_t)eeconfig_read_default_layer(); +#else + /* stored as a layer number, so left shift 1 by the stored value */ + layer_state_t default_layer = (layer_state_t)(1UL << eeconfig_read_default_layer()); +#endif default_layer_set(default_layer); /* Also initialize layer state to trigger callback functions for layer_state */