From bfe38620bd98144482c9ddd49169349ca85e1914 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sat, 12 May 2018 18:00:45 +0200 Subject: [PATCH] Updated to use the new plugin APIs Signed-off-by: Gergely Nagy --- README.md | 3 +++ examples/Heatmap/Heatmap.ino | 13 +++++++----- src/Kaleidoscope/Heatmap.cpp | 38 +++++++++++++++++++++++++----------- src/Kaleidoscope/Heatmap.h | 17 ++++++++++------ 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 4598c0bc..9d216d30 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ include the header, and make sure the plugin is in use: ```c++ #include +#include #include static const cRGB heat_colors[] PROGMEM = { @@ -33,6 +34,8 @@ static const cRGB heat_colors[] PROGMEM = { { 25, 25, 255} // red }; +KALEIDOSCOPE_INIT_PLUGINS(LEDControl, HeatmapEffect); + void setup() { Kaleidoscope.use(&HeatmapEffect); diff --git a/examples/Heatmap/Heatmap.ino b/examples/Heatmap/Heatmap.ino index 882f33bf..8be87587 100644 --- a/examples/Heatmap/Heatmap.ino +++ b/examples/Heatmap/Heatmap.ino @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-Heatmap -- Heatmap LED effect for Kaleidoscope. - * Copyright (C) 2016, 2017 Gergely Nagy + * Copyright (C) 2016, 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 @@ -17,9 +17,10 @@ */ #include +#include #include - +// *INDENT-OFF* const Key keymaps[][ROWS][COLS] PROGMEM = { [0] = KEYMAP_STACKED (Key_LEDEffectNext, Key_1, Key_2, Key_3, Key_4, Key_5, Key_LEDEffectNext, @@ -32,16 +33,18 @@ 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_NoKey), }; +// *INDENT-ON* -void setup() { - Kaleidoscope.use(&HeatmapEffect); +KALEIDOSCOPE_INIT_PLUGINS(LEDControl, + HeatmapEffect); +void setup() { Kaleidoscope.setup(); HeatmapEffect.activate(); diff --git a/src/Kaleidoscope/Heatmap.cpp b/src/Kaleidoscope/Heatmap.cpp index 8264b7ac..c96b1d2c 100644 --- a/src/Kaleidoscope/Heatmap.cpp +++ b/src/Kaleidoscope/Heatmap.cpp @@ -123,22 +123,17 @@ void Heatmap::resetMap(void) { highest_ = 1; } -void Heatmap::setup(void) { - Kaleidoscope.useEventHandlerHook(eventHook); - Kaleidoscope.useLoopHook(loopHook); -} - -Key Heatmap::eventHook(Key mapped_key, byte row, byte col, uint8_t key_state) { +EventHandlerResult Heatmap::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state) { // this methode is called frequently by Kaleidoscope // even if the module isn't activated // if it is a synthetic key, skip it if (key_state & INJECTED) - return mapped_key; + return EventHandlerResult::OK; // if the key is not toggled on, skip it if (!keyToggledOn(key_state)) - return mapped_key; + return EventHandlerResult::OK; // increment the heatmap_ value related to the key heatmap_[row][col]++; @@ -155,10 +150,10 @@ Key Heatmap::eventHook(Key mapped_key, byte row, byte col, uint8_t key_state) { shiftStats(); } - return mapped_key; + return EventHandlerResult::OK; } -void Heatmap::loopHook(bool is_post_clear) { +EventHandlerResult Heatmap::beforeEachCycle() { // this methode is called frequently by Kaleidoscope // even if the module isn't activated @@ -170,6 +165,8 @@ void Heatmap::loopHook(bool is_post_clear) { // between heat_colors[x] and heat_colors[x+1]. if (highest_ > (static_cast(heat_colors_length) << 9)) shiftStats(); + + return EventHandlerResult::OK; } void Heatmap::update(void) { @@ -201,8 +198,27 @@ void Heatmap::update(void) { } } -Heatmap::Heatmap(void) { +// Legacy V1 API +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API +void Heatmap::begin() { + Kaleidoscope.useEventHandlerHook(legacyEventHandler); + Kaleidoscope.useLoopHook(legacyLoopHook); +} + +Key Heatmap::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) { + EventHandlerResult r = ::HeatmapEffect.onKeyswitchEvent(mapped_key, row, col, key_state); + if (r == EventHandlerResult::OK) + return mapped_key; + return Key_NoKey; +} + +void Heatmap::legacyLoopHook(bool is_post_clear) { + if (is_post_clear) + return; + + ::HeatmapEffect.beforeEachCycle(); } +#endif } diff --git a/src/Kaleidoscope/Heatmap.h b/src/Kaleidoscope/Heatmap.h index 7d7b6639..a7912f54 100644 --- a/src/Kaleidoscope/Heatmap.h +++ b/src/Kaleidoscope/Heatmap.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-Heatmap -- Heatmap LED effect for Kaleidoscope. - * Copyright (C) 2016, 2017 Gergely Nagy + * Copyright (C) 2016, 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,17 +24,25 @@ namespace kaleidoscope { class Heatmap : public LEDMode { public: - Heatmap(void); + Heatmap(void) {} static uint16_t update_delay; static const cRGB *heat_colors; static uint8_t heat_colors_length; void resetMap(void); + EventHandlerResult onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state); + EventHandlerResult beforeEachCycle(); + protected: - void setup(void) final; void update(void) final; +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API + void begin(); + static Key legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state); + static void legacyLoopHook(bool is_post_clear); +#endif + private: static uint16_t heatmap_[ROWS][COLS]; static uint16_t highest_; @@ -42,9 +50,6 @@ class Heatmap : public LEDMode { static void shiftStats(void); static cRGB computeColor(float v); - - static Key eventHook(Key mapped_key, byte row, byte col, uint8_t key_state); - static void loopHook(bool is_post_clear); }; }