-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.py
240 lines (190 loc) · 6.62 KB
/
debug.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# pyright: reportMissingImports=false
import config
import io
import log
import re
import scheduler
import sys
import time
IS_MICROPYTHON = sys.implementation.name == "micropython"
STACK_DETAILS = re.compile("File \"(.+)\".+line (\d+).+in (.+)")
BOOT_TIME = time.time()
STATS = {}
class Terminated(Exception):
pass
###################################################################################################
# Parser for exceptions formatted by Micropython's sys.print_exception()
###################################################################################################
class ParseException(io.IOBase):
def __init__(self, ex):
self.ex = ex
self.type = type(ex).__name__
self.message = str(ex)
self.file = ""
self.line = ""
self.function = ""
self._buffer = ""
sys.print_exception(ex, self)
def _on_line(self):
match = STACK_DETAILS.search(self._buffer)
if match:
self.file = match.group(1)
self.line = match.group(2)
self.function = match.group(3)
def write(self, data):
data = data.decode()
for i in range(len(data)):
c = data[i]
if c == "\n":
self._on_line()
self._buffer = ""
else:
self._buffer += c
@property
def location(self):
return f"{self.file}:{self.line}"
def log_exception(ex):
inc_stat("Exceptions")
if IS_MICROPYTHON:
details = ParseException(ex)
log.error(f"{details.type}: {details.message}")
log.error(f" {details.function}()")
log.error(f" {details.location}")
else:
log.error(f"{type(ex).__name__}: {ex}")
###################################################################################################
# Stats
###################################################################################################
def inc_stat(stat):
add_stat(stat, 1)
def add_stat(stat, value):
value = STATS.get(stat, 0) + value
STATS[stat] = value
###################################################################################################
# Misc Helpers
###################################################################################################
LED = None
if IS_MICROPYTHON:
from machine import Pin
LED = Pin(config.LED_PIN, Pin.OUT)
LED.value(config.LED_INITIAL_VALUE)
log.info(f"Set pin {config.LED_PIN} to {config.LED_INITIAL_VALUE}")
def led_toggle():
if LED:
LED.toggle()
def led_value(value):
if LED:
LED.value(1 if value else 0)
###################################################################################################
# Debug Loco Functions
###################################################################################################
debug_value = 0
is_errored = False
def function_view_stats(active):
if (active):
view_stats(log.info)
def function_delete_mainpy(active):
if (active):
delete_mainpy(log.info)
def function_exit_script(active):
if (active):
exit_script(log.info)
def activate_error():
import com
global is_errored
log.warn("Entering error state")
is_errored = True
com.write([0x61, 0x00, 0x61])
def clear_error():
global is_errored
log.info("Clearing error state")
is_errored = False
def function_device_error(active):
if active:
log.warn(f"Will error in {debug_value}s")
scheduler.run_in(debug_value, activate_error)
else:
clear_error()
###################################################################################################
# Debug Menu Actions
###################################################################################################
def return_to_alink(out):
out("Returning to aLink mode")
return True
def view_log(out):
import memlog
memlog.output(out)
def view_stats(out):
ss = int(time.time() - BOOT_TIME)
mm = int(ss / 60)
ss -= (mm * 60)
out(f"Uptime: {mm:02d}:{ss:02d}")
out(f"Is Errored: {is_errored}")
for k, v in STATS.items():
out(f"{k}: {v}")
def mem_info(_):
import micropython
micropython.mem_info()
def raise_exception(out):
out("Throwing exception...")
raise AssertionError("Test Exception")
def schedule_message(out):
out("Scheduling test messae in 5 seconds")
scheduler.run_in(5, log.info, ("Test",))
def schedule_exception(out):
out("Scheduling test exception in 5 seconds")
scheduler.run_in(5, raise_exception, (lambda _: None,))
def toggle_error_state(out):
out(f"Requesting error state: {not is_errored}")
function_device_error(not is_errored)
def delete_mainpy(out):
try:
import os
os.stat("main.py")
out("Removing main.py")
os.remove("main.py")
except OSError:
out("main.py not found")
def exit_script(out):
out("Exit requested")
raise Terminated()
###################################################################################################
# Debug Menu Handler
###################################################################################################
def should_display(item):
return item[3] == ALL or IS_MICROPYTHON
def open_debug_menu():
while True:
print("")
print("Debug Menu:")
for item in DEBUG_MENU_ITEMS:
if should_display(item):
print(f" {item[0]}) {item[1]}")
print("")
while True:
c = sys.stdin.read(1)
action = DEBUG_ACTION_MAP.get(c)
if action is None:
continue
if action(print):
return
break
ALL = object()
MICROPYTHON = object()
DEBUG_MENU_ITEMS = [
("0", "Return to aLink mode", return_to_alink, ALL),
("1", "View log", view_log, ALL),
("2", "Stats", view_stats, ALL),
("3", "Memory info", mem_info, MICROPYTHON),
("4", "Toggle onboard led", lambda _: led_toggle(), MICROPYTHON),
("5", "Trigger exception", raise_exception, ALL),
("6", "Schedule test message", schedule_message, ALL),
("7", "Schedule test exception", schedule_exception, ALL),
("8", "Toggle error state", toggle_error_state, ALL),
("9", "Delete main.py", delete_mainpy, MICROPYTHON),
("x", "Exit script", exit_script, ALL)
]
DEBUG_ACTION_MAP = {}
for item in DEBUG_MENU_ITEMS:
if should_display(item):
DEBUG_ACTION_MAP[item[0]] = item[2]