diff --git a/main.c b/main.c index 378c506579696..448a3e2af2561 100755 --- a/main.c +++ b/main.c @@ -156,9 +156,6 @@ STATIC void start_mp(supervisor_allocation* heap) { mp_obj_list_init(mp_sys_argv, 0); #if CIRCUITPY_ALARM - // Record which alarm woke us up, if any. An object may be created so the heap must be functional. - alarm_save_wakeup_alarm(); - // Reset alarm module only after we retrieved the wakeup alarm. alarm_reset(); #endif @@ -403,7 +400,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) { asleep = true; new_status_color(BLACK); board_deinit(); - if (!supervisor_workflow_active()) { + if (true) { // Enter true deep sleep. When we wake up we'll be back at the // top of main(), not in this loop. alarm_enter_deep_sleep(); diff --git a/shared-bindings/alarm/__init__.c b/shared-bindings/alarm/__init__.c index 526f5453d1c3d..4b7caa02c5212 100644 --- a/shared-bindings/alarm/__init__.c +++ b/shared-bindings/alarm/__init__.c @@ -37,6 +37,9 @@ #include "shared-bindings/time/__init__.h" #include "supervisor/shared/autoreload.h" #include "supervisor/shared/workflow.h" +#include "esp_log.h" + +static const char *TAG = "sleep"; //| """Alarms and sleep //| @@ -158,9 +161,14 @@ STATIC mp_obj_t alarm_exit_and_deep_sleep_until_alarms(size_t n_args, const mp_o } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(alarm_exit_and_deep_sleep_until_alarms_obj, 1, MP_OBJ_FUN_ARGS_MAX, alarm_exit_and_deep_sleep_until_alarms); +STATIC mp_obj_t alarm_save(void) { + alarm_save_wake_alarm(); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_0(alarm_save_obj, alarm_save); + STATIC const mp_map_elem_t alarm_pin_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pin) }, - { MP_ROM_QSTR(MP_QSTR_PinAlarm), MP_OBJ_FROM_PTR(&alarm_pin_pin_alarm_type) }, }; @@ -173,7 +181,6 @@ STATIC const mp_obj_module_t alarm_pin_module = { STATIC const mp_map_elem_t alarm_time_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) }, - { MP_ROM_QSTR(MP_QSTR_TimeAlarm), MP_OBJ_FROM_PTR(&alarm_time_time_alarm_type) }, }; @@ -200,6 +207,8 @@ STATIC const mp_obj_module_t alarm_touch_module = { STATIC mp_map_elem_t alarm_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_alarm) }, + { MP_ROM_QSTR(MP_QSTR_save), MP_OBJ_FROM_PTR(&alarm_save_obj) }, + // wake_alarm is a mutable attribute. { MP_ROM_QSTR(MP_QSTR_wake_alarm), mp_const_none }, @@ -216,19 +225,30 @@ STATIC mp_map_elem_t alarm_module_globals_table[] = { }; STATIC MP_DEFINE_MUTABLE_DICT(alarm_module_globals, alarm_module_globals_table); -STATIC void alarm_set_wake_alarm(mp_obj_t alarm) { - // Equivalent of: - // alarm.wake_alarm = alarm - mp_map_elem_t *elem = - mp_map_lookup(&alarm_module_globals.map, MP_ROM_QSTR(MP_QSTR_wake_alarm), MP_MAP_LOOKUP); - if (elem) { - elem->value = alarm; +STATIC void debug_print(mp_obj_t obj) { + if (MP_OBJ_IS_TYPE(obj, &mp_type_NoneType)) { + ESP_LOGI(TAG, "wake_alarm NoneType"); + } else if (MP_OBJ_IS_TYPE(obj, &alarm_touch_touchalarm_type)) { + ESP_LOGI(TAG, "wake_alarm TouchType"); + } else if (MP_OBJ_IS_TYPE(obj, &alarm_time_time_alarm_type)) { + ESP_LOGI(TAG, "wake_alarm TimeType"); + } else { + ESP_LOGI(TAG, "wake_alarm UnknownType"); } } // Initialize .wake_alarm value. -void alarm_save_wakeup_alarm(void) { - alarm_set_wake_alarm(common_hal_alarm_get_wake_alarm()); +void alarm_save_wake_alarm(void) { + // Equivalent of: + // alarm.wake_alarm = alarm + mp_obj_t alarm = common_hal_alarm_get_wake_alarm(); + if (alarm != mp_const_none) { + mp_map_elem_t *elem = + mp_map_lookup(&alarm_module_globals.map, MP_ROM_QSTR(MP_QSTR_wake_alarm), MP_MAP_LOOKUP); + debug_print(elem->value); + elem->value = alarm; + debug_print(elem->value); + } } const mp_obj_module_t alarm_module = { diff --git a/shared-bindings/alarm/__init__.h b/shared-bindings/alarm/__init__.h index 8c4b6cad96c17..c2063b78468b3 100644 --- a/shared-bindings/alarm/__init__.h +++ b/shared-bindings/alarm/__init__.h @@ -46,8 +46,7 @@ extern NORETURN void alarm_enter_deep_sleep(void); extern mp_obj_t common_hal_alarm_get_wake_alarm(void); // Used by wake-up code. -void alarm_save_wakeup_alarm(void); - +void alarm_save_wake_alarm(void); // True if an alarm is alerting. This is most useful for pretend deep sleep. extern bool alarm_woken_from_sleep(void);