GPIO button to exit emulator
-
Just to summarise to see if I do this correctly:
#!/bin/bash # Terminate any emulatorcall! # This works just for RetroPie! emucall="$(sed -n 4p /dev/shm/runcommand.info | tr -d '\\"' | tr '^$[]*.()|+?{}' '.')" if [[ -n "$emucall" ]]; then emupid="$(pgrep -f "$emucall")" pkill -P "$emupid" kill "$emupid" sleep 4 fi
- create directory home/pi/scripts
- Create 'resetbutton.py'
- Paste or type into resetbutton.py:
import RPi.GPIO as GPIO import time # Define which pin you're using for the reset button (change this to whatever pin you use) resetBtn = 29 # 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 resetBtn pin to an input and enable the pull-up resistor GPIO.setup(resetBtn, GPIO.IN, pull_up_down = GPIO.PUD_UP) # Define a function which will be called when your reset button is pressed def interrupt_resetBtn(channel): # Print indication to console print "You pressed the reset button!" # Code for detecting/ending an # emulator would go here import os os.system('/home/pi/exit.sh') # Enable reset button interrupt to trigger on a falling edge (i.e. high-to-low transition) GPIO.add_event_detect(resetBtn, GPIO.FALLING, callback = interrupt_resetBtn, 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)
-
add an entry to your /etc/rc.local
-
test with:
python /home/pi/scripts/resetbutton.py &
and it should run right?
I seem to get this error:
-
@kalidor
Well ... read your text on your first point again!I think you have problems with user rights because this will end in a permission denied messagePlease update the
emucall="$(sed -n 4p /dev/shm/runcommand.info | tr -d '\\"' | tr '^$[]*.()|+?{}' '.')"
withemucall="$(sed '4!d; s/\([\\"]\|[[:alnum:]_]\+=[^ ]* \)//g; s/[][(){}^$*.|+? ]/\\&/g' /dev/shm/runcommand.info
EDIT: LOL
please make exit.sh executablechmod +x exit.sh
what does happen you run
./exit.sh
if a emulator is running? Does it exit? -
-
-
I know this topic is old. Has there been any updates to this procedure? I'm looking to do this exact same thing on my own build but I have one minor (major?) difference. I'm using an Odroid XU4. From their wiki I have determined the pin I want to use. I have a wire which I will connect to the pin when ready. The part I'm not sure about is the script for the exit. It says it only works with RetroPie. Is this 100% true or will it work on the THERA build (simply put, it's an "almost" 1:1 copy with some paths changed (pigaming vs pi and RetroArena vs RetroPie). I know how to make these changes in the scripts. I'd just like to know if anybody has done this with the XU4 yet and if the method presented here works.
-
@hansolo77 you can take a look to my github there I've an exit script for Xu4 and its OTG case
-
@cyperghost said in GPIO button to exit emulator:
@hansolo77 you can take a look to my github there I've an exit script for Xu4 and its OTG case
You always pull through! :) I'll take a look at it soon. Thanks!
-
@cyperghost thanks for this, think this is exactly what I need to get my reset button working.
Total noob question - where/how do I add the code from the reset button script?
I have a custom n64 case for my odroid xu4 (running theRA) and think I have the power button figured out...but still trying to figure out the reset button (which would be wired to a gpio pin) that when pressed, would exit whatever rom I'm in.
-
@3drinksahead I add the button code always to
/opt/configs/retropie/autostart.sh
just before theemulationstation #auto
line.For the ODROID you can use this script here. The script supports command line inputs for gpio button as default (for the OGST case pin 24 is assigned as reset button)
So a possible autostart would look like this (please don't forget the &-sign!)
# Start Resetbutton code /home/pigaming/scripts/button.sh & # Emulationstation emulationstation #auto
-
@cyperghost so just to play back the entire process (again very new to all this so assume I know nothing)
-
create directory home/pigaming/scripts
(How do I do this?) -
Create 'button.py'
(Again, how do I do this?) -
Paste or type your script into button.py
4 . Edit /opt/configs/retropie/autostart.sh by adding your "Start Resetbutton code...." above the emulationststion autostart line
Is that right?
-
-
@3drinksahead Step by step
mkdir $HOME/scripts
cd $HOME/scripts
wget https://raw.githubusercontent.com/crcerror/XU4-ORA-scripts/master/reset_button.sh
chmod +x reset_button.sh
nano /opt/configs/retropie/all/autostart.sh
- add the line
sudo $HOME/scripts/reset_button.sh &
above the line of emulationstation
I'm not sure if the pathes in pt. 5 are correct. I sold my XU4 a few weeks ago.... So I can't recheck. The XU4 and the OGST case was fine but the Raspberry is the better supported platform.
-
@cyperghost thanks. Followed the above steps (the path for step 5 where the autostart.sh file is was "...configs/all...")
But now when I start up, when emulationststion boots, I get the repeating message "cat: /sys/class/gpio/gpio24/value : No such file or directory"
Thoughts?
-
@3drinksahead Sorry my bad the line should be
sudo $HOME/scripts/reset_button.sh &
you need sudo command for exporting the GPIOs. I corrected the "guide" from posting above.
-
@cyperghost thanks for the quick replies, but now emulationstation continuously reboots with the message "terminated" popping up, without me pressing my reset switch.
To confirm, I have the reset switch hooked up to gpio #24 (using the zig-zagging naming convention,so 4th from the end) and one of the ground pins.
FWIW I tried removing the switch and am still getting the same effect where emulationstation is auto terminating
-
@3drinksahead does it work if you hold the reset button? The script was intended to work with the OGST case. You can remove the script with SSH because only ES starts over and over again.
Maybe the manufacturer changed something on the case -
@cyperghost oh, I'm using a custom 3d printed case, not the OGST one. I'm using my own button which is just connected to a standard reset button switch. Thought I could just change the gpio pin number to get it to work but I guess it's more involved than that?
-
@3drinksahead Now I understand. Yes that's the point. The GPIO in this script is setted to input state. And it's so that the GPIO is feed with current and is hold high (state 1). If you press the button then the circuit is closed and you receive a power drop. That is detected as low (state 0)... So technically you connect 3,3V directly to the GPIO (=state 1) and by cutting this connection you set state 0.
I think the easiest way is to use a libary like wPi... There is a good reference for the ODROID here and with this documents you will be able to realize your own switch.
I've written annother script for just reading the status of these GPIOs.
You can get with herewget https://raw.githubusercontent.com/crcerror/XU4-ORA-scripts/master/key14.sh
This will indicate what happens to your button. You may change line 10 to
while true; do
and then you see what happens with the GPIO if you press and release button.Download this and start with
sudo bash key14.sh
-
@cyperghost great, so to play it back
-
Install WiringPi and your script to get a readout of my GPIO pins and how they interact with my switch.
-
(Where I am a bit fuzzy) look for a gpio pin that would be essentially the opposite of the gpio pin 24, i.e. one where the current state is 0 and pressing the switch would put it to a 1 and thus run the script?
-
If above is right, then edit the original script to change pin 24 to whichever pin I determined via testing above
-
-
@3drinksahead said in GPIO button to exit emulator:
@cyperghost great, so to play it back
- Install WiringPi and your script to get a readout of my GPIO pins and how they interact with my switch.
Yes you see a list of the status of all GPIO if you type
gpio readall
Then you press the button and type againgpio readall
so you identify what changes by button press.- (Where I am a bit fuzzy) look for a gpio pin that would be essentially the opposite of the gpio pin 24, i.e. one where the current state is 0 and pressing the switch would put it to a 1 and thus run the script?
No the idea is behind is that you are confident in building a small circuit and show what happens if you press a button. For this purpose you can use the script I've posted. It suppors command line options so if you type
bash key14.sh 5
then you check GPIO 5 for some action.- If above is right, then edit the original script to change pin 24 to whichever pin I determined via testing above
Yes, you invoke wiringPi and readout status with
gpio read #PINNBR
. This would work in all means.
I'm not sure how python and the GPIO library is integrated to the XU4 device. This would be the kings way. All other methods I descriped make use of an external program that read out status of the GPIO and give output to the bash shell.
-
For who wants to make this work in parallel with power button, this will work and exit any ROM, as the guys mentioned, however a 10K resistor will be needed or your button will not detect that it is being released. Recommend watching the explanation in the first video, (I used exactly as in the video GPIO pin 16, Ground from GPIO 14, and 3.3V available on pin 1). I performed another step for OFF button already, clip at the end. You need to do the following:
-
Relax, this will work, you will need some patience.
-
Create the exit.sh file in /home/pi using:
nano /home/pi/exit.sh -
Paste the code below, save and exit (Ctrl X, Y).
#!/bin/bash # Terminate any emulatorcall! # This works just for RetroPie! emucall="$(sed -n 4p /dev/shm/runcommand.info | tr -d '\\"' | tr '^$[]*.()|+?{}' '.')" if [[ -n "$emucall" ]]; then emupid="$(pgrep -f "$emucall")" pkill -P "$emupid" kill "$emupid" sleep 4 fi
-
Make it executable using:
cd /home/pi/
chmod +x exit.sh -
Create the resetbutton.py script under /home/pi/scripts using:
mkdir /home/pi/scripts
nano /home/pi/scripts/resetbutton.py -
Paste the below code, save and exit (Ctrl X, Y):
#!/usr/bin/python import RPi.GPIO as GPIO import time import os # we will use the pin numbering to match the pins on the Pi, instead of the # GPIO pin outs (makes it easier to keep track of things) GPIO.setmode(GPIO.BOARD) GPIO.setup(16, GPIO.IN) # check to see if button has been pushed try: while True: if GPIO.input(16)==0: pass else: os.system("sudo /home/pi/exit.sh") time.sleep(4) finally: GPIO.cleanup()
- Now to add the resistor follow the step from the video below, pause at 3:12
- Reboot using:
sudo reboot
This is all, in case you do not already have a functional power button and python installed I do recommend checking the video below, it is what I did prior to the above steps.
Enjoy ;)
-
Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.
Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.