How to - Use the GPIO for Buttons instead of a Teensy

Various user-contributed guides for hardware-related things
prerunnerseth
Posts: 279
Joined: Fri May 06, 2016 5:36 pm
Has thanked: 16 times
Been thanked: 228 times

How to - Use the GPIO for Buttons instead of a Teensy

Post by prerunnerseth » Sat May 07, 2016 2:14 pm

This tutorial provided by http://www.prs-tech.com

Updated 4/6/2017

Update 7/24/2017
Changed the Start button to Enter instead of E

Most of this is copied from another tutorial.



This mapping skips the SPI pins used by the SPI TFT screens as well as the analog audio out pins so that it may be used with those screens attached and analog audio.

First of all you will need to solder the required pins to the GPIO of your Pi Zero. Here is a map of where to solder the pins. solder the ground to any of the ground pins on the GPIO.

Image


I suggest starting with a fresh image of retropie as this will use a certain set of keyboard keys that I preselected. With a fresh retropie install you will be able to map these key presses on first startup. map the following table of keys with your keyboard so that things will work with my Code. Then you can get in and setup network so you can use SSH and FTP.

Up - up
Down - down
left - left
right - right
start - enter
select - s
A - a
B - b
X - x
Y - y
L - l
R - r



Download the retrogame.c file at the google drive link below and place in a directory on your Pi Zero SD card.
https://drive.google.com/open?id=0B9I5H ... EhKUFJOb2M

I used FTP to get to my pi zero and made the Adafruit-Retrogame directory in the /home/pi directory. This is the directory referenced later in code in this tutorial. So its best to stick to this.

as of retropie 4.2 SSH is now disabled by default. to enable
press F4 on a keyboard then.

Code: Select all

sudo raspi-config
Select interfacing options >> SSH >> Enable >> reboot your pi

In the middle of this wiki it will show you how to FTP into retropie
https://github.com/retropie/retropie-se ... rring-Roms

In your FTP Client make sure you are in the pi directory then right click and click new then folder and name it Adafruit-Retrogame

Copy the retrogame.c file to the /home/pi/Adafruit-Retrogame directory on the SD Card using your preferred method. I found that FTP was the easiest for me

At this point you will need a terminal connection. I highly suggest using SSH as this will allow you to test run retrogame with the gui running on your device for button mapping testing.

Here is the wiki on how to SSH into retropie. Your pi zero will need to have a network connection for this to work.
https://github.com/RetroPie/RetroPie-Setup/wiki/SSH



type:

Code: Select all

cd /home/pi/Adafruit-Retrogame
then type:

Code: Select all

make retrogame
This should build the executable retrogame utility. If you instead get an error message, there’s a problem in the edited table — most likely a missing curly brace, comma or semicolon.
One more step is to create a custom udev rule which will allow retrogame input events to be visible to applications built using the latest SDL2 library. Some applications like the latest version of RetroPie's EmulationStation frontend require SDL2 and can't see input events from retrogame without this rule. It can't hurt to add this rule even if you aren't using EmulationStation or other SDL2 apps.

To add the rule, run the following command to create and edit the file /etc/udev/rules.d/10-retrogame.rules in the nano text editor:

Code: Select all

sudo nano /etc/udev/rules.d/10-retrogame.rules
Now copy in the following line exactly as shown (this should all be on a single line in the rules file, i.e. don't put in any linebreaks):

Code: Select all

SUBSYSTEM=="input", ATTRS{name}=="retrogame", ENV{ID_INPUT_KEYBOARD}="1"
Save the file by pressing Ctrl-O and pressing enter, then exit nano by pressing Ctrl-X.

Restart your Raspberry Pi to make sure the new configuration is loaded.

Code: Select all

sudo reboot
Now we’re in good shape to test it!

Navigate back to the Adafruit-Retrogame directory

Code: Select all

cd /home/pi/Adafruit-Retrogame
Retrogame needs to be run as root, i.e.:

Code: Select all

sudo ./retrogame
Give it a try. If it seems to be working, press control+C to stop the program and we’ll then set up the system to launch this automatically in the background at startup.

Code: Select all

sudo nano /etc/rc.local
Before the final “exit 0” line, insert this line:

Code: Select all

/home/pi/Adafruit-Retrogame/retrogame &
Save the file by pressing Ctrl-O and pressing enter, then exit nano by pressing Ctrl-X

If you placed the software in a different location, this line should be changed accordingly. “sudo” isn’t necessary here because the rc.local script is already run as root.

Reboot the system to test the startup function:

Code: Select all

sudo reboot
The software will now be patiently waiting in the background, ready for use with any emulators.

Each emulator will have its own method for configuring keyboard input. Set them up so the keys match your controller outputs. Up/down/left/right from the arrow keys is a pretty common default among these programs, but the rest will usually require some tweaking.
Last edited by prerunnerseth on Mon Jul 24, 2017 10:01 pm, edited 9 times in total.

User avatar
crispy_tofu
Posts: 340
Joined: Thu May 05, 2016 1:51 am
Location: Australia
Been thanked: 5 times

Re: How to - Use the GPIO for Buttons instead of a Teensy

Post by crispy_tofu » Sat May 07, 2016 6:37 pm

This is very informative, thanks! :)

User avatar
wermy
Site Admin
Posts: 1344
Joined: Tue May 03, 2016 8:51 pm
Has thanked: 617 times
Been thanked: 1319 times
Contact:

Re: How to - Use the GPIO for Buttons instead of a Teensy

Post by wermy » Sun May 08, 2016 1:52 pm

Nice, thanks! Sticky-ing. :)
ImageImageImageImage

Retrohead95
Posts: 80
Joined: Thu May 05, 2016 6:00 pm
Has thanked: 3 times
Been thanked: 4 times

Re: How to - Use the GPIO for Buttons instead of a Teensy

Post by Retrohead95 » Sun May 08, 2016 5:27 pm

Was always curious about this. If the controller can be wired to the gpio pins why use a teensy?

User avatar
crispy_tofu
Posts: 340
Joined: Thu May 05, 2016 1:51 am
Location: Australia
Been thanked: 5 times

Re: How to - Use the GPIO for Buttons instead of a Teensy

Post by crispy_tofu » Sun May 08, 2016 7:55 pm

Retrohead95 wrote:Was always curious about this. If the controller can be wired to the gpio pins why use a teensy?
A teensy makes it easier to do this, and it might eliminate any compatibility problems with the emulators. :)

Retrohead95
Posts: 80
Joined: Thu May 05, 2016 6:00 pm
Has thanked: 3 times
Been thanked: 4 times

Re: How to - Use the GPIO for Buttons instead of a Teensy

Post by Retrohead95 » Sun May 08, 2016 8:44 pm

crispy_tofu wrote:
Retrohead95 wrote:Was always curious about this. If the controller can be wired to the gpio pins why use a teensy?
A teensy makes it easier to do this, and it might eliminate any compatibility problems with the emulators. :)
Thanks for explaining. Definitely going with the teensy then :)

User avatar
1461748123
Posts: 143
Joined: Wed May 04, 2016 8:36 pm
Has thanked: 9 times
Been thanked: 17 times

Re: How to - Use the GPIO for Buttons instead of a Teensy

Post by 1461748123 » Sun May 08, 2016 11:29 pm

Well done man! :D I kinda wanna use GPIO as well at first but I went for teensy instead.

mynameisbambambam
Posts: 2
Joined: Mon May 09, 2016 1:19 pm

Re: How to - Use the GPIO for Buttons instead of a Teensy

Post by mynameisbambambam » Mon May 09, 2016 1:48 pm

how can i do it if i use l1, l2 , r1 and r2 ?

prerunnerseth
Posts: 279
Joined: Fri May 06, 2016 5:36 pm
Has thanked: 16 times
Been thanked: 228 times

Re: How to - Use the GPIO for Buttons instead of a Teensy

Post by prerunnerseth » Mon May 09, 2016 6:22 pm

mynameisbambambam wrote:how can i do it if i use l1, l2 , r1 and r2 ?
There are a few more un-used GPIO pins and many more if youre not using a SPI bus display. you can modify the table in the C file i sent. Its really not hard. the program will tell you when you run it if there is a conflict.

User avatar
dirtybeagles
Posts: 385
Joined: Thu May 05, 2016 6:04 am
Has thanked: 34 times
Been thanked: 63 times

Re: How to - Use the GPIO for Buttons instead of a Teensy

Post by dirtybeagles » Sun May 29, 2016 10:49 pm

I just completed mine. Thanks for writing this up. Worked without any problems for me. :D

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest