-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreset.py
67 lines (58 loc) · 1.8 KB
/
reset.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
""" The Pico and uPython seem to get stuck with threading.
This class is here as a reset method. Otherwise the
flash has to be wiped and all files reloaded.
"""
import os
import sys
import utime
import machine
from boom import Imu, Servo
class Reset:
TMP_MAIN = "_rst_main_.py"
MAIN = "main.py"
@staticmethod
def end():
"""Reset ended. Put main.py back in its rightful place.
If there is no tmp_main, then the reset was not start.
"""
try:
with open(Reset.TMP_MAIN, 'r'):
pass
os.remove(Reset.MAIN)
os.rename(Reset.TMP_MAIN, Reset.MAIN)
sys.stdin.read(1)
except Exception:
pass
@staticmethod
def start(imu: Imu, servo: Servo):
"""Stop all components running and reset the Pico.
Args:
imu (Imu): IMU
servo (Servo): Servo
"""
imu.stop()
servo.stop()
utime.sleep(1)
os.rename(Reset.MAIN, Reset.TMP_MAIN)
with open(Reset.MAIN, 'w') as f:
f.write(Reset._tmp_main())
print("####################################")
print("# Resetting")
print("####################################")
print("#")
print("# After the reset either ...")
print("#")
print("# Press Ctrl-D to restart, or, Upload")
print("# new files including main.py and restart")
print("#")
print("# Pres any key to continue")
print("####################################")
sys.stdin.read(1)
machine.reset()
@staticmethod
def _tmp_main():
"""Write out a main.py file to be used after the reset.
Returns:
str: main.py content
"""
return "from reset import Reset\nReset.end()\n"