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.
    • K
      Kalidor
      last edited by Kalidor

      Just to summarise to see if I do this correctly:

      1. Create directory 'pi' on root
      2. create 'exit.sh'
      3. type or past into exit.sh:
      #!/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. create directory home/pi/scripts
      2. Create 'resetbutton.py'
      3. 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)
      
      1. add an entry to your /etc/rc.local

      2. test with:

      python /home/pi/scripts/resetbutton.py &
      

      and it should run right?


      I seem to get this error:
      alt text

      cyperghostC 1 Reply Last reply Reply Quote 1
      • cyperghostC
        cyperghost @Kalidor
        last edited by cyperghost

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

        Please update the emucall="$(sed -n 4p /dev/shm/runcommand.info | tr -d '\\"' | tr '^$[]*.()|+?{}' '.')" with emucall="$(sed '4!d; s/\([\\"]\|[[:alnum:]_]\+=[^ ]* \)//g; s/[][(){}^$*.|+? ]/\\&/g' /dev/shm/runcommand.info


        EDIT: LOL
        please make exit.sh executable chmod +x exit.sh

        what does happen you run ./exit.sh if a emulator is running? Does it exit?

        1 Reply Last reply Reply Quote 0
        • K
          Kalidor
          last edited by Kalidor

          it was permissions on exit.sh.

          I used:

          sudo chmod 777 exit.sh (ill set to better permission later)

          I'm completely new to linux, so didn't realise this would be an issue.

          Thank you very much for your help, I truly appreciate it.

          lostlessL 1 Reply Last reply Reply Quote 0
          • lostlessL
            lostless @Kalidor
            last edited by

            @kalidor ya you need to make sure the exit.sh is executable.

            1 Reply Last reply Reply Quote 0
            • hansolo77H
              hansolo77
              last edited by

              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.

              Who's Scruffy Looking?

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

                @hansolo77 you can take a look to my github there I've an exit script for Xu4 and its OTG case

                hansolo77H 3 2 Replies Last reply Reply Quote 1
                • 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
                                            • 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.