Skip to content

Commit

Permalink
wip fix wake-alarm
Browse files Browse the repository at this point in the history
  • Loading branch information
microdev1 committed Dec 21, 2020
1 parent ecd7c08 commit 16a203f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
5 changes: 1 addition & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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();
Expand Down
42 changes: 31 additions & 11 deletions shared-bindings/alarm/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
//|
Expand Down Expand Up @@ -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) },
};

Expand All @@ -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) },
};

Expand All @@ -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 },

Expand All @@ -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 = {
Expand Down
3 changes: 1 addition & 2 deletions shared-bindings/alarm/__init__.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 16a203f

Please sign in to comment.