GPIO button to exit emulator
-
@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 ;)
-
-
Here is how to set Long press and Short press function. I added on my reset ROM button a long press feature that resets the entire system:
-
Create a script that will be executed at long press
nano /home/pi/switch.sh -
Paste the below code and save using CTRL + X, Y
#!/bin/bash sudo reboot
-
Make it executable:
chmod +x /home/pi/switch.sh -
Edit the code in the first mentioned post resetbutton.py:
nano /home/pi/scripts/resetbutton.py
- Replace all text with the below code, save using 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) seq_cust = 0 tim_cust = 0 # check to see if button has been pushed try: while True: if GPIO.input(16)==0: tim_cust = 0 if seq_cust > 0: os.system("sudo /home/pi/exit.sh") # Short press action seq_cust = 0 time.sleep(1) else: seq_cust = 0 else: tim_cust = tim_cust + 1 time.sleep(.01) if tim_cust >= 200: # Time for long press >= x os.system("sudo /home/pi/switch.sh") # Long press action tim_cust = 0 time.sleep(1) else: seq_cust = 1 finally: GPIO.cleanup()
I used 0.01 seconds to eliminate delay in button press.
At 200 should result in ~2 seconds.You can put your own script in switch.sh
Also the script supports more than 2 actions per button with a bit of tweak.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.