www.NewsDownload.co.uk Page 12

Raspberry Pi MAME GPIO Joystick

2012-11-24 13:20 By Jason Birch

How to configure and use the Raspberry Pi GPIO pins as a joystick in MAME. This article describes how to make a MAME hand held console with the Raspberry Pi.

The video here demonstrates the project which this article describes how to build.

The article breaks the project down into several stages:

  • Hardware
  • XMAME software
  • Menu software
  • Configuring the OS
  • Configuring XMAME

The requirements for the hardware are to have a set of switches and to provide enough for a player to control a standard MAME game. This involves the following switches:

GPIO Pin Allocation
One Player Start GPIO 9
Two Player Start GPIO 22
Credit GPIO 4
Exit Game GPIO 18
Down GPIO 11
Left GPIO 10
Right GPIO 7
Button 1 GPIO 17
Button 2 GPIO 2
Button 3 GPIO 15
Button 4 GPIO 27
Button 5 GPIO 3
Button 6 GPIO 14

Circuit for each switch of a GPIO pin

The hardware is very simple, the circuit required for each switch is identical. However, because there are fourteen switches, the wiring required on tri-pad board is quite a lot. There is a 10K pull up resister on each circuit to ensure the default state of the switch when open is high. And there is a 1K resistor on each switch to restrict the current flowing from the GPIO pin when the switch is closed, in case the pin has been configured as an output.
The switches are the only components mounted on the top of the tri-pad board, so that the lid fits flush with the board. This is to keep the profile of the enclosure as low as possible. The board is cut to fit around the tall components on the Raspberry Pi board, again to keep the profile of the overall case as low as possible.
The resistors are mounted on the lower side of the tri-pad board. Though there are fourteen switches to wire up and many wires are required, the layout management below the board is still relatively easy. The headers are just the right height to mount the board on the Raspberry Pi, I have added the P6 pins for support of the opposite side of the board to the GPIO pins.
The Raspberry Pi is mounted in the lower part of the case using the mounting holes on the Rev 2 Raspberry Pi.
The tri-pad board mounts directory on the header pins which hold the board in place. The direction I have located the switches means the HDMI port is at the top. On reflection I will do this the other way around when the Model A Raspberry Pi is available, in the hope to mount a PAL LCD display and audio amp on top of the composite and audio sockets. To attempt to take advantage of the lower power of the Model A, and add a battery to make a portable gaming console.
With the lid in place the console is a very compact gaming device with lots of potential.

XMAME software
A full description of how to build the XMAME source code can be found in the following article. The source code has been modified to add a Raspberry Pi GPIO joystick driver, and the configuration files for the build have been modified to make the build very simple.
Raspberry Pi MAME & MESS Build Article

Only one file has been added to the source code for the Raspberry Pi GPIO code, the file is:

The other modifications are to add the joy_RPi.c file to the configuration files and add a type 6 joystick to the configuration.

Menu software
When the Raspberry Pi is booted, there will be no keyboard or mouse connected. The device boots into this menu system which displays valid available game ROMs. The menu is controlled by the GPIO keys. I have made the source code for this menu system open source on GitHub. To retrieve the source code and build it use the following commands in a terminal session on the Raspberry Pi:
cd ~
rm master.zip
wget https://github.com/BirchJD/MameVT100/archive/master.zip
pacman -S unzip
unzip master.zip
cd MameVT100-master
make clean

Copy the application to the .xmame directory:
cp MameVT100 ~/.xmame/

The menu application starts up very quickly in general use by separating the process for scanning valid ROMs into a manual process which should be run whenever the ROMs in the roms directory are altered. Run the scan now using the following commands:
cd ~/.xmame/
./MameVT100 /root/.xmame/ /root/.xmame/roms/ "|correct|best available|" RESCAN

The menu system can also be controlled from a keyboard using the cursor keys, '1' to select an item and '2' to exit.

If there are no valid ROMs present, the menu will be a blank screen, pressing '2' to exit the menu system will still work in this event.

Configuring XMAME
To configure the Raspberry Pi default keys, connect a USB keyboard and start a game from the menu. When the game is running, press the tab key, this will display the MAME configuration menu. Select Input (general), in this menu the default keys can be set for all games. Select User Interface. Define the Raspberry Pi keys for the user interface by selecting an item using the keyboard, press enter and then press the key on the Raspberry Pi for that item. This stage is a little confusing, as the keys are defined on the Raspberry Pi, from that point use the key on the Raspberry Pi rather than the keyboard. Set the keys for items UI Up, UI Down, UI Left, UI Right, UI Select, UI Clear. From now on the keys on the Raspberry Pi can be used to perform the remaining configuration. Press the exit key (the key which was just defined as UI Clear).

Now select Player 1 Controls. Set the keys for P1 Up, P1 Down, P1 Left, P1 Right, P1 Button 1, P1 Button 2, P1 Button 3, P1 Button 4, P1 Button 5, P1 Button 6, P1 Start, P1 Select. I set P1 Start, P1 Select to be the same as P1 Button 1. Press the exit key.

Now select Other Controls. Set the keys for 1 Player Start, 2 Player Start, Coin 1 . Press the exit key three times.

The controls should now be configured for use in MAME.


MAME "Input (general)" Menu

MAME "User Interface" Menu

MAME "Player 1 Controls" Menu

MAME "Other Controls" Menu

Configuring the OS
At this point the following files should be in the /root/.xmame/ directory:
xmame.SDL XMAME application.
MameVT100 ROM Menu application.
MameVT100.dat ROM Menu application session data, remembers last selected ROM between uses.
MameGameList.txt ROM Menu application list of all games which this version of MAME supports.
MameGameValid.txt ROM Menu application list of local ROM status for each ROM present which this version of MAME supports.
roms Directory containing ROMs which have been uploaded to the Raspberry Pi.

Configure the system to auto start the MameVT100 menu application, on login, by editing the .bash_profile file and adding to the end of the file the following lines. The last line will automatically shutdown the Raspberry Pi when the menu is exited. After this if you need to access the Raspberry Pi again, you can press ^C with a keyboard when the menu is displayed and the command prompt will be displayed.
cd ~
nano .bash_profile
cd ~/.xmame/
./MameVT100 /root/.xmame/ /root/.xmame/roms/ "|correct|best available|"
shutdown -h now

Configure the system to auto login at boot. This process is for Arch Linux, other Linux distributions probably require a different method to configure auto login.
cp /usr/lib/systemd/system/getty\@.service \

Use nano to edit the following file:
nano /etc/systemd/system/getty.target.wants/autologin\@tty1.service

Find the section [Service], and then modify the following line:
ExecStart=-/sbin/agetty --noclear -a root %I 38400
Find the section [Install], and then modify the following line:
Save the file and exit:

Now run the following commands:
systemctl daemon-reload
systemctl disable getty@tty1
systemctl start getty@tty1

Now reboot the system by typing reboot. When the system boots it should automatically login and start the menu application. The menu application will respond to Raspberry Pi GPIO key presses.

UPDATE 2012-12-10

Project Cost
Item Reference Qty Each Cost
Tactile switch 6x6mm - Square Button (Vertical) BitsBox 14 £0.11 £1.54
Caps for 6x6mm square button (Round) BitsBox 14 £0.09 £1.26
1K Resistor Carbon film 1/4W 5% BitsBox 14 £0.03 £0.42
10K Resistor Carbon film 1/4W 5% BitsBox 14 £0.03 £0.42
Kynar Wire - 5m length BitsBox 1 £1.55 £1.55
Single Row Sockets 0.1" pitch (14-way) BitsBox 2 £0.40 £0.80
Stripboard 127 x 95mm 50 holes x 36 holes BitsBox 1 £1.59 £1.59
Pozi Pan Head Machine Screws M2.5 10mm BitsBox 2 £0.03 £0.06
Nuts M2.5 BitsBox 2 £0.02 £0.04
Washers M3 BitsBox 4 £0.025 £0.10
BitsBox Shipping BitsBox 1 £1.75 £1.75
Playing Card Case eBay 1 £0.99 £0.99
SUB TOTAL £10.52
Raspberry Pi Farnell (element 14) 1 £33.16 £33.16
TOTAL £43.68