Skip to content

Commit

Permalink
Fix USB sleep logic to match what tinyusb does (#69)
Browse files Browse the repository at this point in the history
* Expose more TinyUSB helpers
* Change USB connected logic to match how tinyusb behaves
  • Loading branch information
tomsci authored Jun 4, 2022
1 parent 003d1f6 commit c755294
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
12 changes: 12 additions & 0 deletions drivers/tidal_helpers/tidal_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ STATIC mp_obj_t tidal_helper_usb_connected() {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(tidal_helper_usb_connected_obj, tidal_helper_usb_connected);

STATIC mp_obj_t tidal_helper_usb_suspended() {
return tud_suspended() ? mp_const_true : mp_const_false;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(tidal_helper_usb_suspended_obj, tidal_helper_usb_suspended);

STATIC mp_obj_t tidal_helper_usb_mounted() {
return tud_mounted() ? mp_const_true : mp_const_false;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(tidal_helper_usb_mounted_obj, tidal_helper_usb_mounted);

STATIC mp_obj_t tidal_esp_sleep_pd_config(mp_obj_t domain_obj, mp_obj_t option_obj) {
esp_sleep_pd_domain_t domain = (esp_sleep_pd_domain_t)mp_obj_get_int(domain_obj);
esp_sleep_pd_option_t option = (esp_sleep_pd_option_t)mp_obj_get_int(option_obj);
Expand Down Expand Up @@ -379,6 +389,8 @@ STATIC const mp_rom_map_elem_t tidal_helpers_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ota) },
{ MP_ROM_QSTR(MP_QSTR_get_variant), MP_ROM_PTR(&tidal_helper_get_variant_obj) },
{ MP_ROM_QSTR(MP_QSTR_usb_connected), MP_ROM_PTR(&tidal_helper_usb_connected_obj) },
{ MP_ROM_QSTR(MP_QSTR_usb_suspended), MP_ROM_PTR(&tidal_helper_usb_suspended_obj) },
{ MP_ROM_QSTR(MP_QSTR_usb_mounted), MP_ROM_PTR(&tidal_helper_usb_mounted_obj) },
{ MP_ROM_QSTR(MP_QSTR_esp_sleep_enable_gpio_wakeup), MP_ROM_PTR(&tidal_esp_sleep_enable_gpio_wakeup_obj) },
{ MP_ROM_QSTR(MP_QSTR_esp_sleep_pd_config), MP_ROM_PTR(&tidal_esp_sleep_pd_config_obj) },
{ MP_ROM_QSTR(MP_QSTR_gpio_wakeup), MP_ROM_PTR(&tidal_gpio_wakeup_obj) },
Expand Down
2 changes: 1 addition & 1 deletion modules/app_launcher/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def update_title(self, redraw):
if not get_scheduler().is_sleep_enabled():
title += "\nSLEEP DISABLED"
pwr = tidal.CHARGE_DET.value() == 0 and 1 or 0
conn = tidal_helpers.usb_connected() and 1 or 0
conn = (tidal_helpers.usb_connected() and not tidal_helpers.usb_suspended()) and 1 or 0
if pwr or conn:
title += f"\nUSB pwr={pwr} conn={conn}"
if title != self.window.title:
Expand Down
3 changes: 2 additions & 1 deletion modules/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,11 @@ def is_sleep_enabled(self):
return self.sleep_enabled

def can_sleep(self):
usb_connected = tidal_helpers.usb_connected() and not tidal_helpers.usb_suspended()
return (
self.sleep_enabled and
tidal_helpers.get_variant() != "devboard" and
not tidal_helpers.usb_connected() and
not usb_connected and
not wifi.active() and
time.ticks_ms() >= self.no_sleep_before
)
Expand Down

0 comments on commit c755294

Please sign in to comment.