Guest User

Untitled

a guest
Mar 22nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #!/usr/bin/python
  2. # A quick hack of my button monitor script to monitor headlights and reverse lights to
  3. # Dim the screen and switch to the reverse camera.
  4. # I have not tested this...
  5. # Remember, the switch or opto is switching to ground, so the logic is inverted... 0 = on 1 = off
  6. # Also, you would run only one of these "Monitor" scripts. you can't be running RearviewMonitor.py AND LightMonitor.py
  7.  
  8. import RPi.GPIO as GPIO
  9. import time
  10. import subprocess, os
  11. import signal
  12. GPIO.setmode(GPIO.BCM)
  13. GPIO.setwarnings(False)
  14. RearView_Switch = 14 # pin 18 (You must use an optocoupler and resistor as shown in the video)
  15. Brightness_Switch = 15 # pin 16 (You must use an optocoupler and resistor as shown in the video)
  16. #Extra_Switch = 1 # pin 3
  17. GPIO.setup(RearView_Switch,GPIO.IN, pull_up_down=GPIO.PUD_UP)
  18. GPIO.setup(Brightness_Switch,GPIO.IN, pull_up_down=GPIO.PUD_UP)
  19.  
  20. print " Press Ctrl & C to Quit"
  21.  
  22. try:
  23.  
  24. run = 0
  25. bright = 0
  26. while True :
  27. 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.
  28. # If the reverse reverse lights come on, do this:
  29. if GPIO.input(RearView_Switch)==0 and run == 0:
  30. print "Switching Rearview Camera On"
  31. rpistr = "raspivid -t 0 -vf -h 480 -w 800"
  32. p=subprocess.Popen(rpistr,shell=True, preexec_fn=os.setsid)
  33. run = 1
  34. # When the reverse lights go off, do this:
  35. if GPIO.input(RearView_Switch)==1 and run == 1:
  36. os.killpg(p.pid, signal.SIGTERM)
  37. print "Killing the reverse camera feed"
  38. run = 0
  39.  
  40. #These next Two blocks monitor the headlights or marker light and adjust the screen brightness settings.
  41. if GPIO.input(Brightness_Switch)==0 and bright == 0:
  42. print "Setting Brightness to 20" # 20 is about 10%
  43. subprocess.call ("/usr/local/bin/backlight.sh 20", shell=True)
  44. bright = 1
  45.  
  46. if GPIO.input(Brightness_Switch)==1 and bright == 1:
  47. print "Setting Brightness back to 255" #255 is 100%
  48. subprocess.call ("/usr/local/bin/backlight.sh 255", shell=True)
  49. bright = 0
  50.  
  51.  
  52. except KeyboardInterrupt:
  53. print " Quit"
  54. GPIO.cleanup()
Add Comment
Please, Sign In to add comment