Skip to content

Commit 3cde1ba

Browse files
committed
Resync WLED-sync with code from WLED that now has the fix for the heap fragmentation issue I spotted
1 parent 09cf85f commit 3cde1ba

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

WLED-sync.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
#if defined(ESP8266) // ESP8266
99
#include <ESP8266WiFi.h>
1010
#endif
11-
11+
12+
#define UDPSOUND_MAX_PACKET 96 // max packet size for audiosync, with a bit of "headroom"
13+
uint8_t fftUdpBuffer[UDPSOUND_MAX_PACKET+1] = { 0 }; // static buffer for receiving
14+
1215
static bool isValidUdpSyncVersion(const char *header) {
1316
return strncmp_P(header, PSTR(UDP_SYNC_HEADER), 6) == 0;
1417
}
@@ -44,30 +47,26 @@ bool WLEDSync::read() // check & process new data. return TRUE in case that ne
4447
bool haveFreshData = false;
4548

4649
size_t packetSize = fftUdp.parsePacket();
47-
if (packetSize > 5) {
50+
if ((packetSize > 0) && ((packetSize < 5) || (packetSize > UDPSOUND_MAX_PACKET))) fftUdp.flush(); // discard invalid packets (too small or too big)
51+
if ((packetSize > 5) && (packetSize <= UDPSOUND_MAX_PACKET)) {
4852
//DEBUGSR_PRINTLN("Received UDP Sync Packet");
49-
uint8_t fftBuff[packetSize];
50-
fftUdp.read(fftBuff, packetSize);
53+
fftUdp.read(fftUdpBuffer, packetSize);
5154

5255
// VERIFY THAT THIS IS A COMPATIBLE PACKET
53-
if (packetSize == sizeof(audioSyncPacket) && (isValidUdpSyncVersion((const char *)fftBuff))) {
54-
WLEDSync::decodeAudioData(packetSize, fftBuff);
56+
if (packetSize == sizeof(audioSyncPacket) && (isValidUdpSyncVersion((const char *)fftUdpBuffer))) {
57+
decodeAudioData(packetSize, fftUdpBuffer);
5558
//DEBUGSR_PRINTLN("Finished parsing UDP Sync Packet v2");
5659
haveFreshData = true;
5760
receivedFormat = 2;
58-
}
59-
else {
60-
if (packetSize == sizeof(audioSyncPacket_v1) && (isValidUdpSyncVersion_v1((const char *)fftBuff))) {
61-
WLEDSync::decodeAudioData_v1(packetSize, fftBuff);
61+
} else {
62+
if (packetSize == sizeof(audioSyncPacket_v1) && (isValidUdpSyncVersion_v1((const char *)fftUdpBuffer))) {
63+
decodeAudioData_v1(packetSize, fftUdpBuffer);
6264
//DEBUGSR_PRINTLN("Finished parsing UDP Sync Packet v1");
6365
haveFreshData = true;
6466
receivedFormat = 1;
6567
} else receivedFormat = 0; // unknown format
6668
}
6769
}
68-
else if(packetSize > 0) {
69-
Serial.println("packetSize=" + packetSize);
70-
}
7170
return haveFreshData;
7271
}
7372

0 commit comments

Comments
 (0)