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 8.1k 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.
    • 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.