• Recent
  • Tags
  • Popular
  • Home
  • Docs
  • Register
  • Login
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.
  • L
    lostless
    last edited by 28 Sept 2017, 03:41

    Hello coders out there. I just got a nespi case and and want to use the reset button to act just like the reset button on the real nes classic. Just exit the emulator or program running and go back to ES. First thing connecting the button. Do just run it from a GPIO pin to ground? so when i press it, it shorts the pin to ground. Will that work?
    second @meleu and @cyperghost. I like how the script you guys built to exit the emulators and then shut down, I would like some help to write a script that reads this gpio and then just does the first part of the other script without the exit emulation station and shut down. Or if you guys, or anyone else, have any another other suggestions?

    C 1 Reply Last reply 28 Sept 2017, 03:49 Reply Quote 0
    • C
      cyperghost @lostless
      last edited by cyperghost 28 Sept 2017, 03:49

      @lostless
      I think the GPIO part is yours or annother user will help - but you can copy and paste the coding parts out of the shutdown scripts. The part posted above will just terminate the emulator. Maybe you can remove sleep part to get quicker response to ES.

      AFAIK you can direclty connect 3,3V to set high level to GPIO but it's better to use resistors to get a clean signal... (therefore the name PullUP/PullDOWN - resistor) and to check the change from HIGH-level to LOW-level by action. Because a change from LOW to HIGH can also be triggered by touching GPIO or via radiation caused by your mobile (afaik!)

      #!/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 Reply Last reply Reply Quote 2
      • H
        Heyoeyo
        last edited by Heyoeyo 10 Feb 2017, 00:45 1 Oct 2017, 23:37

        Just to follow up on the GPIO question.
        There are a few things you'll want to do to get the button working properly/safely.

        1. In software, configure your GPIO pin as an input.

        2. Either physically connect a resistor from your GPIO pin to 3.3V
          OR
          Configure (in software) your GPIO pin to use it's internal pull-up resistor.

        3. Connect your button from the GPIO pin to ground.

        Notes:

        • You definitely don't want to connect directly from the GPIO pin to 3.3V. If you do this, then pressing the button will create a short-circuit path from 3.3V to ground. That might damage your pi or cause the power to cycle or some other nastiness... or the 3.3V output might get current limited to the point of being harmless, but it's best not to tempt fate ;)
        • In case you're wondering about that connection to 3.3V, it's there to make sure the GPIO has a well defined value when the button isn't being pressed. It needs to be 3.3V (i.e. 'high') so that when the button is pressed (GPIO connected to ground), there's a clear transition from high to low on the pin
        • If you decide to hook up your own external resistor, the resistance value isn't too important. The bare minimum would be around 100 ohms, but it'd be much better to use 1000, 10000 or even 100000 to cut down on the current draw (just don't go too much higher or it starts looking like an open circuit and you can get some finicky behaviour)
        • If you happen to be using pin 5 (a.k.a BCM pin 3, see here) it seems to enable it's pull-up resistor by default so you can skip that part. I'm not sure if any other pins do that though (if you try it on an already enabled pin, you get a little print out message, so it's mostly harmless)

        As far as configuring the pins (set to input and pull-up enable) in software, I'm not sure how to do it with bash scripting, though I think it's possible. It's very easy to do with python (using RPi.GPIO library) but then I don't know bash well enough to understand the parsing done in cyperghost's script to find running emulators, so I can't really convert that part to python for you. However, here's the basic structure of a python script which configures a GPIO pin and sets it up to run a function ('interrupt_resetBtn') when the button is pressed (though missing the code to detect/end running emulators):

        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 = 7
        
        
        # 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
        
        
        # 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)
        

        You'd want to save that as a python file (i.e. resetbutton.py) somewhere on your pi and then add an entry to your /etc/rc.local file to have it run on startup in the background:

        python /path/to/the/python/file/resetbutton.py & 
        

        Edit:
        If you get really stuck trying to setup the GPIO pins with bash, you could have the python script call cyperghost's bash script to check/end running emulators.

        L 1 Reply Last reply 4 Oct 2017, 02:11 Reply Quote 2
        • L
          lostless @Heyoeyo
          last edited by lostless 10 Apr 2017, 03:18 4 Oct 2017, 02:11

          @heyoeyo @cyperghost Thank you you 2. I have a fully working reset switch. And at the same time learned something. (Dang you Raspberry PI foundation and wanting to teach about computers. All i wanted to do is play games LOL)
          so I ended up using this python script with @cyperghost bash shell file and it works. Ended up using this python command.

              # Code for detecting/ending an
              # emulator would go here
              import os
              os.system('/home/pi/exit.sh')
          

          The only issue I have is if i press the button in the terminal on the pi, it just sticks and never gives me control back of the terminal. Is there and "exit" command or something i'm missing or is is that just how python takes controll?

          Edit: Never mind. just had to press enter and "BOOM" exit. LOL

          C 1 Reply Last reply 4 Oct 2017, 04:21 Reply Quote 1
          • C
            cyperghost @lostless
            last edited by cyperghost 10 Apr 2017, 05:21 4 Oct 2017, 04:21

            @lostless To stay clear... this sniplet is part of the whole shutdown script. The emulator terminating part you are using was mostly written by @meleu.

            Fine you get it work!

            L 1 Reply Last reply 4 Oct 2017, 05:38 Reply Quote 0
            • L
              lostless @cyperghost
              last edited by 4 Oct 2017, 05:38

              @cyperghost well I will thank @meleu as well. I know he did that snipit. So thanks to all 3 of you 👍🏻

              1 Reply Last reply Reply Quote 0
              • K
                Kalidor
                last edited by Kalidor 14 Nov 2017, 19:25

                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

                C 1 Reply Last reply 14 Nov 2017, 19:39 Reply Quote 1
                • C
                  cyperghost @Kalidor
                  last edited by cyperghost 14 Nov 2017, 19:39

                  @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 14 Nov 2017, 20:19

                    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.

                    L 1 Reply Last reply 14 Nov 2017, 20:23 Reply Quote 0
                    • L
                      lostless @Kalidor
                      last edited by 14 Nov 2017, 20:23

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

                      1 Reply Last reply Reply Quote 0
                      • hansolo77H
                        hansolo77
                        last edited by 21 Apr 2019, 00:59

                        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?

                        C 1 Reply Last reply 21 Apr 2019, 10:31 Reply Quote 0
                        • C
                          cyperghost @hansolo77
                          last edited by 21 Apr 2019, 10:31

                          @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 22 Apr 2019, 22:03 Reply Quote 1
                          • hansolo77H
                            hansolo77 @cyperghost
                            last edited by 22 Apr 2019, 22:03

                            @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 29 May 2019, 21:14

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

                              C 1 Reply Last reply 30 May 2019, 04:57 Reply Quote 0
                              • C
                                cyperghost @3drinksahead
                                last edited by cyperghost 30 May 2019, 04:57

                                @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 30 May 2019, 15:15 Reply Quote 0
                                • 3
                                  3drinksahead @cyperghost
                                  last edited by 3drinksahead 30 May 2019, 15:15

                                  @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
                                  • C
                                    cyperghost
                                    last edited by cyperghost 30 May 2019, 16:50

                                    @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 31 May 2019, 04:23 Reply Quote 0
                                    • 3
                                      3drinksahead @cyperghost
                                      last edited by 31 May 2019, 04:23

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

                                      C 1 Reply Last reply 31 May 2019, 04:56 Reply Quote 0
                                      • C
                                        cyperghost @3drinksahead
                                        last edited by cyperghost 31 May 2019, 04:56

                                        @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 31 May 2019, 12:13 Reply Quote 0
                                        • 3
                                          3drinksahead @cyperghost
                                          last edited by 31 May 2019, 12:13

                                          @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

                                          C 1 Reply Last reply 31 May 2019, 14:07 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.

                                            [[user:consent.lead]]
                                            [[user:consent.not_received]]