And... it looks like the library I loaded from XploD's tutorial has interrupts, even better!
(why don't people just tell me these things up front...
)
So, even easier with interrupts (I think):
- Code: Select all
!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import subprocess
GPIO.setmode(GPIO.BOARD)
# Select unused GPIO header pin to be used for shutdown
InputPin = 13
# Set selected pin to input, and enable internal pull-up
GPIO.setup(InputPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Wait for a button press on the selected pin (pulled to ground, falling edge)
GPIO.wait_for_edge(InputPin, GPIO.FALLING)
# When pressed, execute a shutdown, or any other command
print "*** Soft shutdown activated ***"
subprocess.call("/sbin/shutdown now", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
edit:
I managed to get this to run as a service, using the method posted by Frank for MPD-watchdog.
As I thought, there was very little original programming (much to my relief). The effort consisted of research mostly, and some cut and paste.
1) Looking up which GPIO pins remain free once my HiFiBerry DAC+ is installed.
2) Finding a suitable Python example to activate shutdown
3) Installing Python 2 and associated libraries using tutorial posted by XploD:
http://www.runeaudio.com/forum/post11623.html#p11623 with RPi.GPIO Version 0.5.11
4) Modifying the example to use my GPIO pin of choice, and interrupts
5) Testing using command line
6) Installing and running as a service, using method posted by Frank for MPD-watchdog, here:
http://www.runeaudio.com/forum/auto-reconnect-for-web-radio-t3278.htmlI can't say how well it fits into the RuneAudio scheme of things, but it runs, Top shows no added CPU load, and shutdown in response to a button press is essentially instantaneous.