Main > Raspberry Pi & Dev Board

Retropie SNES, GPIO reset switch how to.

<< < (7/7)

severdhed:

--- Quote from: severdhed on August 31, 2015, 09:21:16 am ---ok...I'm not sure which OS build it is, i downloaded the retropie v3 sd card image.

i am following the post in this link...

https://www.reddit.com/r/raspberry_pi/comments/2yw4fn/finally_set_up_retropie_complete_with_a_gpio/

that slippyblade pointed me to in the second post of this thread.


about 3/4ths of the way down the page, there is a post by b4ux1t3 that lists the instructions.  it contains:

escape.py
from time import sleep
import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def exitEmulator(channel):
    print('exitEmulator')
    pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]

    for pid in pids:
        try:
            commandpath = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read()
            if commandpath[0:24] == '/opt/retropie/emulators/':
                os.system('kill -QUIT %s' % pid)
                print('kill -QUIT %s' % pid)
        except IOError:
            continue
GPIO.add_event_detect(17, GPIO.RISING, callback=exitEmulator, bouncetime=500)

while True:
    sleep(10)


escape.sh
#!/bin/sh

cd /
cd /home/pi
sudo python escape.py
cd /


Add this to cron (sudo crontab -e)
@reboot sh /home/pi/escape.sh


but there were no instructions on how to do this.  I eventually figured out how to create the files and here is what i did:

connected to pi via SSH using putty
cd /home
cd pi

nano escape.py

entered the contents listed in the link

saved it

nano escape.sh

entered the contents listed in the link

saved it

sudo crontab -e

entered the line listed in the link

saved it

rebooted

according to this GPIO pinout diagram, the 5th pin on the left column is a ground, and the 6th pin is GPIO17.  so i connected a standard arcade microswitch to these two pins. (i used pin 17, because from what I can tell, that is the pin referenced in the script, if I am understanding it correctly)

I then launch a game and press the reset button on the console, but nothing happens.

so, what am I doing wrong?  do i need to wire the switch up differently?  i looked around and i see various information, some people say you need a circuit with a pull up resistor (no idea what that is).

i created an account on the site where that was originally posted and sent the original poster a message last evening....perhaps they will be able to shed some light on it.  any help would be appreciated. 

it isn't a huge deal breaker, i can exit the games using the start+select buttons on the gamepad, but it feels stupid to have that switch there and not serve a purpose.



--- End quote ---

yes, i did create these scripts, but it is like they arent running or something

DaOld Man:
According to the script, it looks like the Pi has internal programmable pull up/pull down resistors.

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

Ive seen other processors that have this ability, didnt know if Pi did or not.
Try taking the  1k resistor to ground out of the circuit and see what happens. It could be keeping the input pulled low, not real sure, but its worth a try.

Im not real sure about what I just said. Someone with more experience with the Pi needs to chime in.
Maybe the script is saying the pin needs to go low, which would mean connect to ground.
You can try connecting the switch to pin 17 and ground, and the resistor in pull up mode connected to pin 17 and +
That would reverse what you currently have, switch open pin= 17 high, switch closed= pin 17 low

Looks like I might be right about the pull up/pull down.
Some research found this:

What’s all this about internal ones then?
In our button circuit, we used resistors to pull down the voltage. This was to demonstrate the idea.
But, in fact, the Raspberry Pi has built-in pull-up and pull-down resistors which can be enabled in software. This means we can eliminate our pull-down resistors for the button – as long as we enable the internal ones.
How do you do that? RPi.GPIO to the rescue.
You enable these internal pull-ups/pull-downs at the time of setting up the port for input or output, by adding an extra, optional, argument to the GPIO.setup() function call.
We’re using pull-down, so it’s pull_up_down=GPIO.PUD_DOWN. If you want/need pull-up you can change the PUD_DOWN for PUD_UP. (It depends on how you’ve wired your circuit.)
GPIO.setup(port_or_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

More I learn about this little computer, more I like it.

severdhed:
I finally got this working!!!  The problem was with the scripting the entire time, and my basic lack of understanding of what was going on.  Since this wasn't working, i ended up re-evaluating what i wished to accomplish. Instead of having the reset switch exit the emulator like i originally had planned, i decided it would be better served to send a safe shutdown command instead.  that way i can still exit the games via the gamepad, but before i turn the power switch off, i simply press the reset switch and wait for it to shutdown gracefully.  to do that, i needed a different script, which i found. but even that wouldn't work.  things really started coming together when i tried running the script manually via the ssh terminal.  that is when i started seeing errors...specifically,after the line: import RPi.GPIO   i would get     ImportError: No module named GPIO


as it turns out, i needed to install this as a separate component.  once that was done, i ran the new shutdown script manually and got it to successfully shut down.  this script was quite basic, it sets pin 17 as pull up, which means i can wire a standard switch to pins 17 and ground, with no resistors, and it just works.    The big problem i had was getting the script to run, without messing up emulation station.  The original article told me to make an entry calling the script in the /etc/rc.local  file, which i did. the problem is, when i booted it up, i'd get the emulation station splash screen, then it would just go to a console window with a bunch of 1s repeating down the screen.  so i had to remove that entry.  on another forum, they suggested adding it in something called crontab....calling my python script directly there caused everything to work.  here is the script i used:



import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down = GPIO.PUD_UP)
while True:
    print GPIO.input(17)
    if(GPIO.input(17) == False):
        os.system("sudo shutdown -h now")
        break
    time.sleep(1)

I am going to edit the first post of this thread with accurate, step by step instructions on how i got this working, in case anyone else needs it down the road.

Thank you all for your help with this project.

Slippyblade:

DaOld Man:
I havent checked out Slippys link yet, so it might have already been answered, but I think the reason you were seeing the ones is because of the:

print GPIO.input(17)

in the script.
This prints the state of the pin to the screen, which since you are pulling it up, is 1.
Although handy for trouble shooting, this line should be deleted or commented out before regular use.

Good work severdhed, I am learning a lot from your work. I think if we can figure how to turn on and shutdown with the power switch we wont need to buy that mausberry switch circuit, but the mausberry does make it a lot easier, especially for beginners like me.

EDit: just watch the video Slippy linked. Did not shed light on the 1s being displayed, but did give me a warm fuzzy feeling. LOL

Navigation

[0] Message Index

[*] Previous page

Go to full version