Last active
August 2, 2021 12:19
-
-
Save ddepaoli3/d08d0ae6f689869b756cda5933f8b84c to your computer and use it in GitHub Desktop.
retropie-shutdown-and-switch-to-x11.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Initial script from: | |
# https://retropie.org.uk/forum/topic/13141/gpio-button-to-exit-emulator/3 | |
import RPi.GPIO as GPIO | |
import time | |
import subprocess | |
#https://github.com/crcerror/ES-generic-shutdown script location | |
es_generic_shutdown_path = "/home/pi/ES-generic-shutdown/multi_switch.sh" | |
# Define which pin you're using for the reset button (change this to whatever pin you use) | |
x11Btn = 13 | |
poweroffBtn = 5 | |
# Use 'board' pin numbering (i.e. the zig-zaging numbering scheme) | |
GPIO.setmode(GPIO.BOARD) | |
# See: https://pinout.xyz/ for the pin layout | |
# Set the x11Btn pin to an input and enable the pull-up resistor | |
GPIO.setup(x11Btn, GPIO.IN, pull_up_down = GPIO.PUD_UP) | |
GPIO.setup(poweroffBtn, GPIO.IN, pull_up_down = GPIO.PUD_UP) | |
# Define a function which will be called when your reset button is pressed | |
def interrupt_x11Btn(channel): | |
print("You pressed the X11 button!") | |
es_pid = int(subprocess.check_output( ["/bin/bash", es_generic_shutdown_path, "--es-pid"] )) | |
if es_pid != 0: | |
print("ES found with PID " + str(es_pid) + ". Back to console") | |
rc = subprocess.call( ["/bin/bash", es_generic_shutdown_path, "--es-systemd"] ) | |
else: | |
print("ES not found. Back to emulationstation") | |
rc1 = subprocess.call( ["killall xinit"] ) | |
time.sleep(2) | |
rc2 = subprocess.call( ["/usr/bin/sudo -u pi /usr/bin/emulationstation"] ) | |
# Define a function which will be called when your reset button is pressed | |
def interrupt_poweroffBtn(channel): | |
print("You pressed the PowerOff button!") | |
es_pid = int(subprocess.check_output( ["/bin/bash", es_generic_shutdown_path, "--es-pid"] )) | |
if es_pid != 0: | |
print("ES found with PID " + str(es_pid)) | |
rc = subprocess.check_output( ["/bin/bash", es_generic_shutdown_path, "--es-poweroff"] ) | |
else: | |
print("ES not found") | |
rc = subprocess.check_output( ["/sbin/poweroff"] ) | |
# Enable reset button interrupt to trigger on a falling edge (i.e. high-to-low transition) | |
GPIO.add_event_detect(x11Btn, GPIO.FALLING, callback = interrupt_x11Btn, bouncetime = 1000) | |
GPIO.add_event_detect(poweroffBtn, GPIO.FALLING, callback = interrupt_poweroffBtn, bouncetime = 1000) | |
# -------------------------------------------------------------------- | |
# Now just wait forever for the user to press a button | |
# The sleep time doesn't really matter, make it long enough so it isn't wasting cpu cycles | |
while 1: | |
time.sleep(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment