Skip to content

Commit 7417970

Browse files
committed
Refactor ESP8266WiFiMulti to use esp_delay. Optimisation opportunity: don't thrash the recurrent functions scheduler by using intvl_ms != 0. Keeping the MCU out of the idle task increases power consumption.
1 parent df1460f commit 7417970

File tree

2 files changed

+28
-44
lines changed

2 files changed

+28
-44
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m) {
434434
//tasks to wait correctly.
435435
constexpr unsigned int timeoutValue = 1000; //1 second
436436
if(can_yield()) {
437+
// The final argument, intvl_ms, to esp_delay influences how frequently
438+
// the scheduled recurrent functions (Schedule.h) are probed.
437439
esp_delay(timeoutValue, [m]() { return wifi_get_opmode() != m; }, 5);
438440

439441
//if at this point mode still hasn't been reached, give up

libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp

+26-44
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "ESP8266WiFiMulti.h"
2828
#include <limits.h>
2929
#include <string.h>
30+
#include <coredecls.h>
3031

3132
/**
3233
* @brief Print WiFi status
@@ -83,36 +84,25 @@ static void printWiFiStatus(wl_status_t status)
8384
static wl_status_t waitWiFiConnect(uint32_t connectTimeoutMs)
8485
{
8586
wl_status_t status;
87+
// The final argument, intvl_ms, to esp_delay influences how frequently
88+
// the scheduled recurrent functions (Schedule.h) are probed.
89+
esp_delay(connectTimeoutMs,
90+
[&status]() { status = WiFi.status(); return status != WL_CONNECTED && status != WL_CONNECT_FAILED; }, 0);
8691

87-
// Set WiFi connect timeout
88-
using esp8266::polledTimeout::oneShotMs;
89-
oneShotMs connectTimeout(connectTimeoutMs);
90-
91-
// Wait for WiFi status change or timeout
92-
do {
93-
// Refresh watchdog
94-
delay(0);
95-
96-
// Get WiFi status
97-
status = WiFi.status();
98-
99-
// Check status
100-
if (status == WL_CONNECTED) {
101-
// Connected, print WiFi status
102-
printWiFiStatus(status);
103-
104-
// Return WiFi status
105-
return status;
106-
} else if (status == WL_CONNECT_FAILED) {
107-
DEBUG_WIFI_MULTI("[WIFIM] Connect failed\n");
108-
109-
// Return WiFi connect failed
110-
return WL_CONNECT_FAILED;
111-
}
112-
} while (!connectTimeout);
92+
// Check status
93+
if (status == WL_CONNECTED) {
94+
// Connected, print WiFi status
95+
printWiFiStatus(status);
11396

114-
DEBUG_WIFI_MULTI("[WIFIM] Connect timeout\n");
97+
// Return WiFi status
98+
return status;
99+
} else if (status == WL_CONNECT_FAILED) {
100+
DEBUG_WIFI_MULTI("[WIFIM] Connect failed\n");
101+
} else {
102+
DEBUG_WIFI_MULTI("[WIFIM] Connect timeout\n");
103+
}
115104

105+
// Return WiFi connect failed
116106
return WL_CONNECT_FAILED;
117107
}
118108

@@ -242,24 +232,16 @@ int8_t ESP8266WiFiMulti::startScan()
242232
// Start wifi scan in async mode
243233
WiFi.scanNetworks(true);
244234

245-
// Set WiFi scan timeout
246-
using esp8266::polledTimeout::oneShotMs;
247-
oneShotMs scanTimeout(WIFI_SCAN_TIMEOUT_MS);
248-
249235
// Wait for WiFi scan change or timeout
250-
do {
251-
// Refresh watchdog
252-
delay(0);
253-
254-
// Check scan timeout which may occur when scan does not report completion
255-
if (scanTimeout) {
256-
DEBUG_WIFI_MULTI("[WIFIM] Scan timeout\n");
257-
return WIFI_SCAN_FAILED;
258-
}
259-
260-
// Get scan result
261-
scanResult = WiFi.scanComplete();
262-
} while (scanResult < 0);
236+
// The final argument, intvl_ms, to esp_delay influences how frequently
237+
// the scheduled recurrent functions (Schedule.h) are probed.
238+
esp_delay(WIFI_SCAN_TIMEOUT_MS,
239+
[&scanResult]() { scanResult = WiFi.scanComplete(); return scanResult < 0; }, 0);
240+
// Check for scan timeout which may occur when scan does not report completion
241+
if (scanResult < 0) {
242+
DEBUG_WIFI_MULTI("[WIFIM] Scan timeout\n");
243+
return WIFI_SCAN_FAILED;
244+
}
263245

264246
// Print WiFi scan result
265247
printWiFiScan();

0 commit comments

Comments
 (0)