-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpikeSerial.py
342 lines (278 loc) · 10.8 KB
/
SpikeSerial.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# Code to generate a generic webpage
Start_html = '''
<html>
<head>
<title>MicroPython on {}</title>
</head>
<body style="width:1200px; margin: 5px auto;">
<section style="width: 98%;height: 45px;background:#FFCC00;margin: auto;padding: 10px ;float:right">
<div style="width: 80%;margin: auto;height: 35px;float: left;">
<p style="font-size: 18 px;color:white;">You are connected to <b>{}</b> on <b>{}</b></p>
</div>
<div style="margin: auto;float: right;">
<form action="/IP" method="POST">
<button style="background-color:red;font-size: 15px;color:white;" name="Close" type="submit" value="Close">Disconnect</button>
</form>
</div>
</section>
<section style="width: 98%;height: 650px;background: #FFCC00;margin: auto;padding: 10px;float:right">
<div style="width: 48%;height: 620px;background: #FFFFFF;float: left;overflow: scroll;padding: 10px 10px 10px 10px">
<form action="/" method="POST">
<i style="font-size: 14px;"> Select Chapter. </i> <br>
<select style="background-color:blue;padding: 10px 12px;font-size: 12px;margin: 2px 1px;color:white;" name = "page">
{}
</select>
<button style="background-color:blue;padding: 5px 12px;font-size: 12px;margin: 2px 1px;color:white;" name="Page" type="submit" value="Close">Go To</button>
</form>
<hr>
'''
Form_html = '''
<form action="/" method="POST">
<section>
<div style="width: 550px;background-color: #FFFFFF;font-size: 12px;float: left;padding: 5px">
<h3>{}</h3>
<p style="font-family:Helvetica"> {} </p> <br>
<div style="width: 420px;font-size: 12px;float: left;">
<textarea rows="{}" cols="80" name = "{}" style="width: 420px;background-color: #E1E1E1; font-family:Courier New;padding: 5px">{}</textarea>
</div>
<div style="width: 100px;background-color: #FFFFFF;color:black;font-size: 12px; float: right;">
<center> <button style=" text-align: center;background-color: #FFFFFF;padding: 5px 12px;" name="REPL" type="submit" > Try it!!! </button> </center>
</div>
<p style="font-family:Helvetica;float: left;"> {} </p> <br>
</div>
</section>
</form>
'''
Rest_html = '''
</p>
</div>
<div style=" margin-left: 1%;height: 620px;background: #FFFFFF;float: right;padding: 10px 10px 10px 10px">
<center><h4>{} Terminal Window</h4></center>
<center> <b>Script</b> </center>
<form action="/" method="POST">
<textarea style="background: #E1E1E1" rows="15" cols="70" name = "Text to send"> {} </textarea><br>
<input style="background-color:green;color:white;" type="submit" name="SendCommand" value="Run Script">
</form>
<center><b>REPL</b></center>
<form action="/" method="POST">
<textarea style="background: #E1E1E1" rows="18" cols="70" name = "TerminalWindow">{} </textarea><br>
<section style="width: 100%;height: 10px;background:#FFFFFF;margin: auto;padding: 2px ;float:right">
<div style="width: 50%;margin: auto;height: 35px;float: left;">
<input style="background-color:green;color:white;" type="submit" name="SendCommand" value="UPDATE">
</form>
</div>
<div style="margin: auto;height: 35px;float: right;">
<form action="/" method="POST">
<input style="background-color:green;color:white;" type="submit" name="Clear" value="Clear REPL" onclick="myFunction()">
</form>
</div>
</section>
</div>
</section>
</body>
</html>
'''
Init_html = '''
<html>
<body style="width:960px; margin: 20px auto;">
<br> <br>
<center>
<h1> <form action="/" method="POST">{} Terminal Window</h1>
<br>
<hr>
<form action="/IP" method="POST">
<p>Select port and press Connect to connect to {}</p>
<form action="/" method="POST">
<select name = "processors">
{}
</select>
<input type="submit" name = "Connect", value = "Connect">
</form>
<p>
<i> If it does not connect, make sure it is on and plugged in. </i>
</p>
</center>
</body>
</html>
'''
from time import sleep
from http.server import BaseHTTPRequestHandler
import getpass, sys, socket, os
import serial,glob,time
from urllib.parse import unquote
# Initialize global variables
connected = False
terminal = "" #intialize blank terminal
ser = None
spike = ''
page = "start"
script = 'Type Here'
def InitSerial(port, bps = 115200, to = 0):
global ser
ser = serial.Serial(port, bps, timeout = to) # open serial port
return (ser.name)
def CloseSerial():
global ser
ser.close()
return('done')
def WriteSerial(string):
global ser
return(ser.write(string.encode())) # write a string
def ReadSerial():
global ser
reply = ''
if ser.in_waiting:
reply = ser.read(ser.in_waiting).decode()
return(reply)
def serial_ports():
result = []
if sys.platform.startswith('win'):
for i in range(256):
try:
s = serial.Serial('COM%s' % (i + 1))
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
for port in ports:
result.append(port)
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
for port in ports:
if 'usbmodem' in port:
result.append(port)
else:
raise EnvironmentError('Unsupported platform')
print(result)
return result
def StartConnection():
print('looking')
reply = ''
try:
reply = serial_ports()
except:
pass
return(reply)
def WaitForIt():
doneReading = False
text = ''
starttime = time.time()
while not doneReading:
text = text + ReadSerial()
doneReading = '>>>' in text
if (time.time() > starttime+1):
break
return text
def SendIt(text):
global ser
WriteSerial(text + '\r\n')
reply = WaitForIt()
return(reply)
# Webserver
class MyServer(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def _redirect(self, path):
self.send_response(303)
self.send_header('Content-type', 'text/html')
self.send_header('Location', path)
self.end_headers()
def do_GET(self):
global connected
global terminal,script
global spike
print('page = ' + page)
self.do_HEAD()
if (page == 'start'):
connections = StartConnection()
p_list = ''
for p in connections:
p_list = p_list + '''<option selected="{}">{}</option>'''.format(p,p)
self.wfile.write(Init_html.format(processor,processor,p_list).encode("utf-8"))
else:
page_list = ''
for p in pyCode:
if p == page:
select = 'selected'
else:
select = ''
page_list = page_list + '<option {}>{}</option>'.format(select,p)
pageContent = Start_html.format(processor,processor,spike,page_list)
if page in pyCode:
for line in pyCode[page]:
endnote= pyCode[page][line][3]
buttonName = pyCode[page][line][0]
python = pyCode[page][line][2]
rows = len(python.split('\n'))
introText = pyCode[page][line][1]
pageAppend = Form_html.format(buttonName,introText,rows,line,python,endnote)
pageContent = pageContent + pageAppend
else:
pageContent = 'Error - you are asking for a page that does not exist'
pageContent = pageContent + Rest_html.format(processor,script.strip(),terminal)
self.wfile.write(pageContent.encode("utf-8"))
def do_POST(self):
global connected
global terminal,script
global ser
global spike, page
content_length = int(self.headers['Content-Length']) # Get the size of data
post_data = self.rfile.read(content_length).decode('utf-8') # Get the data
#print(post_data)
post_data = post_data.split("=")[1] # Only keep the value
print('POST data ' + post_data) # Uncomment for debugging
if 'Connect' in post_data and spike == '':
spike = unquote(post_data.split("&")[0]) #StartConnection()[0].strip()
if not (spike == ''):
InitSerial(spike)
WriteSerial('\x03\n')
reply = WaitForIt()
terminal = "Starting...\n"+ reply
page = 'Chapter1_GettingStarted'
else:
terminal = "Failed to connect\n"
print("-----------Connection Initiated-----------")
elif 'SendCommand' in post_data and not (spike == ''):
command = post_data.split("&")[0]
if not (len(command) == 0):
command = command.replace("+", " ")
command = unquote(command).split(">>>")[-1]
command = command.strip() #replace('\r','\n')
script = command #keep this in memory to update the terminal
cmds = command.split('\n')
for sendcmd in cmds:
sendcmd = sendcmd.strip()
print('Command to send ' + sendcmd)
terminal = terminal+SendIt(sendcmd)
elif 'Close' in post_data and not (spike == ''):
CloseSerial()
terminal = terminal + '\n closed'
spike = ''
page = 'start'
elif 'Clear' in post_data:
print('clearing')
SendIt("\n")
SendIt("\n")
SendIt("\n")
terminal = '>>>'
elif 'Page' in post_data:
print('new page: ' + post_data)
if 'Chapter1_GettingStarted' in post_data:
page = 'Chapter1_GettingStarted'
if 'Chapter2_Accelerometer' in post_data:
page = 'Chapter2_Accelerometer'
if 'sensor' in post_data:
page = 'sense'
if 'advance' in post_data:
page = 'advance'
elif not (spike == ''):
LinesOfCode = unquote(post_data.split("&")[0].replace("+", " ")).split('\n')
print(LinesOfCode)
for line in LinesOfCode:
terminal = terminal + SendIt(line.strip())
self._redirect('/') # Redirect back to the root url