forked from MitchRatquest/armbian-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
340 lines (307 loc) · 11.4 KB
/
functions.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
#!/usr/bin/python
import curses, curses.panel, os, subprocess
COLUMNS=79 #default tty size - 1
LINES=23
def make_panel(h,l, y,x):
win = curses.newwin(h,l, y,x)
win.erase()
win.box()
panel = curses.panel.new_panel(win)
return win, panel
def initializePin(index,screen,pinNumbers):
screen.hline(24,0," ",COLUMNS) #clear line completely
if pinNumbers[index] == -1:
response = "cannot init this pin"
elif createPinsysfs(pinNumbers[index]) == 0:
response = "successfully initialized pin " + str((index)+1)
screen.addstr(24,80/2-len(response)/2,response,curses.A_STANDOUT)
def uninitializePin(index,screen,pinNumbers):
response=''
screen.hline(24,0," ",COLUMNS) #clear line completely
if pinNumbers[index] == -1:
response = "cannot uninit this pin"
elif removePinsysfs(pinNumbers[index]) == 0:
response = "successfully uninitialized pin " + str((index)+1)
elif removePinsysfs(pinNumbers[index]) == -1:
response = "pin wasn't initialized"
screen.addstr(24,80/2-len(response)/2,response,curses.A_STANDOUT)
def readPin(index,screen,pinNumbers):
screen.hline(24,0," ",COLUMNS) #clear line completely
if pinNumbers[index] == -1:
response = "This pin's state is not able to be changed"
pass
else:
screen.hline(24,0," ",COLUMNS)
value = readPinsysfs(pinNumbers[index])
if value is None:
response = "This pin is not initialized"
else:
response = "Pin "+str(index+1)+"'s value is "+str(value)
screen.addstr(24,80/2-len(response)/2,response,curses.A_STANDOUT)
def pinInput(index, screen, pinNumbers):
screen.hline(24,0," ",COLUMNS) #clear line completely
if pinNumbers[index] == -1:
response = "This pin's state is not able to be changed"
pass
else:
value = directionPinsysfs(pinNumbers[index],'in')
if value == 0:
response = "Pin "+str(index+1)+" is an input pin and can sink up to 10 milliamps"
if value == -1:
response = "Pin is already set to input"
screen.addstr(24,80/2-len(response)/2,response,curses.A_STANDOUT)
def pinOutput(index, screen, pinNumbers):
screen.hline(24,0," ",COLUMNS) #clear line completely
if pinNumbers[index] == -1:
response = "This pin's state is not able to be changed"
screen.addstr(24,80/2-len(response)/2,response,curses.A_STANDOUT)
pass
else:
value = directionPinsysfs(pinNumbers[index],'out')
if value == 0:
response = "Pin "+str(index)+" is an output pin and can source up to 10 milliamps"
if value == -1:
response = "Pin is already set to output"
screen.addstr(24,80/2-len(response)/2,response,curses.A_STANDOUT)
def pinHigh(index,screen,pinNumbers):
screen.hline(24,0," ",COLUMNS) #clear line completely
response=''
if pinNumbers[index] == -1:
response = "This pin's state is not able to be changed"
elif os.path.isdir('/sys/class/gpio/gpio'+str(orangepiPCPins[(index-3)*2-1])) == False: #not configured
createPinsysfs(pinNumbers[index])
elif readSysfs(pinNumbers[index], 'direction') == 'in':
response = "This pin is an input and cannot be set to 1 (3.3 volts)"
screen.addstr(24,80/2-len(response)/2,response,curses.A_STANDOUT)
else:
value = valuePinsysfs(pinNumbers[index],'On')
if value == 0:
response = "Pin "+str(index)+"'s value is set to 1 (3.3 volts)"
if value == -1:
response = "Pin's value is already at 1"
screen.addstr(24,80/2-len(response)/2,response,curses.A_STANDOUT)
def pinLow(index,screen,pinNumbers):
screen.hline(24,0," ",COLUMNS) #clear line completely
if pinNumbers[index] == -1:
response = "This pin's state is not able to be changed"
elif os.path.isdir('/sys/class/gpio/gpio'+str(pinNumbers[index])) == False: #not configured
createPinsysfs(pinNumbers[index])
response = 'pin not configured'
elif readSysfs(pinNumbers[index],'direction').strip('\n')=='in':
response = "This is set to input and cannot be pulled low"
else:
value = valuePinsysfs(pinNumbers[index],0)
if value == 0:
response = "Pin "+str(index)+"'s value is set to 0 (ground)"
if value == -1:
response = "Pin's value is already at 0"
screen.addstr(24,80/2-len(response)/2,response,curses.A_STANDOUT)
def helpMenuShow(windowpanel,string):
windowpanel[1].show()
windowpanel[0].refresh()
windowpanel[0].timeout(500)
index = 0
helpMenuUpdate(windowpanel,string,index)
while True:
c = windowpanel[0].getch()
if c == 98: # b key
windowpanel[1].hide()
return
if c == 66: #arrow down
index = index + 1
index = helpMenuUpdate(windowpanel,string,index)
if index > len(string)-LINES-1:
index = index -1
if c == 65: #arrow up
index = index - 1
index = helpMenuUpdate(windowpanel,string,index)
if index <= 0:
index = index + 1
updateScreen(windowpanel[0])
def helpMenuUpdate(windowpanel,gpio,index):
for x in range(LINES-3): #clear window without erasing border
windowpanel[0].addstr(1+x,2,' ')
for x in range(LINES-4):
try:
windowpanel[0].addstr(2+x,4,str(gpio.strip('\n').split('\n')[x+index]))
except:
pass
warning = "up/down arrow to scroll, b to exit"
windowpanel[0].addstr(LINES-2,4,warning,curses.A_BOLD)
return index
def pinStats(pin,screen,pinNumbers):
for x in range(0,5):
screen.addstr(x,0,' ')
actualpin = pinNumbers[pin] #get actual pin number, aka PC7=71 or whatever
if actualpin == -1:
slot1="cant change"
screen.addstr(0,0,slot1,curses.A_STANDOUT)
updateScreen(screen)
return
else:
slot1='sysfs#: '
screen.addstr(0,0,slot1+str(actualpin),curses.A_STANDOUT)
if os.path.isdir('/sys/class/gpio/gpio'+str(actualpin)) == False:#no config dir
configured1="This pin not"
configured2="configured"
screen.addstr(1,0,configured1,curses.A_STANDOUT)
screen.addstr(2,1,configured2,curses.A_STANDOUT)
else:
success="configured"
screen.addstr(1,0,success,curses.A_STANDOUT)
status = "cur val: " + str(readPinsysfs(actualpin))
screen.addstr(2,0,status,curses.A_STANDOUT)
status2 = "cur dir: "
file = '/sys/class/gpio/gpio'+str(actualpin)+'/direction'
p = subprocess.Popen(['cat',file],stdout=subprocess.PIPE)
out, err = p.communicate()
status2 += out.strip('\n')
screen.addstr(3,0,status2,curses.A_STANDOUT)
status3= "activelow: "
activelow=str(readSysfs(actualpin,'active_low').strip('\n'))
screen.addstr(4,0,status3+activelow,curses.A_STANDOUT)
updateScreen(screen)
def highlightMenu(menuitem,screen,board): #need to pass it screen
'''
takes a string which is in an array and highlights the element it is referring to as described in the array
"STRING"[starty,startx,endy,endx]
startx
starty-> .------------------.
| |
| |
.------------------. <---endy
^endx
'''
try: #errors out sometimes
for x in range(0,len(board[0].split('\n'))): #redraw screen with outline
screen.addstr(x,0,board[0].split('\n')[x])
except:
pass
counter = 0
for x in range(board[1][board[1].index(menuitem)+1][0],board[1][board[1].index(menuitem)+1][2]+1): #only highlight the areas that need it
screen.addstr((board[1][board[1].index(menuitem)+1][0])+counter,board[1][board[1].index(menuitem)+1][1],board[0].split('\n')[x][board[1][board[1].index(menuitem)+1][1]:board[1][board[1].index(menuitem)+1][3]], curses.A_BOLD | curses.A_STANDOUT)
counter=counter+1
updateScreen(screen)
def createPinsysfs(pin):
if os.path.isdir('/sys/class/gpio/gpio'+str(pin)) == False:
command = 'echo ' + str(pin) + ' > /sys/class/gpio/export'
subprocess.call(command,shell=True)
return 0
else:
return -1
def removePinsysfs(pin):
if os.path.isdir('/sys/class/gpio/gpio'+str(pin)) == True:
command = 'echo ' + str(pin) + ' > /sys/class/gpio/unexport'
subprocess.call(command,shell=True)
return 0
else:
return -1
def directionPinsysfs(pin,direction):
if os.path.isdir('/sys/class/gpio/gpio'+str(pin)) == True:
command = 'echo ' + str(direction) + ' > /sys/class/gpio/gpio' + str(pin) + '/direction'
subprocess.call(command, shell=True)
return 0
else:
return -1
def valuePinsysfs(pin, value):
if os.path.isdir('/sys/class/gpio/gpio'+str(pin)) == True:
if value == "True" or value == "On" or value == 1:
value = 1
else:
value = 0
command = 'echo ' + str(value) + ' > /sys/class/gpio/gpio'+str(pin)+'/value'
subprocess.call(command,shell=True)
if readSysfs(pin, 'direction') == 'in': #can't set input high
return -1
return 0
else:
return -1
def readPinsysfs(pin):
if os.path.isdir('/sys/class/gpio/gpio'+str(pin)) == True:
file = '/sys/class/gpio/gpio'+str(pin)+'/value'
p = subprocess.Popen(["cat",file],stdout=subprocess.PIPE)
out, err = p.communicate()
return out.strip('\n')
def readSysfs(pin, filename):
file = '/sys/class/gpio/gpio'+str(pin)+'/'+str(filename)
p = subprocess.Popen(["cat",file],stdout=subprocess.PIPE)
out, err = p.communicate()
return out.strip('\n')
def readFile(filename):
with open(filename,"r+") as a:
lines = a.readlines()
return lines
def writeFile(filename,strings):
with open(filename,"w+") as a:
a.write(strings)
a.close()
def replaceModules(replace): #modify boot modules
original = readfile('/etc/modules')
newstring = ''
for line in original:
if line.strip('\n') == replace:
newstring += replace + '\n'
newstring += line
writefile('/etc/modules',newstring)
def Bash(command):
return subprocess.check_output(['bash','-c',command]).strip('\n').split('\n')
def findThis(command,pattern):
try:
Bash(command).index(pattern)
return 1
except:
return -1
def setupScreen(screen):
try:
curses.curs_set(0)
screen.nodelay(1)
except:
pass
curses.start_color()
curses.use_default_colors()
for i in range(0, curses.COLORS):
curses.init_pair(i+1, i, 0)
screen.timeout(-1)
#screen.bkgd(' ', curses.color_pair(0 ) )
def clearScreen(screen):
for x in range(0,LINES):
screen.hline(x,0,' ',COLUMNS) #erase entire screen
screen.chgat(x,0,curses.A_NORMAL) #reset to normal attributes
def updateScreen(screen):
curses.panel.update_panels()
screen.refresh()
def getBoardMenu(board):
menuitems=[]
for x in range(0,len(board[1]),2): #get menu item titles
menuitems.append(board[1][x])
return menuitems
def initGpioMenu(screen,board,index=4):
screen.erase()
updateScreen(screen)
warning="White arrow on board always points to pin 1"
screen.addstr(0,COLUMNS/2-len(warning)/2,warning,curses.A_BOLD)
pinNumbers=board[3]
pinNames=board[2]
offset=1
for x in range(0,len(pinNumbers)+1): #create pin numbers
if x % 2==1: #right side
screen.addstr((x+offset-(x/2)), (COLUMNS/2-1), str(x), curses.A_BOLD) #put even numbers on right side
screen.addstr((x+offset-(x/2)), (COLUMNS/2-4)-len(pinNames[x]), pinNames[x]) #names on right side at the right offset
else:
screen.addstr((x+offset-(x/2)), (COLUMNS/2+2), str(x), curses.A_BOLD) #put odd numbers on left side
screen.addstr((x+offset-(x/2)), (COLUMNS/2+7), pinNames[x]) #names on left side
#now draw a box
screen.addstr(offset,36,'.-------.') #top bar
screen.addstr(offset+len(pinNumbers)/2+1,36,"'-------'") #bottom bar
screen.vline(offset+1,36,'|',len(pinNumbers)/2) #left side
screen.vline(offset+1,44,'|',len(pinNumbers)/2) #right side
helpstring = "press h for help"
screen.addstr(offset+len(pinNumbers)/2+2,(COLUMNS/2)-(len(helpstring))/2,helpstring,curses.A_BOLD)
index=4 #place on screen to draw
screen.chgat(index/2,15,25,curses.A_STANDOUT) #first pin
updateScreen(screen)
def initHelpMenu(screen):
helpwin=make_panel(LINES,COLUMNS,0,0)
helpwin[1].hide()
helpindex=0
return helpwin