Skip to content

Commit

Permalink
Added flickering example
Browse files Browse the repository at this point in the history
  • Loading branch information
ZodiusInfuser committed Jul 30, 2024
1 parent ef6c106 commit a83ee2c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 23 deletions.
45 changes: 45 additions & 0 deletions examples/tiny_fx/examples/effects/single_flicker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from tiny_fx import TinyFX
from picofx import MonoPlayer
from picofx.mono import FlickerFX

"""
Play a flickering effect on one of 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


# Create and set up a blink effect to play
player.effects = [
FlickerFX(brightness=1.0, # The brightness to use when being bright (from 0.0 to 1.0)
dimness=0.5, # How much to dim the brightness by (from 0.0 to 1.0) when being dim
bright_min=0.05, # The min amount of time (in seconds) to be bright for
bright_max=0.1, # The max amount of time (in seconds) to be bright for
dim_min=0.02, # The min amount of time (in seconds) to be dim for
dim_max=0.04), # The max amount of time (in seconds) to be dim for

# No effects played on the rest of the outputs (unnecessary to list, but show for clarity)
None,
None,
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.clear()
26 changes: 13 additions & 13 deletions examples/tiny_fx/examples/showcase/ship_thrusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"""

# Constants
MIN_LOCKOUT = 0.2 # The minimum amount of time the thrusters will be at full brightness for (in seconds)
CENTRAL_BRIGHTNESS = 1.0 # The brightness of the central thrusters (from 0.0 to 1.0)
SIDE_BRIGHTNESS = 0.3 # The brightness of the side thrusters (from 0.0 to 1.0)
DIMNESS_PERCENT = 0.1 # How much to dim the thuster brightness by (from 0.0 to 1.0)
MIN_BRIGHTNESS_DURATION = 0.2 # The minimum amount of time the thrusters will be at full brightness for (in seconds)

PLANETSHINE_HUE = 0.5 # The colour of the planetshine (from 0.0 to 1.0)
PLANETSHINE_SATURATION = 0.5 # The saturation/intensity of the planetshine (from 0.0 to 1.0)
Expand All @@ -26,21 +26,21 @@
rgb_player = ColourPlayer(tiny.rgb) # Create a new effect player to control TinyFX's RGB output

# Set up the effects for the thruster banks
left_centre = FlickerFX(lockout_min=MIN_LOCKOUT,
brightness=CENTRAL_BRIGHTNESS,
dimness=DIMNESS_PERCENT)
left_centre = FlickerFX(brightness=CENTRAL_BRIGHTNESS,
dimness=DIMNESS_PERCENT,
bright_min=MIN_BRIGHTNESS_DURATION)

right_centre = FlickerFX(lockout_min=MIN_LOCKOUT,
brightness=CENTRAL_BRIGHTNESS,
dimness=DIMNESS_PERCENT)
right_centre = FlickerFX(brightness=CENTRAL_BRIGHTNESS,
dimness=DIMNESS_PERCENT,
bright_min=MIN_BRIGHTNESS_DURATION)

left = FlickerFX(lockout_min=MIN_LOCKOUT,
brightness=SIDE_BRIGHTNESS,
dimness=DIMNESS_PERCENT)
left = FlickerFX(brightness=SIDE_BRIGHTNESS,
dimness=DIMNESS_PERCENT,
bright_min=MIN_BRIGHTNESS_DURATION)

right = FlickerFX(lockout_min=MIN_LOCKOUT,
brightness=SIDE_BRIGHTNESS,
dimness=DIMNESS_PERCENT)
right = FlickerFX(brightness=SIDE_BRIGHTNESS,
dimness=DIMNESS_PERCENT,
bright_min=MIN_BRIGHTNESS_DURATION)

# Set up the mono effects to play
# Both left and right LEDs are close together on the ship, so it looks better if they share the same flicker
Expand Down
20 changes: 10 additions & 10 deletions picofx/mono/flicker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@


class FlickerFX(Updateable):
def __init__(self, dim_min=0.02, dim_max=0.04, lockout_min=0.05, lockout_max=0.1, brightness=1.0, dimness=0.5):
self.dim_min = dim_min
self.dim_max = dim_max
self.lockout_min = lockout_min
self.lockout_max = lockout_max
def __init__(self, brightness=1.0, dimness=0.5, bright_min=0.05, bright_max=0.1, dim_min=0.02, dim_max=0.04):
self.brightness = brightness
self.dimness = dimness
self.bright_min = bright_min
self.bright_max = bright_max
self.dim_min = dim_min
self.dim_max = dim_max

self.__is_dim = False
self.__bright_dur = 0
self.__dim_dur = 0
self.__lockout_dur = 0
self.__time = 0

def __call__(self):
Expand All @@ -31,13 +31,13 @@ def tick(self, delta_ms):
if self.__time >= self.__dim_dur:
self.__time -= self.__dim_dur

self.__lockout_dur = int(random.uniform(self.lockout_min, self.lockout_max) * 1000)
self.__bright_dur = int(random.uniform(self.bright_min, self.bright_max) * 1000)
self.__is_dim = False

else:
# Only attempt to flicker if not in lockout period
if self.__time >= self.__lockout_dur:
self.__time -= self.__lockout_dur
# Only attempt to flicker if not in bright period
if self.__time >= self.__bright_dur:
self.__time -= self.__bright_dur

self.__dim_dur = int(random.uniform(self.dim_min, self.dim_max) * 1000)
self.__is_dim = True

0 comments on commit a83ee2c

Please sign in to comment.