-
Notifications
You must be signed in to change notification settings - Fork 6
/
configure.py
executable file
·64 lines (54 loc) · 1.83 KB
/
configure.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
#!/usr/bin/env python3
import sys
import time
from pathlib import Path
import serial
if len(sys.argv) != 3:
print(f'Usage: {sys.argv[0]} <config_file> <device_path>')
sys.exit()
txt_path, usb_path = sys.argv[1:]
def send(line_: str) -> None:
print(f'Sending: {line_}')
checksum_ = 0
for c in line_:
checksum_ ^= ord(c)
port.write((f'{line_}@{checksum_:02x}\n').encode())
with serial.Serial(usb_path, baudrate=115200, timeout=1.0) as port:
startup = Path(txt_path).read_text('utf-8')
if not startup.endswith('\n'):
startup += '\n'
checksum = sum(ord(c) for c in startup) % 0x10000
send('!-')
for line in startup.splitlines():
send(f'!+{line}')
send('!.')
send('core.restart()')
# Wait for "Ready." message with a deadline depending on the number of expanders
timeout = 3.0 + 3.0 * startup.count('Expander')
deadline = time.time() + timeout
while time.time() < deadline:
try:
line = port.read_until(b'\r\n').decode().rstrip()
except UnicodeDecodeError:
continue
if line == 'Ready.':
print('ESP32 booted and sent "Ready."')
break
else:
raise TimeoutError('Timeout waiting for device to restart!')
# Immediately check checksum after ready
send('core.startup_checksum()')
deadline = time.time() + 3.0
while time.time() < deadline:
try:
line = port.read_until(b'\r\n').decode().rstrip()
except UnicodeDecodeError:
continue
if line.startswith('checksum: '):
if int(line.split()[1].split('@')[0], 16) == checksum:
print('Checksum matches.')
break
else:
raise ValueError('Checksum mismatch!')
else:
raise TimeoutError('Timeout waiting for checksum!')