From 051778048dcec8e7ae000a3febb70b29c9724826 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sat, 28 Jan 2017 14:37:15 +0100 Subject: [PATCH 1/2] Turn the plugin into a macro The expected usage is now calling `NumLock.toggle(row, col, layer)` from within a macro. Signed-off-by: Gergely Nagy --- src/Keyboardio-LEDEffect-Numlock.cpp | 65 +++++++++++++++++----------- src/Keyboardio-LEDEffect-Numlock.h | 14 ++++-- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/src/Keyboardio-LEDEffect-Numlock.cpp b/src/Keyboardio-LEDEffect-Numlock.cpp index 2f8f9759..d80a6d86 100644 --- a/src/Keyboardio-LEDEffect-Numlock.cpp +++ b/src/Keyboardio-LEDEffect-Numlock.cpp @@ -3,50 +3,63 @@ #include "KeyboardioFirmware.h" #include "layers.h" -static uint8_t numpadIndex; -static uint8_t storedLEDMode; -static uint8_t us; +uint8_t NumLock_::previousLEDMode; +uint8_t NumLock_::us; +bool NumLock_::isActive; +byte NumLock_::row = 255, NumLock_::col = 255; -LEDNumlock::LEDNumlock (uint8_t numpadIdx) { - numpadIndex = numpadIdx; +NumLock_::NumLock_ (void) { } void -LEDNumlock::begin (void) { - us = LEDControl.mode_add (this); - loop_hook_use (this->loopHook); +NumLock_::begin (void) { + us = LEDControl.mode_add (this); } void -LEDNumlock::init (void) { - if (!Layer.isOn (numpadIndex)) { +NumLock_::init (void) { + if (!isActive) { LEDControl.next_mode(); } } void -LEDNumlock::update (void) { - for (uint8_t i = 0; i < 44; i++) { - LEDControl.led_set_crgb_at(i, {0, 0, 0}); - } - for (uint8_t i = 44; i < LED_COUNT; i++) { - LEDControl.led_set_crgb_at(i, {255, 0, 0}); +NumLock_::update (void) { + for (uint8_t r = 0; r < ROWS; r++) { + for (uint8_t c = 0; c < COLS; c++) { + Key k = Layer.lookup (r, c); + + if (k.raw < Key_NumLock.raw || k.raw > Key_KeypadDot.raw) + continue; + + LEDControl.led_set_crgb_at(r, c, {255, 0, 0}); + } } - cRGB color = breath_compute (); - LEDControl.led_set_crgb_at (60, color); + if (row > ROWS || col > COLS) + return; + + cRGB color = breath_compute(); + LEDControl.led_set_crgb_at (row, col, color); } -void -LEDNumlock::loopHook (bool postClear) { - if (!postClear) - return; +const macro_t * +NumLock_::toggle (byte row_, byte col_, uint8_t numPadLayer) { + row = row_; + col = col_; - if (!Layer.isOn (numpadIndex)) { - if (LEDControl.get_mode () != us) - storedLEDMode = LEDControl.get_mode (); - LEDControl.set_mode (storedLEDMode); + if (Layer.isOn (numPadLayer)) { + isActive = false; + LEDControl.set_mode (previousLEDMode); + Layer.off (numPadLayer); } else { + isActive = true; + previousLEDMode = LEDControl.get_mode (); LEDControl.set_mode (us); + Layer.on (numPadLayer); } + + return MACRO(T(NumLock), END); } + +NumLock_ NumLock; diff --git a/src/Keyboardio-LEDEffect-Numlock.h b/src/Keyboardio-LEDEffect-Numlock.h index 46ea71d7..b6af6d55 100644 --- a/src/Keyboardio-LEDEffect-Numlock.h +++ b/src/Keyboardio-LEDEffect-Numlock.h @@ -1,17 +1,25 @@ #pragma once #include "Keyboardio-LEDControl.h" +#include "Keyboardio-Macros.h" #include "LEDUtils.h" -class LEDNumlock : LEDMode { +class NumLock_ : LEDMode { public: - LEDNumlock (uint8_t numpadIndex); + NumLock_ (void); virtual void begin (void) final; virtual void update (void) final; virtual void init (void) final; + static const macro_t *toggle (byte row, byte col, uint8_t numPadLayer); + private: - static void loopHook (bool postClear); + static uint8_t previousLEDMode; + static uint8_t us; + static bool isActive; + static byte row, col; }; + +extern NumLock_ NumLock; From 17046f281e9000a6b6ac5be49c8181f18992aea7 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sun, 29 Jan 2017 08:23:12 +0100 Subject: [PATCH 2/2] Introduce two helper macros `TOGGLENUMLOCK` is the index of the macro, to be used in the `macroAction` function, and `Key_ToggleNumlock` is to be used in the keymap, instead of `M(0)`. This is considerably friendlier than before. Signed-off-by: Gergely Nagy --- src/Keyboardio-LEDEffect-Numlock.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Keyboardio-LEDEffect-Numlock.h b/src/Keyboardio-LEDEffect-Numlock.h index b6af6d55..8a71f8fc 100644 --- a/src/Keyboardio-LEDEffect-Numlock.h +++ b/src/Keyboardio-LEDEffect-Numlock.h @@ -4,6 +4,9 @@ #include "Keyboardio-Macros.h" #include "LEDUtils.h" +#define TOGGLENUMLOCK 0 +#define Key_ToggleNumlock M(TOGGLENUMLOCK) + class NumLock_ : LEDMode { public: NumLock_ (void);