Updated to use the new plugin APIs

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/365/head
Gergely Nagy 7 years ago
parent 50073dc27b
commit ee5cc576a0

@ -21,11 +21,14 @@ This can be done from a macro, or via the `AlphaSquareEffect` LED mode.
```c++ ```c++
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
#include <Kaleidoscope-LEDControl.h>
#include <Kaleidoscope-LED-AlphaSquare.h> #include <Kaleidoscope-LED-AlphaSquare.h>
void setup() { KALEIDOSCOPE_INIT_PLUGINS(LEDControl,
Kaleidoscope.use(&AlphaSquare, &AlphaSquareEffect); AlphaSquare,
AlphaSquareEffect);
void setup() {
Kaleidoscope.setup(); Kaleidoscope.setup();
AlphaSquare.display (Key_A); AlphaSquare.display (Key_A);

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-LED-AlphaSquare -- 4x4 pixel LED alphabet * 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 * 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
@ -21,6 +21,7 @@
#include <Kaleidoscope-LED-AlphaSquare.h> #include <Kaleidoscope-LED-AlphaSquare.h>
#include <Kaleidoscope-Macros.h> #include <Kaleidoscope-Macros.h>
// *INDENT-OFF*
const Key keymaps[][ROWS][COLS] PROGMEM = { const Key keymaps[][ROWS][COLS] PROGMEM = {
[0] = KEYMAP_STACKED [0] = KEYMAP_STACKED
( (
@ -40,6 +41,7 @@ const Key keymaps[][ROWS][COLS] PROGMEM = {
Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl,
Key_skip), Key_skip),
}; };
// *INDENT-ON*
const macro_t *macroAction(uint8_t macro_index, uint8_t key_state) { const macro_t *macroAction(uint8_t macro_index, uint8_t key_state) {
if (!keyToggledOn(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; return MACRO_NONE;
} }
void setup() { KALEIDOSCOPE_INIT_PLUGINS(LEDControl,
Kaleidoscope.use(&AlphaSquare, &AlphaSquareEffect, &Macros); AlphaSquare,
AlphaSquareEffect,
Macros);
void setup() {
Kaleidoscope.setup(); Kaleidoscope.setup();
AlphaSquare.color = { 0xcb, 0xc0, 0xff }; AlphaSquare.color = { 0xcb, 0xc0, 0xff };

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-LED-AlphaSquare -- 4x4 pixel LED alphabet * 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 * 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,10 +24,6 @@ uint16_t AlphaSquareEffect::length = 1000;
uint32_t AlphaSquareEffect::end_time_left_, AlphaSquareEffect::end_time_right_; uint32_t AlphaSquareEffect::end_time_left_, AlphaSquareEffect::end_time_right_;
Key AlphaSquareEffect::last_key_left_, AlphaSquareEffect::last_key_right_; Key AlphaSquareEffect::last_key_left_, AlphaSquareEffect::last_key_right_;
void AlphaSquareEffect::setup(void) {
Kaleidoscope.useEventHandlerHook(eventHandlerHook);
}
void AlphaSquareEffect::update(void) { void AlphaSquareEffect::update(void) {
if (end_time_left_ && millis() > end_time_left_) { if (end_time_left_ && millis() > end_time_left_) {
::AlphaSquare.clear(last_key_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) if (::LEDControl.get_mode() != &::AlphaSquareEffect)
return key; return EventHandlerResult::OK;
if (key_state & INJECTED) if (keyState & INJECTED)
return key; return EventHandlerResult::OK;
if (key < Key_A || key > Key_0) if (mappedKey < Key_A || mappedKey > Key_0)
return key; return EventHandlerResult::OK;
if (!keyIsPressed(key_state)) if (!keyIsPressed(keyState))
return key; return EventHandlerResult::OK;
uint8_t display_col = 2; uint8_t display_col = 2;
Key prev_key = last_key_left_; Key prev_key = last_key_left_;
if (col < COLS / 2) { if (col < COLS / 2) {
last_key_left_ = key; last_key_left_ = mappedKey;
end_time_left_ = millis() + length; end_time_left_ = millis() + length;
} else { } else {
prev_key = last_key_right_; prev_key = last_key_right_;
last_key_right_ = key; last_key_right_ = mappedKey;
end_time_right_ = millis() + length; end_time_right_ = millis() + length;
display_col = 10; display_col = 10;
} }
if (prev_key != key) if (prev_key != mappedKey)
::AlphaSquare.clear(prev_key, display_col); ::AlphaSquare.clear(prev_key, display_col);
::AlphaSquare.display(key, display_col); ::AlphaSquare.display(mappedKey, display_col);
return key;
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
} }

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-LED-AlphaSquare -- 4x4 pixel LED alphabet * 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 * 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
@ -27,15 +27,19 @@ class AlphaSquareEffect : public LEDMode {
AlphaSquareEffect(void) {} AlphaSquareEffect(void) {}
static uint16_t length; static uint16_t length;
EventHandlerResult onKeyswitchEvent(Key &mappedKey, byte row, byte col, uint8_t keyState);
protected: protected:
void setup(void) final;
void update(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: private:
static uint32_t end_time_left_, end_time_right_; static uint32_t end_time_left_, end_time_right_;
static Key last_key_left_, last_key_right_; static Key last_key_left_, last_key_right_;
static Key eventHandlerHook(Key key, uint8_t row, uint8_t col, uint8_t key_state);
}; };
} }

@ -63,12 +63,6 @@ static const uint16_t alphabet[] PROGMEM = {
cRGB AlphaSquare::color = {0x80, 0x80, 0x80}; 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) { void AlphaSquare::display(Key key, uint8_t row, uint8_t col, cRGB key_color) {
if (key < Key_A || key > Key_0) if (key < Key_A || key > Key_0)
return; return;

@ -33,11 +33,9 @@
p30 << 12 | p31 << 13 | p32 << 14 | p33 << 15 ) p30 << 12 | p31 << 13 | p32 << 14 | p33 << 15 )
namespace kaleidoscope { namespace kaleidoscope {
class AlphaSquare : public KaleidoscopePlugin { class AlphaSquare : public kaleidoscope::Plugin {
public: public:
AlphaSquare(void); AlphaSquare(void) {}
void begin(void) final;
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, cRGB key_color);
static void display(Key key, uint8_t row, uint8_t col); static void display(Key key, uint8_t row, uint8_t col);

Loading…
Cancel
Save