The MotorPiTX I’m using for my robot comes with a power switch that can either hard power-off the Raspberry Pi, or signal software running on the Pi to do a clean shutdown:
Purely to control the ATTiny chip, to turn on the Pi, but also to shutdown as well. With the Pi off, pressing it once will turn the Raspberry Pi and MotorPiTX board on. With the Pi active, pressing it again will set GPIO 8 high, signalling that the button has been pressed and should be used to shutdown the OS. See Installation for more details.
Holding the button for 3 seconds will cut all power, useful if something goes wrong, such as the Raspberry Pi crashes and refuses to respond.
I’d never bothered to write any software to listen for GPIO8 going high before, I’d always relied on being able to SSH in to shut the machine down. Recently however I’ve been playing with the Pi camera, this was causing the Pi to pull too much power and it would shut down the USB ports cutting off my (wireless) SSH access and my USB keyboard both at the same time leaving me with no alternative but to hard power off. It was time to do something with that GPIO8 pin signal, and so I wrote motorpitx_power_control.
motorpitx_power_control is simple, from the README:
A program to listen for MotorPiTX power button presses and cleanly shutdown your Raspberry Pi.
It’s a really small program written in Go that checks the status of GPIO8 every half a second and initiates a shutdown sequence if the pin goes high. I’ve included instructions for building and installing on Arch linux, but it will work on other distros too.
Why Go?
I think Go is a great language for the Raspberry Pi. Go creates small, efficient, statically compiled binaries that easily fit within the resource limits of the Raspberry Pi. motorpitx_power_control is a relatively small 2.5MB binary that uses almost no CPU at all.
Getting it
The project requires a working Go install to compile, this is simple enough to get on Linux and OS X, check your package manager or homebrew. If you have Go installed head over to the motorpitx_power_control Github page and clone the repo. Follow the build instructions, copy the binary file to your Pi and run it. That’s it!
Check the Contributing & TODO sections of the README if you want to contribute back to the project.