-
Notifications
You must be signed in to change notification settings - Fork 1
/
PiControl.py
executable file
·790 lines (606 loc) · 16 KB
/
PiControl.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
#!/usr/bin/python
# Example using a character LCD plate.
# KT VENT
#SLOOSH
import math
import time
import socket
import Adafruit_CharLCD as LCD
import fcntl
import struct
import os
import subprocess
import re
from configobj import ConfigObj
# Initialize the LCD using the pins
lcd = LCD.Adafruit_CharLCDPlate()
# first menu screen by default
global menuCurr
global menuSelect
global diagSelect
global lcd_columns
lcd_columns = 16
menuSelect = 0
menuCurr = 0
diagSelect = 0
# create some custom characters
lcd.create_char(1, [0,27,14,4,14,27,0, 0]) #cross X
lcd.create_char(2, [0, 1, 3, 22, 28, 8, 0, 0]) #tick
lcd.create_char(3, [0, 14, 21, 23, 17, 14, 0, 0]) #clock
lcd.create_char(4, [0,10,31,31,14,4,0, 0]) #heart
lcd.create_char(5, [8, 12, 10, 9, 10, 12, 8, 0]) #right arrow
lcd.create_char(6, [2, 6, 10, 18, 10, 6, 2, 0]) # left arrow
#lcd.create_char(7, [31, 17, 21, 21, 21, 21, 17, 31]) #domino?
lcd.create_char(7, [31,31,31,31,31,31,31,0]) #square
# TIME TILL BACKLIGHT OFF
MENU_BLANK_TIME = 20
CLOCK_BLANK_TIME = 120
#config:
menuList=["Idle mode","WiFi Diag", "Pi Diag", "Halt", "Reboot", "Display OFF", "Settings", "About", "PiNumberStation", "Stop PiNS"]
menuDesc=["Show Clock&Date", "Connection Info", "Temp/Mem/Clock", "Shutdown System", "Reboot System", "Turn off display", "Change Options", ".. PiControl", "tvoya tsel zdes", "PiNumberStation"]
piDiagList=["Temp", "Memory Split", "Clocks"]
wifiDiagList=["SSID Info", "IP Info", "Test Con."]
wifiDiagDesc=["Show wifi SSID", "List interfaces", "Router&Web access"]
def findThisProcess( process_name ):
ps = subprocess.Popen("ps -eaf | grep "+process_name, shell=True, stdout=subprocess.PIPE)
output = ps.stdout.read()
ps.stdout.close()
ps.wait()
return output
# This is the function you can use
def isThisRunning( process_name ):
output = findThisProcess( process_name )
if re.search('path/of/process'+process_name, output) is None:
return False
else:
return True
# Restart Function: Reboots Pi
def reboot():
lcd.clear()
lcd.message("System Reboot in\n5 Seconds..")
time.sleep(5)
lcd.set_backlight(0)
lcd.enable_display(False)
command = "/usr/bin/sudo /sbin/shutdown -r now"
import subprocess
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
# Shutsdown pi
def halt():
lcd.clear()
lcd.message("System Halt in\n5 Seconds..")
time.sleep(5)
lcd.set_backlight(0)
lcd.enable_display(False)
command = "/usr/bin/sudo /sbin/halt"
import subprocess
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
def get_ssid():
from subprocess import check_output
ssid = "NONE"
ssid = check_output(["iwgetid", "-r"])
return ssid
def get_temp():
from subprocess import check_output
#vc gen cmd causes issues. Try not to use it in future.
#temp = check_output(["vcgencmd", "measure_temp"])
temp = int(check_output(["cat", "/sys/class/thermal/thermal_zone0/temp"])) / 1000
return temp
def get_curclock():
from subprocess import check_output
cur = int(check_output(["cat", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"]))
return str(cur/100000) + "MHz"
def get_maxclock():
from subprocess import check_output
max = int(check_output(["cat", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"]))
return str(max/100000) + "MHz"
def get_memARM():
from subprocess import check_output
mem = check_output(["vcgencmd", "get_mem arm"])
return mem
def get_memGPU():
from subprocess import check_output
mem = check_output(["vcgencmd", "get_mem gpu"])
return mem
def is_connected(REMOTE_SERVER):
try:
host = socket.gethostbyname(REMOTE_SERVER)
s = socket.create_connection((host, 80), 2)
return True
except:
pass
return False
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
# Make list of button value
buttons = [LCD.SELECT, LCD.LEFT, LCD.UP, LCD.DOWN, LCD.RIGHT]
def check_connectivity():
response2 = False
response1 = check_router()
if response1 == True:
response2 = check_google()
lcd.clear()
lcd.message("Connectivity: \n")
if response1==True and response2==True:
lcd.message("Full web access")
if response1==True and response2 ==False:
lcd.message("No web access")
if response1==False:
lcd.message("No router")
time.sleep(2)
lcd.clear()
return
def check_router():
response = False
lcd.clear()
lcd.message('Checking Router..\n')
if is_connected('10.0.0.2') == True:
lcd.message('->ROUTER OK \2')
response = True
else:
lcd.message('->ROUTER FAIL \1')
response = False
time.sleep(1)
lcd.clear()
return response
def check_google():
response = False
lcd.clear()
lcd.message('Checking Google..\n')
if is_connected('www.google.com') == True:
lcd.message('->GOOGLE OK \2')
response = True
else:
lcd.message('->GOOGLE FAIL \1')
response = False
time.sleep(1)
lcd.clear()
return response
def update_wifi_menu( param ):
lcd.clear()
lcd.message("[1][" + str(param) + "]-")
lcd.message(str(wifiDiagList[param]) + "\n")
lcd.message(str(wifiDiagDesc[param]))
lcd.show_cursor(True)
lcd.set_cursor(4,0)
time.sleep(0.1)
def wifi_diag():
diagSelect = 0
update_wifi_menu(diagSelect)
while True:
if lcd.is_pressed(LCD.RIGHT):
#if diagSelect == len(wifiDiagList) - 1:
# return
if diagSelect < len(wifiDiagList) - 1:
diagSelect = diagSelect + 1
time.sleep(0.1)
update_wifi_menu(diagSelect)
if lcd.is_pressed(LCD.LEFT):
if diagSelect == 0:
return
if diagSelect > 0:
diagSelect = diagSelect - 1
time.sleep(0.1)
update_wifi_menu(diagSelect)
if lcd.is_pressed(LCD.SELECT):
if diagSelect == 0:
display_ssid()
if diagSelect == 1:
display_ip()
if diagSelect == 2:
check_connectivity()
update_wifi_menu( diagSelect )
def prompt( text ):
lcd.clear()
lcd.autoscroll(False)
lcd.set_cursor(0,0)
lcd.blink(False)
i = 0
while (i < len(text)):
if i == 16 or i == 48 or i == 80 or i == 112:
lcd.message("\n")
if i == 32 or i == 64 or i == 96 or i == 128:
lcd.blink(True)
time.sleep(1)
lcd.clear()
lcd.blink(False)
lcd.home()
if i < len(text):
lcd.message(text[i])
else:
lcd.message(" ")
i = i + 1
time.sleep(0.05)
lcd.autoscroll(False)
lcd.home()
time.sleep(1)
lcd.clear()
lcd.blink(True)
def about_diag():
#prompt("This software is distributed under the MIT license. Please read the readme on github.")
msg = ['P','i','C','o','n','t','r','o','l']
lcd.clear()
#lcd.autoscroll(True)
k = 0
i = 0
while (k<2):
while (i < 17):
lcd.set_cursor(i,k)
lcd.message("\7")
i=i+1
k=k+1
i=0
while (k>=0):
while (i < 17):
if i > 5 and k==0:
lcd.message(msg[i-15])
else:
lcd.message(" ")
lcd.set_cursor(i-1,k)
i=i+1
k=k-1
i=0
'''
i = 14
while i < 16:
lcd.set_cursor(i,0)
lcd.message(" ")
time.sleep(0.1)
i=i+1 '''
lcd.clear()
lcd.message(" PiControl \n korn101 ")
warning(1)
lcd.show_cursor(False)
lcd.blink(False)
time.sleep(1)
lcd.clear()
lcd.message(" Written in \n Python ")
time.sleep(0.5)
lcd.message("\2")
time.sleep(0.5)
lcd.clear()
def pi_diag():
diagSelect = 0
update_diag_menu(diagSelect)
while True:
if lcd.is_pressed(LCD.RIGHT):
if diagSelect < len(piDiagList) - 1:
diagSelect = diagSelect + 1
time.sleep(0.1)
update_diag_menu(diagSelect)
if lcd.is_pressed(LCD.LEFT):
if diagSelect == 0:
return
if diagSelect > 0:
diagSelect = diagSelect - 1
time.sleep(0.1)
update_diag_menu(diagSelect)
def setting_diag():
lcd.clear()
#lcd.message(str(booleanDialog("Show splash?")))
#open config
import configobj
confwrite = configobj.ConfigObj()
confwrite.filename = "piconf.ini"
splash = str(booleanDialog("Show Splash ?"))
load_men = str(booleanDialog("Boot to menu ?"))
if load_men == "True":
def_men = integerDialog("Default menu ?", 0, len(menuList) - 1)
else:
def_men = 0
confwrite["show_splash"] = splash
confwrite["load_default_menu"] = load_men
confwrite["default_menu"] = def_men
confwrite.write()
lcd.message("Done \2")
time.sleep(1)
return
def anythingPressed():
if lcd.is_pressed(LCD.RIGHT) or lcd.is_pressed(LCD.LEFT) or lcd.is_pressed(LCD.UP) or lcd.is_pressed(LCD.DOWN) or lcd.is_pressed(LCD.SELECT):
return True
else:
return False
def idleMode():
el_start = time.time()
lcd.clear()
lcd.message("To exit clock..\nPress Left")
time.sleep(1)
lcd.clear()
lcd.message(" " + time.strftime("%H:%M") + " \n")
lcd.message(" " + time.strftime("%Y/%m/%d") + " ")
while True:
if lcd.is_pressed(LCD.RIGHT) or lcd.is_pressed(LCD.UP) or lcd.is_pressed(LCD.DOWN) or lcd.is_pressed(LCD.SELECT):
lcd.set_backlight(1)
el_start = time.time()
if lcd.is_pressed(LCD.LEFT):
return
else:
# get time:
disp = " " + time.strftime("%H:%M") + " \n" + " " + time.strftime("%Y/%m/%d") + " "
time.sleep(0.1)
disp2 = " " + time.strftime("%H:%M") + " \n" + " " + time.strftime("%Y/%m/%d") + " "
if (disp2 != disp):
lcd.clear()
lcd.message(" " + time.strftime("%H:%M") + " \n")
lcd.message(" " + time.strftime("%Y/%m/%d") + " ")
el_time = time.time() - el_start
if el_time >= CLOCK_BLANK_TIME:
lcd.set_backlight(0)
else:
lcd.set_backlight(1)
def update_menu( param ):
lcd.clear()
lcd.message("[" + str(param) + "] - ")
message = str(menuList[param]) + "\n" + str(menuDesc[param])
lcd.message(message)
lcd.show_cursor(True)
lcd.set_cursor(1,0)
def update_diag_menu( param ):
lcd.clear()
lcd.message("[2][" + str(param) + "]-")
lcd.message(str(piDiagList[param]) + "\n")
time.sleep(0.1)
#lcd.blink(False)
if param == 0:
lcd.message("CPU0: " + str(get_temp()) + " degC")
if param == 1:
lcd.clear
lcd.message(get_memGPU())
if param == 2:
lcd.message("\nCur:" + get_curclock() + " Max:" + get_maxclock())
time.sleep(0.1)
#lcd.blink(True)
def booleanDialog( strPrompt ):
lcd.clear()
if len(strPrompt) < 17:
lcd.message( str(strPrompt) )
else:
prompt( strPrompt )
lcd.set_cursor(0,1)
lcd.message( "Option: " )
lcd.set_cursor(14,1)
boolVal = True
lcd.message( "Y" )
#lcd.set_cursor(14,1)
while lcd.is_pressed(LCD.SELECT) == False:
# loop checking keys and flipping Y/N
if lcd.is_pressed(LCD.UP):
boolVal = True
lcd.set_cursor(14,1)
lcd.message("Y")
if lcd.is_pressed(LCD.DOWN):
boolVal = False
lcd.set_cursor(14,1)
lcd.message("N")
time.sleep(0.1)
lcd.set_cursor(0,0)
lcd.clear()
return boolVal
def integerDialog( strPrompt , rangeLow, rangeHigh):
lcd.clear()
if len(strPrompt) < 17:
lcd.message( str(strPrompt) )
else:
prompt( strPrompt )
lcd.set_cursor(0,1)
lcd.message( "Option: " )
lcd.set_cursor(14,1)
intVal = rangeLow
lcd.message( str(intVal) )
#lcd.set_cursor(14,1)
while lcd.is_pressed(LCD.SELECT) == False:
# loop checking keys and flipping Y/N
if lcd.is_pressed(LCD.UP):
if intVal < rangeHigh:
intVal+=1
lcd.set_cursor(14,1)
lcd.message(str(intVal))
if lcd.is_pressed(LCD.DOWN):
if intVal > rangeLow:
intVal-=1
lcd.set_cursor(14,1)
lcd.message(str(intVal))
time.sleep(0.1)
lcd.set_cursor(0,0)
lcd.clear()
return intVal
def backlightOff():
lcd.clear()
lcd.enable_display(False)
lcd.set_backlight(0)
time.sleep(1)
while True:
if lcd.is_pressed(LCD.UP):
lcd.enable_display(True)
lcd.set_backlight(1)
return
if lcd.is_pressed(LCD.DOWN):
lcd.enable_display(True)
lcd.set_backlight(1)
return
if lcd.is_pressed(LCD.RIGHT):
lcd.enable_display(True)
lcd.set_backlight(1)
return
if lcd.is_pressed(LCD.LEFT):
lcd.enable_display(True)
lcd.set_backlight(1)
return
if lcd.is_pressed(LCD.SELECT):
lcd.enable_display(True)
lcd.set_backlight(1)
return
def pins():
lcd.clear()
lcd.message("PiNumberStation")
time.sleep(1)
lcd.clear()
#strBroadcast = "103.3"
strBroadcast = str(integerDialog("hundreds", 0, 2))
strBroadcast += str(integerDialog(strBroadcast[0] + "x", 0, 9))
strBroadcast += str(integerDialog(strBroadcast[0] + strBroadcast[1] + "x", 0, 9))
strBroadcast += "."
strBroadcast += str(integerDialog(strBroadcast[0] + strBroadcast[1] + strBroadcast[2] + ".x",0,9))
if (strBroadcast == "000.0"):
prompt("Invalid frequency")
return
else:
subprocess.Popen(["sudo","python", "/home/pi/PiNumberStation/PiNS.py", strBroadcast, "&"])
prompt("PiNumberStation begun on frequency " + strBroadcast)
time.sleep(1)
return
def exit_pins():
subprocess.call(["sudo", "killall", "pifm"])
subprocess.call(["sudo", "pkill", "-9", "-f", "PiNS.py"])
import RPi.GPIO as GPIO
GPIO.setup(4, GPIO.OUT)
GPIO.output(4, GPIO.LOW)
return
def menu_exec( param ):
lcd.blink(False)
if param == 0:
idleMode()
if param == 1:
wifi_diag()
if param == 2:
pi_diag()
if param == 3:
halt()
if param == 4:
reboot()
if param == 5:
backlightOff()
if param == 6:
setting_diag()
if param == 7:
about_diag()
if param == 8:
pins()
if param == 9:
exit_pins()
update_menu(menuSelect)
lcd.blink(True)
def display_ip():
lcd.clear()
interfaces = ['wlan0','eth0','lo']
for intf in interfaces:
try:
lcd.message(intf + ":\n " + get_ip_address(intf))
except:
lcd.message(intf + ":\n disconnected")
time.sleep(1)
lcd.clear()
return
def display_ssid():
lcd.clear()
lcd.message("Checking Wifi..")
ssid = get_ssid()
lcd.message("\nDone \2")
time.sleep(0.1)
lcd.clear()
if ssid != "NONE":
lcd.message("Connected to:\n")
lcd.message(str(ssid))
else:
lcd.message("SSID missing\n")
time.sleep(3)
return
def createConfig():
import configobj
confwrite = configobj.ConfigObj()
confwrite.filename = "piconf.ini"
splash = "True"
load_men = "False"
def_men = 0
confwrite["show_splash"] = splash
confwrite["load_default_menu"] = load_men
confwrite["default_menu"] = def_men
confwrite.write()
def warning( flashes ):
tdelay = 0.1
i = 0
while (i < flashes):
lcd.set_backlight(0)
time.sleep(tdelay)
lcd.set_backlight(1)
time.sleep(tdelay)
i = i + 1
# START:
try:
from subprocess import check_output
lines = (check_output(["df", "-h", "/dev/sda1"])).splitlines()
#fields = lines[1].split(' ')
#prompt(str(fields[0] + ": \n" + (fields[6].split(' '))[0]))
lcd.clear()
lcd.message("External HDD:\nMounted \2")
time.sleep(0.5)
except:
lcd.clear()
lcd.message("External HDD:\nNot mounted \1")
warning(1)
time.sleep(1)
lcd.show_cursor(True)
lcd.blink(True)
lcd.clear()
# load config file: default menu, splash?
lcd.message("Loading Config..\n")
try:
config = ConfigObj("piconf.ini")
splash = config["show_splash"]
default_menu_enabled = config["load_default_menu"]
default_menu = config["default_menu"]
lcd.message("SUCCESS \2")
time.sleep(0.5)
except:
lcd.message("FAILED \1")
warning(1)
time.sleep(1)
splash = True
default_menu_enabled = False
prompt("A config file was not found, or has become corrupted.")
if booleanDialog("Use default config?") == True:
createConfig()
lcd.clear()
if splash == "True":
lcd.clear()
about_diag()
lcd.clear()
lcd.message('Select Menu Item\n with UP/DOWN')
time.sleep(1)
lcd.clear()
update_menu(0)
# Main Loop
if default_menu_enabled == "True":
menu_exec(int(default_menu))
start_time = time.time()
while True:
if lcd.is_pressed(LCD.DOWN):
lcd.set_backlight(1)
if menuSelect < len(menuList) - 1:
menuSelect = menuSelect + 1
time.sleep(0.1)
update_menu(menuSelect)
start_time = time.time()
if lcd.is_pressed(LCD.UP):
lcd.set_backlight(1)
if menuSelect > 0:
menuSelect = menuSelect - 1
time.sleep(0.1)
update_menu(menuSelect)
start_time = time.time()
if lcd.is_pressed(LCD.SELECT) or lcd.is_pressed(LCD.RIGHT):
lcd.set_backlight(1)
menu_exec(menuSelect)
time.sleep(0.1)
start_time = time.time()
elapsed_time = time.time() - start_time
if elapsed_time >= MENU_BLANK_TIME:
lcd.set_backlight(0)
else:
lcd.set_backlight(1)