Skip to content
This repository has been archived by the owner on Nov 18, 2020. It is now read-only.

Commit

Permalink
Always start modes with initial settings
Browse files Browse the repository at this point in the history
In previous versions the board kept runtime information per mode when
switching between modes. This could have odd effects, for example the
"color wipe" mode could be invisible for some time when it was in the
"fade out" phase when switching to the mode.

This change now separates the mode member variables into two categories:

Config: These are initialized with their default values at class level
once and need to be overridden with the applyConfig() method.

State: These are not allowed to be persisted with the config and may
change at any time during animation. They need to be initialized in the
initialize() method.
  • Loading branch information
LarsMichelsen committed Dec 16, 2019
1 parent c482d6c commit c8d20af
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 33 deletions.
6 changes: 6 additions & 0 deletions Super_Simple_RGB_WiFi_Lamp/LEDs.ino
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ void adjustBrightness() {
// Clear the LEDs
FastLED.clear();

// Initialize state of the new mode
auto modeIter = modes.find(Mode);
if (modeIter != modes.end()) {
modeIter->second->initialize();
}

// Set the currentMode to Mode
currentMode = Mode;
modeChangeFadeAmount = 0;
Expand Down
3 changes: 3 additions & 0 deletions Super_Simple_RGB_WiFi_Lamp/ModeBellCurve.ino
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
class ModeBellCurve : public ModeBase
{
private:
// Config
int bellCurveRed = 128;
int bellCurveGreen = 128;
int bellCurveBlue = 128;
public:
ModeBellCurve() {}
virtual void initialize() {}

virtual void render() {
// Set the top brightness
for (int i = 0; i < topNumLeds; i++) {
Expand Down
7 changes: 6 additions & 1 deletion Super_Simple_RGB_WiFi_Lamp/ModeCircle.ino
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
class ModeCircle : public ModeBase
{
private:
int circleActiveLedNumber = 0;
int circleActiveLedNumber;
public:
ModeCircle() {}

virtual void initialize() {
circleActiveLedNumber = 0;
}

virtual void render() {
// First bring our logical arrays into a list of led numbers to iterate over
int i;
Expand Down
13 changes: 11 additions & 2 deletions Super_Simple_RGB_WiFi_Lamp/ModeClock.ino
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
class ModeClock : public ModeBase
{
private:
// Config
int clockHourRed = 128;
int clockHourGreen = 128;
int clockHourBlue = 128;
int clockMinRed = 128;
int clockMinGreen = 128;
int clockMinBlue = 128;
int clockOnPauseBrightness = 255;
unsigned long lastClockExecution = 0;

// State
int clockOnPauseBrightness;
unsigned long lastClockExecution;
public:
ModeClock() {}

virtual void initialize() {
clockOnPauseBrightness = 255;
lastClockExecution = 0;
}

virtual void render() {
if (ntpTimeSet) {
// Get the number of seconds between each LED
Expand Down
21 changes: 15 additions & 6 deletions Super_Simple_RGB_WiFi_Lamp/ModeColorWipe.ino
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
class ModeColorWipe : public ModeBase
{
private:
int colorWipePosition = -1;
bool TurningOn = true;
int colorWipeRed = 255;
int colorWipeGreen = 0;
int colorWipeBlue = 255;
int colorWipeSpeed = 20;
// State
int colorWipePosition;
bool TurningOn;

// Config
int colorWipeRed = 255;
int colorWipeGreen = 0;
int colorWipeBlue = 255;
int colorWipeSpeed = 20;

public:
ModeColorWipe() {}

virtual void initialize() {
colorWipePosition = -1;
TurningOn = true;
}

virtual void render() {
EVERY_N_MILLISECONDS(colorWipeSpeed) {
colorWipePosition++;
Expand Down
3 changes: 3 additions & 0 deletions Super_Simple_RGB_WiFi_Lamp/ModeColour.ino
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
class ModeColour : public ModeBase
{
private:
// Config
int colourRed = 128;
int colourGreen = 128;
int colourBlue = 128;
public:
ModeColour() {}
virtual void initialize() {}

virtual void render() {
fill_solid(ledString, NUM_LEDS, CRGB(colourRed, colourGreen, colourBlue));
}
Expand Down
13 changes: 11 additions & 2 deletions Super_Simple_RGB_WiFi_Lamp/ModeConfetti.ino
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
class ModeConfetti : public ModeBase
{
private:
bool confettiActive = true;
// State
bool confettiActive;
int confettiPixel;

// Config
int confettiSpeed = 100;
int confettiPixel = random(NUM_LEDS);

public:
ModeConfetti() {}

virtual void initialize() {
confettiActive = true;
confettiPixel = random(NUM_LEDS);
}

virtual void render() {
EVERY_N_MILLISECONDS(confettiSpeed) {
if (confettiActive) {
Expand Down
16 changes: 12 additions & 4 deletions Super_Simple_RGB_WiFi_Lamp/ModeNightRider.ino
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
class ModeNightRider : public ModeBase
{
private:
int nightRiderTopLedNumber = 0;
int nightRiderBottomLedNumber = 0;
int nightRiderTopIncrement = 1;
int nightRiderBottomIncrement = 1;
int nightRiderTopLedNumber;
int nightRiderBottomLedNumber;
int nightRiderTopIncrement;
int nightRiderBottomIncrement;
public:
ModeNightRider() {}

virtual void initialize() {
nightRiderTopLedNumber = 0;
nightRiderBottomLedNumber = 0;
nightRiderTopIncrement = 1;
nightRiderBottomIncrement = 1;
}

virtual void render() {
int delayTime = 500 / topNumLeds;
EVERY_N_MILLISECONDS(delayTime) {
Expand Down
17 changes: 13 additions & 4 deletions Super_Simple_RGB_WiFi_Lamp/ModeRainbow.ino
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
class ModeRainbow : public ModeBase
{
private:
int rainbowStartHue = 0;
int rainbowSpeed = 10;
int rainbowBri = 100;
float rainbowAddedHue = 0;
// Config
int rainbowStartHue = 0;
int rainbowSpeed = 10;
int rainbowBri = 100;

// State
float rainbowAddedHue;

public:
ModeRainbow() {}

virtual void initialize() {
rainbowAddedHue = 0;
}

virtual void render() {
int startHue = rainbowStartHue;
int speed = rainbowSpeed;
Expand Down
22 changes: 16 additions & 6 deletions Super_Simple_RGB_WiFi_Lamp/ModeSparkle.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@ class ModeSparkle : public ModeBase
{

private:
int sparkleSpeed = 30;
bool sparkleActive = true;
int sparkleRed = 128;
int sparkleGreen = 128;
int sparkleBlue = 128;
int sparklePixel = random(NUM_LEDS);
// Config
int sparkleSpeed = 30;
int sparkleRed = 128;
int sparkleGreen = 128;
int sparkleBlue = 128;

// State
bool sparkleActive;
int sparklePixel;

public:
ModeSparkle() {}

virtual void initialize() {
sparkleActive = true;
sparklePixel = random(NUM_LEDS);
}

virtual void render() {
EVERY_N_MILLISECONDS(sparkleSpeed) {
if (sparkleActive) {
Expand Down
25 changes: 17 additions & 8 deletions Super_Simple_RGB_WiFi_Lamp/ModeVisualiser.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@
class ModeVisualiser : public ModeBase
{
private:
// State
ADC_MODE(ADC_TOUT);
arduinoFFT FFT = arduinoFFT();
double visualiserRealSamples[VISUALISER_NUM_SAMPLES];
double visualiserImaginarySamples[VISUALISER_NUM_SAMPLES];
unsigned long visualiserLastSampleTime = 0;
uint16_t visualiserPeriod = 250;
uint16_t visualiserMinThreshold = 100;
uint16_t visualiserMaxThreshold = 750;
uint8_t visualiserNumBinsToSkip = 3;
uint8_t visualiserFadeUp = 32;
uint8_t visualiserFadeDown = 32;
uint8_t visualiserHueOffset = 170;
unsigned long visualiserLastSampleTime;
uint8_t visualiserNumBinsToSkip;

// Config
uint16_t visualiserPeriod = 250;
uint16_t visualiserMinThreshold = 100;
uint16_t visualiserMaxThreshold = 750;
uint8_t visualiserFadeUp = 32;
uint8_t visualiserFadeDown = 32;
uint8_t visualiserHueOffset = 170;
public:
ModeVisualiser() {}

virtual void initialize() {
visualiserLastSampleTime = 0;
visualiserNumBinsToSkip = 3;
}

virtual void render() {
// Only use visualiser when not trying to access the NTP server
if (((WiFi.isConnected() && ntpTimeSet) || softApStarted) && !webSocketConnecting) {
Expand Down
6 changes: 6 additions & 0 deletions Super_Simple_RGB_WiFi_Lamp/Super_Simple_RGB_WiFi_Lamp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ String Password = "";
class ModeBase
{
public:
/// Override this to initialize state specific variables
virtual void initialize();

// Is called once per frame to update the LEDs
virtual void render();

// Update config member variables based on the handed over settings
virtual void applyConfig(JsonVariant& settings);
};

Expand Down

0 comments on commit c8d20af

Please sign in to comment.