-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShell2.py
547 lines (502 loc) · 17.5 KB
/
Shell2.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#TRS_Shell 2
import os
import time
import gc
import gcAbstractor
from math import ceil
from math import floor
import machine
import sys
import hashlib
from binascii import crc32
def doesExist(path):
try:
os.stat(path)
return True
except:
return False
mColor = gcAbstractor.monoColor
beepfreq = 4040
beepdur = 40
beepvol = 100
trs_shell_ver = 05
rows = floor(gcAbstractor.ScreenHeight()/gcAbstractor.FixedWidthFontSize()[1])-1
print(rows)
columns = floor(gcAbstractor.ScreenWidth()/gcAbstractor.FixedWidthFontSize()[0])
fw=gcAbstractor.FixedWidthFontSize()[0]
fh=gcAbstractor.FixedWidthFontSize()[1]
#thumby.display.setFPS(60)
#wd = "/"
textArea = []
for i in range(rows):
textArea.append("")
textAreaWindowBottom = len(textArea)-1
global inputSpace
inputSpace = True
fontOverRide = False
if doesExist("/lib/thumby.py"):
fontOverRide = True
else:
font = open('/font.bin', 'rb')
pressedCheck = []
psv = None
try:
psv=Voice()
psv.envelope(0,0,100,0)
#this sets up adsr envelope, each value in the order of the acronym.
#0ms atk,0ms dcy,100% stn, 0ms rel
psv.bend(0,0)
#this sets up pitch bend.
#0hz amount, 0 milliseconds duration.
psv.effects(0,0,0)
#sets up fx.
#0 ms reverb, 0 ms noise, 0% distort
v=gcAbstractor.AbstractAudio(psv)
except Exception as errorInfo:
print("sound module is not picosystem")
v=gcAbstractor.AbstractAudio(None)
def drawText(string,x,y,colorWord,proportional):
global font
global fontOverRide
colorWord = gcAbstractor.monoColor(colorWord)
if proportional or fontOverRide:
gcAbstractor.originalText(string,x,y,colorWord)
else:
gcAbstractor.drawMonoSpaceText(string,x,y,colorWord,font,gcAbstractor.FixedWidthFontSize()[0])
def ConsoleSplitter(pendingOut):
#12 characters per line for 5x7 font
toAppend = []
charDump = ''
for i in range(len(pendingOut)):
charDump = charDump + pendingOut[i]
if charDump[-1] == "\n":
toAppend.append(charDump[:-1])
charDump = ""
if len(charDump)==columns:
toAppend.append(charDump)
charDump = ""
if len(charDump)>0:
toAppend.append(charDump)
charDump = ""
return(toAppend)
def ConsoleWriteLine(stringIn):
print(stringIn)
toWrite = ConsoleSplitter(stringIn)
for item in toWrite:
textArea.append(item)
textAreaWindowBottom = len(textArea)-1
drawConsoleToScreen(textAreaWindowBottom)
def CullConsoleHistory(maxLines):
global textArea
while len(textArea) > maxLines:
textArea.pop(0)
return 0
def drawConsoleToScreen(winBottom):
gcAbstractor.monoFill(0)
screenLines = rows
#if inputSpace:
# #//draw text Box
# screenLines = rows - 1
for line in range(screenLines):
pos = rows - screenLines
#drawText(textArea[winBottom-line],0,(screenLines-line-pos)*gcAbstractor.FixedWidthFontSize()[1],mColor(1),False)
drawText(textArea[winBottom-line], 0, fh*(screenLines-(1+line)), mColor(1),False)
gcAbstractor.updDisplay()
def getLineInput(start):
u = 0
d = 1
l = 2
r = 3
a = 4
b = 5
global inputSpace
global textAreaWindowBottom
global beepfreq
global beepdur
global beepvol
global pressedCheck
global rows
global ScreenWidth
prevInputSpace = inputSpace
inputSpace = True
textAreaWindowBottom = len(textArea)-1
drawConsoleToScreen(textAreaWindowBottom)
editing = True
viewWidth = 11
viewPos = 0
newCharacter = 33
inputString = start
counter = 0
justPressed = []
while editing:
justPressed = gcAbstractor.justPressedList()
#print("editing tick")
if True in justPressed:
v.play(beepfreq, beepdur, beepvol)
if gcAbstractor.keyY():
inputString = Quickies(inputString)
if gcAbstractor.keyX():
editing = False
if gcAbstractor.keyB():
#print("b pushed")
if gcAbstractor.keyA():
if justPressed[u]:
textAreaWindowBottom -= 1
if textAreaWindowBottom < 3:
textAreaWindowBottom = 3
drawConsoleToScreen(textAreaWindowBottom)
counter = 0
if justPressed[d]:
textAreaWindowBottom += 1
if textAreaWindowBottom > len(textArea)-1:
textAreaWindowBottom = len(textArea)-1
drawConsoleToScreen(textAreaWindowBottom)
counter = 0
if justPressed[l]:
inputString = Quickies(inputString)
else:
if justPressed[r]:
editing = False #Confirm
if justPressed[l]:
if ConfirmChoice("Erase input?"):
viewWidth = 12 #DelAll
viewPos = 0
inputString = ""
if justPressed[u]:
newCharacter += 32
counter = 0
if justPressed[d]:
newCharacter -= 32
counter = 0
elif gcAbstractor.keyA():
if justPressed[r]:
newCharacter = ord("(")
if justPressed[u]:
newCharacter += 10
counter = 0
if justPressed[d]:
newCharacter -= 10
counter = 0
if justPressed[l]:
newCharacter = 33
counter = 0
else:
if justPressed[r]:
if newCharacter<32:
newCharacter = 32
if newCharacter>126:
newCharacter = 126
inputString = inputString + chr(newCharacter) #NextChar
if justPressed[l]:
inputString = inputString[:-1]
if justPressed[u]:
newCharacter += 1
counter = 0
if justPressed[d]:
newCharacter -= 1
counter = 0
if newCharacter<32:
newCharacter = 32
if newCharacter>126:
newCharacter = 126
viewPos = len(inputString) - (columns-1) if len(inputString) > (columns-1) else 0
gcAbstractor.monoRectF(0, rows*fh, gcAbstractor.ScreenWidth(), fh, 1)
drawText(inputString[viewPos:],0,rows*fh,0,False)
gcAbstractor.monoRectF(gcAbstractor.ScreenWidth()-fw, rows*fh, fw, fh, 0)
counter = (counter + 1) % 15
if viewPos>0:
gcAbstractor.monoRectF(0, rows*fh, fw, fh, 1)
drawText("<",0,rows*fh,0,False)
if counter<7:
drawText(chr(newCharacter),gcAbstractor.ScreenWidth()-fw, rows*fh,1,False)
gcAbstractor.updDisplay()
return inputString
def prompt(inPrompt):
ConsoleWriteLine(">"+inPrompt)
try:
exec(inPrompt+"\n")
except Exception as errorInfo:
try:
if inPrompt in os.listdir():
handle = open(inPrompt,"r")
program = handle.read()
handle.close()
exec(program)
elif inPrompt in os.listdir("/"):
handle = open(inPrompt,"r")
program = handle.read()
handle.close()
exec(program)
else:
ConsoleWriteLine(repr(errorInfo))
print(errorInfo)
print(sys.print_exception(errorInfo))
except Exception as errorInfo2:
ConsoleWriteLine(repr(errorInfo2))
print(sys.print_exception(errorInfo2))
def ls():
#print('\n'.join(os.listdir()))
ConsoleWriteLine('\n'.join(os.listdir()))
def cd(path):
os.chdir(path)
pwd()
def pwd():
ConsoleWriteLine(os.getcwd())
def ConfirmChoice(caption):
gcAbstractor.monoFill(1)
drawText(caption,0,0,0,False)
drawText(" A:Yes B:No",0,rows*fh,0,False)
while gcAbstractor.keyA() or gcAbstractor.keyB():
time.sleep(0.3)
while True: #confirm replace
gcAbstractor.updDisplay()
if gcAbstractor.keyA():
drawConsoleToScreen(textAreaWindowBottom)
return True
if gcAbstractor.keyB():
drawConsoleToScreen(textAreaWindowBottom)
return False
drawConsoleToScreen(textAreaWindowBottom)
def isFolder(path):
if doesExist(path):
return os.stat(path)[0] == 16384
else:
return False
def getFreeSpaceBlocks():
return os.statvfs("")[4]
def getTotalSpaceBlocks():
return os.statvfs("")[2]
def getUsedSpaceBlocks():
return os.statvfs("")[2] - os.statvfs("")[4]
def Blocks2Bytes(blocks):
return os.statvfs("")[1] * blocks
def getBlockSize():
return os.statvfs("")[0]
def copyAbsolute(source,destination,delSource):
if isFolder(source):
raise Exception("\nSource is \na directory")
if isFolder(destination):
raise Exception("\nDestination\nis \na directory")
fsinfo = os.stat(source) #generate enoent exception if source doesnt exist
fsinfo = os.statvfs("") #get block size
copyChunkSize = fsinfo[0]
sourceHandle = open(source,"rb")
sourceHandle.read()#advance seek to the end of the file to find how big it is
sourceFileSize = sourceHandle.tell()
sourceHandle.seek(0) #seek back to start to begin copying
sourceSizeBlocks = ceil(sourceFileSize / copyChunkSize)
if doesExist(destination):
if ConfirmChoice("Overwrite?") == False:
raise Exception("\nDestination\nalready\nexists")
destHandle = open(destination, "wb")
for i in range(sourceSizeBlocks):
destHandle.write(sourceHandle.read(copyChunkSize))
ConsoleWriteLine("Copying\n"+ str(i)+ " of "+ str(sourceSizeBlocks))
if getFreeSpaceBlocks()<2:
destHandle.close()
os.remove(destination)
raise Exception("\nMemory full")
ConsoleWriteLine("Done copying")
if delSource:
os.remove(source)
ConsoleWriteLine("Source file removed")
def copy(source,dest):
copyAbsolute(source,dest,False)
def move(source,dest):
copyAbsolute(source,dest,True)
def r(path): ##relative path to absolute converter
if path[0] != "/":
path = os.getcwd() + path
return path
def ipt(path):
__import__(r(path))
#saveConfigSetting("lastgame", r(path))
#machine.mem32[0x4005800C]=1 # WDT scratch register '0'
#machine.soft_reset()
def saveConfigSetting(key, setting):
cfgfile = open("thumby.cfg", "r")
cfg = cfgfile.read().split(',')
cfgfile.close()
for k in range(len(cfg)):
if(cfg[k] == key):
cfg[k+1] = setting
cfgfile = open("thumby.cfg", "w")
cfgfile.write(','.join(cfg))
cfgfile.close()
def typeFile(path):
_f = open(r(path))
print(_f.read())
_f.seek(0)
ConsoleWriteLine(_f.read())
_f.close()
def echo(stringin):
ConsoleWriteLine(stringin)
print(stringin)
def sh_help():
echo("""HELP
(Variables)
wd: working
directory
more to come later
""")
def usbin():
while True:
prompt(input())
def TextMenu(editline, quickiesList, menuTitle):
print(editline)
global inputSpace
global beepfreq
global beepdur
global beepvol
global pressedCheck
u = 0
d = 1
l = 2
r = 3
a = 4
b = 5
global textAreaWindowBottom
quickiesIndex = 0
gcAbstractor.monoFill(1)
drawText(menuTitle,0,0,0,False)
while gcAbstractor.keyA() or gcAbstractor.keyB():
print("waiting")
time.sleep(0.3)
drawText("A:OK B:Cancel",0,rows*fh,0,False)
selecting = True
output = editline
justPressed = []
while selecting:
justPressed = gcAbstractor.justPressedList()
#while gcAbstractor.keyB():
# print("waiting")
# time.sleep(0.3)
#print("select start")
if True in justPressed:
v.play(beepfreq, beepdur, beepvol)
drawText("A:OK B:Cancel",0,rows*fh,0,False)
drawText(menuTitle,0,0,0,False)
drawText(str(quickiesIndex),gcAbstractor.ScreenWidth()-(fw*2),0,0,False)
drawText(quickiesList[quickiesIndex],0,fw*2,0,False)
time.sleep(0.02)
gcAbstractor.updDisplay()
if justPressed[u]:
print("up")
quickiesIndex -=1
gcAbstractor.monoFill(1)
if quickiesIndex<0: #scroll up
quickiesIndex = 0
if justPressed[d]:
print("down")
quickiesIndex +=1 #scroll down
gcAbstractor.monoFill(1)
if quickiesIndex>len(quickiesList)-1:
quickiesIndex = len(quickiesList)-1
if gcAbstractor.keyB():
print("b")
drawConsoleToScreen(textAreaWindowBottom)
return editline #cancel
if gcAbstractor.keyA():
print("a")
if ConfirmChoice("Confirm?"):
drawConsoleToScreen(textAreaWindowBottom)
return quickiesList[quickiesIndex]
while gcAbstractor.keyA() or gcAbstractor.keyB():
time.sleep(0.3)
def Quickies(editline):
menus = { "!Quickies" :["cd(\"",
"pwd()",
"sh_help()",
"ls()",
"echo(\"",
"ipt(\"",
"type(\"",
"f=open(\"",
"f.write(\"",
"g=f.read()",
"f.close()",
"os.remove(\"",
"os.rename(\"",
"os.rmdir(\"",
"os.mkdir(\"",
"gc.collect()",
"CullConsoleHistory(rows)",
"beepfreq=",
"beepdur=",
"fontOverRide=" ],
"os, etc.": ["un=os.uname()",
"ur=os.urandom(",
"wd=os.getcwd()",
"ild=os.ilistdir()",
"dir=os.listdir()",
"st=os.stat()",
"sv=os.statvfs(\""]
}
picked = TextMenu("quit", list(menus.keys()), "Category")
if picked=="quit":
return editline
pickedSub = TextMenu("quit", menus[picked], picked)
if pickedSub=="quit":
return editline
return pickedSub
def picoMgr():
print("UUUUUTRS_Shell")
ConsoleWriteLine("PC\nConnected")
def crcHexStr(dataIn):
return hex(crc32(dataIn))[2:]
def crcHexFile(path):
handle = open(path,"rb")
handle.seek(0,2)
size = handle.tell()-1
handle.seek(0,0)
crc = crc32(handle.read(1))
while handle.tell() <= size:
crc = crc32(handle.read(1), crc)
handle.close()
return hex(crc)[2:]
def loopWriteSer(handle): #file needs to be open()ed before this is called
try:
while True:
decimal = int(input())
if not decimal in range(0,255):
raise ValueError()
if getFreeSpaceBlocks() < 3:
raise OSError(28)
handle.write(decimal.to_bytes(1,"little")) #screw endianness, I'm using single bytes
#I think I can just leave the subroutine like this, we'll see if anything goes wrong
except Exception as e:
print(repr(e))
print("ERR")
def copyAbsoluteOverwrite(source,destination,delSource):
if isFolder(source):
raise Exception("\nSource is \na directory")
if isFolder(destination):
raise Exception("\nDestination\nis \na directory")
fsinfo = os.stat(source) #generate enoent exception if source doesnt exist
fsinfo = os.statvfs("") #get block size
copyChunkSize = fsinfo[0]
sourceHandle = open(source,"rb")
sourceHandle.read()#advance seek to the end of the file to find how big it is
sourceFileSize = sourceHandle.tell()
sourceHandle.seek(0) #seek back to start to begin copying
sourceSizeBlocks = ceil(sourceFileSize / copyChunkSize)
#if doesExist(destination):
# if ConfirmChoice("Overwrite?") == False:
# raise Exception("\nDestination\nalready\nexists")
destHandle = open(destination, "wb")
for i in range(sourceSizeBlocks):
destHandle.write(sourceHandle.read(copyChunkSize))
ConsoleWriteLine("Copying\n"+ str(i)+ " of "+ str(sourceSizeBlocks))
if getFreeSpaceBlocks()<2:
destHandle.close()
os.remove(destination)
raise Exception("\nMemory full")
ConsoleWriteLine("Done copying")
if delSource:
os.remove(source)
ConsoleWriteLine("Source file removed")
if doesExist("/trsauto.py"):
prompt("/trsauto.py")
while True:
prompt(getLineInput(""))
time.sleep(5)