-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RGB emergency - Add support for RGB channel, and use button long press to toggle effect #13
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,42 @@ | ||
import time | ||
from tiny_fx import TinyFX | ||
from picofx import MonoPlayer | ||
from picofx import ColourPlayer, MonoPlayer | ||
from picofx.colour import RGBBlinkFX, RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA | ||
from picofx.mono import FlashSequenceFX, StaticFX | ||
|
||
""" | ||
Play an alternating flashing sequence on two of TinyFX's outputs, | ||
recreating the effect of rescue vehicle beacons. | ||
and the RGB channel, recreating the effect of rescue vehicle beacons. | ||
The other outputs are static for illuminated head and tail lights. | ||
|
||
Press "Boot" to exit the program. | ||
Press "Boot" to exit the program, or long press to toggle RGB effects. | ||
""" | ||
|
||
# Constants | ||
EMERGENCY = COLOURS = [RED,RED,RED,RED,BLUE,BLUE,BLUE,BLUE] | ||
EMERGENCY_BLINK_SPEED = 8 | ||
ORANGE = (228,114,28) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps (255, 32, 0)? |
||
HAZARD = [ORANGE] | ||
HAZARD_BLINK_SPEED = 1.8 | ||
|
||
# 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 | ||
player = ColourPlayer(tiny.rgb) # Create a new effect player to control TinyFX's RGB output | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
monoplayer = MonoPlayer(tiny.outputs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
# Create and set up an red blue flashing effect to play, and a hazard one for later | ||
rgbEffect = RGBBlinkFX(colour=COLOURS, # The colour (or colours to blink in sequence) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We tend to use snake case for our variable names, rather than camelCase. E.g. |
||
phase=0.0, # The start time in the cycle (0-1) | ||
speed=EMERGENCY_BLINK_SPEED, # The speed to cycle through colours at, with 1.0 being 1 second (1/T) | ||
duty=0.5) # Amount of the cycle to be "on" | ||
|
||
hazardEffect = RGBBlinkFX(colour=HAZARD, # The colour (or colours to blink in sequence) | ||
phase=0.0, # The start time in the cycle (0-1) | ||
speed=HAZARD_BLINK_SPEED, # The speed to cycle through colours at, with 1.0 being 1 second (1/T) | ||
duty=0.4) # Amount of the cycle to be "on" | ||
|
||
player.effects = [rgbEffect] | ||
|
||
|
||
|
||
# Create a FlashSequenceFX effect for the beacon lights | ||
|
@@ -27,7 +51,7 @@ | |
|
||
|
||
# Set up the mono effects to play. The first two are flashing, the rest are static | ||
player.effects = [ | ||
monoplayer.effects = [ | ||
flashing(0), | ||
flashing(1), | ||
headlights, | ||
|
@@ -36,16 +60,27 @@ | |
taillights | ||
] | ||
|
||
player.pair(monoplayer) | ||
|
||
# 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 | ||
|
||
t = 0 # keep track of boot press duration | ||
dont_exit_yet = True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A simpler name would be |
||
while dont_exit_yet: | ||
player.start() # Start the effects running | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
|
||
# Loop until the effect stops or the "Boot" button is pressed | ||
while player.is_running() and not tiny.boot_pressed(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The player is never stopped, so checking |
||
pass | ||
t = time.ticks_ms() | ||
while player.is_running() and tiny.boot_pressed(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checking |
||
pass | ||
if time.ticks_ms() - t > 500: # long press - toggle effect | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The more robust way to do this check is Also, from my resting, I found having short press be effect switch, and long press be stop, felt like a better user experience |
||
player.effects = [hazardEffect] if player.effects[0] is rgbEffect else [rgbEffect] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm surprised this worked! Perhaps a safer way would be to have a separate |
||
else: # short press - exit | ||
dont_exit_yet = False | ||
# Stop any running effects and turn off all the outputs | ||
finally: | ||
player.stop() | ||
tiny.shutdown() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EMERGENCY doesn't seem to be used anywhere?