-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathauto_timed.py
113 lines (87 loc) · 3.01 KB
/
auto_timed.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import RPi.GPIO as GPIO
import random
import time
pins = [{'pin_num': 23, 'color': 'red'},
{'pin_num': 24, 'color': 'yellow'},
{'pin_num': 25, 'color': 'blue'},
{'pin_num': 22, 'color': 'red'},
{'pin_num': 12, 'color': 'yellow'},
{'pin_num': 16, 'color': 'blue'},
{'pin_num': 20, 'color': 'red'},
{'pin_num': 21, 'color': 'green'},
{'pin_num': 13, 'color': 'yellow'}]
GPIO.setmode(GPIO.BCM) # use GPIO numbering, not generic
GPIO.setwarnings(False)
# setup all pins based on above configuration
for pin in pins:
GPIO.setup(pin['pin_num'], GPIO.OUT, initial=GPIO.LOW)
def toggle_color(color: str, state: str):
pin_nums = [pin['pin_num'] for pin in pins if pin['color'] == color]
for pin_num in pin_nums:
if state == 'on':
GPIO.output(pin_num, GPIO.HIGH)
elif state == 'off':
GPIO.output(pin_num, GPIO.LOW)
def color_on(color: str):
toggle_color(color, 'on')
def color_off(color: str):
toggle_color(color, 'off')
def all_on():
for pin in pins:
GPIO.output(pin['pin_num'], GPIO.HIGH)
def all_off():
for pin in pins:
GPIO.output(pin['pin_num'], GPIO.LOW)
def strobe_reg(period=0.5, run_time=20):
start = time.time()
curr = time.time()
while curr - start < run_time:
all_on()
time.sleep(period)
all_off()
time.sleep(period)
curr = time.time()
def strobe_rand(min_time=0, max_time=1.2, run_time=20):
start = time.time()
curr = time.time()
while curr-start < run_time:
all_on()
time.sleep(random.uniform(min_time, max_time))
all_off()
time.sleep(random.uniform(min_time, max_time))
curr = time.time()
def wave_reg(period=0.1, run_time=20):
start = time.time()
curr = time.time()
while curr - start < run_time:
for pin in pins:
GPIO.output(pin['pin_num'], GPIO.HIGH)
time.sleep(period)
for pin in reversed(pins):
GPIO.output(pin['pin_num'], GPIO.LOW)
time.sleep(period)
curr = time.time()
def wave_rand(min_time=0, max_time=0.4, run_time=20):
start = time.time()
curr = time.time()
while curr - start < run_time:
period = random.uniform(min_time, max_time)
for pin in pins:
GPIO.output(pin['pin_num'], GPIO.HIGH)
time.sleep(period)
period = random.uniform(min_time, max_time)
for pin in reversed(pins):
GPIO.output(pin['pin_num'], GPIO.LOW)
time.sleep(period)
curr = time.time()
def wave_rand_ex(min_time=0, max_time=0.4, run_time=20):
start = time.time()
curr = time.time()
while curr - start < run_time:
for pin in pins:
GPIO.output(pin['pin_num'], GPIO.HIGH)
time.sleep(random.uniform(min_time, max_time))
for pin in reversed(pins):
GPIO.output(pin['pin_num'], GPIO.LOW)
time.sleep(random.uniform(min_time, max_time))
curr = time.time()