-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhighbeam_delay.py
More file actions
executable file
·62 lines (49 loc) · 1.39 KB
/
highbeam_delay.py
File metadata and controls
executable file
·62 lines (49 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import subprocess
try:
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT) #highbeam output. 1 turn on high beams
GPIO.setup(13,GPIO.IN, pull_up_down=GPIO.PUD_UP)# switch state
GPIO.setup(15,GPIO.IN, pull_up_down=GPIO.PUD_UP)# high beam state. 0 high beams on. 1 high beams off
highbeams_out = False
last_time = time.time()
last_switch = False
last_switch_time = time.time()
switch_count = 0
for i in range(3):
GPIO.output(11,1)
time.sleep(0.3)
GPIO.output(11,0)
time.sleep(0.3)
while True:
switch = GPIO.input(13)
highbeams_in = not GPIO.input(15)
if highbeams_in == highbeams_out:
last_time = time.time()
if (time.time() - last_time) > 1.5:
highbeams_out = highbeams_in
GPIO.output(11,highbeams_out)
print("Switch %i, Highbeams In %i, Highbeams Out %i" % (switch,highbeams_in,highbeams_out))
if switch != last_switch:
switch_count += 1
last_switch_time = time.time()
last_switch = switch
if time.time() - last_switch_time > 1:
switch_count = 0
if switch_count >= 6:
print("shutdown")
for i in range(3):
GPIO.output(11,1)
time.sleep(0.3)
GPIO.output(11,0)
time.sleep(0.3)
subprocess.call("sudo shutdown -h now", shell=True)
break
time.sleep(0.03)
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
print("Closed")