/
command.py
169 lines (138 loc) · 4.55 KB
/
command.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
import config
def guessDevice():
import platform
ostype = platform.system()
if ostype == 'Linux':
return "/dev/ttyUSB0"
elif ostype == 'Darwin':
return "/dev/tty.SLAB_USBtoUART"
elif ostype == 'Windows':
return None
device = None
def selectDevice():
global device
from six.moves import input
from serial.tools.list_ports import comports
availableDevices = [info.device for info in comports()]
defaultDevice = guessDevice()
message = "Type the number of the device: "
if defaultDevice is not None:
if defaultDevice not in availableDevices:
print("No device {}. Plugged in? Drivers installed?".format(defaultDevice))
defaultDevice = None
if len(availableDevices) == 1:
defaultDevice = availableDevices[0]
if defaultDevice is not None:
message += "({}) ".format(defaultDevice)
while device is None:
for index, item in enumerate(availableDevices):
print("{} : {}".format(index, item))
try:
choice = input(message)
if choice == "":
if defaultDevice is not None:
device = defaultDevice
else:
device = availableDevices[int(choice)]
except ValueError:
print("Cannot accept {}".format(choice))
pass
return device
def detectBoardConfig():
import sys
import re
import io
import esptool
oldOut = sys.stdout
newOut = io.StringIO()
emulateInvocation("esptool.py --port ${port} flash_id", config.hardwareConfig())
try:
sys.stdout = newOut
esptool.main()
finally:
sys.stdout = oldOut
printed = newOut.getvalue()
def extractBackreference(pattern):
return re.search(pattern, printed, re.MULTILINE).group(1)
return {
"chip": extractBackreference("^Chip is (\w+)$"),
"manufacturer": extractBackreference('^Manufacturer: (\w+)$'),
"device": extractBackreference('^Device: (\w+)$'),
"flash_size": extractBackreference('^Detected flash size: (\w+)$'),
}
def emulateInvocation(templateString, config):
import sys
import string
command = string.Template(templateString).substitute(config)
print("Running '" + command + "'")
sys.argv = command.split()
def releaseBoard():
from ampy import cli
if cli._board is not None:
try:
cli._board.close()
except:
pass
def resetBoard():
from config import hardwareConfig
from ampy import pyboard, cli
try:
print('Resetting Board')
emulateInvocation("ampy --port ${port} reset", hardwareConfig())
try:
cli.cli()
except SystemExit:
pass
except pyboard.PyboardError:
print("Is cockle unplugged or in use by another program?")
releaseBoard()
def eraseBoard():
import esptool
emulateInvocation("esptool.py --port ${port} --baud ${baud} erase_flash", config.hardwareConfig())
esptool.main()
def putFile(frompath, topath):
from config import hardwareConfig
from ampy import pyboard, cli
try:
putCommand = "ampy --port ${port} put ${frompath} ${topath}"
putConfig = hardwareConfig()
putConfig.update(
frompath=frompath,
topath=topath
)
emulateInvocation(putCommand, putConfig)
try:
cli.cli()
except SystemExit:
pass
except pyboard.PyboardError:
print("Is cockle unplugged or in use by another program?")
releaseBoard()
# precede sync by
# ampy --port /dev/ttyUSB0 mkdir --exists-okay vgkits
# rshell --ascii --buffer-size=30 --port /dev/ttyUSB0 rsync ../modules/vgkits/ /pyboard/vgkits/
def rsync(fromdir, todir):
params = config.hardwareConfig()
params.update(
fromdir=fromdir,
todir=todir
)
# run ampy to lazy-create target directory if needed
if todir != '' and todir != '/':
from ampy import pyboard, cli
try:
emulateInvocation("ampy --port ${port} mkdir --exists-okay ${todir}", params)
try:
cli.cli()
except SystemExit:
pass
except pyboard.PyboardError:
print("Is cockle unplugged or in use by another program?")
return
# run rshell to recursively 'rsync' the folder
import rshell.main
try:
emulateInvocation("rshell --ascii --buffer-size=30 --port ${port} rsync ${fromdir} /pyboard/${todir}", params)
rshell.main.main()
finally:
pass