-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.py
65 lines (57 loc) · 2.49 KB
/
button.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
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
import subprocess
global counter
import time
import vlc
counter = 0
global launch, kill
launch = vlc.MediaPlayer("file:///home/pi/PiGlassv2/launch.mp3")
kill = vlc.MediaPlayer("file:///home/pi/PiGlassv2/kill.mp3")
import psutil
def checkIfProcessRunning(processName):
'''
Check if there is any running process that contains the given name processName.
'''
#Iterate over the all the running process
for proc in psutil.process_iter():
try:
# Check if process name contains the given name string.
if processName.lower() in proc.name().lower():
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False;
def button_callback(channel):
global counter, launch, kill
counter += 1
print("counter: "+str(counter))
print("Mod: "+str(counter%2))
if(counter%2 == 1):
print("Launcher")
#subprocess.Popen(["sudo", "mpg123", "/home/pi/PiGlassv2/launcher.mp3"], shell=False)
launch.play()
launch = vlc.MediaPlayer("file:///home/pi/PiGlassv2/launch.mp3")
# time.sleep(2)
subprocess.Popen(["python3", "/home/pi/PiGlassv2/newmenu.py"], shell=False)
elif(counter%2 == 0):
if checkIfProcessRunning('kodi'):
print('kodi is running')
subprocess.Popen(["sudo", "systemctl", "restart", "lightdm.service"], shell=False)
else:
print('No kodi process was running')
# subprocess.Popen(["sudo", "systemctl", "restart", "lightdm.service"], shell=False)
print("Killed")
#subprocess.Popen(["sudo", "mpg123", "/home/pi/PiGlassv2/killing.mp3"], shell=False)
kill.play()
kill = vlc.MediaPlayer("file:///home/pi/PiGlassv2/kill.mp3")
time.sleep(3)
subprocess.Popen(['sudo', 'killall', "raspivid", "ffmpeg", "steamlink", 'kodi.bin_v7', 'retroarch', 'emulationstatio', 'python3'], shell=False)
print("Button was pushed!")
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BCM) # Use physical pin numbering
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(17,GPIO.RISING,callback=button_callback, bouncetime=3000) # Setup event on pin 10 rising edge
#message = input("Press enter to quit\n\n") # Run until someone presses enter
while True:
time.sleep(.5)
GPIO.cleanup() # Clean up