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

Commit

Permalink
: Set currentMode on start, fix led flicker
Browse files Browse the repository at this point in the history
* LED update is based on the currentMode variable, not the Mode
variable. On initial startup getConfig sets Mode, but currentMode is
left unset. As this isn't a valid mode the refresh loop sets both values
to the default Colour, requiring the config be rerear. FIX: Set
currentMode when initializing the LEDs.
* More complex modes make the LEDs flicker; instead of running the
render code every update add a toggle to alternate between render or
show giving other code a chance to run between slow renders and
strip refresh. Most likely being caused by wifi processing/buffering and
this fixes it on my devices.
  • Loading branch information
jennytoo committed Dec 16, 2019
1 parent ed08f37 commit 1cac87c
Showing 1 changed file with 73 additions and 40 deletions.
113 changes: 73 additions & 40 deletions Super_Simple_RGB_WiFi_Lamp/LEDs.ino
Original file line number Diff line number Diff line change
@@ -1,55 +1,76 @@
void ledStringInit() {
void ledStringInit()
{
// add the leds to fast led and clear them
FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER>(ledString, NUM_LEDS);
FastLED.clear ();
FastLED.clear();
FastLED.show();

// Set the maximum power draw
// FastLED.setMaxPowerInVoltsAndMilliamps(5,1000);

// Set initial mode
currentMode = "Colour"; // Automatically jump back to colour

// Debug
Serial.println("[handleMode] - LED string was set up correctly");
}

void handleMode() {
bool renderPass = true;
void handleMode()
{
// Adapt the leds to the current mode

auto modeIter = modes.find(currentMode);
if (modeIter == modes.end()) {
if (modeIter == modes.end())
{
// not found
Serial.println("[handleMode] - Mode \"" + Mode + "\" not found, resetting to default");
Mode = "Colour"; // Automatically jump back to colour
currentMode = "Colour"; // Automatically jump back to colour
Mode = "Colour"; // Automatically jump back to colour
currentMode = "Colour"; // Automatically jump back to colour
return;
}
else {
else
{
// Apply currentMode always?

// If mode is found run its render function
modeIter->second->render();

// Globally adjust the brightness
adjustBrightness();
if (renderPass)
{
// If mode is found run its render function
modeIter->second->render();

// Handle Fast LED
FastLED.show();
// FastLED.delay(1000 / FRAMES_PER_SECOND);
}
// Globally adjust the brightness
adjustBrightness();
}
else
{
// Handle Fast LED
FastLED.show();
// FastLED.delay(1000 / FRAMES_PER_SECOND);
}
// Let other code run before we do the update
renderPass = !renderPass;
}
}

void adjustBrightness() {
void adjustBrightness()
{
// Adjust the brightness depending on the mode
if (autoOnWithModeChange || State) {
if (Mode != currentMode) {
// Dim lights off first
if (modeChangeFadeAmount > 0) {
if (autoOnWithModeChange || State)
{
if (Mode != currentMode)
{
// Dim lights off first
if (modeChangeFadeAmount > 0)
{
// Set the dimming variables and apply
EVERY_N_MILLISECONDS(20) {
modeChangeFadeAmount -= (FadeTime > 0) ? (255 / ((float)FadeTime/20)) : 255;
EVERY_N_MILLISECONDS(20)
{
modeChangeFadeAmount -= (FadeTime > 0) ? (255 / ((float)FadeTime / 20)) : 255;
modeChangeFadeAmount = constrain(modeChangeFadeAmount, 0, 255);
};
}
else {
else
{
// Debug
Serial.println("[handleMode] - Mode changed to: " + Mode);

Expand All @@ -61,48 +82,60 @@ void adjustBrightness() {
modeChangeFadeAmount = 0;
}
}
else if (currentMode != previousMode) {
else if (currentMode != previousMode)
{
// On mode change dim lights up
if (modeChangeFadeAmount < 255) {
EVERY_N_MILLISECONDS(20) {
modeChangeFadeAmount += (FadeTime > 0) ? (255 / ((float)FadeTime/20)) : 255;
if (modeChangeFadeAmount < 255)
{
EVERY_N_MILLISECONDS(20)
{
modeChangeFadeAmount += (FadeTime > 0) ? (255 / ((float)FadeTime / 20)) : 255;
modeChangeFadeAmount = constrain(modeChangeFadeAmount, 0, 255);
};
}
else {
else
{
// Set the currentMode to Mode
previousMode = currentMode;
}
}
}

// Adjust the brightness depending on the state
if (!State && previousState) {
if (!State && previousState)
{
// Turn Lights off slowly
if (modeChangeFadeAmount > 0) {
EVERY_N_MILLISECONDS(20) {
modeChangeFadeAmount -= (FadeTime > 0) ? (255 / ((float)FadeTime/20)) : 255;
if (modeChangeFadeAmount > 0)
{
EVERY_N_MILLISECONDS(20)
{
modeChangeFadeAmount -= (FadeTime > 0) ? (255 / ((float)FadeTime / 20)) : 255;
modeChangeFadeAmount = constrain(modeChangeFadeAmount, 0, 255);
};
}
else {
else
{
// Debug
Serial.println("[handleMode] - LED's turned off");

// Set the previous state
previousState = false;
}
}
else if (State && !previousState) {
else if (State && !previousState)
{
// Turn on light slowly
if (modeChangeFadeAmount < 255) {
EVERY_N_MILLISECONDS(20) {
modeChangeFadeAmount += (FadeTime > 0) ? (255 / ((float)FadeTime/20)) : 255;
if (modeChangeFadeAmount < 255)
{
EVERY_N_MILLISECONDS(20)
{
modeChangeFadeAmount += (FadeTime > 0) ? (255 / ((float)FadeTime / 20)) : 255;
modeChangeFadeAmount = constrain(modeChangeFadeAmount, 0, 255);
};
}
else {
// Debug
else
{
// Debug
Serial.println("[handleMode] - LED's turned on");

// Set the previous values
Expand Down

0 comments on commit 1cac87c

Please sign in to comment.