RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login

    GPIO button to exit emulator

    Scheduled Pinned Locked Moved Ideas and Development
    gpioexit emulatorbutton
    27 Posts 7 Posters 7.9k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • hansolo77H
      hansolo77 @cyperghost
      last edited by

      @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!

      Who's Scruffy Looking?

      1 Reply Last reply Reply Quote 0
      • 3
        3drinksahead @cyperghost
        last edited by

        @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.

        cyperghostC 1 Reply Last reply Reply Quote 0
        • cyperghostC
          cyperghost @3drinksahead
          last edited by cyperghost

          @3drinksahead I add the button code always to /opt/configs/retropie/autostart.sh just before the emulationstation #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
          
          3 1 Reply Last reply Reply Quote 0
          • 3
            3drinksahead @cyperghost
            last edited by 3drinksahead

            @cyperghost so just to play back the entire process (again very new to all this so assume I know nothing)

            1. create directory home/pigaming/scripts
              (How do I do this?)

            2. Create 'button.py'
              (Again, how do I do this?)

            3. 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?

            1 Reply Last reply Reply Quote 0
            • cyperghostC
              cyperghost
              last edited by cyperghost

              @3drinksahead Step by step

              1. mkdir $HOME/scripts
              2. cd $HOME/scripts
              3. wget https://raw.githubusercontent.com/crcerror/XU4-ORA-scripts/master/reset_button.sh
              4. chmod +x reset_button.sh
              5. nano /opt/configs/retropie/all/autostart.sh
              6. 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.

              3 1 Reply Last reply Reply Quote 0
              • 3
                3drinksahead @cyperghost
                last edited by

                @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?

                cyperghostC 1 Reply Last reply Reply Quote 0
                • cyperghostC
                  cyperghost @3drinksahead
                  last edited by cyperghost

                  @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.

                  3 1 Reply Last reply Reply Quote 0
                  • 3
                    3drinksahead @cyperghost
                    last edited by

                    @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

                    cyperghostC 1 Reply Last reply Reply Quote 0
                    • cyperghostC
                      cyperghost @3drinksahead
                      last edited by cyperghost

                      @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

                      3 1 Reply Last reply Reply Quote 0
                      • 3
                        3drinksahead @cyperghost
                        last edited by

                        @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?

                        1 Reply Last reply Reply Quote 0
                        • cyperghostC
                          cyperghost
                          last edited by cyperghost

                          @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 here wget 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

                          3 1 Reply Last reply Reply Quote 0
                          • 3
                            3drinksahead @cyperghost
                            last edited by

                            @cyperghost great, so to play it back

                            1. Install WiringPi and your script to get a readout of my GPIO pins and how they interact with my switch.

                            2. (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?

                            3. If above is right, then edit the original script to change pin 24 to whichever pin I determined via testing above

                            cyperghostC 1 Reply Last reply Reply Quote 0
                            • cyperghostC
                              cyperghost @3drinksahead
                              last edited by

                              @3drinksahead said in GPIO button to exit emulator:

                              @cyperghost great, so to play it back

                              1. 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 again gpio readall so you identify what changes by button press.

                              1. (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.

                              1. 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.

                              1 Reply Last reply Reply Quote 0
                              • J
                                Johny
                                last edited by Johny

                                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:

                                1. Relax, this will work, you will need some patience.

                                2. Create the exit.sh file in /home/pi using:
                                  nano /home/pi/exit.sh

                                3. 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
                                
                                1. Make it executable using:
                                  cd /home/pi/
                                  chmod +x exit.sh

                                2. Create the resetbutton.py script under /home/pi/scripts using:
                                  mkdir /home/pi/scripts
                                  nano /home/pi/scripts/resetbutton.py

                                3. 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()
                                
                                1. Now to add the resistor follow the step from the video below, pause at 3:12

                                1. 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 ;)

                                1 Reply Last reply Reply Quote 0
                                • J
                                  Johny
                                  last edited by Johny

                                  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:

                                  1. Create a script that will be executed at long press
                                    nano /home/pi/switch.sh

                                  2. Paste the below code and save using CTRL + X, Y

                                  #!/bin/bash
                                  
                                  sudo reboot
                                  
                                  1. Make it executable:
                                    chmod +x /home/pi/switch.sh

                                  2. Edit the code in the first mentioned post resetbutton.py:

                                  nano /home/pi/scripts/resetbutton.py

                                  1. 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 ;)

                                  1 Reply Last reply Reply Quote 0
                                  • First post
                                    Last post

                                  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.