Skip to content

Instantly share code, notes, and snippets.

Last active February 16, 2024 15:22
Show Gist options
  • Save savetheclocktower/c7644597c9c4c3990b7fee363d5f03c1 to your computer and use it in GitHub Desktop.
Save savetheclocktower/c7644597c9c4c3990b7fee363d5f03c1 to your computer and use it in GitHub Desktop.
Poor-man's LEDBlinky with RetroPie and Pac-Drive

NOTE: This Gist was an early write-up of this blog post, part of what became an eleven-part series on my arcade cabinet. I'd suggest you read that post instead of this, but some of the comments on this Gist contain updates and field reports that you might find useful.

RetroPie, LED control, and you

I wanted LEDBlinky-style functionality out of my RetroPie cabinet. But I didn't need RGB control or magical frontend integration or anything like that. I had buttons with simple single-color LEDs.

I've got a simple control panel with six buttons per player. All I wanted was this:

  • When I launch Street Fighter 2, all twelve buttons should light up.
  • When I launch The Simpsons, only the first two buttons for each player should light up.
  • When I launch Pac-Man, none of the buttons should light up.

You get the idea.

Here's a demonstration in the form of a shaky video.

Here's how I did it.


I got a Pac-Drive from Ultimarc. It's got 16 terminals for LEDs. 1–6 control Player 1's buttons; 7–12 control Player 2's buttons. The remaining four terminals got devoted to the start buttons (13 and 14), the coin buttons (both on 15), and four small buttons I use for functions (all on 16).

After all that's wired up, all you need is a single USB cable running to your Pi — or, even better, to a powered USB hub that is itself connected to the Pi. The LEDs get powered from USB.


Most of the software for the Pac-Drive is meant for a Windows environment. After some digging, I found this third-party utility for Linux that allows for controlling a Pac-Drive from the command-line. It was exactly what I needed, though it was eight years old and took some wrangling to get installed on a Pi.

From a stock RPi with Raspbian Jessie, you'll need at least these packages:

sudo apt-get install libusb-dev build-essential

You'll also need libhid, which is hard to find because it's deprecated. There's no package for it in APT, the source code is behind a login for some reason, and then you've got to make a change to the source to get it to compile on the Pi. I solved all three problems by applying that fix and then putting up the fixed source on my server.

From your home directory:

mkdir src && cd src

wget ""
tar xvzf libhid-0.2.16.tar.gz
cd libhid-0.2.16
sudo make install

Now we're ready to install the pacdrive utility.

cd ~/src
wget ""
tar xvzf pacdrive_0_2.tar.gz
cd pacdrive_0_2

If you do make install it'll put the pacdrive binary in /usr/bin, though you can edit the Makefile beforehand to specify a different directory. I did neither; I just manually copied the pacdrive binary to /home/pi/bin. (If you do this, make sure you put /home/pi/bin in your PATH.)

Hooking into RetroPie

So now you can control the LEDs from the command line, but that doesn't help much.

How will it know which LEDs to light up for which game?

One option here would be to use something called controls.dat, which is a project for cataloguing the controls for most MAME games. (4-way joystick? 8-way? How many buttons? How many players? Hotseat, or does each player have their own controls? And so on.)

The problem with this approach is that (a) the controls.dat project is dormant; (b) there are lots of gaps in its database.

If you're anything like me, you don't need a comprehensive solution. I don't care how many buttons some random mah-jongg game uses because I'll never play it. I've got about 100 arcade games set up and I don't mind specifying their configs manually.

So here's what I came up with:

  1. Create a /home/pi/leds directory.
  2. Inside that directory, create another directory for each system you want to control. (I've got arcade and daphne; you might have others.)
  3. Inside each system's directory, define a text file whose name matches the name of the game. For instance: simpsons2p is the ROM name for the two-player version of The Simpsons, so I'd create a file called simpsons2p.cfg with these contents: 1, 2, 7, 8. In other words: light up buttons 1, 2, 7, and 8, and turn off all the other LEDs.

This way it's simple to define new configs and simple for RetroPie to know where to look for configs knowing only the system and the ROM name.

How will it light up the LEDs on game launch?

All emulator launches go through something called There's a new feature in RetroPie 4.x: the ability to define scripts called and These scripts will run before a game starts and after a game ends, respectively, and they receive some metadata, including the name of the system and the name of the game.

If you want to do it exactly how I did:

  1. cd /opt/retropie/configs/all
  2. nano (shouldn't need sudo, but I might be wrong)
  3. Paste the contents of the from this gist.
  4. Repeat steps 3 and 4 for

These scripts reference two other scripts called led-start and led-end, which you should put in /home/pi/bin. This is completely overengineered, but it lets me define some buttons as "always on" without having to include them in every single config file. That happens in a file called /home/pi/.ledrc. Here's what mine looks like:

+13, +14, +15, +16

That means "always light up both start buttons, the coin buttons, and the small function buttons." You can read the source of led-start to learn more.

If you want to use these scripts, you'll need Ruby, which isn't installed by default on Raspbian Jessie. sudo apt-get install ruby2.1 should take care of it.

What if a game doesn't have a config? Well, if you followed these instructions to the letter, none of the buttons for either player will light up. That's fine with me, because I don't have any games without configs. You might want it to do something different in that situation.


If this doesn't work, it's probably because I left something out. Leave a comment and we'll figure it out together.

#!/usr/bin/env ruby
# For now, let's just turn all LEDs back on.
exec(%Q[/home/pi/bin/pacdrive -a -q])
#!/usr/bin/env ruby
require 'optparse'
require 'pathname'
require 'pp'
$options = {
debug: false,
debug_path: '/home/pi/led_log.txt'
# Takes a string like "1,2,3,7,8,9" and determines which LEDs should be lit
# up. Shells out to the `pacdrive` binary to do it.
$usage = do |opts|
opts.banner = "Usage: led-start [OPTIONS] system game"
opts.separator ""
opts.on('-d', '--debug', "Writes debug output to the specified file (defaults to ~/led_log.txt).") do |path|
$options[:debug] = true
$options[:debug_path] = path unless (path.nil? || path.empty?)
opts.on_tail('-h', '--help', "Displays this help message.")
# The path to the file that sets defaults for LED state. If no config is
# found for a certain game, this file ends up determining all states. If a
# game config omits a certain button, the default config can specify that
# that button should nonetheless be on. (This is useful for, e.g., coin
# buttons, aux buttons, and such. These should almost always be on, but it's
# dumb to have to specify them in every game config file.)
DEFAULT_CONFIG_PATH ='/home/pi/.ledrc')
# The base directory where all configs are stored. Each system should be its
# own folder; each game should be a text file in that folder whose name
# matches the game's name and has a "cfg" extension.
BASE_CONFIG_PATH ='/home/pi/leds')
class LEDState
def initialize(path)
@contents = path.is_a?(String) ? path :
@button_map = {}
debug "config file contents: #{@contents}"
buttons = @contents.split(/,\s*/)
(1..16).each do |digit|
state = { value: 0, force: false }
if buttons.include?(digit.to_s)
state = { value: 1, force: false }
elsif buttons.include?("+#{digit}")
# "+x" syntax means we should force this button to be on.
state = { value: 1, force: true }
elsif buttons.include?("-#{digit}")
# "-x" syntax means we should force this button to be off.
state = { value: 0, force: true }
debug "State for button #{digit}:"
debug state
@button_map[digit] = state
def with_default(path)
debug "Combining with default config"
default =
def map
# Converts the state to the format expected by the `pacdrive` utility.
def to_s
str = (1..16).map do |digit|
state = '0'
data = @button_map[digit]
if data && data.has_key?(:value)
state = data[:value] == 1 ? '1' : '0'
binary = str.join('').reverse
"0x%04x" % binary.to_i(2)
# Calls the external `pacdrive` app to set the LEDs to the represented
# state.
# Uses `exec`, so once this method is called, we implicitly exit.
def apply!
exec("/home/pi/bin/pacdrive", "-q", "-s", to_s)
# For combining this state with another. The argument to this function is
# assumed to be a default, which means it will take precedence over this
# state when conflicts happen.
def merge!(default)
output = {}
default_map =
@button_map.keys.each do |digit|
own = @button_map[digit]
dflt = default_map[digit]
result = nil
if !dflt[:force]
# Our version always wins.
result = own[:value]
# Default wants to force a value...
if !own[:force]
# ...and we don't, so use the default.
result = dflt[:value]
# ...and so do we, so we win.
result = own[:value]
output[digit] = { value: result }
end # each
# Replace the button map with the merged version.
@button_map = output
end # merge
end # LEDState
def debug(str)
return unless $options[:debug]
`echo "#{str}" >> #{$options[:debug_path]}`
if ARGV.size == 1
# We've been given a string like "1,2,3,7,8,9" (probably for debugging).
# Ignore all configs and just create and apply an LED state.
debug "Given input: #{ARGV[0]}"[0]).apply!
elsif ARGV.size == 2
# We've been given a system and a ROM name. Look up its config file and
# turn it into a binary string.
system, game = ARGV
debug "Setting LEDs for game #{game} on system #{system}..."
game_config_path = BASE_CONFIG_PATH.join(system, "#{game}.cfg")
if game_config_path.exist?
state =
# No specific LEDs for this game, so just apply the default.
state =
puts $usage
# 1. System (e.g., "arcade")
# 2. Emulator (e.g. "lr-fba-next")
# 3. Full path to game (e.g., /home/pi/RetroPie/roms/arcade/
if [ -z "$3" ]; then
exit 0
# Gets the basename of the game (e.g., "wjammers")
game=$(basename $3)
/home/pi/bin/led-end "$system" "$game" >/dev/null
# 1. System (e.g., "arcade")
# 2. Emulator (e.g. "lr-fba-next")
# 3. Full path to game (e.g., /home/pi/RetroPie/roms/arcade/
if [ -z "$3" ]; then
exit 0
# Gets the basename of the game (e.g., "wjammers")
game=$(basename $3)
# Turn on the relevant LEDs for this game.
/home/pi/bin/led-start "$system" "$game" >/dev/null
Copy link

StevenLee75 commented Mar 1, 2023 via email

Copy link

StevenLee75 commented Mar 2, 2023 via email

Copy link

StevenLee75 commented Mar 3, 2023 via email

Copy link

whel i try to install it on a nieuw rsb 3+ build bad only get erros and dont have a idee how to fix it.
2 -Wall -W -Werror -MT linux.lo -MD -MP -MF .deps/linux.Tpo -c linux.c -fPIC -DPIC -o .libs/linux.o
In file included from linux.c:6:
../include/hid.h:5:10: fatal error: usb.h: No such file or directory
#include <usb.h>
compilation terminated.
make[1]: *** [Makefile:427: linux.lo] Error 1
make[1]: Leaving directory '/home/pi/src/libhid-0.2.16/src'
make: *** [Makefile:394: install-recursive] Error 1

pi@retropie:/src $ cd pacdrive_0_2
/src/pacdrive_0_2 $ make
/usr/bin/gcc -Wall -c -DLINUX pac_prog.c
In file included from pac_prog.c:5:
pac_prog.h:30:10: fatal error: hid.h: No such file or directory
#include <hid.h>
compilation terminated.
make: *** [Makefile:40: pac_prog.o] Error 1

can sombady please help my ?

Copy link

wel i fount the problem by the ./configure i nedit to put ./configure --disable-warnings now i can use pacdrive -f bad it wont change the leds when i start a game

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment