forked from ntoll/microrepl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
microrepl.py
executable file
·106 lines (96 loc) · 3.4 KB
/
microrepl.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
#!/usr/bin/env python
"""
A simple shim around PySerial that detects the correct port to which the
micro:bit is connected and attempts to make a serial connection to it in order
to bring up the Python REPL.
"""
import sys
import serial
import serial.tools.miniterm
from serial.tools.miniterm import (comports, console, Miniterm,
key_description)
MICROBIT_PID = 516
MICROBIT_VID = 3368
BAUDRATE = 115200
PARITY = 'N'
ARM = 'https://developer.mbed.org/handbook/Windows-serial-configuration'
if sys.version_info >= (3, 0):
def character(b):
return b.decode('latin1')
else:
def character(b):
return b
def find_microbit():
"""
Returns the port for the first micro:bit found connected to the computer
running this script. If no micro:bit is found, returns None.
"""
ports = comports()
platform = sys.platform
if platform.startswith('linux'):
for port in ports:
if 'VID:PID=0d28:0204' in port[2]:
return port[0]
elif platform.startswith('darwin'):
for port in ports:
if 'VID:PID=d28:204' in port[2]:
return port[0]
elif platform.startswith('win'):
for port in ports:
if 'VID:PID=0D28:0204' in port[2]:
return port[0]
# No COM port found, so give an informative prompt.
sys.stderr.write('Have you installed the micro:bit driver?\n')
sys.stderr.write('For more details see: {}\n'.format(ARM))
return None
def main():
"""
The function that actually runs the REPL.
"""
port = find_microbit()
if not port:
sys.stderr.write('Could not find micro:bit. Is it plugged in?\n')
sys.exit(0)
serial.tools.miniterm.EXITCHARCTER = character(b'\x1d')
try:
miniterm = Miniterm(
port,
BAUDRATE,
PARITY,
rtscts=False,
xonxoff=False,
echo=False,
convert_outgoing=2,
repr_mode=0,
)
except serial.SerialException as e:
if e.errno == 16:
# Device is busy. Explain what to do.
sys.stderr.write("Found micro:bit, but the device is busy. " +
"Wait up to 20 seconds, or " +
"press the reset button on the " +
"back of the device next to the yellow light; " +
"then try again.\n")
else:
# Try to be as helpful as possible.
sys.stderr.write("Found micro:bit, but could not connect via" +
" port %r: %s\n" % (port, e))
sys.stderr.write("I'm not sure what to suggest. :-(\n")
sys.exit(1)
# Emit some helpful information about the program and MicroPython.
shortcut_message = 'Quit: {} | Stop program: Ctrl+C | Reset: Ctrl+D\n'
help_message = 'Type \'help()\' (without the quotes) then press ENTER.\n'
exit_char = key_description(serial.tools.miniterm.EXITCHARCTER)
sys.stderr.write(shortcut_message.format(exit_char))
sys.stderr.write(help_message)
# Start everything.
console.setup()
miniterm.start()
miniterm.serial.write(b'\x03') # Connecting stops the running program.
try:
miniterm.join(True)
except KeyboardInterrupt:
pass
sys.stderr.write('\nEXIT - see you soon... :-)\n')
if __name__ == '__main__':
main()