diff --git a/doc/plugin/LEDEffect-Chase.md b/doc/plugin/LEDEffect-Chase.md new file mode 100644 index 00000000..739cd450 --- /dev/null +++ b/doc/plugin/LEDEffect-Chase.md @@ -0,0 +1,29 @@ +# Kaleidoscope-LEDEffect-Chase + +A simple LED effect where one color chases another across the keyboard and back, +over and over again. Playful colors they are. + +## Using the extension + +To use the plugin, include the header, and tell the firmware to use it: + +```c++ +#include +#include + +KALEIDOSCOPE_INIT_PLUGINS(LEDControl, + LEDEffect-Chase); + +void setup() { + Kaleidoscope.setup(); +} +``` + +## Plugin methods + +The plugin provides the `LEDChaseEffect` object, which has no public methods or +properties, outside of those provided by all LED modes. + +## Dependencies + +* [Kaleidoscope-LEDControl](LEDControl.md) diff --git a/src/Kaleidoscope-LEDEffect-Chase.h b/src/Kaleidoscope-LEDEffect-Chase.h new file mode 100644 index 00000000..e755e738 --- /dev/null +++ b/src/Kaleidoscope-LEDEffect-Chase.h @@ -0,0 +1,19 @@ +/* Kaleidoscope-LEDEffect-Chase - A Chase LED effect for Kaleidoscope. + * Copyright (C) 2017-2018 Keyboard.io, Inc. + * + * 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 the Free Software + * Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#pragma once + +#include "kaleidoscope/plugin/LEDEffect-Chase.h" diff --git a/src/kaleidoscope/plugin/LEDEffect-Chase.cpp b/src/kaleidoscope/plugin/LEDEffect-Chase.cpp new file mode 100644 index 00000000..ae8426e1 --- /dev/null +++ b/src/kaleidoscope/plugin/LEDEffect-Chase.cpp @@ -0,0 +1,66 @@ +/* Kaleidoscope-LEDEffect-Chase - A Chase LED effect for Kaleidoscope. + * Copyright (C) 2017-2018 Keyboard.io, Inc. + * + * 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 the Free Software + * Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include "Kaleidoscope-LEDEffect-Chase.h" + +namespace kaleidoscope { +namespace plugin { +void LEDChaseEffect::update(void) { + // Check to see if it's time to change the positions of the red and blue lights + if (current_chase_counter++ < chase_threshold) { + return; + } + current_chase_counter = 0; + + // The red LED is at `pos`; the blue one follows behind. `chase_sign` is either +1 or + // -1; `chase_pixels` is the gap between them. + byte pos2 = pos - (chase_sign * chase_pixels); + + // First, we turn off the LEDs that were turned on in the previous update. `pos` is + // always in the valid range (0 <= pos < LED_COUNT), but after it changes direction, for + // the first few updates, `pos2` will be out of bounds. Since it's an unsigned integer, + // even when it would have a value below zero, it underflows and so one test is good for + // both ends of the range. + ::LEDControl.setCrgbAt(pos, {0, 0, 0}); + if (pos2 < LED_COUNT) + ::LEDControl.setCrgbAt(pos2, {0, 0, 0}); + + // Next, we adjust the red light's position. If the direction hasn't changed (the red + // light isn't out of bounds), we also adjust the blue light's position to match the red + // one. If the new position puts it out of bounds, we reverse the direction, and bring + // it back in bounds. When this happens, the blue light "jumps" behind the red one, and + // will be out of bounds. The simplest way to do this is to assign it a value that is + // known to be invalid (LED_COUNT). + pos += chase_sign; + if (pos < LED_COUNT) { + pos2 += chase_sign; + } else { + chase_sign = -chase_sign; + pos += chase_sign; + pos2 = LED_COUNT; + } + + // Last, we turn on the LEDs at their new positions. As before, the blue light (pos2) is + // only set if it's in the valid LED range. + ::LEDControl.setCrgbAt(pos, {0, 0, 255}); + if (pos2 < LED_COUNT) + ::LEDControl.setCrgbAt(pos2, {255, 0, 0}); +} + +} +} + +kaleidoscope::plugin::LEDChaseEffect LEDChaseEffect; diff --git a/src/kaleidoscope/plugin/LEDEffect-Chase.h b/src/kaleidoscope/plugin/LEDEffect-Chase.h new file mode 100644 index 00000000..6174eae5 --- /dev/null +++ b/src/kaleidoscope/plugin/LEDEffect-Chase.h @@ -0,0 +1,40 @@ +/* Kaleidoscope-LEDEffect-Chase - A Chase LED effect for Kaleidoscope. + * Copyright (C) 2017-2018 Keyboard.io, Inc. + * + * 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 the Free Software + * Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#pragma once + +#include "Kaleidoscope-LEDControl.h" + +namespace kaleidoscope { +namespace plugin { +class LEDChaseEffect : public LEDMode { + public: + LEDChaseEffect(void) {} + + protected: + void update(void) final; + + private: + uint8_t pos = 0; + int8_t chase_sign = 1; //negative values when it's going backwar + uint8_t chase_pixels = 5; + uint8_t current_chase_counter = 0; + static const uint8_t chase_threshold = 150; +}; +} +} + +extern kaleidoscope::plugin::LEDChaseEffect LEDChaseEffect;