Skip to content

Commit

Permalink
Fix: output-glitching on ESPNow remote command reception
Browse files Browse the repository at this point in the history
Processing of received button command is no longer processed in the callback, instead the value is saved to a variable and processed in the main loop.
The actual fix is to not access the file system while data is being sent out: even just trying to open a non-existing file causes glitches on the C3. Waiting for the bus to finish fixes this BUT it causes a frame-delay which is the lesser evil than random color flashes.
  • Loading branch information
DedeHai committed Dec 15, 2024
1 parent b238eae commit da09c50
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions wled00/fcn_declare.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ bool getPresetName(byte index, String& name);

//remote.cpp
void handleRemote(uint8_t *data, size_t len);
void processESPNowButton();

//set.cpp
bool isAsterisksOnly(const char* str, byte maxLen);
Expand Down
19 changes: 16 additions & 3 deletions wled00/remote.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "wled.h"
#ifndef WLED_DISABLE_ESPNOW

#define ESPNOW_BUSWAIT_TIMEOUT 30 // timeout in ms to wait for bus to finish updating

#define NIGHT_MODE_DEACTIVATED -1
#define NIGHT_MODE_BRIGHTNESS 5

Expand Down Expand Up @@ -38,6 +40,7 @@ typedef struct WizMoteMessageStructure {

static uint32_t last_seq = UINT32_MAX;
static int brightnessBeforeNightMode = NIGHT_MODE_DEACTIVATED;
static uint8_t ESPNowButton = 0; // set in callback if new button value is received

// Pulled from the IR Remote logic but reduced to 10 steps with a constant of 3
static const byte brightnessSteps[] = {
Expand Down Expand Up @@ -121,6 +124,9 @@ static bool remoteJson(int button)

sprintf_P(objKey, PSTR("\"%d\":"), button);

unsigned long start = millis();
while (strip.isUpdating() && millis()-start < ESPNOW_BUSWAIT_TIMEOUT) yield(); // wait for strip to finish updating, accessing FS during sendout causes glitches

// attempt to read command from remote.json
readObjectFromFile(PSTR("/remote.json"), objKey, pDoc);
JsonObject fdo = pDoc->as<JsonObject>();
Expand Down Expand Up @@ -202,8 +208,14 @@ void handleRemote(uint8_t *incomingData, size_t len) {
DEBUG_PRINT(F("] button: "));
DEBUG_PRINTLN(incoming->button);

if (!remoteJson(incoming->button))
switch (incoming->button) {
ESPNowButton = incoming->button; // save state, do not process in callback (can cause glitches)
last_seq = cur_seq;
}

void processESPNowButton() {
if(ESPNowButton > 0) {
if (!remoteJson(ESPNowButton))
switch (ESPNowButton) {
case WIZMOTE_BUTTON_ON : setOn(); break;
case WIZMOTE_BUTTON_OFF : setOff(); break;
case WIZMOTE_BUTTON_ONE : presetWithFallback(1, FX_MODE_STATIC, 0); break;
Expand All @@ -219,7 +231,8 @@ void handleRemote(uint8_t *incomingData, size_t len) {
case WIZ_SMART_BUTTON_BRIGHT_DOWN : brightnessDown(); break;
default: break;
}
last_seq = cur_seq;
}
ESPNowButton = 0;
}

#else
Expand Down
3 changes: 3 additions & 0 deletions wled00/wled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ void WLED::loop()
#ifndef WLED_DISABLE_INFRARED
handleIR();
#endif
#ifndef WLED_DISABLE_ESPNOW
processESPNowButton();
#endif
#ifndef WLED_DISABLE_ALEXA
handleAlexa();
#endif
Expand Down

0 comments on commit da09c50

Please sign in to comment.