From 3748fe7669e56a2f91c634761a8d4a7e76d2816f Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 11 Aug 2017 21:01:27 +0200 Subject: [PATCH 1/2] Update the key cache on layer change and at keyboard setup When we change layers, we want to update the key cache for the whole keyboard, so that LED modes and other things that depend on all keys being up-to-date will work as expected. Do the same at `Kaleidoscope.setup` time, so we start with a good state too. This fixes keyboardio/Kaleidoscope-Numlock#7. Signed-off-by: Gergely Nagy --- src/Kaleidoscope.cpp | 1 + src/layers.cpp | 11 +++++++++++ src/layers.h | 1 + 3 files changed, 13 insertions(+) diff --git a/src/Kaleidoscope.cpp b/src/Kaleidoscope.cpp index e5c38a1e..c50d7d64 100644 --- a/src/Kaleidoscope.cpp +++ b/src/Kaleidoscope.cpp @@ -14,6 +14,7 @@ Kaleidoscope_::setup(void) { // A workaround, so that the compiler does not optimize this out... handleKeyswitchEvent(Key_NoKey, 255, 255, 0); + Layer.updateKeyCache(); } void diff --git a/src/layers.cpp b/src/layers.cpp index 73a3c341..23377a3a 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -80,6 +80,15 @@ Layer_::updateKeyCache(byte row, byte col) { } } +void +Layer_::updateKeyCache(void) { + for (byte row = 0; row < ROWS; row++) { + for (byte col = 0; col < COLS; col++) { + updateKeyCache(row, col); + } + } +} + uint8_t Layer_::top(void) { for (int8_t i = 31; i >= 0; i--) { if (bitRead(LayerState, i)) @@ -97,12 +106,14 @@ void Layer_::on(uint8_t layer) { bitSet(LayerState, layer); if (layer > highestLayer) highestLayer = layer; + updateKeyCache(); } void Layer_::off(uint8_t layer) { bitClear(LayerState, layer); if (layer == highestLayer) highestLayer = top(); + updateKeyCache(); } boolean Layer_::isOn(uint8_t layer) { diff --git a/src/layers.h b/src/layers.h index acc25011..0814bf6f 100644 --- a/src/layers.h +++ b/src/layers.h @@ -33,6 +33,7 @@ class Layer_ { static Key getKeyFromPROGMEM(uint8_t layer, byte row, byte col); static void updateKeyCache(byte row, byte col); + static void updateKeyCache(void); private: static uint8_t highestLayer; From 130845f8978405f8d3a08e6182c48f67cb0f5f67 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 11 Aug 2017 22:49:02 +0200 Subject: [PATCH 2/2] Add a few comments explaining why we call Layer.updateKeyCache() Signed-off-by: Gergely Nagy --- src/Kaleidoscope.cpp | 2 ++ src/layers.cpp | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/Kaleidoscope.cpp b/src/Kaleidoscope.cpp index c50d7d64..3a6bbdd0 100644 --- a/src/Kaleidoscope.cpp +++ b/src/Kaleidoscope.cpp @@ -14,6 +14,8 @@ Kaleidoscope_::setup(void) { // A workaround, so that the compiler does not optimize this out... handleKeyswitchEvent(Key_NoKey, 255, 255, 0); + + // Update the key cache, so we start with a non-empty state. Layer.updateKeyCache(); } diff --git a/src/layers.cpp b/src/layers.cpp index 23377a3a..100d1f7c 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -106,6 +106,9 @@ void Layer_::on(uint8_t layer) { bitSet(LayerState, layer); if (layer > highestLayer) highestLayer = layer; + + // Update the key cache, so that if anything depends on knowing the active + // layout, the layout will be in sync. updateKeyCache(); } @@ -113,6 +116,9 @@ void Layer_::off(uint8_t layer) { bitClear(LayerState, layer); if (layer == highestLayer) highestLayer = top(); + + // Update the key cache, so that if anything depends on knowing the active + // layout, the layout will be in sync. updateKeyCache(); }