Merge pull request #4386 from DedeHai/ESPNow_glitchfix
Fix for ESPNow remote causing output glitches
This commit is contained in:
		| @@ -318,7 +318,8 @@ void deletePreset(byte index); | |||||||
| bool getPresetName(byte index, String& name); | bool getPresetName(byte index, String& name); | ||||||
|  |  | ||||||
| //remote.cpp | //remote.cpp | ||||||
| void handleRemote(uint8_t *data, size_t len); | void handleWiZdata(uint8_t *incomingData, size_t len); | ||||||
|  | void handleRemote(); | ||||||
|  |  | ||||||
| //set.cpp | //set.cpp | ||||||
| bool isAsterisksOnly(const char* str, byte maxLen); | bool isAsterisksOnly(const char* str, byte maxLen); | ||||||
|   | |||||||
| @@ -1,6 +1,8 @@ | |||||||
| #include "wled.h" | #include "wled.h" | ||||||
| #ifndef WLED_DISABLE_ESPNOW | #ifndef WLED_DISABLE_ESPNOW | ||||||
|  |  | ||||||
|  | #define ESPNOW_BUSWAIT_TIMEOUT 24 // one frame timeout to wait for bus to finish updating | ||||||
|  |  | ||||||
| #define NIGHT_MODE_DEACTIVATED     -1 | #define NIGHT_MODE_DEACTIVATED     -1 | ||||||
| #define NIGHT_MODE_BRIGHTNESS      5 | #define NIGHT_MODE_BRIGHTNESS      5 | ||||||
|  |  | ||||||
| @@ -38,6 +40,7 @@ typedef struct WizMoteMessageStructure { | |||||||
|  |  | ||||||
| static uint32_t last_seq = UINT32_MAX; | static uint32_t last_seq = UINT32_MAX; | ||||||
| static int brightnessBeforeNightMode = NIGHT_MODE_DEACTIVATED; | static int brightnessBeforeNightMode = NIGHT_MODE_DEACTIVATED; | ||||||
|  | static int16_t ESPNowButton = -1; // 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 | // Pulled from the IR Remote logic but reduced to 10 steps with a constant of 3 | ||||||
| static const byte brightnessSteps[] = { | static const byte brightnessSteps[] = { | ||||||
| @@ -121,6 +124,9 @@ static bool remoteJson(int button) | |||||||
|  |  | ||||||
|   sprintf_P(objKey, PSTR("\"%d\":"), 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 |   // attempt to read command from remote.json | ||||||
|   readObjectFromFile(PSTR("/remote.json"), objKey, pDoc); |   readObjectFromFile(PSTR("/remote.json"), objKey, pDoc); | ||||||
|   JsonObject fdo = pDoc->as<JsonObject>(); |   JsonObject fdo = pDoc->as<JsonObject>(); | ||||||
| @@ -176,7 +182,7 @@ static bool remoteJson(int button) | |||||||
| } | } | ||||||
|  |  | ||||||
| // Callback function that will be executed when data is received | // Callback function that will be executed when data is received | ||||||
| void handleRemote(uint8_t *incomingData, size_t len) { | void handleWiZdata(uint8_t *incomingData, size_t len) { | ||||||
|   message_structure_t *incoming = reinterpret_cast<message_structure_t *>(incomingData); |   message_structure_t *incoming = reinterpret_cast<message_structure_t *>(incomingData); | ||||||
|  |  | ||||||
|   if (strcmp(last_signal_src, linked_remote) != 0) { |   if (strcmp(last_signal_src, linked_remote) != 0) { | ||||||
| @@ -202,8 +208,15 @@ void handleRemote(uint8_t *incomingData, size_t len) { | |||||||
|   DEBUG_PRINT(F("] button: ")); |   DEBUG_PRINT(F("] button: ")); | ||||||
|   DEBUG_PRINTLN(incoming->button); |   DEBUG_PRINTLN(incoming->button); | ||||||
|  |  | ||||||
|   if (!remoteJson(incoming->button)) |   ESPNowButton = incoming->button; // save state, do not process in callback (can cause glitches) | ||||||
|     switch (incoming->button) { |   last_seq = cur_seq; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // process ESPNow button data (acesses FS, should not be called while update to avoid glitches) | ||||||
|  | void handleRemote() { | ||||||
|  |   if(ESPNowButton >= 0) { | ||||||
|  |   if (!remoteJson(ESPNowButton)) | ||||||
|  |     switch (ESPNowButton) { | ||||||
|       case WIZMOTE_BUTTON_ON             : setOn();                                         break; |       case WIZMOTE_BUTTON_ON             : setOn();                                         break; | ||||||
|       case WIZMOTE_BUTTON_OFF            : setOff();                                        break; |       case WIZMOTE_BUTTON_OFF            : setOff();                                        break; | ||||||
|       case WIZMOTE_BUTTON_ONE            : presetWithFallback(1, FX_MODE_STATIC,        0); break; |       case WIZMOTE_BUTTON_ONE            : presetWithFallback(1, FX_MODE_STATIC,        0); break; | ||||||
| @@ -219,9 +232,10 @@ void handleRemote(uint8_t *incomingData, size_t len) { | |||||||
|       case WIZ_SMART_BUTTON_BRIGHT_DOWN  : brightnessDown();                                break; |       case WIZ_SMART_BUTTON_BRIGHT_DOWN  : brightnessDown();                                break; | ||||||
|       default: break; |       default: break; | ||||||
|     } |     } | ||||||
|   last_seq = cur_seq; |   } | ||||||
|  |   ESPNowButton = -1; | ||||||
| } | } | ||||||
|  |  | ||||||
| #else | #else | ||||||
| void handleRemote(uint8_t *incomingData, size_t len) {} | void handleRemote() {} | ||||||
| #endif | #endif | ||||||
|   | |||||||
| @@ -963,7 +963,7 @@ void espNowReceiveCB(uint8_t* address, uint8_t* data, uint8_t len, signed int rs | |||||||
|  |  | ||||||
|   // handle WiZ Mote data |   // handle WiZ Mote data | ||||||
|   if (data[0] == 0x91 || data[0] == 0x81 || data[0] == 0x80) { |   if (data[0] == 0x91 || data[0] == 0x81 || data[0] == 0x80) { | ||||||
|     handleRemote(data, len); |     handleWiZdata(data, len); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -84,6 +84,9 @@ void WLED::loop() | |||||||
|   #ifndef WLED_DISABLE_INFRARED |   #ifndef WLED_DISABLE_INFRARED | ||||||
|   handleIR(); |   handleIR(); | ||||||
|   #endif |   #endif | ||||||
|  |   #ifndef WLED_DISABLE_ESPNOW | ||||||
|  |   handleRemote(); | ||||||
|  |   #endif | ||||||
|   #ifndef WLED_DISABLE_ALEXA |   #ifndef WLED_DISABLE_ALEXA | ||||||
|   handleAlexa(); |   handleAlexa(); | ||||||
|   #endif |   #endif | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 netmindz
					netmindz