-
Notifications
You must be signed in to change notification settings - Fork 0
/
safemode.py
43 lines (34 loc) · 1.29 KB
/
safemode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
handle selected modes of safe mode
"""
import time
# pylint: disable=import-error
import alarm
import microcontroller
import storage
# pylint: disable=import-error
import supervisor
# safemode.py & boot.py file write
def precode_file_write(file, data):
"""
Append data to file with newline. Meant to be run before code.py gets to run.
"""
storage.remount("/", False) # writeable by CircuitPython
with open(file, "a+", encoding="ascii") as fp:
fp.write(f"{data}\n")
fp.flush()
storage.remount("/", True) # writeable by USB host
reason = supervisor.runtime.safe_mode_reason
precode_file_write("/safemode.log", f"{supervisor.ticks_ms()}: {str(reason)}")
if reason == supervisor.SafeModeReason.HARD_FAULT:
# pylint: disable=no-member
microcontroller.reset() # Reset and start over.
elif reason == supervisor.SafeModeReason.BROWNOUT:
# Sleep for ten minutes and then run code.py again.
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 10 * 60)
alarm.exit_and_deep_sleep_until_alarms(time_alarm)
elif reason == supervisor.SafeModeReason.WATCHDOG:
# pylint: disable=no-member
microcontroller.reset() # Reset and start over.
# Otherwise, do nothing. The safe mode reason will be printed in the
# console, and nothing will run.