diff options
| author | Dan Halbert <halbert@halwitz.org> | 2020-11-25 17:52:06 -0500 |
|---|---|---|
| committer | Dan Halbert <halbert@halwitz.org> | 2020-11-25 17:52:06 -0500 |
| commit | ef0830bfe241b544b05bd3081f5bba03af57fe0d (patch) | |
| tree | 28b6c45fdd03288bbf43b77f42025d1773622f13 /main.c | |
| parent | 9dbea36eac9a78cf324bba8d32a5cb2c9940d411 (diff) | |
| parent | e778fc1f876116fd8f07b95f9eb8439745988fba (diff) | |
merge from upstream + wip
Diffstat (limited to 'main.c')
| -rwxr-xr-x | main.c | 56 |
1 files changed, 28 insertions, 28 deletions
@@ -66,6 +66,7 @@ #include "boards/board.h" +// REMOVE *********** #include "esp_log.h" #if CIRCUITPY_ALARM @@ -97,8 +98,8 @@ #include "common-hal/canio/CAN.h" #endif -// How long to wait for host to enumerate (secs). -#define CIRCUITPY_USB_ENUMERATION_DELAY 5 +// How long to wait for host to start connecting.. +#define CIRCUITPY_USB_CONNECTING_DELAY 1 // How long to flash errors on the RGB status LED before going to sleep (secs) #define CIRCUITPY_FLASH_ERROR_PERIOD 10 @@ -258,6 +259,17 @@ STATIC void print_code_py_status_message(safe_mode_t safe_mode) { } } +// Should we go into deep sleep when program finishes? +// Normally we won't deep sleep if there was an error or if we are connected to a host +// but either of those can be enabled. +// It's ok to deep sleep if we're not connected to a host, but we need to make sure +// we're giving enough time for USB to start to connect +STATIC bool deep_sleep_allowed(void) { + return + (ok || supervisor_workflow_get_allow_deep_sleep_on_error()) && + !supervisor_workflow_connecting() + (supervisor_ticks_ms64() > CIRCUITPY_USB_CONNECTING_DELAY * 1024); + STATIC bool run_code_py(safe_mode_t safe_mode) { bool serial_connected_at_start = serial_connected(); #if CIRCUITPY_AUTORELOAD_DELAY_MS > 0 @@ -307,7 +319,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) { // Program has finished running. // Display a different completion message if the user has no USB attached (cannot save files) - if (!serial_connected_at_start) { + if (!serial_connected_at_start && !deep_sleep_allowed()) { serial_write_compressed(translate("\nCode done running. Waiting for reload.\n")); } @@ -318,32 +330,16 @@ STATIC bool run_code_py(safe_mode_t safe_mode) { rgb_status_animation_t animation; bool ok = result.return_code != PYEXEC_EXCEPTION; - ESP_LOGI("main", "common_hal_alarm_enable_deep_sleep_alarms()"); #if CIRCUITPY_ALARM // Enable pin or time alarms before sleeping. - // If immediate_wake is true, then there's alarm that would trigger immediately, + // If immediate_wake is true, then there's an alarm that would trigger immediately, // so don't sleep. bool immediate_wake = !common_hal_alarm_enable_deep_sleep_alarms(); #endif - prep_rgb_status_animation(&result, found_main, safe_mode, &animation); while (true) { - // Normally we won't deep sleep if there was an error or if we are connected to a host - // but either of those can be enabled. - // It's ok to deep sleep if we're not connected to a host, but we need to make sure - // we're giving enough time for USB enumeration to happen. - bool deep_sleep_allowed = - !immediate_wake && - (ok || supervisor_workflow_get_allow_deep_sleep_on_error()) && - (!supervisor_workflow_active() || supervisor_workflow_get_allow_deep_sleep_when_connected()) && - (supervisor_ticks_ms64() > CIRCUITPY_USB_ENUMERATION_DELAY * 1024); - - ESP_LOGI("main", "ok %d", deep_sleep_allowed); - ESP_LOGI("main", "...on_error() %d", supervisor_workflow_get_allow_deep_sleep_on_error()); - ESP_LOGI("main", "supervisor_workflow_active() %d", supervisor_workflow_active()); - ESP_LOGI("main", "...when_connected() %d", supervisor_workflow_get_allow_deep_sleep_when_connected()); - ESP_LOGI("main", "supervisor_ticks_ms64() %lld", supervisor_ticks_ms64()); + RUN_BACKGROUND_TASKS; if (reload_requested) { supervisor_set_run_reason(RUN_REASON_AUTO_RELOAD); @@ -365,7 +361,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) { print_code_py_status_message(safe_mode); } // We won't be going into the REPL if we're going to sleep. - if (!deep_sleep_allowed) { + if (!deep_sleep_allowed()) { print_safe_mode_message(safe_mode); serial_write("\n"); serial_write_compressed(translate("Press any key to enter the REPL. Use CTRL-D to reload.")); @@ -385,7 +381,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) { bool animation_done = false; - if (deep_sleep_allowed && ok) { + if (deep_sleep_allowed() && ok) { // Skip animation if everything is OK. animation_done = true; } else { @@ -393,7 +389,11 @@ STATIC bool run_code_py(safe_mode_t safe_mode) { } // Do an error animation only once before deep-sleeping. if (animation_done) { - if (deep_sleep_allowed) { + if (immediate_wake) { + // Don't sleep, we are already supposed to wake up. + return true; + } + if (deep_sleep_allowed()) { common_hal_mcu_deep_sleep(); // Does not return. } @@ -402,10 +402,10 @@ STATIC bool run_code_py(safe_mode_t safe_mode) { if (!ok) { port_interrupt_after_ticks(CIRCUITPY_FLASH_ERROR_PERIOD * 1024); } else { - int64_t remaining_enumeration_wait = - CIRCUITPY_USB_ENUMERATION_DELAY * 1024 - supervisor_ticks_ms64(); - if (remaining_enumeration_wait > 0) { - port_interrupt_after_ticks(remaining_enumeration_wait); + int64_t remaining_connecting_wait = + CIRCUITPY_USB_CONNECTING_DELAY * 1024 - supervisor_ticks_ms64(); + if (remaining_connecting_wait > 0) { + port_interrupt_after_ticks(remaining_connecting_wait); } port_sleep_until_interrupt(); } |
