-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtraffic_light.py
50 lines (38 loc) · 1.76 KB
/
traffic_light.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
from tiny_fx import TinyFX
from picofx import MonoPlayer
from picofx.mono import TrafficLightFX
"""
Play a traffic light sequence on TinyFX's outputs.
Press "Boot" to exit the program.
"""
# Variables
tiny = TinyFX() # Create a new TinyFX object to interact with the board
player = MonoPlayer(tiny.outputs) # Create a new effect player to control TinyFX's mono outputs
# Effects
traffic = TrafficLightFX(red_interval=5, # The time (in seconds) to stay on Red
red_amber_interval=2.5, # The time (in seconds) to stay on Red+Amber (or Amber Flashing if enabled)
green_interval=5, # The time (in seconds) to stay on Green
amber_interval=2.5, # The time (in seconds) to stay on Amber
fade_rate=0.01, # How quickly the lights respond to changes. Low values are more like bulbs
amber_flashing=False) # Whether to have Amber be flashing rather than Red+Amber, as some traffic lights use
# Set up the traffic light effect to play.
# The 3 light colours are assigned to the first 3 outputs
player.effects = [
traffic.red(),
traffic.amber(),
traffic.green(),
# No effects played on the rest of the outputs (unnecessary to list, but show for clarity)
None,
None,
None,
]
# Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt)
try:
player.start() # Start the effects running
# Loop until the effect stops or the "Boot" button is pressed
while player.is_running() and not tiny.boot_pressed():
pass
# Stop any running effects and turn off all the outputs
finally:
player.stop()
tiny.shutdown()