forked from kmpm/nodemcu-uploader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnodemcu-uploader.py
executable file
·282 lines (229 loc) · 7.84 KB
/
nodemcu-uploader.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python
# Copyright (C) 2015 Peter Magnusson
# For NodeMCU version 0.9.4 build 2014-12-30 and newer.
import os
import serial
import sys
import argparse
import time
import logging
log = logging.getLogger(__name__)
def minify(script):
return ' '.join([line.strip() for line in script.split('\n')])
save_lua = \
r"""
function recv_block(d)
if string.byte(d, 1) == 1 then
size = string.byte(d, 2)
if size > 0 then
file.write(string.sub(d, 3, 3+size))
uart.write(0,'\006')
else
uart.write(0,'\006')
file.close()
uart.on('data')
uart.setup(0,9600,8,0,1,1)
end
else
uart.write(0, '\021' .. d)
uart.setup(0,9600,8,0,1,1)
uart.on('data')
end
end
function recv_name(d)
d = string.gsub(d, '\000', '')
file.remove(d)
file.open(d, 'w')
uart.on('data', 130, recv_block, 0)
uart.write(0, '\006')
end
function recv()
uart.setup(0,9600,8,0,1,0)
uart.on('data', '\000', recv_name, 0)
uart.write(0, 'C')
end
"""
#save_lua = minify(save_lua)
#save_lua = ' '.join([line.strip().replace(', ', ',') for line in save_lua.split('\n')])
CHUNK_END = '\v'
CHUNK_REPLY = '\v'
class Uploader:
BAUD = 9600
PORT = '/dev/ttyUSB0'
TIMEOUT = 1
def __init__(self, port = 0, baud = BAUD):
self._port = serial.Serial(port, Uploader.BAUD, timeout=Uploader.TIMEOUT)
# Keeps things working, if following conections are made:
## RTS = CH_PD (i.e reset)
## DTR = GPIO0
self._port.setRTS(False)
self._port.setDTR(False)
time.sleep(0.5)
self.dump()
if baud != Uploader.BAUD:
log.info('Changing communication to %s baud', baud)
self._port.write('uart.setup(0,%s,8,0,1,1)\r\n' % baud)
log.info(self.dump())
self._port.close()
self._port = serial.Serial(port, baud, timeout=Uploader.TIMEOUT)
self.line_number = 0
def close(self):
self._port.write('uart.setup(0,%s,8,0,1,1)\r\n' % Uploader.BAUD)
self._port.close()
def dump(self, timeout=TIMEOUT):
t = self._port.timeout
if self._port.timeout != timeout:
self._port.timeout = timeout
n = self._port.read()
data = ''
while n != '':
data += n
n = self._port.read()
self._port.timeout = t
return data
def prepare(self):
log.info('Preparing esp for transfer.')
self.write_lines(save_lua.replace('9600', '%d' % self._port.baudrate))
self._port.write('\r\n')
d = self.dump(0.1)
if 'unexpected' in d or len(d) > len(save_lua)+10:
log.error('error in save_lua "%s"' % d)
return
def write_file(self, path, destination = ''):
filename = os.path.basename(path)
if not destination:
destination = filename
log.info('Transfering %s as %s' %(filename, destination))
self.dump()
self._port.write(r"recv()" + '\n')
count = 0
while not 'C' in self.dump(0.2):
time.sleep(1)
count += 1
if count > 5:
log.error('Error waiting for esp "%s"' % self.dump())
return
self.dump(0.5)
log.debug('sending destination filename "%s"', destination)
self._port.write(destination + '\x00')
if not self.got_ack():
log.error('did not ack destination filename: "%s"' % self.dump())
return
f = open( path, 'rt' ); content = f.read(); f.close()
log.debug('sending %d bytes in %s' % (len(content), filename))
pos = 0
chunk_size = 128
error = False
while pos < len(content):
data = content[pos: pos+chunk_size]
if not self.write_chunk(data):
error = True
d = self.dump()
log.error('Bad chunk response "%s" %s' % (d, ':'.join(x.encode('hex') for x in d)))
break
pos += chunk_size
if pos + chunk_size > len(content):
chunk_size = len(content) - pos
log.debug('sending zero block')
if not error:
#zero size block
self.write_chunk('')
def got_ack(self):
log.debug('waiting for ack')
r = self._port.read(1)
return r == '\x06' #ACK
def write_lines(self, data):
lines = data.replace('\r', '').split('\n')
for line in lines:
self._port.write(line + '\r\n')
d = self.dump(0.1)
log.debug(d)
return
def write_chunk(self, chunk):
log.debug('writing %d bytes chunk' % len(chunk))
data = '\x01' + chr(len(chunk)) + chunk
if len(chunk) < 128:
padding = 128 - len(chunk)
log.debug('pad with %d characters' % padding)
data = data + (' ' * padding)
log.debug("packet size %d" % len(data))
self._port.write(data)
return self.got_ack()
def file_list(self):
log.info('Listing files')
self._port.write('for key,value in pairs(file.list()) do print(key,value) end' + '\r\n')
r = self.dump()
log.info(r)
return r
def file_format(self):
log.info('Format')
self._port.write('file.format()' + '\r\n')
r = self.dump()
log.info(r)
return r
def node_heap(self):
log.info('Heap')
self._port.write('print(node.heap())\r\n')
r = self.dump()
log.info(r)
return r
def arg_auto_int(x):
return int(x, 0)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description = 'NodeMCU Lua file uploader', prog = 'nodemcu-uploader')
parser.add_argument(
'--verbose', '-v',
help = 'verbose output',
action = 'store_true',
default = False)
parser.add_argument(
'--port', '-p',
help = 'Serial port device',
default = Uploader.PORT)
parser.add_argument(
'--baud', '-b',
help = 'Serial port baudrate',
type = arg_auto_int,
default = Uploader.BAUD)
subparsers = parser.add_subparsers(
dest='operation',
help = 'Run nodemcu-uploader {command} -h for additional help')
upload_parser = subparsers.add_parser(
'upload',
help = 'Path to one or more files to be uploaded. Destination name will be the same as the file name.')
upload_parser.add_argument(
'--filename', '-f',
help = 'File to upload. You can specify this option multiple times.',
action='append')
upload_parser.add_argument(
'--destination', '-d',
help = 'Name to be used when saving in NodeMCU. You should specify one per file.',
action='append')
file_parser = subparsers.add_parser(
'file',
help = 'File functions')
file_parser.add_argument('cmd', choices=('list', 'format'))
args = parser.parse_args()
formatter = logging.Formatter('%(message)s')
logging.basicConfig(level=logging.INFO, format='%(message)s')
uploader = Uploader(args.port, args.baud)
if args.verbose:
log.setLevel(logging.DEBUG)
if args.operation == 'upload':
if not args.destination:
uploader.prepare()
for f in args.filename:
uploader.write_file(f)
elif len(args.destination) == len(args.filename):
uploader.prepare()
for f, d in zip(args.filename, args.destination):
uploader.write_file(f, d)
else:
raise Exception('You must specify a destination filename for each file you want to upload.')
print 'All done!'
elif args.operation == 'file':
if args.cmd == 'list':
uploader.file_list()
elif args.cmd == 'format':
uploader.file_format()
uploader.close()