See Threaded callbacks in this page: https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/
Basically you might be able to swap the while loop for something like this:
def onButtonPress(channel): if GPIO.input(channel) shell_command = "echo 0 > /sys/class/backlight/rpi_backlight/bl_power && shutdown -h now"; else shell_command = "echo 1 > /sys/class/backlight/rpi_backlight/bl_power"; subprocess.call(shell_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) GPIO.add_event_detect(5, GPIO.BOTH, callback=onButtonPress)
That still isn't the greatest code, but it's (possibly) good enough, assuming it works; I don't know Python or understand what I've read of the GPIO stuff, so... *shrug* :)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP) oldButtonState1 = True while True: #grab the current button state buttonState1 = GPIO.input(5) # check to see if button has been pushed if buttonState1 != oldButtonState1 and buttonState1 == False: subprocess.call("echo 1 > /sys/class/backlight/rpi_backlight/bl_power && shutdown -h now", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) oldButtonState1 = buttonState1 time.sleep(.1)On a click of the button it turns off the backlight and shuts the Pi down. I only need that one line as it automatically turns the backlight back on on a power up. However, although it was working initially on a rebuild, the sound's dropped off again. :C
My sound broke in Arch by doing something that buggered around with PulseAudio, which then fucked up ALSA, because PulseAudio is a buggy bloated piece of shit that nobody actually needs.
So maybe you installed/upgraded (possibly automatically) something that did fiddled with PA?
If you do really want to do it this way, since you only care about shutdown, you don't need to store/check oldButtonState and can simplify it to:
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP) # endless loop until button is pressed while GPIO.input(5): time.sleep(.1) # verify button state before switching off if not GPIO.input(5): subprocess.call("echo 1 > /sys/class/backlight/rpi_backlight/bl_power && shutdown -h now", shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
The if verification is probably not necessary (depending on how Python works), but even so it acts as both a safety against unwanted shutdown (incase something goes wrong), and clarifies the intent slightly. (The event-driven/callback method would make it even clearer.)