From 3b3054d216cfbfa4643d2e6a24892a7aae6088e4 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 5 Oct 2018 13:11:38 +0200 Subject: [PATCH 1/2] Migrate to the onFocusEvent API Migrate from the older `Focus` API to `onFocusEvent` provided by Kaleidoscope. As a side-effect, this drops the per-layer focus command support, as that was a workaround for a Chrysalis bug fixed since. Signed-off-by: Gergely Nagy --- README.md | 36 +++++----- .../LED-Palette-Theme/LED-Palette-Theme.ino | 16 ++--- src/Kaleidoscope/LED-Palette-Theme.cpp | 70 ++++++------------- src/Kaleidoscope/LED-Palette-Theme.h | 15 +--- 4 files changed, 45 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index 5b852f2c..232d07c1 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ full extent, we need to create our own plugin on top of it. #include #include #include -#include +#include namespace example { @@ -36,7 +36,7 @@ class TestLEDMode : public LEDMode { void setup(void) final; void update(void) final; - static bool focusHook(const char *command); + kaleidoscope::EventHandlerResult onFocusEvent(const char *command); private: static uint16_t map_base_; @@ -52,27 +52,26 @@ void TestLEDMode::update(void) { LEDPaletteTheme.updateHandler(map_base_, 0); } -bool -TestLEDMode::focusHook(const char *command) { - return LEDPaletteTheme.themeFocusHandler(command, PSTR("testLedMode.map"), map_base_, 1); +kaleidoscope::EventHandlerResult +TestLEDMode::onFocusEvent(const char *command) { + return LEDPaletteTheme.themeFocusEvent(command, PSTR("testLedMode.map"), map_base_, 1); } } example::TestLEDMode TestLEDMode; -void setup() { - Serial.begin(9600); - - Kaleidoscope.use(&Focus, &LEDPaletteTheme, &TestLEDMode, &EEPROMSettings); +KALEIDOSCOPE_INIT_PLUGINS( + Focus, + LEDPaletteTheme, + TestLEDMode, + EEPROMSettings +); +void setup() { Kaleidoscope.setup(); - EEPROMSettings.seal(); TestLEDMode.activate(); - - Focus.addHook(FOCUS_HOOK_LEDPALETTETHEME); - Focus.addHook(FOCUS_HOOK(TestLEDMode.focusHook, "testLEDMode.map")); } ``` @@ -100,7 +99,7 @@ The plugin provides the `LEDPaletteTheme` object, which has the following method > The `theme` argument can be any index between zero and `max_themes`. How the > plugin decides which theme to display depends entirely on the plugin. -### `.themeFocusHandler(command, expected_command, theme_base, max_themes)` +### `.themeFocusEvent(command, expected_command, theme_base, max_themes)` > To be used in a custom `Focus` handler: handles the `expected_command` Focus > command, and provides a way to query and update the themes supported by the @@ -109,14 +108,11 @@ The plugin provides the `LEDPaletteTheme` object, which has the following method > When queried, it will list the color indexes. When used as a setter, it > expects one index per key. > -> The palette can be set via the `palette` focus command, implemented by the -> `FOCUS_HOOK_LEDPALETTETHEME` hook explained below. +> The palette can be set via the `palette` focus command, provided by the +> `LEDPaletteTheme` plugin. ## Focus commands -The plugin provides a single `Focus` hook, `FOCUS_HOOK_LEDPALETTETHEME`, -implementing the following command: - ### `palette` > Without arguments, prints the palette: RGB values for all 16 colors. @@ -130,7 +126,7 @@ implementing the following command: ## Dependencies * [Kaleidoscope-EEPROM-Settings](https://github.com/keyboardio/Kaleidoscope-EEPROM-Settings) -* [Kaleidoscope-Focus](https://github.com/keyboardio/Kaleidoscope-Focus) +* [Kaleidoscope-FocusSerial](https://github.com/keyboardio/Kaleidoscope-FocusSerial) * [Kaleidoscope-LEDControl](https://github.com/keyboardio/Kaleidoscope-LEDControl) ## Further reading diff --git a/examples/LED-Palette-Theme/LED-Palette-Theme.ino b/examples/LED-Palette-Theme/LED-Palette-Theme.ino index e21ca80f..ac1e63ba 100644 --- a/examples/LED-Palette-Theme/LED-Palette-Theme.ino +++ b/examples/LED-Palette-Theme/LED-Palette-Theme.ino @@ -19,7 +19,7 @@ #include #include #include -#include +#include namespace example { @@ -27,7 +27,7 @@ class TestLEDMode : public kaleidoscope::LEDMode { public: TestLEDMode() {} - static bool focusHook(const char *command); + EventHandlerResult onFocusEvent(const char *command); protected: void setup() final; @@ -49,9 +49,9 @@ TestLEDMode::update(void) { LEDPaletteTheme.updateHandler(map_base_, 0); } -bool -TestLEDMode::focusHook(const char *command) { - return LEDPaletteTheme.themeFocusHandler(command, PSTR("testLedMode.map"), map_base_, 1); +EventHandlerResult +TestLEDMode::onFocusEvent(const char *command) { + return LEDPaletteTheme.themeFocusEvent(command, PSTR("testLedMode.map"), map_base_, 1); } } @@ -83,15 +83,9 @@ const Key keymaps[][ROWS][COLS] PROGMEM = { KALEIDOSCOPE_INIT_PLUGINS(Focus, LEDPaletteTheme, TestLEDMode, EEPROMSettings); void setup() { - Serial.begin(9600); - Kaleidoscope.setup(); - EEPROMSettings.seal(); TestLEDMode.activate(); - - Focus.addHook(FOCUS_HOOK_LEDPALETTETHEME); - Focus.addHook(FOCUS_HOOK(TestLEDMode.focusHook, "testLEDMode.map")); } void loop() { diff --git a/src/Kaleidoscope/LED-Palette-Theme.cpp b/src/Kaleidoscope/LED-Palette-Theme.cpp index 22ec0ef3..6ae4d33f 100644 --- a/src/Kaleidoscope/LED-Palette-Theme.cpp +++ b/src/Kaleidoscope/LED-Palette-Theme.cpp @@ -17,7 +17,7 @@ #include #include -#include +#include #include namespace kaleidoscope { @@ -92,9 +92,14 @@ void LEDPaletteTheme::updateColorIndexAtPosition(uint16_t map_base, uint16_t pos EEPROM.update(map_base + position / 2, indexes); } -bool LEDPaletteTheme::paletteFocusHook(const char *command) { - if (strcmp_P(command, PSTR("palette")) != 0) - return false; +EventHandlerResult LEDPaletteTheme::onFocusEvent(const char *command) { + const char *cmd = PSTR("palette"); + + if (::Focus.handleHelp(command, cmd)) + return EventHandlerResult::OK; + + if (strcmp_P(command, cmd) != 0) + return EventHandlerResult::OK; if (Serial.peek() == '\n') { for (uint8_t i = 0; i < 16; i++) { @@ -105,7 +110,7 @@ bool LEDPaletteTheme::paletteFocusHook(const char *command) { ::Focus.printSpace(); } Serial.println(); - return true; + return EventHandlerResult::EVENT_CONSUMED; } uint8_t i = 0; @@ -120,13 +125,18 @@ bool LEDPaletteTheme::paletteFocusHook(const char *command) { i++; } - return true; + return EventHandlerResult::EVENT_CONSUMED; } -bool LEDPaletteTheme::themeFocusHandler(const char *command, const char *expected_command, - uint16_t theme_base, uint8_t max_themes) { +EventHandlerResult LEDPaletteTheme::themeFocusEvent(const char *command, + const char *expected_command, + uint16_t theme_base, + uint8_t max_themes) { + if (::Focus.handleHelp(command, expected_command)) + return EventHandlerResult::OK; + if (strcmp_P(command, expected_command) != 0) - return false; + return EventHandlerResult::OK; uint16_t max_index = (max_themes * ROWS * COLS) / 2; @@ -140,7 +150,7 @@ bool LEDPaletteTheme::themeFocusHandler(const char *command, const char *expecte ::Focus.printSpace(); } Serial.println(); - return true; + return EventHandlerResult::EVENT_CONSUMED; } uint16_t pos = 0; @@ -154,45 +164,7 @@ bool LEDPaletteTheme::themeFocusHandler(const char *command, const char *expecte pos++; } - return true; -} - -bool LEDPaletteTheme::themeLayerFocusHandler(const char *command, const char *expected_command, - uint16_t theme_base, uint8_t max_themes) { - if (strcmp_P(command, expected_command) != 0) - return false; - - uint16_t count_per_layer = (ROWS * COLS) / 2; - - uint8_t layer = Serial.parseInt(); - - uint16_t offset = theme_base + (layer * count_per_layer); - - if (Serial.peek() == '\n') { - for (uint16_t pos = 0; pos < count_per_layer; pos++) { - uint8_t indexes = EEPROM.read(offset + pos); - - ::Focus.printNumber(indexes >> 4); - ::Focus.printSpace(); - ::Focus.printNumber(indexes & ~0xf0); - ::Focus.printSpace(); - } - Serial.println(); - return true; - } - - uint16_t pos = 0; - - while ((Serial.peek() != '\n') && (pos < count_per_layer)) { - uint8_t idx1 = Serial.parseInt(); - uint8_t idx2 = Serial.parseInt(); - uint8_t indexes = (idx1 << 4) + idx2; - - EEPROM.update(offset + pos, indexes); - pos++; - } - - return true; + return EventHandlerResult::EVENT_CONSUMED; } } diff --git a/src/Kaleidoscope/LED-Palette-Theme.h b/src/Kaleidoscope/LED-Palette-Theme.h index 4483f01e..52bc935d 100644 --- a/src/Kaleidoscope/LED-Palette-Theme.h +++ b/src/Kaleidoscope/LED-Palette-Theme.h @@ -36,15 +36,10 @@ class LEDPaletteTheme : public kaleidoscope::Plugin { static const cRGB lookupPaletteColor(uint8_t palette_index); - static bool paletteFocusHook(const char *command); - static bool themeFocusHandler(const char *command, const char *expected_command, - uint16_t theme_base, uint8_t max_themes); - - static bool themeLayerFocusHandler(const char *command, + EventHandlerResult onFocusEvent(const char *command); + EventHandlerResult themeFocusEvent(const char *command, const char *expected_command, - uint16_t theme_base, - uint8_t max_themes); - + uint16_t theme_base, uint8_t max_themes); EventHandlerResult onSetup(); private: @@ -54,7 +49,3 @@ class LEDPaletteTheme : public kaleidoscope::Plugin { } extern kaleidoscope::LEDPaletteTheme LEDPaletteTheme; - -#define FOCUS_HOOK_LEDPALETTETHEME \ - FOCUS_HOOK(LEDPaletteTheme.paletteFocusHook, \ - "palette") From e6604d7dbcdfedf0f409a5195576f75f9d84b4b0 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Fri, 5 Oct 2018 10:21:08 -0700 Subject: [PATCH 2/2] EventHandlerResult needs to be namespaced to work here --- examples/LED-Palette-Theme/LED-Palette-Theme.ino | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/LED-Palette-Theme/LED-Palette-Theme.ino b/examples/LED-Palette-Theme/LED-Palette-Theme.ino index ac1e63ba..6abc5481 100644 --- a/examples/LED-Palette-Theme/LED-Palette-Theme.ino +++ b/examples/LED-Palette-Theme/LED-Palette-Theme.ino @@ -20,14 +20,13 @@ #include #include #include - namespace example { class TestLEDMode : public kaleidoscope::LEDMode { public: TestLEDMode() {} - EventHandlerResult onFocusEvent(const char *command); + kaleidoscope::EventHandlerResult onFocusEvent(const char *command); protected: void setup() final; @@ -49,7 +48,7 @@ TestLEDMode::update(void) { LEDPaletteTheme.updateHandler(map_base_, 0); } -EventHandlerResult +kaleidoscope::EventHandlerResult TestLEDMode::onFocusEvent(const char *command) { return LEDPaletteTheme.themeFocusEvent(command, PSTR("testLedMode.map"), map_base_, 1); }