From 71a956e3f2e543b1af8f08fc76d10c35c28db395 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sat, 12 May 2018 17:54:21 +0200 Subject: [PATCH] Updated to use the new plugin APIs Signed-off-by: Gergely Nagy --- README.md | 10 +++++-- examples/FingerPainter/FingerPainter.ino | 16 ++++++++--- src/Kaleidoscope/FingerPainter.cpp | 35 ++++++++++++++++-------- src/Kaleidoscope/FingerPainter.h | 13 ++++++--- 4 files changed, 53 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 294b37b8..bb613727 100644 --- a/README.md +++ b/README.md @@ -24,13 +24,19 @@ and register the `Focus` hooks: ```c++ #include +#include +#include #include #include #include -void setup() { - Kaleidoscope.use(&EEPROMSettings, &FingerPainter, &Focus); +KALEIDOSCOPE_INIT_PLUGINS(LEDControl, + EEPromSettings, + LEDPaletteTheme, + FingerPainter, + Focus); +void setup() { Kaleidoscope.setup(); EEPROMSettings.seal(); diff --git a/examples/FingerPainter/FingerPainter.ino b/examples/FingerPainter/FingerPainter.ino index 0a27b6e6..ac3f45db 100644 --- a/examples/FingerPainter/FingerPainter.ino +++ b/examples/FingerPainter/FingerPainter.ino @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-FingerPainter -- On-the-fly keyboard painting. - * Copyright (C) 2017 Gergely Nagy + * Copyright (C) 2017, 2018 Gergely Nagy * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,12 +18,14 @@ #include #include +#include #include #include #include #include "LED-Off.h" +// *INDENT-OFF* const Key keymaps[][ROWS][COLS] PROGMEM = { [0] = KEYMAP_STACKED (Key_NoKey, Key_1, Key_2, Key_3, Key_4, Key_5, Key_LEDEffectNext, @@ -36,16 +38,22 @@ const Key keymaps[][ROWS][COLS] PROGMEM = { Key_skip, Key_6, Key_7, Key_8, Key_9, Key_0, Key_skip, Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals, - Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote, + Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote, Key_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, Key_skip), }; +// *INDENT-ON* -void setup() { - Kaleidoscope.use(&LEDOff, &EEPROMSettings, &FingerPainter, &Focus); +KALEIDOSCOPE_INIT_PLUGINS(LEDControl, + LEDOff, + EEPROMSettings, + LEDPaletteTheme, + FingerPainter, + Focus); +void setup() { Kaleidoscope.setup(); EEPROMSettings.seal(); diff --git a/src/Kaleidoscope/FingerPainter.cpp b/src/Kaleidoscope/FingerPainter.cpp index ac9aa707..0e72846f 100644 --- a/src/Kaleidoscope/FingerPainter.cpp +++ b/src/Kaleidoscope/FingerPainter.cpp @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-FingerPainter -- On-the-fly keyboard painting. - * Copyright (C) 2017 Gergely Nagy + * Copyright (C) 2017, 2018 Gergely Nagy * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,11 +28,9 @@ namespace kaleidoscope { uint16_t FingerPainter::color_base_; bool FingerPainter::edit_mode_; -void FingerPainter::setup(void) { - Kaleidoscope.use(&::LEDPaletteTheme); - Kaleidoscope.useEventHandlerHook(eventHandlerHook); - +EventHandlerResult FingerPainter::onSetup() { color_base_ = ::LEDPaletteTheme.reserveThemes(1); + return EventHandlerResult::OK; } void FingerPainter::update(void) { @@ -47,15 +45,16 @@ void FingerPainter::toggle(void) { edit_mode_ = !edit_mode_; } -Key FingerPainter::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { +EventHandlerResult FingerPainter::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state) { if (!edit_mode_) - return mapped_key; + return EventHandlerResult::OK; - if (!keyToggledOn(key_state)) - return Key_NoKey; + if (!keyToggledOn(key_state)) { + return EventHandlerResult::EVENT_CONSUMED; + } if (row >= ROWS || col >= COLS) - return Key_NoKey; + return EventHandlerResult::EVENT_CONSUMED; uint8_t color_index = ::LEDPaletteTheme.lookupColorIndexAtPosition(color_base_, KeyboardHardware.getLedIndex(row, col)); @@ -76,7 +75,7 @@ Key FingerPainter::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t ::LEDPaletteTheme.updateColorIndexAtPosition(color_base_, KeyboardHardware.getLedIndex(row, col), color_index); - return Key_NoKey; + return EventHandlerResult::EVENT_CONSUMED; } bool FingerPainter::focusHook(const char *command) { @@ -108,6 +107,20 @@ bool FingerPainter::focusHook(const char *command) { return true; } +// Legacy V1 API +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API +void FingerPainter::begin() { + Kaleidoscope.useEventHandlerHook(legacyEventHandler); +} + +Key FingerPainter::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) { + EventHandlerResult r = ::FingerPainter.onKeyswitchEvent(mapped_key, row, col, key_state); + if (r == EventHandlerResult::OK) + return mapped_key; + return Key_NoKey; +} +#endif + } kaleidoscope::FingerPainter FingerPainter; diff --git a/src/Kaleidoscope/FingerPainter.h b/src/Kaleidoscope/FingerPainter.h index 9e975975..73d3a000 100644 --- a/src/Kaleidoscope/FingerPainter.h +++ b/src/Kaleidoscope/FingerPainter.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-FingerPainter -- On-the-fly keyboard painting. - * Copyright (C) 2017 Gergely Nagy + * Copyright (C) 2017, 2018 Gergely Nagy * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,16 +28,21 @@ class FingerPainter : public LEDMode { static void toggle(void); static bool focusHook(const char *command); + EventHandlerResult onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state); + EventHandlerResult onSetup(); + protected: - void setup(void) final; void update(void) final; void refreshAt(byte row, byte col) final; +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API + void begin(); + static Key legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state); +#endif + private: static uint16_t color_base_; static bool edit_mode_; - - static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state); }; };