diff --git a/src/layers.cpp b/src/layers.cpp index 00814752..d9dfa497 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -5,6 +5,7 @@ static uint8_t DefaultLayer; static uint32_t LayerState; uint8_t Layer_::highestLayer; +uint8_t Layer_::keyMap[ROWS][COLS]; static void handle_keymap_key_event(Key keymapEntry, uint8_t keyState) { if (keymapEntry.keyCode >= MOMENTARY_OFFSET) { @@ -51,18 +52,39 @@ Layer_::Layer_ (void) { defaultLayer (0); } -Key Layer_::lookup(byte row, byte col) { - Key mappedKey; - int8_t layer = highestLayer; +void +Layer_::mergeLayers(void) { + + memset (keyMap, DefaultLayer, ROWS * COLS); + + if (LayerState == (uint32_t)(1 << DefaultLayer)) + return; + + for (uint8_t r = 0; r < ROWS; r++) { + for (uint8_t c = 0; c < COLS; c++) { + int8_t layer = highestLayer; + + while (layer > DefaultLayer) { + if (Layer.isOn (layer)) { + Key mappedKey; - mappedKey.raw = Key_Transparent.raw; + mappedKey.raw = pgm_read_word(&(keymaps[layer][r][c])); - while (mappedKey.raw == Key_Transparent.raw && - layer >= DefaultLayer) { - if (Layer.isOn (layer)) - mappedKey.raw = pgm_read_word(&(keymaps[layer][row][col])); - layer--; + if (mappedKey != Key_Transparent) { + keyMap[r][c] = layer; + break; + } + } + layer--; + } + } } +} + +Key Layer_::lookup(byte row, byte col) { + uint8_t layer = keyMap[row][col]; + Key mappedKey; + mappedKey.raw = pgm_read_word(&(keymaps[layer][row][col])); return mappedKey; } @@ -84,12 +106,14 @@ void Layer_::on (uint8_t layer) { bitSet (LayerState, layer); if (layer > highestLayer) highestLayer = layer; + mergeLayers(); } void Layer_::off (uint8_t layer) { bitClear (LayerState, layer); if (layer == highestLayer) highestLayer = top(); + mergeLayers(); } boolean Layer_::isOn (uint8_t layer) { diff --git a/src/layers.h b/src/layers.h index 1d0714dd..74737df1 100644 --- a/src/layers.h +++ b/src/layers.h @@ -3,6 +3,7 @@ #include #include "key_defs.h" #include "plugin.h" +#include KALEIDOSCOPE_HARDWARE_H class Layer_ { public: @@ -28,6 +29,9 @@ class Layer_ { private: static uint8_t highestLayer; + static uint8_t keyMap[ROWS][COLS]; + + static void mergeLayers(void); }; extern Layer_ Layer;