diff --git a/README.md b/README.md index aba5be07..58c2eb30 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,9 @@ [travis:image]: https://travis-ci.org/keyboardio/Kaleidoscope-LED-Stalker.svg?branch=master [travis:status]: https://travis-ci.org/keyboardio/Kaleidoscope-LED-Stalker - [st:stable]: https://img.shields.io/badge/stable-✔-black.png?style=flat&colorA=44cc11&colorB=494e52 - [st:broken]: https://img.shields.io/badge/broken-X-black.png?style=flat&colorA=e05d44&colorB=494e52 - [st:experimental]: https://img.shields.io/badge/experimental----black.png?style=flat&colorA=dfb317&colorB=494e52 + [st:stable]: https://img.shields.io/badge/stable-✔-black.svg?style=flat&colorA=44cc11&colorB=494e52 + [st:broken]: https://img.shields.io/badge/broken-X-black.svg?style=flat&colorA=e05d44&colorB=494e52 + [st:experimental]: https://img.shields.io/badge/experimental----black.svg?style=flat&colorA=dfb317&colorB=494e52 The `StalkerEffect` plugin provides an interesting new typing experience: the LEDs light up as you tap keys, and play one of the selected effects: a haunting @@ -21,12 +21,13 @@ To use the plugin, one needs to include the header, and select the effect. #include #include -void setup () { - Kaleidoscope.setup (); +void setup (){ + USE_PLUGINS(&StalkerEffect); - StalkerEffect.configure (STALKER (Haunt, {0xff, 0, 0})); - USE_PLUGINS (&StalkerEffect); - StalkerEffect.activate (); + Kaleidoscope.setup(); + + StalkerEffect.variant = STALKER(Haunt, (CRGB(0, 128, 0))); + StalkerEffect.activate(); } ``` @@ -40,11 +41,13 @@ recommended. The plugin provides the `StalkerEffect` object, which has the following method: -### `.configure(effect)` +### `.variant` > Set the effect to use with the plugin. See below for a list. > > It is recommended to use the `STALKER` macro to declare the effect itself. +> +> Not a method itself, but a changeable value. ### `.stepLength` @@ -57,11 +60,11 @@ method: ## Plugin helpers -### `STALKER(effect, params...)` +### `STALKER(effect, params)` -> Returns an effect, to be used by the `.configure` method of the +> Returns an effect, to be used to assign a value the `.variant` property of the > `StalkerEffect` object. Any arguments given to the macro, are also passed on -> to the effect. If the effect takes no arguments, use `NULL`. +> to the effect. If the effect takes no arguments, use an empty `params` list. ## Plugin effects diff --git a/examples/LED-Stalker/LED-Stalker.ino b/examples/LED-Stalker/LED-Stalker.ino index 80bbd351..438ebac2 100644 --- a/examples/LED-Stalker/LED-Stalker.ino +++ b/examples/LED-Stalker/LED-Stalker.ino @@ -20,7 +20,6 @@ #include #include "LED-Off.h" - const Key keymaps[][ROWS][COLS] PROGMEM = { [0] = KEYMAP_STACKED ( @@ -38,16 +37,15 @@ const Key keymaps[][ROWS][COLS] PROGMEM = { Key_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, - Key_NoKey - ), + Key_NoKey), }; void setup() { - Kaleidoscope.setup(); - - StalkerEffect.configure(STALKER(BlazingTrail, NULL)); USE_PLUGINS(&LEDOff, &StalkerEffect); + Kaleidoscope.setup(); + + StalkerEffect.variant = STALKER(BlazingTrail); StalkerEffect.activate(); } diff --git a/src/Kaleidoscope/LED-Stalker.cpp b/src/Kaleidoscope/LED-Stalker.cpp index 9aa14db7..e48b572d 100644 --- a/src/Kaleidoscope/LED-Stalker.cpp +++ b/src/Kaleidoscope/LED-Stalker.cpp @@ -19,89 +19,79 @@ #include #include -namespace KaleidoscopePlugins { -namespace LEDEffects { -uint8_t StalkerEffect::map[ROWS][COLS]; -StalkerEffect::ColorComputer *StalkerEffect::colorComputer; -uint16_t StalkerEffect::stepLength = 50; -uint32_t StalkerEffect::stepEndTime; +namespace kaleidoscope { -StalkerEffect::StalkerEffect(void) { -} +uint8_t StalkerEffect::map_[ROWS][COLS]; +StalkerEffect::ColorComputer *StalkerEffect::variant; +uint16_t StalkerEffect::step_length = 50; +uint32_t StalkerEffect::step_end_time_; -void -StalkerEffect::configure(ColorComputer *colorComputer_) { - colorComputer = colorComputer_; +StalkerEffect::StalkerEffect(void) { } -void -StalkerEffect::begin(void) { +void StalkerEffect::begin(void) { event_handler_hook_use(eventHandlerHook); LEDMode::begin(); } -void -StalkerEffect::init(void) { - memset(map, 0, sizeof(map)); +void StalkerEffect::init(void) { + memset(map_, 0, sizeof(map_)); } -Key -StalkerEffect::eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState) { +Key StalkerEffect::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { if (row >= ROWS || col >= COLS) - return mappedKey; + return mapped_key; - if (key_is_pressed(keyState)) { - map[row][col] = 0xff; + if (key_is_pressed(key_state)) { + map_[row][col] = 0xff; } - return mappedKey; + return mapped_key; } -void -StalkerEffect::update(void) { - if (!colorComputer) +void StalkerEffect::update(void) { + if (!variant) return; - bool timeOut = millis() >= stepEndTime; + bool time_out = millis() >= step_end_time_; for (byte r = 0; r < ROWS; r++) { for (byte c = 0; c < COLS; c++) { - uint8_t step = map[r][c]; + uint8_t step = map_[r][c]; if (step) { - LEDControl.led_set_crgb_at(r, c, colorComputer->compute(&step)); + LEDControl.led_set_crgb_at(r, c, variant->compute(&step)); } - bool wasZero = (map[r][c] == 0); + bool was_zero = (map_[r][c] == 0); - if (timeOut) { - map[r][c] = step; + if (time_out) { + map_[r][c] = step; } - if (!wasZero && !map[r][c]) + if (!was_zero && !map_[r][c]) LEDControl.led_set_crgb_at(r, c, (cRGB) { 0, 0, 0 }); } } - if (timeOut) - stepEndTime = millis() + stepLength; + if (time_out) + step_end_time_ = millis() + step_length; } -namespace Stalker { +namespace stalker { -cRGB Haunt::highlightColor; +cRGB Haunt::highlight_color_; // Haunt -Haunt::Haunt(const cRGB highlightColor_) { - highlightColor = highlightColor_; +Haunt::Haunt(const cRGB highlight_color) { + highlight_color_ = highlight_color; } -cRGB -Haunt::compute(uint8_t *step) { - cRGB color = CRGB((uint8_t)min(*step * highlightColor.r / 255, 255), - (uint8_t)min(*step * highlightColor.g / 255, 255), - (uint8_t)min(*step * highlightColor.b / 255, 255)); +cRGB Haunt::compute(uint8_t *step) { + cRGB color = CRGB((uint8_t)min(*step * highlight_color_.r / 255, 255), + (uint8_t)min(*step * highlight_color_.g / 255, 255), + (uint8_t)min(*step * highlight_color_.b / 255, 255)); if (*step >= 0xf0) *step -= 1; @@ -116,11 +106,10 @@ Haunt::compute(uint8_t *step) { } // BlazingTrail -BlazingTrail::BlazingTrail(...) { +BlazingTrail::BlazingTrail(void) { } -cRGB -BlazingTrail::compute(uint8_t *step) { +cRGB BlazingTrail::compute(uint8_t *step) { cRGB color; if (*step >= 0xff - 30) { @@ -144,9 +133,8 @@ BlazingTrail::compute(uint8_t *step) { return color; } -}; +} -}; -}; +} -KaleidoscopePlugins::LEDEffects::StalkerEffect StalkerEffect; +kaleidoscope::StalkerEffect StalkerEffect; diff --git a/src/Kaleidoscope/LED-Stalker.h b/src/Kaleidoscope/LED-Stalker.h index 6af2b1b8..f4764b11 100644 --- a/src/Kaleidoscope/LED-Stalker.h +++ b/src/Kaleidoscope/LED-Stalker.h @@ -16,13 +16,14 @@ * along with this program. If not, see . */ +#pragma once + #include #include -#define STALKER(n, ...) (({static KaleidoscopePlugins::LEDEffects::Stalker::n _effect (__VA_ARGS__); &_effect;})) +#define STALKER(v, ...) ({static kaleidoscope::stalker::v _effect __VA_ARGS__; &_effect;}) -namespace KaleidoscopePlugins { -namespace LEDEffects { +namespace kaleidoscope { class StalkerEffect : public LEDMode { public: class ColorComputer { @@ -33,45 +34,39 @@ class StalkerEffect : public LEDMode { StalkerEffect(void); void begin(void) final; - virtual void init(void) final; - virtual void update(void) final; + void init(void) final; + void update(void) final; - static void configure(ColorComputer *colorComputer); - static uint16_t stepLength; + static ColorComputer *variant; + static uint16_t step_length; private: - static uint32_t stepEndTime; - static ColorComputer *colorComputer; - static uint8_t map[ROWS][COLS]; + static uint32_t step_end_time_; + static uint8_t map_[ROWS][COLS]; - static Key eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState); + static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state); }; -namespace Stalker { +namespace stalker { class Haunt : public StalkerEffect::ColorComputer { public: - Haunt(const cRGB highlightColor); - Haunt(void) : Haunt( { - 0x40, 0x80, 0x80 - }) {}; - Haunt(void *) : Haunt() {}; + explicit Haunt(const cRGB highlight_color); + Haunt(void) : Haunt(CRGB(0x40, 0x80, 0x80)) {} - virtual cRGB compute(uint8_t *step) final; + cRGB compute(uint8_t *step) final; private: - static cRGB highlightColor; + static cRGB highlight_color_; }; class BlazingTrail : public StalkerEffect::ColorComputer { public: - BlazingTrail(...); - - virtual cRGB compute(uint8_t *step) final; -}; + BlazingTrail(void); + cRGB compute(uint8_t *step) final; }; -}; -}; +} +} -extern KaleidoscopePlugins::LEDEffects::StalkerEffect StalkerEffect; +extern kaleidoscope::StalkerEffect StalkerEffect;