Skip to content

Commit 61440d9

Browse files
committed
add WiFi sleep management
Note: testing needed
1 parent bbeaeaa commit 61440d9

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

+38-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ extern "C" void esp_yield();
5252

5353
bool ESP8266WiFiGenericClass::_persistent = true;
5454
WiFiEventCb ESP8266WiFiGenericClass::_cbEvent = NULL;
55+
WiFiMode_t ESP8266WiFiGenericClass::_forceSleepLastMode = WIFI_OFF;
5556

5657
ESP8266WiFiGenericClass::ESP8266WiFiGenericClass() {
5758
wifi_set_event_handler_cb((wifi_event_handler_cb_t) &ESP8266WiFiGenericClass::_eventCallback);
@@ -128,7 +129,7 @@ WiFiPhyMode_t ESP8266WiFiGenericClass::getPhyMode() {
128129
* set the output power of WiFi
129130
* @param dBm max: +20.5dBm min: 0dBm
130131
*/
131-
void ESP8266WiFiGenericClass::setOutputPower(float_t dBm) {
132+
void ESP8266WiFiGenericClass::setOutputPower(float dBm) {
132133

133134
if(dBm > 20.5) {
134135
dBm = 20.5;
@@ -223,7 +224,43 @@ bool ESP8266WiFiGenericClass::enableAP(bool enable){
223224
}
224225

225226

227+
/**
228+
* Disable WiFi for x us when value is not 0
229+
* @param sleep_time_in_us
230+
* @return ok
231+
*/
232+
bool ESP8266WiFiGenericClass::forceSleepBegin(uint32 sleepUs) {
233+
_forceSleepLastMode = getMode();
234+
if(!mode(WIFI_OFF)) {
235+
return false;
236+
}
237+
238+
if(sleepUs == 0) {
239+
sleepUs = 0xFFFFFFF;
240+
}
241+
242+
wifi_fpm_set_sleep_type(MODEM_SLEEP_T);
243+
wifi_fpm_open();
244+
return (wifi_fpm_do_sleep(sleepUs) == 0);
245+
}
226246

247+
/**
248+
* wake up WiFi Modem
249+
* @return ok
250+
*/
251+
bool ESP8266WiFiGenericClass::forceSleepWake() {
252+
wifi_fpm_do_wakeup();
253+
wifi_fpm_close();
254+
255+
// restore last mode
256+
if(mode(_forceSleepLastMode)) {
257+
if((_forceSleepLastMode & WIFI_STA) != 0){
258+
wifi_station_connect();
259+
}
260+
return true;
261+
}
262+
return false;
263+
}
227264

228265

229266
// -----------------------------------------------------------------------------------------------------------------------

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ESP8266WiFiGenericClass {
4646
bool setPhyMode(WiFiPhyMode_t mode);
4747
WiFiPhyMode_t getPhyMode();
4848

49-
void setOutputPower(float_t dBm);
49+
void setOutputPower(float dBm);
5050

5151
void persistent(bool persistent);
5252

@@ -56,9 +56,13 @@ class ESP8266WiFiGenericClass {
5656
bool enableSTA(bool enable);
5757
bool enableAP(bool enable);
5858

59+
bool forceSleepBegin(uint32 sleepUs = 0);
60+
bool forceSleepWake();
61+
5962
protected:
6063
static bool _persistent;
6164
static WiFiEventCb _cbEvent;
65+
static WiFiMode_t _forceSleepLastMode;
6266

6367
static void _eventCallback(void *event);
6468

0 commit comments

Comments
 (0)