Skip to content

Commit

Permalink
Merge pull request #4 from pimoroni/patch-examples
Browse files Browse the repository at this point in the history
Examples for TinyFX, with related effects and functional tweaks
  • Loading branch information
ZodiusInfuser authored Aug 1, 2024
2 parents 982ddfa + 17a069e commit 5945cfc
Show file tree
Hide file tree
Showing 46 changed files with 1,290 additions and 483 deletions.
15 changes: 10 additions & 5 deletions boards/PIMORONI_TINYFX/frozen_libs/pimoroni.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ def __init__(self, pin, amplifier_gain=1, resistor=0, offset=0):
self.offset = offset
self.pin = ADC(pin)

def read_voltage(self):
return max((((self.pin.read_u16() * 3.3) / 65535) + self.offset) / self.gain, 0.0)
def read_voltage(self, samples=1):
val = 0
for _ in range(samples):
val += self.pin.read_u16()
val /= samples

def read_current(self):
return max((((val * 3.3) / 65535) + self.offset) / self.gain, 0.0)

def read_current(self, samples=1):
if self.resistor > 0:
return self.read_voltage() / self.resistor
return self.read_voltage(samples) / self.resistor
else:
return self.read_voltage()
return self.read_voltage(samples)


class AnalogMux:
Expand Down
3 changes: 3 additions & 0 deletions boards/PIMORONI_TINYFX/uf2-manifest.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
main.py
examples/*.py
examples/effects/*.py
examples/effects/mono/*.py
examples/effects/colour/*.py
examples/function/*.py
examples/showcase/*.py
examples/audio/race_start.py
examples/audio/fair_use_encounters.py
Expand Down
2 changes: 1 addition & 1 deletion boards/PIMORONI_TINYFX/visible_libs/tiny_fx.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, init_i2c=True):
def boot_pressed(self):
return self.__switch.value() == 0

def voltage(self, samples=1):
def read_voltage(self, samples=1):
val = 0
for _ in range(samples):
val += self.__v_sense.read_u16()
Expand Down
131 changes: 121 additions & 10 deletions examples/tiny_fx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,152 @@
These are micropython examples for the Pimoroni [TinyFX](https://shop.pimoroni.com/products/tiny_fx), a stamp-sized light and sound effects controller board for model making, construction kits, and dioramas.

- [Function Examples](#function-examples)
- [Effect Examples](#effect-examples)
- [Read Button](#read-button)
- [Sensor Meter](#sensor-meter)
- [Voltage Meter](#voltage-meter)
- [Mono Effect Examples](#mono-effect-examples)
- [Single Blink](#single-blink)
- [Single Flashing](#single-flashing)
- [Single Flicker](#single-flicker)
- [Single Pulse](#single-pulse)
- [Single Random](#single-random)
- [Blink Wave](#blink-wave)
- [Flashing Sequence](#flashing-sequence)
- [Pulse Wave](#pulse-wave)
- [Binary Counter](#binary-counter)
- [Traffic Light](#traffic-light)
- [Wave Sequence](#wave-sequence)
- [Colour Effect Examples](#colour-effect-examples)
- [Rainbow](#rainbow)
- [Random](#random)
- [Hue Step](#hue-step)
- [Audio Examples](#audio-examples)
- [Showcase Examples](#showcase-examples)
- [Rescue Vehicle](#rescue-vehicle)
- [Sensor Wave](#sensor-wave)
- [Ship Thrusters](#ship-thrusters)
- [Space Tales](#space-tales)
- [Space Tales with PIR Sensor](#space-tales-with-pir-sensor)


## Function Examples

### Read Button
[function/read_button.py](function/read_button.py)

Show the state of TinyFX's Boot button on its RGB output.


### Sensor Meter
[function/sensor_meter.py](function/sensor_meter.py)

Use TinyFX's mono outputs as a bargraph to show the voltage measured from a sensor attached to the sensor connector.


### Voltage Meter
[function/voltage_meter.py](function/voltage_meter.py)

Use TinyFX's mono outputs as a bargraph to show the voltage that is powering the board.


## Mono Effect Examples

### Single Blink
[effects/mono/single_blink.py](effects/mono/single_blink.py)

Play a blinking effect on one of TinyFX's outputs.


### Single Flashing
[effects/mono/single_flashing.py](effects/mono/single_flashing.py)

Play a flashing effect on one of TinyFX's outputs.


### Single Flicker
[effects/mono/single_flicker.py](effects/mono/single_flicker.py)

Play a flickering effect on one of TinyFX's outputs.


### Single Pulse
[effects/mono/single_pulse.py](effects/mono/single_pulse.py)

Play a pulsing effect on one of TinyFX's outputs.


### Single Random
[effects/mono/single_random.py](effects/mono/single_random.py)

Play a randomly changing brightness effect on one of TinyFX's outputs.


### Blink Wave
[effects/mono/blink_wave.py](effects/mono/blink_wave.py)

Play a wave of blinks on TinyFX's outputs.


### Flashing Sequence
[effects/mono/flashing_sequence.py](effects/mono/flashing_sequence.py)

Play a flashing sequence across TinyFX's outputs.


### Pulse Wave
[effects/mono/pulse_wave.py](effects/mono/pulse_wave.py)

Play a wave of pulses on TinyFX's outputs.


### Binary Counter
[effects/binary_counter.py](effects/binary_counter.py)

Play an incrementing binary counter on TinyFX's outputs.

## Effect Examples

### Traffic Light
[effects/traffic_light.py](effects/traffic_light.py)
[effects/mono/traffic_light.py](effects/mono/traffic_light.py)

Play a traffic light sequence on TinyFX's outputs.


### Wave Sequence
[effects/wave.py](effects/wave.py)
## Colour Effect Examples

Read Yukon's onboard Buttons.
### Rainbow
[effects/colour/rainbow.py](effects/colour/rainbow.py)

Play a rainbow effect on TinyFX's RGB output.

## Audio Examples

### Random
[effects/colour/random.py](effects/colour/random.py)

Play a randomly changing brightness and colour effect on TinyFX's RGB output.


### Hue Step
[effects/colour/hue_step.py](effects/colour/hue_step.py)

Play a stepped hue effect on TinyFX's RGB output.


## Audio Examples


## Showcase Examples

### Rescue Vehicle
[showcase/rescue_vehicle.py](showcase/rescue_vehicle.py)

Play an alternating flashing sequence on two of TinyFX's outputs, recreating the effect of rescue vehicle beacons. The other outputs are static for illuminated head and tail lights.


### Sensor Wave
[showcase/sensor_wave.py](showcase/sensor_wave.py)

Play a wave of pulses on TinyFX's outputs, who's speed is controlled by a sensor.


### Ship Thrusters
[showcase/ship_thrusters.py](showcase/ship_thrusters.py)

Expand All @@ -52,5 +164,4 @@ Play effects for each space themed "postcard".
### Space Tales with PIR Sensor
[showcase/space_tales_pir.py](showcase/space_tales_with_pir.py)

Play effects for each space themed "postcard" when someone walks past.
A PIR sensor is used to activate the effect, which will turn off after a certain time.
Play effects for each space themed "postcard" when someone walks past. A PIR sensor is used to activate the effect, which will turn off after a certain time.
35 changes: 35 additions & 0 deletions examples/tiny_fx/examples/effects/colour/hue_step.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from tiny_fx import TinyFX
from picofx import ColourPlayer
from picofx.colour import HueStepFX

"""
Play a stepped hue effect on TinyFX's RGB output.
Press "Boot" to exit the program.
"""

# Variables
tiny = TinyFX() # Create a new TinyFX object to interact with the board
player = ColourPlayer(tiny.rgb) # Create a new effect player to control TinyFX's RGB output


# Create and set up a rainbow effect to play
player.effects = HueStepFX(interval=1.0, # The time (in seconds) between each hue step
hue=0.0, # The hue of the colour to start at (from 0.0 to 1.0)
sat=1.0, # The saturation/intensity of the colour (from 0.0 to 1.0)
val=1.0, # The value/brightness of the colour (from 0.0 to 1.0)
steps=6) # The number of steps to take around the colour wheel


# 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()
33 changes: 33 additions & 0 deletions examples/tiny_fx/examples/effects/colour/rainbow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from tiny_fx import TinyFX
from picofx import ColourPlayer
from picofx.colour import RainbowFX

"""
Play a rainbow effect on TinyFX's RGB output.
Press "Boot" to exit the program.
"""

# Variables
tiny = TinyFX() # Create a new TinyFX object to interact with the board
player = ColourPlayer(tiny.rgb) # Create a new effect player to control TinyFX's RGB output


# Create and set up a rainbow effect to play
player.effects = RainbowFX(speed=0.2, # The speed to cycle through colours at, with 1.0 being 1 second
sat=1.0, # The saturation/intensity of the colour (from 0.0 to 1.0)
val=1.0) # The value/brightness of the colour (from 0.0 to 1.0)


# 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()
48 changes: 48 additions & 0 deletions examples/tiny_fx/examples/effects/colour/random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from tiny_fx import TinyFX
from picofx import MonoPlayer
from picofx.mono import RandomFX

"""
Play a randomly changing brightness and colour effect on TinyFX's RGB output.
Press "Boot" to exit the program.
"""

# Constants
INTERVAL = 0.2 # The time (in seconds) between each random brightness
BRIGHTNESS_MIN = 0.0 # The min brightness to randomly go down to
BRIGHTNESS_MAX = 1.0 # The max brightness to randomly go up to

# Variables
tiny = TinyFX() # Create a new TinyFX object to interact with the board
player = MonoPlayer([tiny.rgb.led_r, # Create a new effect player to control TinyFX's RGB output as mono outputs
tiny.rgb.led_g,
tiny.rgb.led_b])


# Create and set up a blink effect to play
player.effects = [
RandomFX(interval=INTERVAL,
brightness_min=BRIGHTNESS_MIN,
brightness_max=BRIGHTNESS_MAX),
RandomFX(interval=INTERVAL,
brightness_min=BRIGHTNESS_MIN,
brightness_max=BRIGHTNESS_MAX),
RandomFX(interval=INTERVAL,
brightness_min=BRIGHTNESS_MIN,
brightness_max=BRIGHTNESS_MAX)
]


# 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()
42 changes: 42 additions & 0 deletions examples/tiny_fx/examples/effects/mono/binary_counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from tiny_fx import TinyFX
from picofx import MonoPlayer
from picofx.mono import BinaryCounterFX

"""
Play an incrementing binary counter 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


# Create a BinaryCounterFX effect
binary = BinaryCounterFX(interval=0.1) # The time (in seconds) between each increment of the binary counter


# Set up the binary effect to play. Each output shows a different bit of the counter
player.effects = [
binary(0),
binary(1),
binary(2),
binary(3),
binary(4),
binary(5)
]


# 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()
Loading

0 comments on commit 5945cfc

Please sign in to comment.