/
pico_adapter.py
165 lines (135 loc) · 5.37 KB
/
pico_adapter.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
"""Implements a Bioloid IO Adapter running on an Espruino Pico."""
import pyb
import packet
from bus import BusError
from stm_usb_port import USB_Port
from stm_uart_port import UART_Port
from bus import Bus
from dump_mem import dump_mem
from log import log, log_to_uart
import os
import struct
import sys
RED_LED = pyb.LED(1)
GREEN_LED = pyb.LED(2)
machine = os.uname().machine
if machine.startswith('Espruino Pico'):
repl_uart_num = 1
device_uart_num = 2
elif machine.startswith('PYB'):
repl_uart_num = 4
device_uart_num = 6
else:
print("Unrecognized board: '{}'".format(machine))
sys.exit(1)
repl_uart = pyb.UART(repl_uart_num, 115200)
log_to_uart(repl_uart)
# By putting the REPL on the logging UART, it means that any tracebacks will
# go to the uart.
pyb.repl_uart(repl_uart)
class LedSequence:
def __init__(self, led, sequence, continuous=True):
self.led = led
self.last_toggle = 0
self.sequence = sequence
self.continuous = continuous
self.seq_idx = -1
self.led.off()
if self.continuous:
self.kick()
def process(self):
if self.seq_idx >= 0 and pyb.elapsed_millis(self.last_toggle) > self.sequence[self.seq_idx]:
self.led.toggle()
self.last_toggle = pyb.millis()
self.seq_idx += 1
if self.seq_idx >= len(self.sequence):
if self.continuous:
self.seq_idx = 0
else:
self.seq_idx = -1
self.led.off() # Just in case we got an odd length
def kick(self):
if self.seq_idx < 0:
self.seq_idx = 0
self.led.on()
self.last_toggle = pyb.millis()
class HeartBeat(LedSequence):
def __init__(self, led):
super().__init__(led, (20, 180, 20, 780), continuous=True)
class Activity(LedSequence):
def __init__(self, led):
super().__init__(led, (20, 20), continuous=False)
class Scanner(object):
def __init__(self, bus):
self.bus = bus
self.ids = []
def dev_found(self, bus, dev_id):
# We want to read the model and version, which is at offset 0, 1,
# and 2 so we do it with a single read.
try:
data = bus.read(dev_id, 0, 3)
except BusError:
log('Device {} READ timed out'.format(dev_id))
return
model, version = struct.unpack('<HB', data)
log(' ID: {:3d} Model: {:5d} Version: {:5d}'.format(dev_id, model, version))
self.ids.append(dev_id)
def scan_range(self, start_id=1, num_ids=32):
log('Scanning IDs from', start_id, 'to', start_id + num_ids)
try:
self.bus.scan(start_id, num_ids, self.dev_found, None)
except BusError:
log('Timeout')
def scan(self):
self.ids = []
self.scan_range(0, 32)
self.scan_range(100, 32)
if len(self.ids) == 0:
log('No devices found')
else:
log('Scan done')
heartbeat = HeartBeat(RED_LED)
activity = Activity(GREEN_LED)
host_uart = USB_Port()
device_uart = UART_Port(device_uart_num, baud=1000000)
button = pyb.Switch()
pkt = packet.Packet()
rsp = packet.Packet()
log('Pico Adapter - Bioloid Packet Forwarder')
log('Control-C - Quit')
log('s - Scan Bus')
log('d - Debug - Show Packets')
show = Bus.SHOW_NONE
while True:
heartbeat.process()
activity.process()
byte = host_uart.read_byte()
if byte is not None:
rc = pkt.process_byte(byte)
if rc != packet.ErrorCode.NOT_DONE:
activity.kick()
device_uart.write_packet(pkt.pkt_bytes)
if show & Bus.SHOW_PACKETS:
dump_mem(pkt.pkt_bytes, prefix=' H->D', show_ascii=False, log=log)
if device_uart.any():
byte = device_uart.read_byte()
rc = rsp.process_byte(byte)
if rc != packet.ErrorCode.NOT_DONE:
activity.kick()
host_uart.write_packet(rsp.pkt_bytes)
if show & Bus.SHOW_PACKETS:
dump_mem(rsp.pkt_bytes, prefix=' D->H', show_ascii=False, log=log)
if repl_uart.any():
byte = repl_uart.readchar()
if byte == 3: # Control-C
log('Control-C')
raise KeyboardInterrupt
elif byte == ord('d'):
show ^= Bus.SHOW_PACKETS
log('Show Packets:', bool(show & Bus.SHOW_PACKETS))
elif byte == ord('s'):
Scanner(Bus(device_uart, show=show)).scan()
if button():
log('Control-C via Button')
raise KeyboardInterrupt
pyb.wfi()