-
Notifications
You must be signed in to change notification settings - Fork 0
/
safemode.py
44 lines (34 loc) · 1.25 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
44
"""
safe mode handling with storing the reason to a file
"""
import json
import time
import microcontroller
# pylint: disable=import-error
import storage
# pylint: disable=import-error
import supervisor
def precode_file_write(file, data):
"""
remount storage for writing here, write the data, remount for writing elsewhere
"""
storage.remount("/", False) # writeable by CircuitPython
with open(file, "w", encoding="utf-8") as file_obj:
file_obj.write(f"{data}\n")
file_obj.flush()
storage.remount("/", True) # writeable by USB host
# set up the safemode dict
safemode_dict = {}
safemode_dict["safemode_reason"] = str(supervisor.runtime.safe_mode_reason)
safemode_dict["safemode_time"] = time.monotonic()
safemode_dict["safemode_time_ns"] = time.monotonic_ns()
# write dict as JSON. This will overwrite any pre-existing file.
precode_file_write("/safemode.json", json.dumps(safemode_dict)) # use storage.remount()
# pylint: disable=using-constant-test
if False: # check for any safemode conditions where we shouldn't RESET
# Do nothing. The safe mode reason will be printed in the console,
# and nothing will run.
pass
else:
# RESET out of safe mode
microcontroller.reset() # pylint: disable=no-member