Skip to content

Commit 7cb8eeb

Browse files
authored
Merge pull request #236 from troyhacks/mdev
Art-Net Improvements and Other Fixes
2 parents d44f9eb + 4a3b7b5 commit 7cb8eeb

8 files changed

Lines changed: 91 additions & 51 deletions

File tree

usermods/audioreactive/audio_reactive.h

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,54 +1836,62 @@ class AudioReactive : public Usermod {
18361836
agcSensitivity = 128.0f; // substitute - V1 format does not include this value
18371837
}
18381838

1839-
bool receiveAudioData() // check & process new data. return TRUE in case that new audio data was received.
1840-
{
1839+
bool receiveAudioData() {
18411840
if (!udpSyncConnected) return false;
18421841
bool haveFreshData = false;
1843-
18441842
size_t packetSize = 0;
1845-
// WLEDMM use exception handler to catch out-of-memory errors
1846-
#if __cpp_exceptions
1847-
try{
1843+
static uint8_t fftUdpBuffer[UDPSOUND_MAX_PACKET + 1] = {0};
1844+
size_t lastValidPacketSize = 0;
1845+
1846+
// Loop to read all available packets
1847+
while (true) {
1848+
#if __cpp_exceptions
1849+
try {
18481850
packetSize = fftUdp.parsePacket();
1849-
} catch(...) {
1850-
packetSize = 0; // low heap memory -> discard packet.
1851-
#ifdef ARDUINO_ARCH_ESP32
1852-
fftUdp.flush(); // this does not work on 8266
1853-
#endif
1851+
} catch (...) {
1852+
packetSize = 0;
1853+
#ifdef ARDUINO_ARCH_ESP32
1854+
fftUdp.flush();
1855+
#endif
18541856
DEBUG_PRINTLN(F("receiveAudioData: parsePacket out of memory exception caught!"));
18551857
USER_FLUSH();
1858+
continue; // Skip to next iteration
18561859
}
1857-
#else
1860+
#else
18581861
packetSize = fftUdp.parsePacket();
1859-
#endif
1862+
#endif
18601863

1861-
#ifdef ARDUINO_ARCH_ESP32
1862-
if ((packetSize > 0) && ((packetSize < 5) || (packetSize > UDPSOUND_MAX_PACKET))) fftUdp.flush(); // discard invalid packets (too small or too big)
1863-
#endif
1864-
if ((packetSize > 5) && (packetSize <= UDPSOUND_MAX_PACKET)) {
1865-
static uint8_t fftUdpBuffer[UDPSOUND_MAX_PACKET+1] = { 0 }; // static buffer for receiving, to reuse the same memory and avoid heap fragmentation
1866-
//DEBUGSR_PRINTLN("Received UDP Sync Packet");
1867-
fftUdp.read(fftUdpBuffer, packetSize);
1864+
#ifdef ARDUINO_ARCH_ESP32
1865+
if ((packetSize > 0) && ((packetSize < 5) || (packetSize > UDPSOUND_MAX_PACKET))) {
1866+
fftUdp.flush();
1867+
continue; // Skip invalid packets
1868+
}
1869+
#endif
1870+
1871+
if (packetSize == 0) break; // No more packets available
18681872

1869-
// VERIFY THAT THIS IS A COMPATIBLE PACKET
1870-
if (packetSize == sizeof(audioSyncPacket) && (isValidUdpSyncVersion((const char *)fftUdpBuffer))) {
1873+
if ((packetSize > 5) && (packetSize <= UDPSOUND_MAX_PACKET)) {
1874+
fftUdp.read(fftUdpBuffer, packetSize);
1875+
lastValidPacketSize = packetSize;
1876+
}
1877+
}
1878+
1879+
// Process only the last valid packet
1880+
if (lastValidPacketSize > 0) {
1881+
if (lastValidPacketSize == sizeof(audioSyncPacket) && (isValidUdpSyncVersion((const char *)fftUdpBuffer))) {
18711882
receivedFormat = 2;
1872-
haveFreshData = decodeAudioData(packetSize, fftUdpBuffer);
1873-
//DEBUGSR_PRINTLN("Finished parsing UDP Sync Packet v2");
1883+
haveFreshData = decodeAudioData(lastValidPacketSize, fftUdpBuffer);
1884+
} else if (lastValidPacketSize == sizeof(audioSyncPacket_v1) && (isValidUdpSyncVersion_v1((const char *)fftUdpBuffer))) {
1885+
decodeAudioData_v1(lastValidPacketSize, fftUdpBuffer);
1886+
receivedFormat = 1;
1887+
haveFreshData = true;
18741888
} else {
1875-
if (packetSize == sizeof(audioSyncPacket_v1) && (isValidUdpSyncVersion_v1((const char *)fftUdpBuffer))) {
1876-
decodeAudioData_v1(packetSize, fftUdpBuffer);
1877-
receivedFormat = 1;
1878-
//DEBUGSR_PRINTLN("Finished parsing UDP Sync Packet v1");
1879-
haveFreshData = true;
1880-
} else receivedFormat = 0; // unknown format
1889+
receivedFormat = 0; // unknown format
18811890
}
18821891
}
18831892
return haveFreshData;
18841893
}
18851894

1886-
18871895
//////////////////////
18881896
// usermod functions//
18891897
//////////////////////
@@ -2319,6 +2327,7 @@ class AudioReactive : public Usermod {
23192327
static float syncVolumeSmth = 0;
23202328
bool have_new_sample = false;
23212329
if (millis() - lastTime > delayMs) {
2330+
// DEBUG_PRINTF(F("AR reading at %d compared to %d max\n"), millis() - lastTime, delayMs); // TroyHacks
23222331
have_new_sample = receiveAudioData();
23232332
if (have_new_sample) {
23242333
last_UDPTime = millis();

wled00/data/settings_leds.htm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,12 @@
246246
gId("dig"+n+"W").innerHTML = "<br />You need at least 1 output!";
247247
}
248248
if (outputs > 1 && fps_limit > 33333/leds_per_output) gId("dig"+n+"W").innerHTML += "<br />FPS limit may be too high for WS281x pixels.";
249-
if (outputs*leds_per_output != total_leds) gId("dig"+n+"W").innerHTML += "<br />Total LEDs doesn't match outputs * LEDs per output.";
249+
if (outputs*leds_per_output != total_leds) gId("dig"+n+"W").innerHTML += "<br />Length ("+total_leds+") doesn't match Number of Outputs * LEDs Per Output ("+outputs*leds_per_output+").";
250250
if (last_octet == 255) {
251251
if (total_leds <= 1024) gId("dig"+n+"W").innerHTML += "<br />Art-Net is in broadcast mode.";
252252
if (total_leds > 1024) gId("dig"+n+"W").innerHTML += "<br />You are sending a lot of broadcast data. Be cautious.";
253253
}
254+
gId("dig"+n+"W").innerHTML += "<br />Art-Net starting universe is "+d.e131Universe+". Change this in <a href='/settings/sync'>Sync Interfaces</a>.";
254255
}
255256
if (!(t > 28 && t < 32)) d.getElementsByName("WO"+n)[0].value = 0; // reset swapping
256257
gId("dig"+n+"c").style.display = ((t >= 40 && t < 48)||(t >= 100 && t < 110)) ? "none":"inline"; // hide count for analog and HUB75
@@ -422,6 +423,7 @@
422423
<option value="80">DDP RGB (network)</option>
423424
<!--option value="81">E1.31 RGB (network)</option-->
424425
<option value="82">Art-Net RGB (network)</option>
426+
<option value="83">Art-Net RGBW (network)</option>
425427
<option value="88">DDP RGBW (network)</option>
426428
<option value="101">Hub75Matrix 32x32</option>
427429
<option value="102">Hub75Matrix 64x32</option>

wled00/data/settings_sync.htm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
<div class="helpB"><button type="button" onclick="H()">?</button></div>
8181
<button type="button" onclick="B()">Back</button><button type="submit">Save</button><hr>
8282
</div>
83-
<h2>Sync setup</h2>
83+
<h2>Sync Interfaces</h2>
8484
<h3>WLED Broadcast</h3>
8585
UDP Port: <input name="UP" type="number" min="1" max="65535" class="d5" required><br>
8686
2nd Port: <input name="U2" type="number" min="1" max="65535" class="d5" required><br>
@@ -139,7 +139,7 @@ <h3>Instance List</h3>
139139
<h3>Realtime</h3>
140140
Receive UDP realtime: <input type="checkbox" name="RD"><br>
141141
Use main segment only: <input type="checkbox" name="MO"><br><br>
142-
<i>Network DMX input</i><br>
142+
<i>Network DMX</i><br>
143143
Type:
144144
<select name=DI onchange="SP(); adj();">
145145
<option value=5568>E1.31 (sACN)</option>
@@ -148,7 +148,7 @@ <h3>Realtime</h3>
148148
</select><br>
149149
<div id=xp>Port: <input name="EP" type="number" min="1" max="65535" value="5568" class="d5" required><br></div>
150150
Multicast: <input type="checkbox" name="EM"><br>
151-
Start universe: <input name="EU" type="number" min="0" max="63999" required><br>
151+
Start universe: <input name="EU" type="number" min="0" max="63999" required> (Also used for sending Art-Net)<br>
152152
<i>Reboot required.</i> Check out <a href="https://github.com/LedFx/LedFx" target="_blank">LedFx</a>!<br>
153153
Skip out-of-sequence packets: <input type="checkbox" name="ES"><br>
154154
DMX start address: <input name="DA" type="number" min="1" max="510" required><br>

wled00/file.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ static bool bufferedFind(const char *target, bool fromStart = true) {
6666

6767
size_t index = 0;
6868
byte buf[FS_BUFSIZE];
69+
#if ESP_IDF_VERSION_MAJOR >= 4
70+
f.setBufferSize(FS_BUFSIZE);
71+
#endif
6972
if (fromStart) f.seek(0);
7073

7174
while (f.position() < f.size() -1) {
@@ -107,6 +110,9 @@ static bool bufferedFindSpace(size_t targetLen, bool fromStart = true) {
107110

108111
size_t index = 0; // better to use size_t instead if uint16_t
109112
byte buf[FS_BUFSIZE];
113+
#if ESP_IDF_VERSION_MAJOR >= 4
114+
f.setBufferSize(FS_BUFSIZE);
115+
#endif
110116
if (fromStart) f.seek(0);
111117

112118
while (f.position() < f.size() -1) {
@@ -150,7 +156,9 @@ static bool bufferedFindObjectEnd() {
150156
uint16_t objDepth = 0; //num of '{' minus num of '}'. return once 0
151157
//size_t start = f.position();
152158
byte buf[FS_BUFSIZE];
153-
159+
#if ESP_IDF_VERSION_MAJOR >= 4
160+
f.setBufferSize(FS_BUFSIZE);
161+
#endif
154162
while (f.position() < f.size() -1) {
155163
size_t bufsize = f.read(buf, FS_BUFSIZE); // better to use size_t instead of uint16_t
156164
size_t count = 0;
@@ -175,7 +183,9 @@ static void writeSpace(size_t l)
175183
{
176184
byte buf[FS_BUFSIZE];
177185
memset(buf, ' ', FS_BUFSIZE);
178-
186+
#if ESP_IDF_VERSION_MAJOR >= 4
187+
f.setBufferSize(FS_BUFSIZE);
188+
#endif
179189
while (l > 0) {
180190
size_t block = (l>FS_BUFSIZE) ? FS_BUFSIZE : l;
181191
f.write(buf, block);

wled00/network.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ int getSignalQuality(int rssi)
161161
return quality;
162162
}
163163

164+
#if ESP_IDF_VERSION_MAJOR >= 4
165+
#define SYSTEM_EVENT_ETH_CONNECTED ARDUINO_EVENT_ETH_CONNECTED
166+
#define SYSTEM_EVENT_ETH_DISCONNECTED ARDUINO_EVENT_ETH_DISCONNECTED
167+
#define SYSTEM_EVENT_ETH_START ARDUINO_EVENT_ETH_START
168+
#define SYSTEM_EVENT_ETH_GOT_IP ARDUINO_EVENT_ETH_GOT_IP
169+
#endif
164170

165171
//handle Ethernet connection event
166172
void WiFiEvent(WiFiEvent_t event)
@@ -170,12 +176,21 @@ void WiFiEvent(WiFiEvent_t event)
170176
case SYSTEM_EVENT_ETH_START:
171177
DEBUG_PRINTLN(F("ETH Started"));
172178
break;
179+
case SYSTEM_EVENT_ETH_GOT_IP:
180+
if (Network.isEthernet()) {
181+
if (!apActive) {
182+
DEBUG_PRINTLN(F("WiFi Connected *and* ETH Connected. Disabling WIFi"));
183+
WiFi.disconnect(true);
184+
} else {
185+
DEBUG_PRINTLN(F("WiFi Connected *and* ETH Connected. Leaving AP WiFi active"));
186+
}
187+
} else {
188+
DEBUG_PRINTLN(F("WiFi Connected. No ETH"));
189+
}
190+
break;
173191
case SYSTEM_EVENT_ETH_CONNECTED:
174192
{
175193
DEBUG_PRINTLN(F("ETH Connected"));
176-
if (!apActive) {
177-
WiFi.disconnect(true);
178-
}
179194
if (staticIP != (uint32_t)0x00000000 && staticGateway != (uint32_t)0x00000000) {
180195
ETH.config(staticIP, staticGateway, staticSubnet, IPAddress(8, 8, 8, 8));
181196
} else {

wled00/udp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ uint8_t IRAM_ATTR_YN realtimeBroadcast(uint8_t type, IPAddress client, uint16_t
911911
const uint_fast16_t ARTNET_CHANNELS_PER_PACKET = isRGBW?512:510; // 512/4=128 RGBW LEDs, 510/3=170 RGB LEDs
912912

913913
uint_fast16_t bufferOffset = 0;
914-
uint_fast16_t hardware_output_universe = 0;
914+
uint_fast16_t hardware_output_universe = e131Universe; // start at the universe defined in Sync Setup
915915

916916
sequenceNumber++;
917917

wled00/wled.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,16 +1370,15 @@ void WLED::handleConnection()
13701370
} else if (!interfacesInited) { //newly connected
13711371
USER_PRINTLN("");
13721372
USER_PRINT(F("Connected! IP address: http://"));
1373-
USER_PRINTLN(Network.localIP());
1374-
//if (Network.isEthernet()) {
1375-
// #if ESP32
1376-
// USER_PRINT(ETH.localIP());
1377-
// USER_PRINTLN(" via Ethernet");
1378-
// #endif
1379-
//} else {
1380-
// USER_PRINT(Network.localIP());
1381-
// USER_PRINTLN(" via WiFi");
1382-
//}
1373+
USER_PRINT(Network.localIP());
1374+
if (Network.isEthernet()) {
1375+
#if ESP32
1376+
USER_PRINTLN(" via Ethernet (disabling WiFi)");
1377+
WiFi.disconnect(true);
1378+
#endif
1379+
} else {
1380+
USER_PRINTLN(" via WiFi");
1381+
}
13831382

13841383
if (improvActive) {
13851384
if (improvError == 3) sendImprovStateResponse(0x00, true);

wled00/xml.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,11 @@ void getSettingsJS(AsyncWebServerRequest* request, byte subPage, char* dest) //W
487487
oappendi(bus->getMaxPixels()); oappend(SET_F(");"));
488488

489489
}
490+
491+
oappend(SET_F("d.e131Universe="));
492+
oappendi(e131Universe); // Art-Net start universe
493+
oappend(SET_F(";"));
494+
490495
sappend('v',SET_F("MA"),strip.ablMilliampsMax);
491496
sappend('v',SET_F("LA"),strip.milliampsPerLed);
492497
if (strip.currentMilliamps)

0 commit comments

Comments
 (0)