Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 40 additions & 31 deletions usermods/audioreactive/audio_reactive.h
Original file line number Diff line number Diff line change
Expand Up @@ -1836,54 +1836,62 @@ class AudioReactive : public Usermod {
agcSensitivity = 128.0f; // substitute - V1 format does not include this value
}

bool receiveAudioData() // check & process new data. return TRUE in case that new audio data was received.
{
bool receiveAudioData() {
if (!udpSyncConnected) return false;
bool haveFreshData = false;

size_t packetSize = 0;
// WLEDMM use exception handler to catch out-of-memory errors
#if __cpp_exceptions
try{
static uint8_t fftUdpBuffer[UDPSOUND_MAX_PACKET + 1] = {0};
size_t lastValidPacketSize = 0;

// Loop to read all available packets
while (true) {
#if __cpp_exceptions
try {
packetSize = fftUdp.parsePacket();
} catch(...) {
packetSize = 0; // low heap memory -> discard packet.
#ifdef ARDUINO_ARCH_ESP32
fftUdp.flush(); // this does not work on 8266
#endif
} catch (...) {
packetSize = 0;
#ifdef ARDUINO_ARCH_ESP32
fftUdp.flush();
#endif
DEBUG_PRINTLN(F("receiveAudioData: parsePacket out of memory exception caught!"));
USER_FLUSH();
continue; // Skip to next iteration
}
#else
#else
packetSize = fftUdp.parsePacket();
#endif
#endif

#ifdef ARDUINO_ARCH_ESP32
if ((packetSize > 0) && ((packetSize < 5) || (packetSize > UDPSOUND_MAX_PACKET))) fftUdp.flush(); // discard invalid packets (too small or too big)
#endif
if ((packetSize > 5) && (packetSize <= UDPSOUND_MAX_PACKET)) {
static uint8_t fftUdpBuffer[UDPSOUND_MAX_PACKET+1] = { 0 }; // static buffer for receiving, to reuse the same memory and avoid heap fragmentation
//DEBUGSR_PRINTLN("Received UDP Sync Packet");
fftUdp.read(fftUdpBuffer, packetSize);
#ifdef ARDUINO_ARCH_ESP32
if ((packetSize > 0) && ((packetSize < 5) || (packetSize > UDPSOUND_MAX_PACKET))) {
fftUdp.flush();
continue; // Skip invalid packets
}
#endif

if (packetSize == 0) break; // No more packets available

// VERIFY THAT THIS IS A COMPATIBLE PACKET
if (packetSize == sizeof(audioSyncPacket) && (isValidUdpSyncVersion((const char *)fftUdpBuffer))) {
if ((packetSize > 5) && (packetSize <= UDPSOUND_MAX_PACKET)) {
fftUdp.read(fftUdpBuffer, packetSize);
lastValidPacketSize = packetSize;
}
}

// Process only the last valid packet
if (lastValidPacketSize > 0) {
if (lastValidPacketSize == sizeof(audioSyncPacket) && (isValidUdpSyncVersion((const char *)fftUdpBuffer))) {
receivedFormat = 2;
haveFreshData = decodeAudioData(packetSize, fftUdpBuffer);
//DEBUGSR_PRINTLN("Finished parsing UDP Sync Packet v2");
haveFreshData = decodeAudioData(lastValidPacketSize, fftUdpBuffer);
} else if (lastValidPacketSize == sizeof(audioSyncPacket_v1) && (isValidUdpSyncVersion_v1((const char *)fftUdpBuffer))) {
decodeAudioData_v1(lastValidPacketSize, fftUdpBuffer);
receivedFormat = 1;
haveFreshData = true;
} else {
if (packetSize == sizeof(audioSyncPacket_v1) && (isValidUdpSyncVersion_v1((const char *)fftUdpBuffer))) {
decodeAudioData_v1(packetSize, fftUdpBuffer);
receivedFormat = 1;
//DEBUGSR_PRINTLN("Finished parsing UDP Sync Packet v1");
haveFreshData = true;
} else receivedFormat = 0; // unknown format
receivedFormat = 0; // unknown format
}
}
return haveFreshData;
}


//////////////////////
// usermod functions//
//////////////////////
Expand Down Expand Up @@ -2319,6 +2327,7 @@ class AudioReactive : public Usermod {
static float syncVolumeSmth = 0;
bool have_new_sample = false;
if (millis() - lastTime > delayMs) {
// DEBUG_PRINTF(F("AR reading at %d compared to %d max\n"), millis() - lastTime, delayMs); // TroyHacks
have_new_sample = receiveAudioData();
if (have_new_sample) {
last_UDPTime = millis();
Expand Down
14 changes: 12 additions & 2 deletions wled00/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ static bool bufferedFind(const char *target, bool fromStart = true) {

size_t index = 0;
byte buf[FS_BUFSIZE];
#if ESP_IDF_VERSION_MAJOR >= 4
f.setBufferSize(FS_BUFSIZE);
#endif
if (fromStart) f.seek(0);

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

size_t index = 0; // better to use size_t instead if uint16_t
byte buf[FS_BUFSIZE];
#if ESP_IDF_VERSION_MAJOR >= 4
f.setBufferSize(FS_BUFSIZE);
#endif
if (fromStart) f.seek(0);

while (f.position() < f.size() -1) {
Expand Down Expand Up @@ -150,7 +156,9 @@ static bool bufferedFindObjectEnd() {
uint16_t objDepth = 0; //num of '{' minus num of '}'. return once 0
//size_t start = f.position();
byte buf[FS_BUFSIZE];

#if ESP_IDF_VERSION_MAJOR >= 4
f.setBufferSize(FS_BUFSIZE);
#endif
while (f.position() < f.size() -1) {
size_t bufsize = f.read(buf, FS_BUFSIZE); // better to use size_t instead of uint16_t
size_t count = 0;
Expand All @@ -175,7 +183,9 @@ static void writeSpace(size_t l)
{
byte buf[FS_BUFSIZE];
memset(buf, ' ', FS_BUFSIZE);

#if ESP_IDF_VERSION_MAJOR >= 4
f.setBufferSize(FS_BUFSIZE);
#endif
while (l > 0) {
size_t block = (l>FS_BUFSIZE) ? FS_BUFSIZE : l;
f.write(buf, block);
Expand Down
21 changes: 18 additions & 3 deletions wled00/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ int getSignalQuality(int rssi)
return quality;
}

#if ESP_IDF_VERSION_MAJOR >= 4
#define SYSTEM_EVENT_ETH_CONNECTED ARDUINO_EVENT_ETH_CONNECTED
#define SYSTEM_EVENT_ETH_DISCONNECTED ARDUINO_EVENT_ETH_DISCONNECTED
#define SYSTEM_EVENT_ETH_START ARDUINO_EVENT_ETH_START
#define SYSTEM_EVENT_ETH_GOT_IP ARDUINO_EVENT_ETH_GOT_IP
#endif

//handle Ethernet connection event
void WiFiEvent(WiFiEvent_t event)
Expand All @@ -170,12 +176,21 @@ void WiFiEvent(WiFiEvent_t event)
case SYSTEM_EVENT_ETH_START:
DEBUG_PRINTLN(F("ETH Started"));
break;
case SYSTEM_EVENT_ETH_GOT_IP:
if (Network.isEthernet()) {
if (!apActive) {
DEBUG_PRINTLN(F("WiFi Connected *and* ETH Connected. Disabling WIFi"));
WiFi.disconnect(true);
} else {
DEBUG_PRINTLN(F("WiFi Connected *and* ETH Connected. Leaving AP WiFi active"));
}
} else {
DEBUG_PRINTLN(F("WiFi Connected. No ETH"));
}
break;
case SYSTEM_EVENT_ETH_CONNECTED:
{
DEBUG_PRINTLN(F("ETH Connected"));
if (!apActive) {
WiFi.disconnect(true);
}
if (staticIP != (uint32_t)0x00000000 && staticGateway != (uint32_t)0x00000000) {
ETH.config(staticIP, staticGateway, staticSubnet, IPAddress(8, 8, 8, 8));
} else {
Expand Down
19 changes: 9 additions & 10 deletions wled00/wled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1370,16 +1370,15 @@ void WLED::handleConnection()
} else if (!interfacesInited) { //newly connected
USER_PRINTLN("");
USER_PRINT(F("Connected! IP address: http://"));
USER_PRINTLN(Network.localIP());
//if (Network.isEthernet()) {
// #if ESP32
// USER_PRINT(ETH.localIP());
// USER_PRINTLN(" via Ethernet");
// #endif
//} else {
// USER_PRINT(Network.localIP());
// USER_PRINTLN(" via WiFi");
//}
USER_PRINT(Network.localIP());
if (Network.isEthernet()) {
#if ESP32
USER_PRINTLN(" via Ethernet (disabling WiFi)");
WiFi.disconnect(true);
#endif
} else {
USER_PRINTLN(" via WiFi");
}

if (improvActive) {
if (improvError == 3) sendImprovStateResponse(0x00, true);
Expand Down