The PowerBlock is a popular tool for managing safe power on/off operations for a Raspberry Pi. It adds functionality like a reset button and status signals. In this post, we’ll walk you through the steps to configure and enable PowerBlock on Batocera 41u, one of the most popular platforms for retro gaming.
Introduction
Integrating PowerBlock with Batocera requires specific configurations since Batocera uses its own service management system. This guide outlines the steps to ensure PowerBlock works seamlessly with:
- Safe Shutdown Button: Allows controlled system shutdown.
- Reset Button: Restarts the graphical environment without rebooting the entire system.
- GPIO Status Signals: Uses a GPIO-connected LED to indicate the power-on state.
Steps Taken
1. Creating a Custom Service for PowerBlock
We created a service to manage the scripts required for PowerBlock functionality. This service can start, stop, and check the status of PowerBlock.
echo "#!/bin/bash
case \"\$1\" in
start)
echo \"Starting PowerBlock service...\"
nohup /userdata/system/powerblock.sh > /var/log/powerblock.log 2>&1 &
;;
stop)
echo \"Stopping PowerBlock service...\"
pkill -f \"/userdata/system/powerblock.sh\"
;;
status)
if pgrep -f \"/userdata/system/powerblock.sh\" > /dev/null; then
echo \"PowerBlock is running.\"
else
echo \"PowerBlock is not running.\"
fi
;;
*)
echo \"Usage: \$0 {start|stop|status}\"
exit 1
;;
esac" > /userdata/system/services/powerblock
chmod +x /userdata/system/services/powerblock
2. Creating the Main Script
The main script, powerblock.sh
, monitors the GPIO pins and performs specific actions based on button states:
- Shutdown: Detects when the shutdown button is relaxed (high state) and safely shuts down the system.
- Reset: Monitors the reset button and restarts the graphical environment when pressed (low state).
echo "#!/bin/bash
SHUTDOWN_PIN=18
STATUS_PIN=17
RESET_PIN=23
pinctrl set \"\$SHUTDOWN_PIN\" ip pu
pinctrl set \"\$STATUS_PIN\" op
pinctrl set \"\$RESET_PIN\" ip pu
pinctrl set \"\$STATUS_PIN\" dh
echo \"Monitoring buttons on GPIO \$SHUTDOWN_PIN (shutdown) and GPIO \$RESET_PIN (reset)...\"
restart_graphics() {
echo \"Reset button pressed. Restarting graphical environment...\"
batocera-es-swissknife --emukill
}
shutdown_system() {
echo \"Shutdown button relaxed. Initiating shutdown...\"
pinctrl set \"\$STATUS_PIN\" dl
batocera-es-swissknife --shutdown
exit 0
}
while true; do
SHUTDOWN_STATE=\$(pinctrl get \"\$SHUTDOWN_PIN\" | awk '{print \$5}')
RESET_STATE=\$(pinctrl get \"\$RESET_PIN\" | awk '{print \$5}')
if [[ \"\$SHUTDOWN_STATE\" == \"hi\" ]]; then
shutdown_system
fi
if [[ \"\$RESET_STATE\" == \"lo\" ]]; then
echo \"Condition met: GPIO \$RESET_PIN is low. Restarting graphical environment...\"
restart_graphics
sleep 1
fi
sleep 0.5
done" > /userdata/system/powerblock.sh
chmod +x /userdata/system/powerblock.sh
3. Enabling the Service
We activated the custom service and verified its status to ensure it is working properly:
batocera-services enable powerblock
batocera-services start powerblock
batocera-services status powerblock
4. Rebooting the System
Finally, we rebooted the system to apply all changes:
reboot