Skip to content

Commit

Permalink
Add focus command for retreiving a list of installed LED effects (#1387)
Browse files Browse the repository at this point in the history
* Implement focus command `led.modes` for querying LED modes

Signed-off-by: Evy Bongers <[email protected]>
  • Loading branch information
EvyBongers authored Mar 13, 2024
1 parent 73d651b commit d617890
Show file tree
Hide file tree
Showing 23 changed files with 122 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class ColormapEffect : public Plugin,
public LEDModeInterface,
public AccessTransientLEDMode {
public:
ColormapEffect() { led_mode_name_ = "Colormap"; }
explicit ColormapEffect(const char *led_mode_name) { led_mode_name_ = led_mode_name; }

void max_layers(uint8_t max_);

EventHandlerResult onLayerChange();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ namespace plugin {
//
class FingerPainter : public LEDMode {
public:
FingerPainter() { led_mode_name_ = "FingerPainter"; }
explicit FingerPainter(const char *led_mode_name) { led_mode_name_ = led_mode_name; }

void toggle();

EventHandlerResult onKeyEvent(KeyEvent &event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include "kaleidoscope/plugin/FocusSerial.h"

#include <Arduino.h> // for PSTR, __FlashStringHelper, F, strcmp_P
#include <Arduino.h> // for PSTR, F, strcmp_P
#include <HardwareSerial.h> // for HardwareSerial
#include <string.h> // for memset

Expand Down Expand Up @@ -77,18 +77,27 @@ EventHandlerResult FocusSerial::afterEachCycle() {
return EventHandlerResult::OK;
}

void sendLedModeCallback_(const char *name) {
Runtime.serialPort().println(name);
}

EventHandlerResult FocusSerial::onFocusEvent(const char *input) {
const char *cmd_help = PSTR("help");
const char *cmd_reset = PSTR("device.reset");
const char *cmd_plugins = PSTR("plugins");
const char *cmd_help = PSTR("help");
const char *cmd_reset = PSTR("device.reset");
const char *cmd_led_modes = PSTR("led.modes");
const char *cmd_plugins = PSTR("plugins");

if (inputMatchesHelp(input))
return printHelp(cmd_help, cmd_reset, cmd_plugins);
return printHelp(cmd_help, cmd_reset, cmd_led_modes, cmd_plugins);

if (inputMatchesCommand(input, cmd_reset)) {
Runtime.device().rebootBootloader();
return EventHandlerResult::EVENT_CONSUMED;
}
if (inputMatchesCommand(input, cmd_led_modes)) {
kaleidoscope::Hooks::onLedEffectQuery(sendLedModeCallback_);
return EventHandlerResult::EVENT_CONSUMED;
}
if (inputMatchesCommand(input, cmd_plugins)) {
kaleidoscope::Hooks::onNameQuery();
return EventHandlerResult::EVENT_CONSUMED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#pragma once

#include <Arduino.h> // for delayMicroseconds
#include <Arduino.h> // for __FlashStringHelper, delayMicroseconds
#include <HardwareSerial.h> // for HardwareSerial
#include <stdint.h> // for uint8_t, uint16_t

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class Heatmap : public Plugin,
public LEDModeInterface,
public AccessTransientLEDMode {
public:
Heatmap() { led_mode_name_ = "Heatmap"; }
explicit Heatmap(const char *led_mode_name) { led_mode_name_ = led_mode_name; }

static uint16_t update_delay;
static const cRGB *heat_colors;
static uint8_t heat_colors_length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class LEDActiveLayerColorEffect : public Plugin,
public LEDModeInterface,
public AccessTransientLEDMode {
public:
LEDActiveLayerColorEffect() { led_mode_name_ = "ActiveLayerColor"; }
explicit LEDActiveLayerColorEffect(const char *led_mode_name) { led_mode_name_ = led_mode_name; }

EventHandlerResult onLayerChange();
void setColormap(const cRGB colormap[]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class LEDActiveLayerKeysEffect : public Plugin,
public LEDModeInterface,
public AccessTransientLEDMode {
public:
LEDActiveLayerKeysEffect() { led_mode_name_ = "ActiveLayerKeys"; }
explicit LEDActiveLayerKeysEffect(const char *led_mode_name) { led_mode_name_ = led_mode_name; }

EventHandlerResult onLayerChange();
template<uint8_t _colormap_size>
void setColormap(cRGB const (&colormap)[_colormap_size]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class AlphaSquareEffect : public Plugin,
public LEDModeInterface,
public AccessTransientLEDMode {
public:
AlphaSquareEffect() { led_mode_name_ = "AlphaSquare"; }
explicit AlphaSquareEffect(const char *led_mode_name) { led_mode_name_ = led_mode_name; }

static uint16_t length;

EventHandlerResult onKeyEvent(KeyEvent &event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class StalkerEffect : public Plugin,
public LEDModeInterface,
public AccessTransientLEDMode {
public:
StalkerEffect() { led_mode_name_ = "Stalker"; }
explicit StalkerEffect(const char *led_mode_name) { led_mode_name_ = led_mode_name; }

class ColorComputer {
public:
virtual cRGB compute(uint8_t *step) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class WavepoolEffect : public Plugin,
public LEDModeInterface,
public AccessTransientLEDMode {
public:
WavepoolEffect() { led_mode_name_ = "Wavepool"; }
explicit WavepoolEffect(const char *led_mode_name) { led_mode_name_ = led_mode_name; }

EventHandlerResult onKeyEvent(KeyEvent &event);

// ms before idle animation starts after last keypress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include "kaleidoscope/plugin/LEDBrightnessConfig.h"

#include <Arduino.h> // for PSTR, strcmp_P, F, __FlashStringHelper
#include <Arduino.h> // for PSTR, strcmp_P, F
#include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <stdint.h> // for uint8_t, uint16_t
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ namespace plugin {
class LEDBreatheEffect : public Plugin,
public LEDModeInterface {
public:
LEDBreatheEffect() { led_mode_name_ = "Breathe"; }
explicit LEDBreatheEffect(const char *led_mode_name) { led_mode_name_ = led_mode_name; }

uint8_t hue = 170;
uint8_t saturation = 255;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ namespace plugin {
class LEDChaseEffect : public Plugin,
public LEDModeInterface {
public:
LEDChaseEffect() { led_mode_name_ = "Chase"; }
explicit LEDChaseEffect(const char *led_mode_name) { led_mode_name_ = led_mode_name; }

uint8_t update_delay() {
return update_delay_;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class LEDDigitalRainEffect : public Plugin,
public LEDModeInterface,
public AccessTransientLEDMode {
public:
LEDDigitalRainEffect() {}
LEDDigitalRainEffect() { led_mode_name_ = "DigitalRain"; }
explicit LEDDigitalRainEffect(const char *led_mode_name) { led_mode_name_ = led_mode_name; }

/**
* Color channel enum.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ namespace plugin {
class LEDRainbowEffect : public Plugin,
public LEDModeInterface {
public:
LEDRainbowEffect() { led_mode_name_ = "Rainbow"; }
explicit LEDRainbowEffect(const char *led_mode_name) { led_mode_name_ = led_mode_name; }

void brightness(uint8_t);
uint8_t brightness() {
return rainbow_value;
Expand Down Expand Up @@ -68,6 +71,9 @@ class LEDRainbowEffect : public Plugin,

class LEDRainbowWaveEffect : public Plugin, public LEDModeInterface {
public:
LEDRainbowWaveEffect() { led_mode_name_ = "RainbowWave"; }
explicit LEDRainbowWaveEffect(const char *led_mode_name) { led_mode_name_ = led_mode_name; }

void brightness(uint8_t);
uint8_t brightness() {
return rainbow_value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ class LEDSolidColor : public Plugin,
public LEDModeInterface {
public:
LEDSolidColor(uint8_t r, uint8_t g, uint8_t b)
: r_(r), g_(g), b_(b) {}
: r_(r), g_(g), b_(b) {
led_mode_name_ = "SolidColor";
}

LEDSolidColor(const char *led_mode_name, uint8_t r, uint8_t g, uint8_t b)
: r_(r), g_(g), b_(b) {
led_mode_name_ = led_mode_name;
}

// This class' instance has dynamic lifetime
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
#include "kaleidoscope/device/device.h" // for CRGB
#include "kaleidoscope/plugin/TriColor.h" // for TriColor

kaleidoscope::plugin::TriColor JukeboxEffect(CRGB(0xc8, 0xe8, 0xee), /* TM */
kaleidoscope::plugin::TriColor JukeboxEffect("Jukebox",
CRGB(0xc8, 0xe8, 0xee), /* TM */
CRGB(0xc3, 0xee, 0x8c), /* VCO */
CRGB(0x21, 0x38, 0xd7)); /* RN */

kaleidoscope::plugin::TriColor JukeboxAlternateEffect(CRGB(0xc8, 0xe8, 0xee), /* TM */
kaleidoscope::plugin::TriColor JukeboxAlternateEffect("JukeboxAlternate",
CRGB(0xc8, 0xe8, 0xee), /* TM */
CRGB(0x21, 0x38, 0xd7), /* RN */
CRGB(0xc3, 0xee, 0x8c)); /* VCO */
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
#include "kaleidoscope/device/device.h" // for CRGB
#include "kaleidoscope/plugin/TriColor.h" // for TriColor

kaleidoscope::plugin::TriColor MiamiEffect(CRGB(0x4e, 0xd6, 0xd6), /* Cyan */
kaleidoscope::plugin::TriColor MiamiEffect("Miami",
CRGB(0x4e, 0xd6, 0xd6), /* Cyan */
CRGB(0xaf, 0x67, 0xfa)); /* Magenta */
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
namespace kaleidoscope {
namespace plugin {

TriColor::TriColor(cRGB base_color, cRGB mod_color, cRGB esc_color) {
base_color_ = base_color;
mod_color_ = mod_color;
esc_color_ = esc_color;
TriColor::TriColor(const char *led_mode_name, cRGB base_color, cRGB mod_color, cRGB esc_color) {
led_mode_name_ = led_mode_name;
base_color_ = base_color;
mod_color_ = mod_color;
esc_color_ = esc_color;
}

void TriColor::TransientLEDMode::update() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ namespace plugin {
class TriColor : public Plugin,
public LEDModeInterface {
public:
TriColor(cRGB base_color, cRGB mod_color, cRGB esc_color);
TriColor(const char *led_mode_name, cRGB base_color, cRGB mod_color, cRGB esc_color);
TriColor(cRGB base_color, cRGB mod_color, cRGB esc_color)
: TriColor("TriColor", base_color, mod_color, esc_color) {}
TriColor(const char *led_mode_name, cRGB base_color, cRGB mod_color)
: TriColor(led_mode_name, base_color, mod_color, mod_color) {}
TriColor(cRGB base_color, cRGB mod_color)
: TriColor(base_color, mod_color, mod_color) {}
: TriColor("TriColor", base_color, mod_color, mod_color) {}

// This class' instance has dynamic lifetime
//
Expand Down
18 changes: 18 additions & 0 deletions src/kaleidoscope/event_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
#define _ABORTABLE true
#define _NOT_ABORTABLE false

// Define the callback for the LED mode hook handler
typedef void (*LedModeCallback)(const char*);

#define _CURRENT_IMPLEMENTATION

//******************************************************************************
Expand Down Expand Up @@ -125,6 +128,17 @@ class SignatureCheckDummy {};
(), (), (), __NL__ \
(), (), ##__VA_ARGS__) __NL__ \
__NL__ \
/* Called by Focus, when handling the `led.modes` command. */ __NL__ \
/* Is implemented in LEDModeInterface.h, but depends on */ __NL__ \
/* the LED plugin to set `led_mode_name_`. */ __NL__ \
OPERATION(onLedEffectQuery, __NL__ \
1, __NL__ \
_CURRENT_IMPLEMENTATION, __NL__ \
_NOT_ABORTABLE, __NL__ \
(), (), (), /* non template */ __NL__ \
(LedModeCallback callback), __NL__ \
(callback), ##__VA_ARGS__) __NL__ \
__NL__ \
OPERATION(onSetup, __NL__ \
1, __NL__ \
_CURRENT_IMPLEMENTATION, __NL__ \
Expand Down Expand Up @@ -298,6 +312,10 @@ class SignatureCheckDummy {};
OP(onSetup, 1) __NL__ \
END(onSetup, 1) __NL__ \
__NL__ \
START(onLedEffectQuery, 1) __NL__ \
OP(onLedEffectQuery, 1) __NL__ \
END(onLedEffectQuery, 1) __NL__ \
__NL__ \
START(onNameQuery, 1) __NL__ \
OP(onNameQuery, 1) __NL__ \
END(onNameQuery, 1) __NL__ \
Expand Down
5 changes: 3 additions & 2 deletions src/kaleidoscope/plugin/LEDControl/LED-Off.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ namespace plugin {
//
class LEDOff : public LEDMode {
public:
LEDOff(void) {}
LEDOff() { led_mode_name_ = "Off"; }
explicit LEDOff(const char *led_mode_name) { led_mode_name_ = led_mode_name; }

protected:
void onActivate(void) final;
void onActivate() final;
void refreshAt(KeyAddr key_addr) final;
};
} // namespace plugin
Expand Down
22 changes: 22 additions & 0 deletions src/kaleidoscope/plugin/LEDModeInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,32 @@

#pragma once

#include <Arduino.h> // for PROGMEM

#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult
#include "kaleidoscope/event_handlers.h" // for LedModeCallback

namespace kaleidoscope {
namespace plugin {

class LEDModeInterface {
public:
LEDModeInterface()
: led_mode_name_{0} {}
explicit LEDModeInterface(const char *led_mode_name)
: led_mode_name_{led_mode_name} {}
void activate();

EventHandlerResult onLedEffectQuery(LedModeCallback callback) {
if (led_mode_name_ == 0) {
// If no name was defined, return a default string
callback("[unnamed led mode]");
} else {
callback(led_mode_name_);
}
return EventHandlerResult::OK;
}

// This auxiliary class helps to generate a verbose error message
// in case that there is no TransientLEDMode typedef or nested
// class present in a derived class of LEDModeInterface.
Expand All @@ -35,6 +54,9 @@ class LEDModeInterface {
// lifetime is handled dynamically.
//
typedef NoLEDMode DynamicLEDMode;

protected:
const char *led_mode_name_ PROGMEM;
};

} // namespace plugin
Expand Down

0 comments on commit d617890

Please sign in to comment.