-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploader.py
368 lines (287 loc) · 10.5 KB
/
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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
''' ==================================================================
uploader2.py - Micropython program / file loader
s.case - 2/21/25
------------------------------------------------------------------
Testing of libraries for esp loader with Raspi Pi (not pico)
RPI.GPIO no longer supports interrupts/events
Fine for twiddling bits. lgpio is just a shim
for RPi.GPIO so it no longer works either
GPIO.add_event_detect() now throws error in Bullseye
and Bookworm. (Testing with kernel 5.10.103 Bullseye)
pigpio Seems to be supporting event callbacks for buttons via
gpio. Uses special daemon [$ sudo pigpiod &] in background
mpfshell==0.9.1 & works.
luma working on SSD1306, just need to make screens persistent
-------------------------------------------------------------------
v.2 Working. Needs ls, lls to put more that one file on the
display at a time. Adding com port selection
===================================================================
'''
import RPi.GPIO as GPIO
import pigpio
import os
from luma.core.interface.serial import i2c
from luma.core.render import canvas
from luma.oled.device import ssd1306
from time import sleep
rstpin = 4
yItemInc = [14, 23, 32, 41, 50]
offset=6
apin = 17
bpin = 27
cpin = 22
total = 0
aflag = False
bflag = False
cflag = False
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(rstpin, GPIO.OUT)
GPIO.output(rstpin, 1)
pi = pigpio.pi()
pi.set_pull_up_down(apin, pigpio.PUD_UP)
pi.set_pull_up_down(bpin, pigpio.PUD_UP)
pi.set_pull_up_down(cpin, pigpio.PUD_UP)
serial = i2c(port=1, address=0x3D)
device = ssd1306(serial, rotate=0, reset=rstpin)
padding = 2
shape_width = 20
top = padding
bottom = device.height - padding - 1
comlookup = ["ttyUSB0", "ttyACM0", "ttyUSB1", "ttyACM1", "ttyUSB2", "ttyACM2", "ttyUSB3", "ttyACM3", "ttyUSB4", "ttyACM4",]
COMPORT = "ttyUSB0"
MAINMENU = 1
RESETTAR = 2
LOCALDIR = 3
TARGETDIR = 4
LOADMENU = 5
flist = []
print("uPython Little Loader running. v0.2")
def cba(gpio, level, tick):
global aflag
aflag = 1
#print(gpio, level, tick, aflag)
def cbb(gpio, level, tick):
global bflag
bflag = 1
#print(gpio, level, tick, bflag)
def cbc(gpio, level, tick):
global cflag
cflag = 1
#print(gpio, level, tick, bflag)
def flushport(port):
os.system('stty -F /dev/' + port + ' -hupcl') # do this the hard way vs changing mfpshell
#print('stty -F /dev/' + port + ' -hupcl')
def filelist():
global flist
with open("out.txt", "r") as file:
flist = file.readlines()
# Remove \n characters from each line
flist = [line.strip() for line in flist]
for line in range(0,4) :
flist.pop(0) # these messages are not necessary for the file list
flist.pop(-1) # cr at the end of the listing, remove it
file.close() # be nice!
def mpf_reset(port):
os.system('mpfshell -n -c ' + '"open ' + port + '; --reset ' + '" > reset.txt')
#os.system('mpfshell -n -c ' + '"open ' + port + '; --reset ' + '" ')
def mpf_ls(port):
# mpfshell target ls command ===============================================================
os.system('mpfshell -n -c ' + '"open ' + port + '; ls ' + '" > out.txt')
filelist()
# print("Programs on target: ")
# print(flist)
flushport(port)
def mpf_lls(port):
# mpfshell local lls command ==============================================================
os.system('mpfshell -n -c ' + '"open ' + port + '; lls ' + '" > out.txt')
filelist()
# print("Programs on tool: ")
# print(flist)
flushport(port)
def mpf_rm(port, fname):
# mpfshell rm command ====================================================================
# file will come from selecting one of the targets programs
### TODO will need ok, cancel
os.system('mpfshell -n -c ' + '"open ' + port + '; rm ' + fname + '" > rmout.txt')
print("Programs ", fname, " deleted from target ")
flushport(port)
def mpf_put(port, fname):
# mpfshell put command ====================================================================
### TODO file check
# mpfshell ls
# see if fname file is in resulting list
# if so issue warning and get user response, to copy over target?
os.system('mpfshell -n -c ' + '"open ' + port + '; put ' + fname + '" > putout.txt')
print("Programs ", fname, " copied to target ")
flushport(port)
def disp_ls(dev, xpos, ypos, stext):
with canvas(dev) as draw:
draw.rectangle(dev.bounding_box, outline="white")
draw.text((6, 20), "Files = ", fill="white")
draw.text((xpos, ypos), stext, fill="grey")
dev.show()
sleep(.1)
cb1 = pi.callback(apin, pigpio.RISING_EDGE, cba)
cb2 = pi.callback(bpin, pigpio.RISING_EDGE, cbb)
cb3 = pi.callback(cpin, pigpio.RISING_EDGE, cbc)
# main()
#==================================================================
# Splash Screen
with canvas(device) as draw:
draw.rectangle(device.bounding_box, outline="white")
draw.text((10, 4), "uPython ", fill="white")
draw.text((10, 20), "Little Loader ", fill="white")
draw.text((10, 40), "s.case 2/19/25", fill="white")
draw.text((10, 50), "ver 0.1 " + COMPORT, fill="white")
device.show()
sleep(5)
menuid = 1
while True :
#==================================================================
# Main Menu
if menuid == MAINMENU :
while cflag == False:
with canvas(device) as draw:
draw.rectangle(device.bounding_box, outline="white")
draw.text((10, 4), "1. Main Menu: ", fill="white")
draw.text((90, 4), "# " + str(menuid), fill="white")
draw.text((10, 20), "2. Reset Target", fill="white")
draw.text((10, 30), "3. Local Files", fill="white") ## TODO change to use multiple files per screen (page)
draw.text((10, 40), "4. Target Files", fill="white") ## TODO change to use multiple files per screen (page)
draw.text((10, 50), "5. Load Target", fill="white")
device.show()
#sleep(8)
p = 0
if (aflag == True):
menuid += 1
if menuid > LOADMENU :
menuid = MAINMENU
aflag = False
if (bflag == True):
menuid -= 1
if menuid < MAINMENU :
menuid = LOADMENU
bflag = False
cflag = False
# execute menu selection
#--------------------------------------------------------------
elif menuid == RESETTAR : # reset target
with canvas(device) as draw:
draw.rectangle(device.bounding_box, outline="white")
draw.text((10, 20), "Resetting Target ", fill="white")
device.show()
flushport(COMPORT)
mpf_reset(COMPORT)
sleep(4)
menuid = MAINMENU
#--------------------------------------------------------------
elif menuid == LOCALDIR : # list files on loader
with canvas(device) as draw:
draw.rectangle(device.bounding_box, outline="white")
draw.text((6, 1), "Local list", fill="white")
device.show()
sleep(.1)
mpf_lls(COMPORT)
fp = 0
while cflag == False:
disp_ls(device, 10, 30, flist[fp] )
device.show()
if aflag == True:
fp += 1
if fp>len(flist)-1 :
fp = 0
aflag = False
if bflag == True:
fp -= 1
if fp>0 :
fp = 0
bflag = False
menuid = MAINMENU
#--------------------------------------------------------------
elif menuid == TARGETDIR : # list target files
while cflag == False:
with canvas(device) as draw:
draw.rectangle(device.bounding_box, outline="white")
draw.text((10, 1), "Target Programs: ", fill="white")
device.show()
sleep(.1)
mpf_ls(COMPORT)
fp = 0
while cflag == False:
disp_ls(device, 10, 30, flist[fp] )
device.show()
if aflag == True:
fp += 1
if fp>len(flist)-1 :
fp = 0
aflag = False
if bflag == True:
fp -= 1
if fp>0 :
fp = 0
bflag = False
menuid = MAINMENU
#------------------------------------------------------------------
elif menuid == LOADMENU : # copy a program to target
with canvas(device) as draw:
draw.rectangle(device.bounding_box, outline="white")
draw.text((6, 1), "Local list", fill="white")
device.show()
sleep(.1)
mpf_lls(COMPORT)
fp = 0
while cflag == False:
disp_ls(device, 10, 30, flist[fp] )
device.show()
if aflag == True:
fp += 1
if fp>len(flist)-1 :
fp = 0
aflag = False
if bflag == True:
fp -= 1
if fp>0 :
fp = 0
bflag = False
selection = flist[fp]
cflag = False
with canvas(device) as draw:
draw.rectangle(device.bounding_box, outline="white")
draw.text((10, 20), "Program = ", fill="white")
draw.text((10, 40), selection, fill="white")
device.show()
sleep(.1)
qry = "No, Cancel"
while cflag == False:
with canvas(device) as draw:
draw.rectangle(device.bounding_box, outline="white")
draw.text((10, 1), "Load ?", fill="white")
draw.text((10, 10), selection, fill="white")
draw.text((10, 40), qry, fill="white")
if (aflag == True) or (bflag == True):
if qry == "No, Cancel" :
qry = "Yes"
else:
qry = "No, Cancel"
aflag = False
bflag = False
device.show()
sleep(.1)
if qry == "Yes" :
with canvas(device) as draw:
draw.rectangle(device.bounding_box, outline="white")
draw.text((10, 20), "Loading = ", fill="white")
draw.text((10, 40), flist[fp], fill="white")
device.show()
menuid = MAINMENU
mpf_put(COMPORT, selection)
sleep(3)
else :
with canvas(device) as draw:
draw.rectangle(device.bounding_box, outline="white")
draw.text((10, 20), "Load Aborted", fill="white")
device.show()
sleep(2)
cflag = False
menuid = MAINMENU