Merge pull request #7 from keyboardio/f/plugin-v2

Updated to use the new plugin APIs
pull/389/head
Gergely Nagy 6 years ago committed by GitHub
commit fb787d59ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -24,6 +24,7 @@ include the header, and make sure the plugin is in use:
```c++ ```c++
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
#include <Kaleidoscope-LEDControl.h>
#include <Kaleidoscope-Heatmap.h> #include <Kaleidoscope-Heatmap.h>
static const cRGB heat_colors[] PROGMEM = { static const cRGB heat_colors[] PROGMEM = {
@ -33,6 +34,8 @@ static const cRGB heat_colors[] PROGMEM = {
{ 25, 25, 255} // red { 25, 25, 255} // red
}; };
KALEIDOSCOPE_INIT_PLUGINS(LEDControl, HeatmapEffect);
void setup() { void setup() {
Kaleidoscope.use(&HeatmapEffect); Kaleidoscope.use(&HeatmapEffect);

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-Heatmap -- Heatmap LED effect for Kaleidoscope. * 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 * 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 * it under the terms of the GNU General Public License as published by
@ -17,9 +17,10 @@
*/ */
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
#include <Kaleidoscope-LEDControl.h>
#include <Kaleidoscope-Heatmap.h> #include <Kaleidoscope-Heatmap.h>
// *INDENT-OFF*
const Key keymaps[][ROWS][COLS] PROGMEM = { const Key keymaps[][ROWS][COLS] PROGMEM = {
[0] = KEYMAP_STACKED [0] = KEYMAP_STACKED
(Key_LEDEffectNext, Key_1, Key_2, Key_3, Key_4, Key_5, Key_LEDEffectNext, (Key_LEDEffectNext, Key_1, Key_2, Key_3, Key_4, Key_5, Key_LEDEffectNext,
@ -38,10 +39,12 @@ const Key keymaps[][ROWS][COLS] PROGMEM = {
Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl,
Key_NoKey), Key_NoKey),
}; };
// *INDENT-ON*
void setup() { KALEIDOSCOPE_INIT_PLUGINS(LEDControl,
Kaleidoscope.use(&HeatmapEffect); HeatmapEffect);
void setup() {
Kaleidoscope.setup(); Kaleidoscope.setup();
HeatmapEffect.activate(); HeatmapEffect.activate();

@ -123,22 +123,17 @@ void Heatmap::resetMap(void) {
highest_ = 1; highest_ = 1;
} }
void Heatmap::setup(void) { EventHandlerResult Heatmap::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state) {
Kaleidoscope.useEventHandlerHook(eventHook);
Kaleidoscope.useLoopHook(loopHook);
}
Key Heatmap::eventHook(Key mapped_key, byte row, byte col, uint8_t key_state) {
// this methode is called frequently by Kaleidoscope // this methode is called frequently by Kaleidoscope
// even if the module isn't activated // even if the module isn't activated
// if it is a synthetic key, skip it // if it is a synthetic key, skip it
if (key_state & INJECTED) if (key_state & INJECTED)
return mapped_key; return EventHandlerResult::OK;
// if the key is not toggled on, skip it // if the key is not toggled on, skip it
if (!keyToggledOn(key_state)) if (!keyToggledOn(key_state))
return mapped_key; return EventHandlerResult::OK;
// increment the heatmap_ value related to the key // increment the heatmap_ value related to the key
heatmap_[row][col]++; heatmap_[row][col]++;
@ -155,10 +150,10 @@ Key Heatmap::eventHook(Key mapped_key, byte row, byte col, uint8_t key_state) {
shiftStats(); shiftStats();
} }
return mapped_key; return EventHandlerResult::OK;
} }
void Heatmap::loopHook(bool is_post_clear) { EventHandlerResult Heatmap::beforeEachCycle() {
// this methode is called frequently by Kaleidoscope // this methode is called frequently by Kaleidoscope
// even if the module isn't activated // 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]. // between heat_colors[x] and heat_colors[x+1].
if (highest_ > (static_cast<uint16_t>(heat_colors_length) << 9)) if (highest_ > (static_cast<uint16_t>(heat_colors_length) << 9))
shiftStats(); shiftStats();
return EventHandlerResult::OK;
} }
void Heatmap::update(void) { 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
} }

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-Heatmap -- Heatmap LED effect for Kaleidoscope. * 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 * 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 * it under the terms of the GNU General Public License as published by
@ -24,17 +24,25 @@
namespace kaleidoscope { namespace kaleidoscope {
class Heatmap : public LEDMode { class Heatmap : public LEDMode {
public: public:
Heatmap(void); Heatmap(void) {}
static uint16_t update_delay; static uint16_t update_delay;
static const cRGB *heat_colors; static const cRGB *heat_colors;
static uint8_t heat_colors_length; static uint8_t heat_colors_length;
void resetMap(void); void resetMap(void);
EventHandlerResult onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state);
EventHandlerResult beforeEachCycle();
protected: protected:
void setup(void) final;
void update(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: private:
static uint16_t heatmap_[ROWS][COLS]; static uint16_t heatmap_[ROWS][COLS];
static uint16_t highest_; static uint16_t highest_;
@ -42,9 +50,6 @@ class Heatmap : public LEDMode {
static void shiftStats(void); static void shiftStats(void);
static cRGB computeColor(float v); 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);
}; };
} }

Loading…
Cancel
Save