Instantly share code, notes, and snippets.

Embed
What would you like to do?
Headlight and Reverse Lights monitor for YouTube Commenters - UNTESTED but should work fine.
#!/usr/bin/python
# A quick hack of my button monitor script to monitor headlights and reverse lights to
# Dim the screen and switch to the reverse camera.
# I have not tested this...
# Remember, the switch or opto is switching to ground, so the logic is inverted... 0 = on 1 = off
# Also, you would run only one of these "Monitor" scripts. you can't be running RearviewMonitor.py AND LightMonitor.py
import RPi.GPIO as GPIO
import time
import subprocess, os
import signal
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
RearView_Switch = 14 # pin 18 (You must use an optocoupler and resistor as shown in the video)
Brightness_Switch = 15 # pin 16 (You must use an optocoupler and resistor as shown in the video)
#Extra_Switch = 1 # pin 3
GPIO.setup(RearView_Switch,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(Brightness_Switch,GPIO.IN, pull_up_down=GPIO.PUD_UP)
print " Press Ctrl & C to Quit"
try:
run = 0
bright = 0
while True :
time.sleep(0.25) #this restricts the script to check the lights every 1/4 second, There is no point in checking 10,000 times a second.
# If the reverse reverse lights come on, do this:
if GPIO.input(RearView_Switch)==0 and run == 0:
print "Switching Rearview Camera On"
rpistr = "raspivid -t 0 -vf -h 480 -w 800"
p=subprocess.Popen(rpistr,shell=True, preexec_fn=os.setsid)
run = 1
# When the reverse lights go off, do this:
if GPIO.input(RearView_Switch)==1 and run == 1:
os.killpg(p.pid, signal.SIGTERM)
print "Killing the reverse camera feed"
run = 0
#These next Two blocks monitor the headlights or marker light and adjust the screen brightness settings.
if GPIO.input(Brightness_Switch)==0 and bright == 0:
print "Setting Brightness to 20" # 20 is about 10%
subprocess.call ("/usr/local/bin/backlight.sh 20", shell=True)
bright = 1
if GPIO.input(Brightness_Switch)==1 and bright == 1:
print "Setting Brightness back to 255" #255 is 100%
subprocess.call ("/usr/local/bin/backlight.sh 255", shell=True)
bright = 0
except KeyboardInterrupt:
print " Quit"
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment