-
Notifications
You must be signed in to change notification settings - Fork 2
/
traffic-signal.py
220 lines (184 loc) · 7.33 KB
/
traffic-signal.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# IMPORTS
import time
import math
from cosmic import CosmicUnicorn
from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY
from bitfonts import BitFont, font2x5, font3x5, font4x5, font5x9
# INITIALISE GALACTIC UNICORN
cu = CosmicUnicorn()
graphics = PicoGraphics(DISPLAY)
bitfont = BitFont(graphics)
width = CosmicUnicorn.WIDTH
height = CosmicUnicorn.HEIGHT
cu.set_brightness(0.8)
# SIGNAL HEAD FUNCTION
def draw_signal_head(x, y, lamp="none"):
# R
graphics.set_pen(graphics.create_pen(15, 0, 0))
if lamp == "R":
graphics.set_pen(graphics.create_pen(255, 0, 0))
graphics.pixel(x, y)
# Y
graphics.set_pen(graphics.create_pen(15, 15, 0))
if lamp == "Y":
graphics.set_pen(graphics.create_pen(255, 255, 0))
graphics.pixel(x, y + 1)
# G
graphics.set_pen(graphics.create_pen(0, 15, 0))
if lamp == "G":
graphics.set_pen(graphics.create_pen(0, 255, 0))
graphics.pixel(x, y + 2)
def draw_signal_row(n, y, state, state_ped):
# ROW LABEL
graphics.set_pen(graphics.create_pen(100, 100, 100))
bitfont.draw_char(str(n), 0, y, font3x5)
# PED COUNTER BG
if state_ped:
graphics.set_pen(graphics.create_pen(15, 0, 0))
bitfont.draw_text("00", 10, y, font2x5)
# SIGNALS MAST
graphics.set_pen(graphics.create_pen(50, 50, 50))
graphics.pixel(16, 5 + y)
graphics.line(18, y - 1, 22, y - 1)
graphics.line(22, y - 1, 22, 6 + y)
# DRAW SIGNAL HEADS
draw_signal_head(16, 2 + y, state)
draw_signal_head(18, 0 + y, state)
draw_signal_head(20, 0 + y, state)
draw_signal_head(23, 2 + y, state)
# PED SYMBOL
if state_ped == "W":
graphics.set_pen(graphics.create_pen(255, 255, 255))
bitfont.draw_char("🚶", 5, y, font3x5)
if state_ped == "R":
graphics.set_pen(graphics.create_pen(255, 0, 0))
bitfont.draw_char("✋", 5, y, font3x5)
if state_ped == "F":
invert_flash = 1 - (time_ms // 500) % 2
graphics.set_pen(graphics.create_pen(255 * invert_flash, 0, 0))
bitfont.draw_char("✋", 5, y, font3x5)
def ped_timing_count_Fs(s):
start_index = s.find('F')
end_index = s.rfind('F')
count = s.count('F')
return count, start_index, end_index
def ped_timer_string(count, start_index, end_index, cycle_seconds):
if cycle_seconds < start_index or cycle_seconds > end_index:
return ""
else:
return str(end_index - cycle_seconds)
# INITIAL STATE
paused = 0
scene = 'A'
# TIMING - 60 second cycle
# phase2_timing = "GGGGGGGGGGGGGGGGGGGGYYYYRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"
# phase2p_timing = "WWWWWFFFFFFFFFFFFFFRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRWW"
# phase4_timing = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGGGGYYYYRRRRR"
# phase4p_timing = "RRRRRRRRRRRRRRRRRRRRRRRRRRWWWWWWWWWFFFFFFFFFFFFFFFRRRRRRRRRR"
# TIMING - 30 second cycle
phase2_timing = "GGGGGGGGGGYYYYRRRRRRRRRRRRRRRRRR"
phase2p_timing = "WWWWFFFFFFRRRRRRRRRRRRRRRRRRRRWW"
phase4_timing = "RRRRRRRRRRRRRRRRGGGGGGGGGYYYYRRR"
phase4p_timing = "RRRRRRRRRRRRRRRWWWWWWWFFFFFFRRRR"
# MAIN LOOP
while True:
time_ms = time.ticks_ms()
# cycle_seconds = (time_ms // 1000) % 60 # 60 second cycle
cycle_seconds = (time_ms // 1000) % 30 # 30 second cycle
graphics.set_pen(graphics.create_pen(0, 0, 0))
graphics.clear()
text = ""
timer_text = str(time_ms)[3:]
if paused:
timer_text = "0123456789"
if cu.is_pressed(CosmicUnicorn.SWITCH_A):
text = "Scene A"
scene = 'A'
if cu.is_pressed(CosmicUnicorn.SWITCH_B):
text = "Scene B"
scene = 'B'
if cu.is_pressed(CosmicUnicorn.SWITCH_C):
text = "Scene C"
scene = 'C'
if cu.is_pressed(CosmicUnicorn.SWITCH_D):
text = "Scene D"
scene = 'D'
if cu.is_pressed(CosmicUnicorn.SWITCH_VOLUME_UP):
text = "Louder!"
if cu.is_pressed(CosmicUnicorn.SWITCH_VOLUME_DOWN):
text = "Quieter"
if cu.is_pressed(CosmicUnicorn.SWITCH_BRIGHTNESS_UP):
cu.adjust_brightness(+0.01)
text = "Brite" + str(round(cu.get_brightness(),2))
if cu.is_pressed(CosmicUnicorn.SWITCH_BRIGHTNESS_DOWN):
cu.adjust_brightness(-0.01)
text = "Dark" + str(round(cu.get_brightness(),2))
if cu.is_pressed(CosmicUnicorn.SWITCH_SLEEP):
text = "Zzz..."
if scene == 'A':
# DRAW SIGNALS
draw_signal_row(2, 1, phase2_timing[cycle_seconds], phase2p_timing[cycle_seconds])
draw_signal_row(4, 26, phase4_timing[cycle_seconds], phase4p_timing[cycle_seconds])
# PED COUNTERS
graphics.set_pen(graphics.create_pen(255, 0, 0))
count, start_index, end_index = ped_timing_count_Fs(phase2p_timing)
bitfont.draw_text(ped_timer_string(count, start_index, end_index, cycle_seconds),15,1,font2x5,-1)
count4, start_index4, end_index4 = ped_timing_count_Fs(phase4p_timing)
bitfont.draw_text(ped_timer_string(count4, start_index4, end_index4, cycle_seconds),15,26,font2x5,-1)
# GLOBAL TIMER
graphics.set_pen(graphics.create_pen(100, 100, 100))
bitfont.draw_text(str(cycle_seconds),32,13,font3x5,-1)
if scene == 'B':
draw_signal_row(' ', 26, phase2_timing[cycle_seconds],'')
# RED
graphics.set_pen(graphics.create_pen(15, 0, 0))
if phase2_timing[cycle_seconds] == 'R':
graphics.set_pen(graphics.create_pen(255, 0, 0))
graphics.circle(25, 6, 6)
# YELLOW
graphics.set_pen(graphics.create_pen(15, 15, 0))
if phase2_timing[cycle_seconds] == 'Y':
graphics.set_pen(graphics.create_pen(255, 255, 0))
graphics.circle(15, 16, 6)
# GREEN
graphics.set_pen(graphics.create_pen(0, 15, 0))
if phase2_timing[cycle_seconds] == 'G':
graphics.set_pen(graphics.create_pen(0, 255, 0))
graphics.circle(6, 25, 6)
# GLOBAL TIMER
graphics.set_pen(graphics.create_pen(50, 50, 50))
bitfont.draw_text(str(cycle_seconds),32,27,font3x5,-1)
if scene == 'C':
draw_signal_row(' ', 26, phase2_timing[cycle_seconds],'')
# RED
graphics.set_pen(graphics.create_pen(15, 0, 0))
if phase2_timing[cycle_seconds] == 'R':
graphics.set_pen(graphics.create_pen(255, 0, 0))
graphics.circle(15, 10, 10)
# YELLOW
graphics.set_pen(graphics.create_pen(15, 15, 0))
if phase2_timing[cycle_seconds] == 'Y':
graphics.set_pen(graphics.create_pen(255, 255, 0))
graphics.circle(15, 31, 10)
# GLOBAL TIMER
graphics.set_pen(graphics.create_pen(50, 50, 50))
bitfont.draw_text(str(cycle_seconds),32,27,font3x5,-1)
if scene == 'D':
# YELLOW
graphics.set_pen(graphics.create_pen(15, 15, 0))
if phase2_timing[cycle_seconds] == 'Y':
graphics.set_pen(graphics.create_pen(255, 255, 0))
graphics.circle(15, 0, 10)
# GREEN
graphics.set_pen(graphics.create_pen(0, 15, 0))
if phase2_timing[cycle_seconds] == 'G':
graphics.set_pen(graphics.create_pen(0, 255, 0))
graphics.circle(15, 21, 10)
# GLOBAL TIMER
graphics.set_pen(graphics.create_pen(50, 50, 50))
bitfont.draw_text(str(cycle_seconds),32,27,font3x5,-1)
# STATUS TEXT
bitfont.draw_text(text.upper(),0,20,font3x5)
cu.update(graphics)
# pause for a moment
# time.sleep(0.1)