Skip to content

Commit 41ea651

Browse files
committed
Add tools/pyboard.py, a simple module to communicate with the pyboard.
Using pyboard.py you can use Python running on your PC to execute commands on the connected pyboard. See examples in that file.
1 parent f704e7f commit 41ea651

1 file changed

Lines changed: 127 additions & 0 deletions

File tree

tools/pyboard.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
"""
2+
pyboard interface
3+
4+
This module provides the Pyboard class, used to communicate with and
5+
control the pyboard over a serial USB connection.
6+
7+
Example usage:
8+
9+
import pyboard
10+
pyb = pyboard.Pyboard('/dev/ttyACM0')
11+
pyb.enter_raw_repl()
12+
pyb.exec('pyb.Led(1).on()')
13+
pyb.exit_raw_repl()
14+
15+
"""
16+
17+
import time
18+
import serial
19+
20+
class Pyboard:
21+
def __init__(self, serial_device):
22+
self.serial = serial.Serial(serial_device)
23+
24+
def close(self):
25+
self.serial.close()
26+
27+
def enter_raw_repl(self):
28+
self.serial.write(b'\r\x01') # ctrl-A: enter raw REPL
29+
self.serial.write(b'\x04') # ctrl-D: soft reset
30+
data = self.serial.read(1)
31+
while self.serial.inWaiting() > 0:
32+
data = data + self.serial.read(self.serial.inWaiting())
33+
time.sleep(0.1)
34+
if not data.endswith(b'raw REPL; CTRL-B to exit\r\n>'):
35+
print(data)
36+
raise Exception('could not enter raw repl')
37+
38+
def exit_raw_repl(self):
39+
self.serial.write(b'\r\x02') # ctrl-B: enter friendly REPL
40+
41+
def eval(self, expression):
42+
ret = self.exec('print({})'.format(expression))
43+
ret = ret.strip()
44+
return ret
45+
46+
def exec(self, command):
47+
command_bytes = bytes(command, encoding='ascii')
48+
for i in range(0, len(command_bytes), 10):
49+
self.serial.write(command_bytes[i:min(i+10, len(command_bytes))])
50+
time.sleep(0.01)
51+
self.serial.write(b'\x04')
52+
data = self.serial.read(2)
53+
if data != b'OK':
54+
raise Exception('could not exec command')
55+
data = self.serial.read(2)
56+
while self.serial.inWaiting() > 0:
57+
data = data + self.serial.read(self.serial.inWaiting())
58+
time.sleep(0.1)
59+
if not data.endswith(b'\x04>'):
60+
print(data)
61+
raise Exception('could not exec command')
62+
if data.startswith(b'Traceback') or data.startswith(b' File '):
63+
print(data)
64+
raise Exception('command failed')
65+
return data[:-2]
66+
67+
def get_time(self):
68+
t = str(self.exec('pyb.time()'), encoding='ascii').strip().split()[1].split(':')
69+
return int(t[0]) * 3600 + int(t[1]) * 60 + int(t[2])
70+
71+
def run_test():
72+
device = '/dev/ttyACM0'
73+
pyb = Pyboard(device)
74+
pyb.enter_raw_repl()
75+
print('opened device {}'.format(device))
76+
77+
print('seconds since boot:', pyb.get_time())
78+
79+
pyb.exec('def apply(l, f):\r\n for item in l:\r\n f(item)\r\n')
80+
81+
pyb.exec('leds=[pyb.Led(l) for l in range(1, 5)]')
82+
pyb.exec('apply(leds, lambda l:l.off())')
83+
84+
## USR switch test
85+
86+
if True:
87+
for i in range(2):
88+
print("press USR button")
89+
pyb.exec('while pyb.switch(): pyb.delay(10)')
90+
pyb.exec('while not pyb.switch(): pyb.delay(10)')
91+
92+
print('USR switch passed')
93+
94+
## accel test
95+
96+
if True:
97+
print("hold level")
98+
pyb.exec('accel = pyb.Accel()')
99+
pyb.exec('while abs(accel.x()) > 10 or abs(accel.y()) > 10: pyb.delay(10)')
100+
101+
print("tilt left")
102+
pyb.exec('while accel.x() > -10: pyb.delay(10)')
103+
pyb.exec('leds[0].on()')
104+
105+
print("tilt forward")
106+
pyb.exec('while accel.y() < 10: pyb.delay(10)')
107+
pyb.exec('leds[1].on()')
108+
109+
print("tilt right")
110+
pyb.exec('while accel.x() < 10: pyb.delay(10)')
111+
pyb.exec('leds[2].on()')
112+
113+
print("tilt backward")
114+
pyb.exec('while accel.y() > -10: pyb.delay(10)')
115+
pyb.exec('leds[3].on()')
116+
117+
print('accel passed')
118+
119+
print('seconds since boot:', pyb.get_time())
120+
121+
pyb.exec('apply(leds, lambda l:l.off())')
122+
123+
pyb.exit_raw_repl()
124+
pyb.close()
125+
126+
if __name__ == "__main__":
127+
run_test()

0 commit comments

Comments
 (0)