Build an Emergency RetroPie (RetroArch/ EmulationStation) Controller Configuration Reset Button
It seems that every time a friend comes over to play games on RetroPie, they invariably screw up the controller settings somehow. Sometimes they'll accidentally press [any] button when a game is loading, panic, and then change the controller settings for a specific ROM. Other times, they'll plug a new controller in and map it incorrectly.
Suddenly, you can't even use the controller to navigate the RetroPie interface correctly. Either way, resetting controller settings for a specific ROM or controller often requires SSHing into the Pi, pulling out the old keyboard, or hoping another controller will work. Not anymore!
Now you can reset all controls, restoring RetroArch/EmulationStation controller mappings to default, with the press of a physical button. :)
How it works
In a nutshell, a normally open (NO) button is connected to the Pi's 40-pin GPIO header. When pressed, it triggers a script that removes your existing controller configuration/mapping files and reboots your Pi. When the Pi reboots, the controller configuration prompt will reappear and all ROM controller configurations will have reset. Nice!
Note: This is meant to be a fun, novel project; there are many other ways to handle and backup your controller configurations.
Here's everything you'll need to complete this guide:
Soldering Iron | × | 1 | ||
Raspberry Pi | × | 1 | ||
Jumper wires | × | 2 | ||
Solder | × | 1 tube | ||
NO momentary switch | × | 1 | ||
Heat-shrink tubing | × | 1 |
Now we'll need to create a few scripts:
- The listen script, which will watch for button presses and call the reset script if the button is pressed, and
- The reset script, which essentially runs the following commands to reset RetroArch config and reboot the Pi:
rm /opt/retropie/configs/all/emulationstation/es_input.cfg
rm /opt/retropie/configs/all/retroarch-joypads/*
sudo reboot
I'll walk you through the process of creating these scripts.
SSH into your Pi and create the reset script:
sudo nano listen-for-reset.py
Then, paste the following into that file:
#!/usr/bin/env python
import RPi.GPIO as GPIO
import subprocess
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.wait_for_edge(3, GPIO.FALLING)
subprocess.call(['rm', '/opt/retropie/configs/all/emulationstation/es_input.cfg'], shell=False)
subprocess.call(['rm', '/opt/retropie/configs/all/retroarch-joypads/*'], shell=False)
subprocess.call(['shutdown', '-r', 'now'], shell=False)
Save and exit. Finally, make the script executable and set it to start on boot:
sudo mv listen-for-reset.py /usr/local/bin/
sudo chmod +x /usr/local/bin/listen-for-reset.py
Now we'll create the listen script that will start/stop our service and watch for button presses. To create the script:
sudo nano listen-for-reset.sh
Enter the following:
#! /bin/sh
### BEGIN INIT INFO
# Provides: listen-for-reset.py
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO
# If you want a command to always run, put it here
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting listen-for-reset.py"
/usr/local/bin/listen-for-reset.py &
;;
stop)
echo "Stopping listen-for-reset.py"
pkill -f /usr/local/bin/listen-for-reset.py
;;
*)
echo "Usage: /etc/init.d/listen-for-reset.sh {start|stop}"
exit 1
;;
esac
exit 0
Save and exit. Then, move the file into /etc/init.d
and make it executable:
sudo mv listen-for-reset.sh /etc/init.d/
sudo chmod +x /etc/init.d/listen-for-reset.sh
Finally, tell the script to run on boot:
sudo update-rc.d listen-for-reset.sh defaults
Now, you can reboot your Pi to start the script or start it manually:
sudo /etc/init.d/listen-for-reset.sh start