Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Listen for GPIO input from a raspberry pi - open menu and power off
# Import the RPi.GPIO and OS
import RPi.GPIO as GPIO
import os
import sys
import time
# GPIO port setup
# Power switch: will send a shutdown message to the OS
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Reset switch: will look for a running emulator process, and send it an interrupt signal
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Looks for an emulator process and send an interrupt signal if found
def exitEmulator(channel):
pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
for pid in pids:
commandpath = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read()
if commandpath[0:24] == '/opt/retropie/emulators/':
os.system('kill -QUIT %s' % pid)
print('kill -QUIT %s' % pid)
except IOError:
# Sends a poweroff command to the OS
def powerOff(channel):
# Here we just wire the GPIO inputs to their respective callback functions
GPIO.add_event_detect(17, GPIO.RISING, callback=powerOff, bouncetime=500)
GPIO.add_event_detect(18, GPIO.RISING, callback=exitEmulator, bouncetime=500)
# And... nothing more to do. let's wait.
while True:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment