matthewjosephcordovaParticipant06/26/2015 at 13:41Post count: 22
I’ve came up with a method to be able to have an external pushbutton that is used to exit an emulator back to emulationstation. The way it works is by either sending a sigterm signal to the emulator, and if that doesn’t work, to try simulating a keypress (ESC). Some emulators don’t accept keypress from python’s uinput, so sigterm must be used. Some emulators don’t accept sigterm, so uinput must be used.
This is a python script, and uinput must be installed. You can test if uinput is properly installed by going to a command prompt and typing in
Then when you are in python idle,
If it comes back with >>> , everything is working fine. If it says ImportError: No module named uinput, then it isn’t installed. On my image I downloaded from here, uinput was not installed correctly. To install uinput, try
pip install python-uinput
If that doesn’t work, try
RPi.GPIO must also be installed. In a command window, try
sudo apt-get install python-dev python-rpi.gpio
Now the script must be created. Save the following script as exit.py in /home/pi/
from time import sleep import os import RPi.GPIO as GPIO import uinput key_events=( uinput.KEY_ESC, ) device=uinput.Device(key_events) GPIO.setmode(GPIO.BCM) GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) def exitEmulator(channel): print('exitEmulator') pids = [pid for pid in os.listdir('/proc') if pid.isdigit()] for pid in pids: try: commandpath = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read() if commandpath[0:24] == '/opt/retropie/emulators/': os.system('kill -TERM %s' % pid) print('kill -TERM %s' % pid) device.emit(uinput.KEY_ESC,1) sleep(2) device.emit(uinput.KEY_ESC,0) except IOError: continue GPIO.add_event_detect(3, GPIO.RISING, callback=exitEmulator, bouncetime=500) while True: sleep(10)
Now create another file, exit.sh in /home/pi/
#!/bin/sh cd / cd /home/pi sudo python exit.py cd /
Now, cron must be edited. Enter in a command terminal,
sudo crontab -e
Add the following line to the end,
@reboot sh /home/pi/exit.sh
Some of the code above has been gathered from around the web. –this link– shows how to exit all emulators too, however if done by this method, it is a hard exit because sigkill is used. This can cause all sorts of headaches like SRM’s not being saved, high scores not being saved using mame, etc… So I modified it to use sigterm. However, mame4all doesn’t accept sigterm signals and I certainly didn’t want to use a sigkill, so then I added uinput KEY_ESC to exit out of mame4all and it works.
The button works for me as intended. I set up a pushbutton attached to GPIO 3 on a Raspberry Pi 2 and it exits the emulator cleanly to emulationstation. Because I have it set to GPIO3, it also boots my Pi when it is shut off.
If there are any other questions, I’ll do my best to answer them.dougie1970Participant07/10/2015 at 23:47Post count: 56
thanks for the info and can you take a pic of how to add the button to the GPIO .matthewjosephcordovaParticipant07/11/2015 at 19:24Post count: 22
It’s simple, just connect from GPIO3 to ground using a push button type button. Look at your rpi documentation for the correct pins. On the rpi2 b+ they are right next to each otherAnonymousInactive07/27/2015 at 15:48Post count: 3
thanks for sharing this.
i do not understand why gpio boots up your pi…what am i missing here?
- The forum ‘GPIO Adapter, ControlBlock etc.’ is closed to new topics and replies.