Skip to content

Commit 1093dea

Browse files
glenn20dpgeorge
authored andcommitted
esp32,esp8266: Add support to set/get power saving mode of WLAN.
For esp32 and esp8266 this commit adds: - a 'pm' option to WLAN.config() to set/get the wifi power saving mode; and - PM_NONE, PM_PERFORMANCE and PM_POWERSAVE constants to the WLAN class. This API should be general enough to use with all WLAN drivers. Documentation is also added.
1 parent 786013d commit 1093dea

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

docs/library/network.WLAN.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,20 @@ Methods
133133
hostname The hostname that will be sent to DHCP (STA interfaces) and mDNS (if supported, both STA and AP). (Deprecated, use :func:`network.hostname` instead)
134134
reconnects Number of reconnect attempts to make (integer, 0=none, -1=unlimited)
135135
txpower Maximum transmit power in dBm (integer or float)
136+
pm WiFi Power Management setting (see below for allowed values)
136137
============= ===========
138+
139+
Constants
140+
---------
141+
142+
.. data:: WLAN.PM_PERFORMANCE
143+
WLAN.PM_POWERSAVE
144+
WLAN.PM_NONE
145+
146+
Allowed values for the ``WLAN.config(pm=...)`` network interface parameter:
147+
148+
* ``PM_PERFORMANCE``: enable WiFi power management to balance power
149+
savings and WiFi performance
150+
* ``PM_POWERSAVE``: enable WiFi power management with additional power
151+
savings and reduced WiFi performance
152+
* ``PM_NONE``: disable wifi power management

ports/esp32/network_wlan.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,10 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_
508508
esp_exceptions(esp_wifi_set_protocol(self->if_id, mp_obj_get_int(kwargs->table[i].value)));
509509
break;
510510
}
511+
case MP_QSTR_pm: {
512+
esp_exceptions(esp_wifi_set_ps(mp_obj_get_int(kwargs->table[i].value)));
513+
break;
514+
}
511515
default:
512516
goto unknown;
513517
}
@@ -602,6 +606,12 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_
602606
val = MP_OBJ_NEW_SMALL_INT(protocol_bitmap);
603607
break;
604608
}
609+
case MP_QSTR_pm: {
610+
wifi_ps_type_t ps_type;
611+
esp_exceptions(esp_wifi_get_ps(&ps_type));
612+
val = MP_OBJ_NEW_SMALL_INT(ps_type);
613+
break;
614+
}
605615
default:
606616
goto unknown;
607617
}
@@ -627,6 +637,11 @@ STATIC const mp_rom_map_elem_t wlan_if_locals_dict_table[] = {
627637
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&network_wlan_isconnected_obj) },
628638
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&network_wlan_config_obj) },
629639
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_network_ifconfig_obj) },
640+
641+
// Constants
642+
{ MP_ROM_QSTR(MP_QSTR_PM_NONE), MP_ROM_INT(WIFI_PS_NONE) },
643+
{ MP_ROM_QSTR(MP_QSTR_PM_PERFORMANCE), MP_ROM_INT(WIFI_PS_MIN_MODEM) },
644+
{ MP_ROM_QSTR(MP_QSTR_PM_POWERSAVE), MP_ROM_INT(WIFI_PS_MAX_MODEM) },
630645
};
631646
STATIC MP_DEFINE_CONST_DICT(wlan_if_locals_dict, wlan_if_locals_dict_table);
632647

ports/esp8266/network_wlan.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,10 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
419419
system_phy_set_max_tpw(power);
420420
break;
421421
}
422+
case MP_QSTR_pm: {
423+
wifi_set_sleep_type(mp_obj_get_int(kwargs->table[i].value));
424+
break;
425+
}
422426
default:
423427
goto unknown;
424428
}
@@ -486,6 +490,10 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
486490
val = mp_obj_new_int(wifi_get_phy_mode());
487491
break;
488492
}
493+
case MP_QSTR_pm: {
494+
val = MP_OBJ_NEW_SMALL_INT(wifi_get_sleep_type());
495+
break;
496+
}
489497
default:
490498
goto unknown;
491499
}
@@ -511,6 +519,11 @@ STATIC const mp_rom_map_elem_t wlan_if_locals_dict_table[] = {
511519
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&esp_isconnected_obj) },
512520
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&esp_config_obj) },
513521
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_ifconfig_obj) },
522+
523+
// Constants
524+
{ MP_ROM_QSTR(MP_QSTR_PM_NONE), MP_ROM_INT(NONE_SLEEP_T) },
525+
{ MP_ROM_QSTR(MP_QSTR_PM_PERFORMANCE), MP_ROM_INT(MODEM_SLEEP_T) },
526+
{ MP_ROM_QSTR(MP_QSTR_PM_POWERSAVE), MP_ROM_INT(LIGHT_SLEEP_T) },
514527
};
515528

516529
STATIC MP_DEFINE_CONST_DICT(wlan_if_locals_dict, wlan_if_locals_dict_table);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy