LEDControl: Add a way to activate a mode by pointer

It would be nice if LED effects could be activated via their object, not
just their number, so that one could write - say, in a macro -
`myEffect.activate()`, and have it become the active mode.

To implement this, `LEDControl_` gains an `activate` method, that takes
a pointer, finds it in the mode array, and switches to its index (if
found). `LEDMode` gains an `activate` method too, which uses
`LEDControl`s new method of the same name to activate itself.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
pull/48/head
Gergely Nagy 8 years ago
parent 4fc567cea5
commit 37f5d1d1b8

@ -1,5 +1,10 @@
#include "LEDControl.h" #include "LEDControl.h"
void
LEDMode::activate (void) {
LEDControl.activate (this);
}
LEDControl_::LEDControl_(void) { LEDControl_::LEDControl_(void) {
memset (modes, 0, LED_MAX_MODES * sizeof (modes[0])); memset (modes, 0, LED_MAX_MODES * sizeof (modes[0]));
} }
@ -54,6 +59,14 @@ LEDControl_::get_mode (void) {
return mode; return mode;
} }
void
LEDControl_::activate (LEDMode *mode) {
for (uint8_t i = 0; i < LED_MAX_MODES; i++) {
if (modes[i] == mode)
return set_mode(i);
}
}
int8_t int8_t
LEDControl_::mode_add (LEDMode *mode) { LEDControl_::mode_add (LEDMode *mode) {
for (int i = 0; i < LED_MAX_MODES; i++) { for (int i = 0; i < LED_MAX_MODES; i++) {

@ -10,6 +10,7 @@ class LEDMode {
virtual void setup (void) {}; virtual void setup (void) {};
virtual void init (void) {}; virtual void init (void) {};
virtual void update (void) {}; virtual void update (void) {};
virtual void activate (void);
}; };
class LEDControl_ { class LEDControl_ {
@ -26,6 +27,8 @@ class LEDControl_ {
void set_all_leds_to(uint8_t r, uint8_t g, uint8_t b); void set_all_leds_to(uint8_t r, uint8_t g, uint8_t b);
void set_all_leds_to(cRGB color); void set_all_leds_to(cRGB color);
void activate (LEDMode *mode);
private: private:
LEDMode *modes[LED_MAX_MODES]; LEDMode *modes[LED_MAX_MODES];
uint8_t previousMode, mode; uint8_t previousMode, mode;

Loading…
Cancel
Save