-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluatool_eth.py
executable file
·179 lines (159 loc) · 6.81 KB
/
luatool_eth.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
#!/usr/bin/env python2
#
# ESP8266 luatool
# Author e-mail: [email protected]
# Site: http://esp8266.ru
# Contributions from: https://github.com/sej7278
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301 USA.
import sys
import serial
from time import sleep
import argparse
from os.path import basename
import telnetlib
version="0.6.3"
def writeln(data, check = 1):
#if s.inWaiting() > 0:
# s.flushInput()
if len( data ) > 0:
sys.stdout.write("\r\n->")
sys.stdout.write(data.split("\r")[0])
s.write(data)
sleep(0.3)
if check > 0 :
line = ''
char = ''
while char != chr(62) : # '>'
data = s.read_some()
for char in data:
if char == chr(62):
break; # '>'
if char == '' :
raise Exception('No proper answer from MCU')
if char == chr(13) or char == chr(10) : # LF or CR
if line != '':
line = line.strip()
if line+'\r' == data :
sys.stdout.write(" -> ok")
else :
if line[:4] == "lua:" :
sys.stdout.write("\r\n\r\nLua ERROR: %s" % line)
raise Exception('ERROR from Lua interpreter\r\n\r\n')
else :
data = data.split("\r")[0]
sys.stdout.write("\r\n\r\nERROR")
sys.stdout.write("\r\n send string : '%s'" % data)
sys.stdout.write("\r\n expected echo : '%s'" % data)
sys.stdout.write("\r\n but got answer : '%s'" % line)
sys.stdout.write("\r\n\r\n")
raise Exception('Error sending data to MCU\r\n\r\n')
line = ''
else :
line += char
else:
sys.stdout.write(" -> send without check")
def writer(data):
writeln("file.writeline([==[" + data + "]==])\r")
def openserial(args):
# Open the selected serial port
try:
# s = serial.Serial(args.port, args.baud)
s = telnetlib.Telnet(args.host, args.port)
except:
sys.stderr.write("Could not open telnet to %s port %s\n" % (args.host, args.port))
sys.exit(1)
if args.verbose: sys.stderr.write("Set timeout %s\r\n" % s.timeout)
s.timeout = 3
return s
if __name__ == '__main__':
# parse arguments or use defaults
parser = argparse.ArgumentParser(description='ESP8266 Lua script uploader.')
parser.add_argument('-p', '--port', default='8080', help='Port of the Telnet Server.')
parser.add_argument('-f', '--src', default='main.lua', help='Source file on computer, default main.lua')
parser.add_argument('-t', '--dest', default=None, help='Destination file on MCU, default to source file name')
parser.add_argument('-H', '--host', default=None, help='IP Address of the telnet server')
parser.add_argument('-c', '--compile', action='store_true', help='Compile lua to lc after upload')
parser.add_argument('-r', '--restart', action='store_true', help='Restart MCU after upload')
parser.add_argument('-d', '--dofile', action='store_true', help='Run the Lua script after upload')
parser.add_argument('-v', '--verbose', action='store_true', help="Show progress messages.")
parser.add_argument('-l', '--list', action='store_true', help='List files on device')
args = parser.parse_args()
if args.list:
s = openserial(args)
writeln("local l = file.list();for k,v in pairs(l) do print('name:'..k..', size:'..v)end\r", 0)
while True :
char = s.read(1)
if char == '' or char == chr(62): break
sys.stdout.write(char)
sys.exit(0)
if args.dest is None:
args.dest = basename(args.src)
# open source file for reading
try:
f = open(args.src,"rt")
except:
sys.stderr.write("Could not open input file \"%s\"\n" % args.src)
sys.exit(1)
# Verify the selected file will not exceed the size of the serial buffer.
# The size of the buffer is 256. This script does not accept files with
# lines longer than 230 characters to have some room for command overhead.
for Ln in f:
if len(Ln) > 230:
sys.stderr.write("File \"%s\" contains a line with more than 240 "\
"characters. This exceeds the size of the serial buffer.\n"
% args.src)
f.close()
sys.exit(1)
# Go back to the beginning of the file after verifying it has the correct
# line length
f.seek(0)
# Open the selected serial port
s = openserial(args)
# set serial timeout
if args.verbose: sys.stderr.write("Upload starting\r\n")
# remove existing file on device
if args.verbose: sys.stderr.write("Stage 1. Deleting old file from flash memory")
writeln("file.open(\""+args.dest+"\", \"w\")\r")
writeln("file.close()\r")
writeln("file.remove(\""+args.dest+"\")\r")
# read source file line by line and write to device
if args.verbose: sys.stderr.write("\r\nStage 2. Creating file in flash memory and write first line")
writeln("file.open(\""+args.dest+"\", \"w+\")\r")
line = f.readline()
if args.verbose: sys.stderr.write("\r\nStage 3. Start writing data to flash memory...")
while line != '':
writer(line.strip())
line = f.readline()
# close both files
f.close()
if args.verbose: sys.stderr.write("\r\nStage 4. Flush data and closing file")
writeln("file.flush()\r")
writeln("file.close()\r")
# compile?
if args.compile:
if args.verbose: sys.stderr.write("\r\nStage 5. Compiling")
writeln("node.compile(\""+args.dest+"\")\r")
writeln("file.remove(\""+args.dest+"\")\r")
# restart or dofile
if args.restart:
writeln("node.restart()\r")
if args.dofile: # never exec if restart=1
writeln("dofile(\""+args.dest+"\")\r",0)
# close serial port
#s.flush()
s.close()
# flush screen
sys.stdout.flush()
sys.stderr.flush()
sys.stderr.write("\r\n--->>> All done <<<---\r\n")