Skip to content

Commit 3577da0

Browse files
authored
Avoid redundant localIP calls, each call takes 0.700 us on ESP32 240Mhz (wled#2206)
* Avoid redundant localIP calls, each call takes 0.700 us on ESP32 240Mhz * Fall through to check Wifi local ip if not connected to ETH * Changed local var from ipAddress to localIP to better reflect content
1 parent b8e8028 commit 3577da0

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

wled00/src/dependencies/network/Network.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
IPAddress NetworkClass::localIP()
44
{
5+
IPAddress localIP;
56
#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_ETHERNET)
6-
if (ETH.localIP()[0] != 0) {
7-
return ETH.localIP();
7+
localIP = ETH.localIP();
8+
if (localIP[0] != 0) {
9+
return localIP;
810
}
911
#endif
10-
if (WiFi.localIP()[0] != 0) {
11-
return WiFi.localIP();
12+
localIP = WiFi.localIP();
13+
if (localIP[0] != 0) {
14+
return localIP;
1215
}
16+
1317
return INADDR_NONE;
1418
}
1519

wled00/udp.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ void sendTPM2Ack() {
120120

121121
void handleNotifications()
122122
{
123+
IPAddress localIP;
124+
123125
//send second notification if enabled
124126
if(udpConnected && notificationTwoRequired && millis()-notificationSentTime > 250){
125127
notify(notificationSentCallMode,true);
@@ -175,9 +177,10 @@ void handleNotifications()
175177

176178
if (!(receiveNotifications || receiveDirect)) return;
177179

180+
localIP = Network.localIP();
178181
//notifier and UDP realtime
179182
if (!packetSize || packetSize > UDP_IN_MAXSIZE) return;
180-
if (!isSupp && notifierUdp.remoteIP() == Network.localIP()) return; //don't process broadcasts we send ourselves
183+
if (!isSupp && notifierUdp.remoteIP() == localIP) return; //don't process broadcasts we send ourselves
181184

182185
uint8_t udpIn[packetSize +1];
183186
uint16_t len;
@@ -186,7 +189,7 @@ void handleNotifications()
186189

187190
// WLED nodes info notifications
188191
if (isSupp && udpIn[0] == 255 && udpIn[1] == 1 && len >= 40) {
189-
if (!nodeListEnabled || notifier2Udp.remoteIP() == Network.localIP()) return;
192+
if (!nodeListEnabled || notifier2Udp.remoteIP() == localIP) return;
190193

191194
uint8_t unit = udpIn[39];
192195
NodesMap::iterator it = Nodes.find(unit);

wled00/wled.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,13 +591,14 @@ void WLED::initConnection()
591591

592592
void WLED::initInterfaces()
593593
{
594+
IPAddress ipAddress = Network.localIP();
594595
DEBUG_PRINTLN(F("Init STA interfaces"));
595596

596597
#ifndef WLED_DISABLE_HUESYNC
597598
if (hueIP[0] == 0) {
598-
hueIP[0] = Network.localIP()[0];
599-
hueIP[1] = Network.localIP()[1];
600-
hueIP[2] = Network.localIP()[2];
599+
hueIP[0] = ipAddress[0];
600+
hueIP[1] = ipAddress[1];
601+
hueIP[2] = ipAddress[2];
601602
}
602603
#endif
603604

0 commit comments

Comments
 (0)