Redo the logic that checks if we need to highlight

Instead of using `Kaleidoscope-Ranges` and custom helper functions with magic
constants to decide whether we need to highlight a key, refresh it, or leave it
alone, use an if-else chain and inner ifs for activity.

Leverages the new `OneShot.isOneShotKey(key)` and `OneShot.isActive(key)`
methods.

The net result is slightly cleaner code (though it can still be improved), and
about 0.2ms saved, along with some PROGMEM space.

Signed-off-by: Gergely Nagy <kaleidoscope@gergo.csillger.hu>
pull/389/head
Gergely Nagy 7 years ago
parent e15aaed8c6
commit 8375838c81

@ -49,7 +49,7 @@ property:
## Dependencies ## Dependencies
* [Kaleidoscope-LEDControl](https://github.com/keyboardio/Kaleidoscope-LEDControl) * [Kaleidoscope-LEDControl](https://github.com/keyboardio/Kaleidoscope-LEDControl)
* [Kaleidoscope-Ranges](https://github.com/keyboardio/Kaleidoscope-Ranges) * [Kaleidoscope-OneShot](https://github.com/keyboardio/Kaleidoscope-OneShot)
## Further reading ## Further reading

@ -17,7 +17,7 @@
*/ */
#include <Kaleidoscope-LED-ActiveModColor.h> #include <Kaleidoscope-LED-ActiveModColor.h>
#include <Kaleidoscope-Ranges.h> #include <Kaleidoscope-OneShot.h>
#include <kaleidoscope/hid.h> #include <kaleidoscope/hid.h>
namespace kaleidoscope { namespace kaleidoscope {
@ -30,42 +30,6 @@ void ActiveModColorEffect::begin(void) {
Kaleidoscope.useLoopHook(loopHook); Kaleidoscope.useLoopHook(loopHook);
} }
uint8_t ActiveModColorEffect::isModifierKeyActive(Key key) {
if (key.raw >= ranges::OSM_FIRST && key.raw <= ranges::OSM_LAST) {
uint8_t idx = key.raw - ranges::OSM_FIRST;
key.flags = 0;
key.keyCode = Key_LeftControl.keyCode + idx;
}
if (key.raw < Key_LeftControl.raw || key.raw > Key_RightGui.raw)
return 0;
if (hid::isModifierKeyActive(key))
return 2;
else
return 1;
}
uint8_t ActiveModColorEffect::isLayerKeyActive(Key key) {
uint8_t layer = 255;
if (key.raw >= ranges::OSL_FIRST && key.raw <= ranges::OSL_LAST) {
layer = key.raw - ranges::OSL_FIRST;
} else if (key.flags == (SYNTHETIC | SWITCH_TO_KEYMAP)) {
layer = key.keyCode;
if (layer >= MOMENTARY_OFFSET)
layer -= MOMENTARY_OFFSET;
}
if (layer == 255)
return 0;
if (Layer.isOn(layer))
return 2;
else
return 1;
}
void ActiveModColorEffect::loopHook(bool is_post_clear) { void ActiveModColorEffect::loopHook(bool is_post_clear) {
if (is_post_clear) if (is_post_clear)
return; return;
@ -73,16 +37,27 @@ void ActiveModColorEffect::loopHook(bool is_post_clear) {
for (byte r = 0; r < ROWS; r++) { for (byte r = 0; r < ROWS; r++) {
for (byte c = 0; c < COLS; c++) { for (byte c = 0; c < COLS; c++) {
Key k = Layer.lookupOnActiveLayer(r, c); Key k = Layer.lookupOnActiveLayer(r, c);
uint8_t is_mod = isModifierKeyActive(k);
uint8_t is_layer = isLayerKeyActive(k);
if (!is_mod && !is_layer) // Neither mod, nor layer key
continue;
if (is_mod == 2 || is_layer == 2) if (::OneShot.isOneShotKey(k)) {
::LEDControl.setCrgbAt(r, c, highlight_color); if (::OneShot.isActive(k))
else ::LEDControl.setCrgbAt(r, c, highlight_color);
::LEDControl.refreshAt(r, c); else
::LEDControl.refreshAt(r, c);
} else if (k.raw >= Key_LeftControl.raw && k.raw <= Key_RightGui.raw) {
if (hid::isModifierKeyActive(k))
::LEDControl.setCrgbAt(r, c, highlight_color);
else
::LEDControl.refreshAt(r, c);
} else if (k.flags == (SYNTHETIC | SWITCH_TO_KEYMAP)) {
uint8_t layer = k.keyCode;
if (layer >= MOMENTARY_OFFSET)
layer -= MOMENTARY_OFFSET;
if (Layer.isOn(layer))
::LEDControl.setCrgbAt(r, c, highlight_color);
else
::LEDControl.refreshAt(r, c);
}
} }
} }
} }

@ -31,8 +31,6 @@ class ActiveModColorEffect : public KaleidoscopePlugin {
static cRGB highlight_color; static cRGB highlight_color;
private: private:
static uint8_t isModifierKeyActive(Key key);
static uint8_t isLayerKeyActive(Key key);
static void loopHook(bool is_post_clear); static void loopHook(bool is_post_clear);
}; };
} }

Loading…
Cancel
Save