Also, would be interested in your progress - there's a small chance it'll motivate me to do the Pi car audio computer thing that's been sitting on my may-happen-one-day-yeah-right-whatever-meh list for past few years.
#!/usr/bin/python import sys sys.path.append('storage/.kodi/addons/python.RPi.GPIO/lib') import RPi.GPIO as GPIO import time import subprocess # we will use the pin numbering to match the pins on the Pi, instead of the # GPIO pin outs (makes it easier to keep track of things) GPIO.setmode(GPIO.BOARD) # use the same pin that is used for the reset button (one button to rule them all!) 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("shutdown -h now", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) oldButtonState1 = buttonState1 time.sleep(.1)What it doesn't do is power off the backlight to the screen. So, the Pi goes off and the screen stays bright white. Using these commands I'm able to turn the screen on and off:
echo 0 > /sys/class/backlight/rpi_backlight/bl_power echo 1 > /sys/class/backlight/rpi_backlight/bl_power
What "echo A > B" is does is write content A to file B - the closest equivalent in Python seems to be print:
print('0',file='/sys/class/backlight/rpi_backlight/bl_power')
But the recommended way appears to be this convoluted thing:
with open('/sys/class/backlight/rpi_backlight/bl_power','w') as f: f.write('0')
Possibly using buttonState1 instead of 0 and 1 ...and actually looking at the code, I just noticed you've got subprocess.call calling what looks like a shutdown shell command, so you can simply use that for shell commands without needing to translate - possibly changing the shutdown one to:
"echo 1 > /sys/class/backlight/rpi_backlight/bl_power && shutdown -h now"and adding the inverse for starting up - it seems weird to have this in an endless polling loop (the while true + sleep combo), but maybe that's the only way this stuff works? :/
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