uinput-pigo-mapper – Turning PiGo's 16 GPIO Buttons Into Linux Input Events

Why GPIO Buttons Need a Userspace Input Mapper
To keep the PiGo carrier PCB compact, affordable, and solder-free, all 16 tactile buttons are
wired directly to Raspberry Pi GPIO pins rather than through a separate USB HID
microcontroller. From Linux's perspective, however, raw GPIO pins are not a keyboard,
gamepad, or mouse — Wayland, SDL2, Godot, LÖVE2D, PICO-8, and classic open-source ports all
expect standard Linux input events from /dev/input/event*.
You could map GPIO pins statically in the kernel using gpio-keys, but that
breaks down quickly on a multi-engine handheld. One game expects arrow keys and
Z/X, another expects WASD and Enter, and
strategy or point-and-click indie games (such as PicoWar or LÖVE2D mouse titles)
need the D-pad to move a mouse cursor with face buttons acting as left,
right, and middle mouse clicks.
uinput-pigo-mapper
solves this cleanly by translating PiGo's GPIO inputs into a virtual Linux input device
("PiGo device") whose key and mouse bindings can be reconfigured on the fly via
command-line arguments.
Purpose: Dynamic Keyboard & Mouse Emulation
With a single CLI invocation, uinput-pigo-mapper can behave as a standard
keyboard, a mouse, or a hybrid of both. That means any Linux game — even one that has zero
gamepad support — becomes immediately playable on PiGo's physical buttons without modifying
a single line of the game's source code.
How the Code Is Structured
The entire mapper lives in a compact, self-contained 223-line Python script
(main.py) built on RPi.GPIO and python3-libevdev. It
is structured into four clear stages:
1. Hardware Pinout Table
At the top of main.py, the buttons list defines every physical
switch on the PiGo PCB, pairing its BCM GPIO pin number with a human-readable logical name
and default Linux event code:
-
D-Pad:
UP(BCM 22),DOWN(BCM 4),LEFT(BCM 17),RIGHT(BCM 27). -
Face Buttons:
A(BCM 23),B(BCM 1),X(BCM 25),Y(BCM 12). -
System & Menu Buttons:
START(BCM 20),SELECT(BCM 26),HOME(BCM 0). -
Shoulder Buttons:
SL1(BCM 15),SR1(BCM 16),SL2(BCM 14),SR2(BCM 24).
2. Argument Parsing (2-Part Keyboard vs. 3-Part Mouse Rules)
When main(args) starts, it splits each command-line argument on =
and matches either the button's logical name or its raw BCM pin number:
-
BUTTON=EVENT(2 parts): Overridestarget_evwith anyEV_KEYsymbol such asKEY_ENTER,KEY_ESC, orBTN_LEFT. -
BUTTON=REL_AXIS=DELTA(3 parts): Setstarget_evto a relative axis (REL_XorREL_Y) and stores the integer pixel step inmouse_move, allowing per-game cursor speed tuning.
3. Virtual uinput Device Registration
All button pins are configured as GPIO.IN with internal pull-down resistors
(GPIO.PUD_DOWN). Next, the script instantiates a libevdev.Device()
named "PiGo device", enables all KEY_*, BTN_*, and
REL_* event codes, and registers it with the kernel via
dev.create_uinput_device(). A deliberate 1-second startup delay ensures
udev, libinput, and the Sway Wayland compositor finish attaching to
the new virtual device before any events are emitted.
4. 100 Hz Polling Loop & Event Emission
The main loop scans all buttons every 10 ms (time.sleep(1 / 100)):
-
When a pin reads
GPIO.HIGH, the button is marked pressed and emits either a key-down event (1) forEV_KEYmappings or a continuous relative motion delta (mouse_move) forEV_RELmappings. -
When a previously pressed pin returns to
GPIO.LOW, it emits a release event (0). -
Every state change is flushed immediately with an
EV_SYN.SYN_REPORTsynchronization event viauinput.send_events(events), and afinally: GPIO.cleanup()block guarantees clean teardown when the process exits.
Role in the PiGo Handheld Ecosystem
uinput-pigo-mapper is the universal input bridge between PiGo's
physical buttons and everything running on PiGo OS. Rather than running with a single static
layout, it is dynamically managed by pigogui's ApplicationManager:
-
While browsing the launcher,
ApplicationManagerruns the mapper withdefaultKeyMap(D-pad as arrow keys,A=KEY_ENTER,B=KEY_ESC,HOME=KEY_DELETE). -
When you start a game,
ApplicationManagerterminates the old mapper instance and spawns a fresh one using the game's customkeymapfield fromgame.json, automatically appendingmandatoryKeyMap(HOME=KEY_DELETE SL1=KEY_F2 SL2=KEY_F3 SR1=KEY_F4 SR2=KEY_F5). -
When you press the Home button mid-game, the game pauses via
SIGSTOPand the mapper switches back todefaultKeyMapso you can navigate the Quick Menu, then restores the game's keymap as soon as you resume.
That tight coordination with pigogui and pigo-games is what makes
both keyboard-driven retro ports and mouse-driven indie games feel completely native on the
PiGo Handheld.
