From 00df66d30ada28fe1c1e21f23f570c92cb8c1e94 Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Wed, 29 Nov 2017 12:59:50 -0600 Subject: [PATCH 1/7] Make updateHighestLayer() use layer_count if it's set Now that `layer_count` is (potentially) available, we can start looking for active layers at the top _defined_ layer instead of the top _possible_ layer. This ought to be more efficient, especially for sketches that don't have lots of layers defined. Also introduced the `MAX_LAYERS` constant (#define). --- src/layers.cpp | 13 ++++++++----- src/layers.h | 4 ++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/layers.cpp b/src/layers.cpp index 27d34e88..bd6b0465 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -9,9 +9,9 @@ uint8_t Layer_::activeLayers[ROWS][COLS]; Key(*Layer_::getKey)(uint8_t layer, byte row, byte col) = Layer.getKeyFromPROGMEM; // The total number of defined layers in the firmware sketch keymaps[] -// array. If the keymap wasn't defined using CREATE_KEYMAP() in the -// sketch file, layer_count gets the default value of zero. -uint8_t layer_count __attribute__((weak)) = 0; +// array. If the keymap wasn't defined using KEYMAPS(), set it to the +// highest possible number of layers. +uint8_t layer_count __attribute__((weak)) = MAX_LAYERS; static void handleKeymapKeyswitchEvent(Key keymapEntry, uint8_t keyState) { if (keymapEntry.keyCode >= LAYER_SHIFT_OFFSET) { @@ -115,12 +115,15 @@ Layer_::updateActiveLayers(void) { } void Layer_::updateHighestLayer(void) { - for (int8_t i = 31; i >= 0; i--) { + // If layer_count is set, start there, otherwise search from the + // highest possible layer for the top active layer + for (int8_t i = (layer_count - 1); i > 0; i++) { if (bitRead(LayerState, i)) { highestLayer = i; return; } } + // return 0 if no higher active layers were found highestLayer = 0; } @@ -133,7 +136,7 @@ void Layer_::move(uint8_t layer) { void Layer_::on(uint8_t layer) { // If we're trying to turn on a layer that doesn't exist, abort (but // if the keymap wasn't defined using the KEYMAPS() macro, proceed anyway - if (layer_count != 0 && layer >= layer_count) + if (layer >= layer_count) return; bool wasOn = isOn(layer); diff --git a/src/layers.h b/src/layers.h index 3d4ae465..6d9583fb 100644 --- a/src/layers.h +++ b/src/layers.h @@ -11,6 +11,10 @@ const Key keymaps[][ROWS][COLS] PROGMEM = { layers }; \ uint8_t layer_count = sizeof(keymaps) / sizeof(*keymaps); +// The maximum number of layers allowed. `LayerState`, which stores +// the on/off status of the layers in a bitfield has only 32 bits, and +// that should be enough for almost any layout. +#define MAX_LAYERS 32 class Layer_ { public: From e822c6afadaedf5fd56882600eb64cda837977e9 Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Thu, 30 Nov 2017 01:33:51 -0600 Subject: [PATCH 2/7] In case defaultLayer isn't zero, stop counting down there `updateActiveLayers()` makes it impossible to turn off the default layer, so there's no point searching past it for the highest layer, and `defaultLayer` can be set to numbers higher than zero. --- src/layers.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/layers.cpp b/src/layers.cpp index bd6b0465..1a266ff0 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -117,14 +117,15 @@ Layer_::updateActiveLayers(void) { void Layer_::updateHighestLayer(void) { // If layer_count is set, start there, otherwise search from the // highest possible layer for the top active layer - for (int8_t i = (layer_count - 1); i > 0; i++) { + for (int8_t i = (layer_count - 1); i > defaultLayer; i++) { if (bitRead(LayerState, i)) { highestLayer = i; return; } } - // return 0 if no higher active layers were found - highestLayer = 0; + // It's not possible to turn off the default layer (see + // updateActiveLayers()), so if no other layers are active: + highestLayer = defaultLayer; } void Layer_::move(uint8_t layer) { From 154f0f32677a86a58e6c0daa1bd3d9a87005b77e Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Thu, 30 Nov 2017 01:45:10 -0600 Subject: [PATCH 3/7] Moved definition of MAX_LAYERS next to LayerState declaration Also redefined it in terms of the size of LayerState --- src/layers.cpp | 5 +++++ src/layers.h | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/layers.cpp b/src/layers.cpp index 1a266ff0..64960a4a 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -3,6 +3,11 @@ static uint8_t DefaultLayer; static uint32_t LayerState; +// The maximum number of layers allowed. `LayerState`, which stores +// the on/off status of the layers in a bitfield has only 32 bits, and +// that should be enough for almost any layout. +#define MAX_LAYERS sizeof(LayerState) * 8; + uint8_t Layer_::highestLayer; Key Layer_::liveCompositeKeymap[ROWS][COLS]; uint8_t Layer_::activeLayers[ROWS][COLS]; diff --git a/src/layers.h b/src/layers.h index 6d9583fb..55ce8516 100644 --- a/src/layers.h +++ b/src/layers.h @@ -11,11 +11,6 @@ const Key keymaps[][ROWS][COLS] PROGMEM = { layers }; \ uint8_t layer_count = sizeof(keymaps) / sizeof(*keymaps); -// The maximum number of layers allowed. `LayerState`, which stores -// the on/off status of the layers in a bitfield has only 32 bits, and -// that should be enough for almost any layout. -#define MAX_LAYERS 32 - class Layer_ { public: Layer_(void); From 34cea2623d87bda59e17bdd14265b91be0c6ed19 Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Thu, 30 Nov 2017 01:46:40 -0600 Subject: [PATCH 4/7] This iterator should count down, not up --- src/layers.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/layers.cpp b/src/layers.cpp index 64960a4a..9e6643da 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -122,7 +122,7 @@ Layer_::updateActiveLayers(void) { void Layer_::updateHighestLayer(void) { // If layer_count is set, start there, otherwise search from the // highest possible layer for the top active layer - for (int8_t i = (layer_count - 1); i > defaultLayer; i++) { + for (int8_t i = (layer_count - 1); i > defaultLayer; i--) { if (bitRead(LayerState, i)) { highestLayer = i; return; From 1f7d9ed2e8dfdf902d818c949d5309999af62772 Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Thu, 30 Nov 2017 01:58:03 -0600 Subject: [PATCH 5/7] Wrong capitalization of `DefaultLayer` variable --- src/layers.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/layers.cpp b/src/layers.cpp index 9e6643da..ba6a9490 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -122,7 +122,7 @@ Layer_::updateActiveLayers(void) { void Layer_::updateHighestLayer(void) { // If layer_count is set, start there, otherwise search from the // highest possible layer for the top active layer - for (int8_t i = (layer_count - 1); i > defaultLayer; i--) { + for (int8_t i = (layer_count - 1); i > DefaultLayer; i--) { if (bitRead(LayerState, i)) { highestLayer = i; return; @@ -130,7 +130,7 @@ void Layer_::updateHighestLayer(void) { } // It's not possible to turn off the default layer (see // updateActiveLayers()), so if no other layers are active: - highestLayer = defaultLayer; + highestLayer = DefaultLayer; } void Layer_::move(uint8_t layer) { From 904bdd37bf673ced7dc285f430e2fef79875f29f Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Thu, 30 Nov 2017 12:51:44 -0600 Subject: [PATCH 6/7] Removed spurious line deletion --- src/layers.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/layers.h b/src/layers.h index 55ce8516..3d4ae465 100644 --- a/src/layers.h +++ b/src/layers.h @@ -11,6 +11,7 @@ const Key keymaps[][ROWS][COLS] PROGMEM = { layers }; \ uint8_t layer_count = sizeof(keymaps) / sizeof(*keymaps); + class Layer_ { public: Layer_(void); From 4d186e022839029c8239fd1d6c3cf5bb21ab79a1 Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Wed, 6 Dec 2017 14:22:48 -0600 Subject: [PATCH 7/7] Use an unsigned integer for the iterator Also made the comment above more clear --- src/layers.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/layers.cpp b/src/layers.cpp index ba6a9490..36ea9632 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -121,8 +121,8 @@ Layer_::updateActiveLayers(void) { void Layer_::updateHighestLayer(void) { // If layer_count is set, start there, otherwise search from the - // highest possible layer for the top active layer - for (int8_t i = (layer_count - 1); i > DefaultLayer; i--) { + // highest possible layer (MAX_LAYERS) for the top active layer + for (byte i = (layer_count - 1); i > DefaultLayer; i--) { if (bitRead(LayerState, i)) { highestLayer = i; return;