From ee5cc576a07d30b15f61d35fdc7d40e38566ead7 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 14 May 2018 06:29:56 +0200 Subject: [PATCH] Updated to use the new plugin APIs Signed-off-by: Gergely Nagy --- README.md | 11 +++-- examples/LED-AlphaSquare/LED-AlphaSquare.ino | 13 ++++-- src/Kaleidoscope/AlphaSquare-Effect.cpp | 47 ++++++++++++-------- src/Kaleidoscope/AlphaSquare-Effect.h | 12 +++-- src/Kaleidoscope/LED-AlphaSquare.cpp | 6 --- src/Kaleidoscope/LED-AlphaSquare.h | 6 +-- 6 files changed, 55 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 82b6b5c2..f87f7116 100644 --- a/README.md +++ b/README.md @@ -21,11 +21,14 @@ This can be done from a macro, or via the `AlphaSquareEffect` LED mode. ```c++ #include +#include #include +KALEIDOSCOPE_INIT_PLUGINS(LEDControl, + AlphaSquare, + AlphaSquareEffect); + void setup() { - Kaleidoscope.use(&AlphaSquare, &AlphaSquareEffect); - Kaleidoscope.setup(); AlphaSquare.display (Key_A); @@ -59,8 +62,8 @@ methods or properties other than those provided by all LED modes. ### `.display(symbol, row, col)` ### `.display(symbol, row, col, color)` -> As the previous function, but instead of a key, it expects a 4x4 bitmap in -> the form of a 16-bit unsigned integer, where the low bit is the top-right +> As the previous function, but instead of a key, it expects a 4x4 bitmap in +> the form of a 16-bit unsigned integer, where the low bit is the top-right > corner, the second-lowest bit is to the right of that, and so on. > > The `SYM4x4` macro can be used to simplify creating these bitmaps. diff --git a/examples/LED-AlphaSquare/LED-AlphaSquare.ino b/examples/LED-AlphaSquare/LED-AlphaSquare.ino index cabbdcce..28b755de 100644 --- a/examples/LED-AlphaSquare/LED-AlphaSquare.ino +++ b/examples/LED-AlphaSquare/LED-AlphaSquare.ino @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-LED-AlphaSquare -- 4x4 pixel LED alphabet - * 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 @@ -21,6 +21,7 @@ #include #include +// *INDENT-OFF* const Key keymaps[][ROWS][COLS] PROGMEM = { [0] = KEYMAP_STACKED ( @@ -34,12 +35,13 @@ 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* const macro_t *macroAction(uint8_t macro_index, uint8_t key_state) { if (!keyToggledOn(key_state)) @@ -93,9 +95,12 @@ const macro_t *macroAction(uint8_t macro_index, uint8_t key_state) { return MACRO_NONE; } -void setup() { - Kaleidoscope.use(&AlphaSquare, &AlphaSquareEffect, &Macros); +KALEIDOSCOPE_INIT_PLUGINS(LEDControl, + AlphaSquare, + AlphaSquareEffect, + Macros); +void setup() { Kaleidoscope.setup(); AlphaSquare.color = { 0xcb, 0xc0, 0xff }; diff --git a/src/Kaleidoscope/AlphaSquare-Effect.cpp b/src/Kaleidoscope/AlphaSquare-Effect.cpp index dc1bc208..2c1446fd 100644 --- a/src/Kaleidoscope/AlphaSquare-Effect.cpp +++ b/src/Kaleidoscope/AlphaSquare-Effect.cpp @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-LED-AlphaSquare -- 4x4 pixel LED alphabet - * 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 @@ -24,10 +24,6 @@ uint16_t AlphaSquareEffect::length = 1000; uint32_t AlphaSquareEffect::end_time_left_, AlphaSquareEffect::end_time_right_; Key AlphaSquareEffect::last_key_left_, AlphaSquareEffect::last_key_right_; -void AlphaSquareEffect::setup(void) { - Kaleidoscope.useEventHandlerHook(eventHandlerHook); -} - void AlphaSquareEffect::update(void) { if (end_time_left_ && millis() > end_time_left_) { ::AlphaSquare.clear(last_key_left_); @@ -39,37 +35,52 @@ void AlphaSquareEffect::update(void) { } } -Key AlphaSquareEffect::eventHandlerHook(Key key, byte row, byte col, uint8_t key_state) { +EventHandlerResult AlphaSquareEffect::onKeyswitchEvent(Key &mappedKey, byte row, byte col, uint8_t keyState) { if (::LEDControl.get_mode() != &::AlphaSquareEffect) - return key; + return EventHandlerResult::OK; - if (key_state & INJECTED) - return key; + if (keyState & INJECTED) + return EventHandlerResult::OK; - if (key < Key_A || key > Key_0) - return key; + if (mappedKey < Key_A || mappedKey > Key_0) + return EventHandlerResult::OK; - if (!keyIsPressed(key_state)) - return key; + if (!keyIsPressed(keyState)) + return EventHandlerResult::OK; uint8_t display_col = 2; Key prev_key = last_key_left_; if (col < COLS / 2) { - last_key_left_ = key; + last_key_left_ = mappedKey; end_time_left_ = millis() + length; } else { prev_key = last_key_right_; - last_key_right_ = key; + last_key_right_ = mappedKey; end_time_right_ = millis() + length; display_col = 10; } - if (prev_key != key) + if (prev_key != mappedKey) ::AlphaSquare.clear(prev_key, display_col); - ::AlphaSquare.display(key, display_col); - return key; + ::AlphaSquare.display(mappedKey, display_col); + + return EventHandlerResult::OK; +} + +// Legacy V1 API +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API +void AlphaSquareEffect::setup(void) { + Kaleidoscope.useEventHandlerHook(legacyEventHandler); +} + +Key AlphaSquareEffect::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) { + EventHandlerResult r = ::AlphaSquareEffect.onKeyswitchEvent(mapped_key, row, col, key_state); + if (r == EventHandlerResult::OK) + return mapped_key; + return Key_NoKey; } +#endif } diff --git a/src/Kaleidoscope/AlphaSquare-Effect.h b/src/Kaleidoscope/AlphaSquare-Effect.h index 0f60a452..34381df2 100644 --- a/src/Kaleidoscope/AlphaSquare-Effect.h +++ b/src/Kaleidoscope/AlphaSquare-Effect.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-LED-AlphaSquare -- 4x4 pixel LED alphabet - * 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 @@ -27,15 +27,19 @@ class AlphaSquareEffect : public LEDMode { AlphaSquareEffect(void) {} static uint16_t length; + + EventHandlerResult onKeyswitchEvent(Key &mappedKey, byte row, byte col, uint8_t keyState); protected: - void setup(void) final; void update(void) final; +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API + void setup(void) final; + static Key legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state); +#endif + private: static uint32_t end_time_left_, end_time_right_; static Key last_key_left_, last_key_right_; - - static Key eventHandlerHook(Key key, uint8_t row, uint8_t col, uint8_t key_state); }; } diff --git a/src/Kaleidoscope/LED-AlphaSquare.cpp b/src/Kaleidoscope/LED-AlphaSquare.cpp index 9a7184f0..ac7c608d 100644 --- a/src/Kaleidoscope/LED-AlphaSquare.cpp +++ b/src/Kaleidoscope/LED-AlphaSquare.cpp @@ -63,12 +63,6 @@ static const uint16_t alphabet[] PROGMEM = { cRGB AlphaSquare::color = {0x80, 0x80, 0x80}; -AlphaSquare::AlphaSquare(void) { -} - -void AlphaSquare::begin(void) { -} - void AlphaSquare::display(Key key, uint8_t row, uint8_t col, cRGB key_color) { if (key < Key_A || key > Key_0) return; diff --git a/src/Kaleidoscope/LED-AlphaSquare.h b/src/Kaleidoscope/LED-AlphaSquare.h index 9595d87f..18cb1706 100644 --- a/src/Kaleidoscope/LED-AlphaSquare.h +++ b/src/Kaleidoscope/LED-AlphaSquare.h @@ -33,11 +33,9 @@ p30 << 12 | p31 << 13 | p32 << 14 | p33 << 15 ) namespace kaleidoscope { -class AlphaSquare : public KaleidoscopePlugin { +class AlphaSquare : public kaleidoscope::Plugin { public: - AlphaSquare(void); - - void begin(void) final; + AlphaSquare(void) {} static void display(Key key, uint8_t row, uint8_t col, cRGB key_color); static void display(Key key, uint8_t row, uint8_t col);