summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorScott Shawcroft <scott@tannewt.org>2020-10-27 17:55:03 -0700
committerScott Shawcroft <scott@tannewt.org>2020-10-27 17:55:03 -0700
commit9a4efed8cbaca3aa48c6cc0ce0a4e267eef7a8e1 (patch)
treea4503697ca0d3fab4e15ee6add1171165ac32146 /main.c
parent85dadf3a561c21e284d5daf42b1b3e367ce7ebc6 (diff)
Start tweaking the workflow to sleep
Diffstat (limited to 'main.c')
-rwxr-xr-xmain.c49
1 files changed, 33 insertions, 16 deletions
diff --git a/main.c b/main.c
index 328c3bbcb..1854a1407 100755
--- a/main.c
+++ b/main.c
@@ -88,6 +88,10 @@
#include "common-hal/canio/CAN.h"
#endif
+#if CIRCUITPY_SLEEPIO
+#include "shared-bindings/sleepio/__init__.h"
+#endif
+
void do_str(const char *src, mp_parse_input_kind_t input_kind) {
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
if (lex == NULL) {
@@ -303,19 +307,6 @@ bool run_code_py(safe_mode_t safe_mode) {
}
}
- for (uint8_t i = 0; i<=100; i++) {
- if (!usb_msc_ejected()) {
- //Go into light sleep
- break;
- }
- mp_hal_delay_ms(10);
- }
-
- if (usb_msc_ejected()) {
- //Go into deep sleep
- common_hal_mcu_deep_sleep();
- }
-
// Display a different completion message if the user has no USB attached (cannot save files)
if (!serial_connected_at_start) {
serial_write_compressed(translate("\nCode done running. Waiting for reload.\n"));
@@ -326,6 +317,14 @@ bool run_code_py(safe_mode_t safe_mode) {
bool refreshed_epaper_display = false;
#endif
rgb_status_animation_t animation;
+ bool ok = result->return_code != PYEXEC_EXCEPTION;
+ #if CIRCUITPY_SLEEPIO
+ // If USB isn't enumerated then deep sleep.
+ if (ok && !supervisor_workflow_active() && supervisor_ticks_ms64() > CIRCUITPY_USB_ENUMERATION_DELAY * 1024) {
+ common_hal_sleepio_deep_sleep();
+ }
+ #endif
+ // Show the animation every N seconds.
prep_rgb_status_animation(&result, found_main, safe_mode, &animation);
while (true) {
RUN_BACKGROUND_TASKS;
@@ -358,8 +357,24 @@ bool run_code_py(safe_mode_t safe_mode) {
refreshed_epaper_display = maybe_refresh_epaperdisplay();
}
#endif
-
- tick_rgb_status_animation(&animation);
+ bool animation_done = tick_rgb_status_animation(&animation);
+ if (animation_done && supervisor_workflow_active()) {
+ #if CIRCUITPY_SLEEPIO
+ int64_t remaining_enumeration_wait = CIRCUITPY_USB_ENUMERATION_DELAY * 1024 - supervisor_ticks_ms64();
+ // If USB isn't enumerated then deep sleep after our waiting period.
+ if (ok && remaining_enumeration_wait < 0) {
+ common_hal_sleepio_deep_sleep();
+ return; // Doesn't actually get here.
+ }
+ #endif
+ // Wake up every so often to flash the error code.
+ if (!ok) {
+ port_interrupt_after_ticks(CIRCUITPY_FLASH_ERROR_PERIOD * 1024);
+ } else {
+ port_interrupt_after_ticks(remaining_enumeration_wait);
+ }
+ port_sleep_until_interrupt();
+ }
}
}
@@ -406,7 +421,9 @@ void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
if (!skip_boot_output) {
// Wait 1.5 seconds before opening CIRCUITPY_BOOT_OUTPUT_FILE for write,
// in case power is momentary or will fail shortly due to, say a low, battery.
- mp_hal_delay_ms(1500);
+ if (common_hal_sleepio_get_reset_reason() == RESET_REASON_POWER_VALID) {
+ mp_hal_delay_ms(1500);
+ }
// USB isn't up, so we can write the file.
filesystem_set_internal_writable_by_usb(false);