From 61440d9e2b7b0a1d4fce83e970a7c9ff6daccdba Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Tue, 29 Dec 2015 16:14:27 +0100 Subject: [PATCH] add WiFi sleep management Note: testing needed --- .../ESP8266WiFi/src/ESP8266WiFiGeneric.cpp | 39 ++++++++++++++++++- .../ESP8266WiFi/src/ESP8266WiFiGeneric.h | 6 ++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp index 78c55f76f..241206148 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -52,6 +52,7 @@ extern "C" void esp_yield(); bool ESP8266WiFiGenericClass::_persistent = true; WiFiEventCb ESP8266WiFiGenericClass::_cbEvent = NULL; +WiFiMode_t ESP8266WiFiGenericClass::_forceSleepLastMode = WIFI_OFF; ESP8266WiFiGenericClass::ESP8266WiFiGenericClass() { wifi_set_event_handler_cb((wifi_event_handler_cb_t) &ESP8266WiFiGenericClass::_eventCallback); @@ -128,7 +129,7 @@ WiFiPhyMode_t ESP8266WiFiGenericClass::getPhyMode() { * set the output power of WiFi * @param dBm max: +20.5dBm min: 0dBm */ -void ESP8266WiFiGenericClass::setOutputPower(float_t dBm) { +void ESP8266WiFiGenericClass::setOutputPower(float dBm) { if(dBm > 20.5) { dBm = 20.5; @@ -223,7 +224,43 @@ bool ESP8266WiFiGenericClass::enableAP(bool enable){ } +/** + * Disable WiFi for x us when value is not 0 + * @param sleep_time_in_us + * @return ok + */ +bool ESP8266WiFiGenericClass::forceSleepBegin(uint32 sleepUs) { + _forceSleepLastMode = getMode(); + if(!mode(WIFI_OFF)) { + return false; + } + if(sleepUs == 0) { + sleepUs = 0xFFFFFFF; + } + + wifi_fpm_set_sleep_type(MODEM_SLEEP_T); + wifi_fpm_open(); + return (wifi_fpm_do_sleep(sleepUs) == 0); +} + +/** + * wake up WiFi Modem + * @return ok + */ +bool ESP8266WiFiGenericClass::forceSleepWake() { + wifi_fpm_do_wakeup(); + wifi_fpm_close(); + + // restore last mode + if(mode(_forceSleepLastMode)) { + if((_forceSleepLastMode & WIFI_STA) != 0){ + wifi_station_connect(); + } + return true; + } + return false; +} // ----------------------------------------------------------------------------------------------------------------------- diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h index 2232941c9..83b4c3fc5 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h @@ -46,7 +46,7 @@ class ESP8266WiFiGenericClass { bool setPhyMode(WiFiPhyMode_t mode); WiFiPhyMode_t getPhyMode(); - void setOutputPower(float_t dBm); + void setOutputPower(float dBm); void persistent(bool persistent); @@ -56,9 +56,13 @@ class ESP8266WiFiGenericClass { bool enableSTA(bool enable); bool enableAP(bool enable); + bool forceSleepBegin(uint32 sleepUs = 0); + bool forceSleepWake(); + protected: static bool _persistent; static WiFiEventCb _cbEvent; + static WiFiMode_t _forceSleepLastMode; static void _eventCallback(void *event);