Skip to content

Commit f9f6619

Browse files
committed
Examples: Tidyup pass, Plasma 2040/2350 W cross compatibility.
1 parent d09fb9e commit f9f6619

File tree

10 files changed

+107
-55
lines changed

10 files changed

+107
-55
lines changed

examples/hue_twinkles_encoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import plasma
99

1010
"""
11-
A simple example for controlling pulsing LED patterns with an Encoder Wheel.
11+
Control pulsing LED patterns with an Encoder Wheel.
1212
1313
Pressing the middle button will change between Hue, Spread, Brightness and Speed mode.
1414

examples/plasma2040/alternating-blinkies.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
# This super simple example sets up two alternating colours, great for festive lights!
2-
31
import time
42

53
import plasma
64

5+
"""
6+
This super simple example sets up two alternating colours, great for festive lights!
7+
"""
8+
79
# Set how many LEDs you have
810
NUM_LEDS = 50
911

examples/plasma2040/level.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@
33
import time
44

55
import machine
6-
# Import msa301 and I2C helper
76
from breakout_msa301 import BreakoutMSA301
8-
# Import helpers for RGB LEDs and Buttons
97
from pimoroni import RGBLED, Button
108

119
import plasma
1210

13-
# A simple balancing game, where you use the MSA301 accelerometer to line up a band with a goal on the strip.
14-
# This can either be done using:
15-
# - Angle mode: Where position on the strip directly matches the accelerometer's angle
16-
# - Velocity mode: Where tilting the accelerometer changes the speed the band moves at
17-
# When the goal position is reached, a new position is randomly selected
11+
"""
12+
A simple balancing game, where you use the MSA301 accelerometer to line up a band with a goal on the strip.
1813
19-
# Press "A" to change the game mode.
20-
# Press "B" to start or stop the game mode.
21-
# Press "Boot" to invert the direction of the accelerometer tilt
14+
This can either be done using:
15+
- Angle mode: Where position on the strip directly matches the accelerometer's angle.
16+
- Velocity mode: Where tilting the accelerometer changes the band's speed.
17+
18+
When the goal position is reached, a new position is randomly selected.
19+
20+
- Press "A" to change the game mode.
21+
- Press "Boot" to start or stop the game mode.
22+
- Press "B" to invert the direction of the accelerometer tilt
23+
"""
2224

2325
# Set how many LEDs you have
2426
NUM_LEDS = 30
@@ -64,7 +66,13 @@
6466

6567
user_sw = Button("USER_SW", repeat_time=0)
6668
button_a = Button("BUTTON_A", repeat_time=0)
67-
button_b = Button("BUTTON_B", repeat_time=0)
69+
70+
try:
71+
# Button B is only available on Plasma 2040
72+
button_b = Button("BUTTON_B", repeat_time=0)
73+
except ValueError:
74+
button_b = None
75+
6876
led = RGBLED("LED_R", "LED_G", "LED_B")
6977

7078
i2c = machine.I2C()
@@ -172,27 +180,29 @@ def colour_band(centre_position, width, goal_position, goal_width, hue):
172180

173181
sw_pressed = user_sw.read()
174182
a_pressed = button_a.read()
175-
b_pressed = button_b.read()
183+
b_pressed = button_b and button_b.read()
176184

177-
if b_pressed:
185+
if sw_pressed:
178186
game_mode = not game_mode
179187

180-
if sw_pressed:
188+
if b_pressed:
181189
invert = not invert
182190

183191
if mode == ANGLE:
184-
if game_mode:
185-
led.set_rgb(255, 255, 0)
186-
else:
187-
led.set_rgb(0, 255, 0)
192+
if led:
193+
if game_mode:
194+
led.set_rgb(255, 255, 0)
195+
else:
196+
led.set_rgb(0, 255, 0)
188197
if a_pressed:
189198
mode = VELOCITY
190199

191200
elif mode == VELOCITY:
192-
if game_mode:
193-
led.set_rgb(255, 0, 255)
194-
else:
195-
led.set_rgb(0, 0, 255)
201+
if led:
202+
if game_mode:
203+
led.set_rgb(255, 0, 255)
204+
else:
205+
led.set_rgb(0, 0, 255)
196206
if a_pressed:
197207
mode = ANGLE
198208

examples/plasma2040/monitor.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import machine
2-
# Import bme68x and I2C helper
32
from breakout_bme68x import BreakoutBME68X
4-
# Import helpers for RGB LEDs and Buttons
53
from pimoroni import RGBLED, Button
64

75
import plasma
86

9-
# Uses a BME68x to monitor the ambient temperature, pressure and humidity, and show them as bars on an LED strip.
10-
# Press "A" to cycle to the next mode.
11-
# Press "B" to cycle to the previous mode.
7+
"""
8+
Uses a BME68x to monitor the ambient temperature, pressure and humidity, and show them as bars on an LED strip.
9+
10+
- Press "A" to cycle to the next mode.
11+
- Press "B" to cycle to the previous mode.
12+
"""
1213

1314
# Set how many LEDs you have
1415
NUM_LEDS = 30
@@ -50,10 +51,11 @@
5051
# WS2812 / NeoPixel™ LEDs
5152
led_strip = plasma.WS2812(NUM_LEDS)
5253

54+
user_sw = Button("USER_SW", repeat_time=0)
5355
button_a = Button("BUTTON_A", repeat_time=0)
54-
button_b = Button("BUTTON_B", repeat_time=0)
5556
led = RGBLED("LED_R", "LED_G", "LED_B")
5657

58+
5759
i2c = machine.I2C()
5860
bme = BreakoutBME68X(i2c)
5961

@@ -121,7 +123,7 @@ def colour_gauge(percent, start_led, end_led, start_hue, end_hue):
121123
colour_gauge(t, 0, NUM_LEDS, HUMIDITY_HUE_START, HUMIDITY_HUE_END)
122124

123125
a_pressed = button_a.read()
124-
b_pressed = button_b.read()
126+
b_pressed = user_sw.read()
125127

126128
if mode == ALL:
127129
led.set_rgb(127, 127, 127)

examples/plasma2040/rainbow.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import time
22

3-
# Import helpers for RGB LEDs, Buttons, and Analog
43
from pimoroni import RGBLED, Analog, Button
54

65
import plasma
76

8-
# Press "B" to speed up the LED cycling effect.
9-
# Press "A" to slow it down again.
10-
# Press "Boot" to reset the speed back to default.
7+
"""
8+
- Press "B" to speed up the LED cycling effect.
9+
- Press "A" to slow it down again.
10+
- Press "Boot" to reset the speed back to default.
11+
"""
1112

1213
# Set how many LEDs you have
1314
NUM_LEDS = 30
@@ -18,6 +19,9 @@
1819
# How many times the LEDs will be updated per second
1920
UPDATES = 60
2021

22+
# Magic values for Plasma 2040 current sense
23+
# 3A * 0.015Ω = 0.045V
24+
# 0.045V * 50 (gain) = 2.25V maximum
2125
ADC_GAIN = 50
2226
SHUNT_RESISTOR = 0.015
2327

@@ -31,9 +35,18 @@
3135

3236
user_sw = Button("USER_SW", repeat_time=0)
3337
button_a = Button("BUTTON_A", repeat_time=0)
34-
button_b = Button("BUTTON_B", repeat_time=0)
38+
try:
39+
# Button B is only available on Plasma 2040
40+
button_b = Button("BUTTON_B", repeat_time=0)
41+
except ValueError:
42+
button_b = None
3543
led = RGBLED("LED_R", "LED_G", "LED_B")
36-
sense = Analog("CURRENT_SENSE", ADC_GAIN, SHUNT_RESISTOR)
44+
45+
try:
46+
# Sense is only available on Plasma 2040
47+
sense = Analog("CURRENT_SENSE", ADC_GAIN, SHUNT_RESISTOR)
48+
except ValueError:
49+
sense = None
3750

3851
# Start updating the LED strip
3952
led_strip.start()
@@ -46,7 +59,7 @@
4659
while True:
4760
sw = user_sw.read()
4861
a = button_a.read()
49-
b = button_b.read()
62+
b = button_b and button_b.read()
5063

5164
if sw:
5265
speed = DEFAULT_SPEED
@@ -67,7 +80,7 @@
6780
led.set_rgb(speed, 0, 255 - speed)
6881

6982
count += 1
70-
if count >= UPDATES:
83+
if sense and count >= UPDATES:
7184
# Display the current value once every second
7285
print("Current =", sense.read_current(), "A")
7386
count = 0

examples/plasma2040/random-blinkies.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
# This simple example randomises LED colours and brightness for a subtly sparkly effect.
2-
31
import time
4-
# Random functions! randrange is for picking integers from a range, and uniform is for floats.
52
from random import randrange, uniform
63

74
import plasma
85

6+
"""
7+
This example randomises LED colours and brightness for a subtly sparkly effect.
8+
"""
9+
910
# Set how many LEDs you have
1011
NUM_LEDS = 50
1112

@@ -32,6 +33,8 @@
3233
led_strip.start()
3334

3435
# Light up all the leds random colours and brightnesses from the specified ranges...
36+
# randrange is for picking integers from a range,
37+
# uniform is for generating random uniform floats between a minimum and maximum value.
3538
for i in range(NUM_LEDS):
3639
led_strip.set_hsv(i, randrange(HUE_START, HUE_END) / 360, 1.0, uniform(BRIGHTNESS_MIN, BRIGHTNESS_MAX))
3740

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
import time
22

3-
# Import helpers for RGB LEDs and Buttons
43
from pimoroni import RGBLED, Button
54

5+
"""
6+
Demonstrate the use of buttons on Plasma 2040 and Plasma 2350 W
7+
"""
8+
69
user_sw = Button("USER_SW", repeat_time=0)
710
button_a = Button("BUTTON_A", repeat_time=0)
8-
button_b = Button("BUTTON_B", repeat_time=0)
11+
12+
try:
13+
# Button B is only available on Plasma 2040
14+
button_b = Button("BUTTON_B", repeat_time=0)
15+
except ValueError:
16+
button_b = None
17+
918
led = RGBLED("LED_R", "LED_G", "LED_B")
19+
1020
led.set_rgb(0, 0, 0)
1121

1222
while True:
1323
if user_sw.read():
1424
print("Pressed User SW - {}".format(time.ticks_ms()))
1525
led.set_rgb(255, 0, 0)
26+
1627
if button_a.read():
1728
print("Pressed A - {}".format(time.ticks_ms()))
1829
led.set_rgb(0, 255, 0)
19-
if button_b.read():
30+
31+
if button_b and button_b.read():
2032
print("Pressed B - {}".format(time.ticks_ms()))
2133
led.set_rgb(0, 0, 255)

examples/plasma2040/rotary.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
import time
33

44
import machine
5-
# Import bme68x and I2C helper
65
from breakout_encoder import BreakoutEncoder
7-
# Import helpers for RGB LEDs and Buttons
86
from pimoroni import RGBLED, Button
97

108
import plasma
119

12-
# Press "B" to enable cycling.
13-
# Press "A" to change the encoder mode.
14-
# Press "Boot" to reset the effects back to default.
10+
"""
11+
- Press "B" to enable cycling.
12+
- Press "A" to change the encoder mode.
13+
- Press "Boot" to reset the effects back to default.
14+
"""
1515

1616
# Set how many LEDs you have
1717
NUM_LEDS = 30
@@ -42,7 +42,13 @@
4242

4343
user_sw = Button("USER_SW", repeat_time=0)
4444
button_a = Button("BUTTON_A", repeat_time=0)
45-
button_b = Button("BUTTON_B", repeat_time=0)
45+
46+
try:
47+
# Button B is only available on Plasma 2040
48+
button_b = Button("BUTTON_B", repeat_time=0)
49+
except ValueError:
50+
button_b = None
51+
4652
led = RGBLED("LED_R", "LED_G", "LED_B")
4753

4854
i2c = machine.I2C()
@@ -141,7 +147,7 @@ def brightness_gauge(v, vmax=100):
141147

142148
sw_pressed = user_sw.read()
143149
a_pressed = button_a.read()
144-
b_pressed = button_b.read()
150+
b_pressed = button_b and button_b.read()
145151

146152
if sw_pressed:
147153
speed = DEFAULT_SPEED

examples/plasma_stick/cheerlights.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
import plasma
88

9-
'''
9+
"""
1010
This Plasma Stick example sets your LED strip to the current #cheerlights colour.
1111
Find out more about the Cheerlights API at https://cheerlights.com/
12-
'''
12+
"""
1313

1414
URL = 'http://api.thingspeak.com/channels/1417/field/2/last.json'
1515
UPDATE_INTERVAL = 120 # refresh interval in secs. Be nice to free APIs!

examples/sparkles.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
import plasma
55

6+
"""
7+
Use multiple overlapping, phase-shifted sine waves to create a sparkly effect.
8+
"""
9+
610
NUM_LEDS = 26
711
EFFECT_SPEED = 2 # Does what it says on the tin! (lower is slower but you might see quantization/stepping)
812
EFFECT_SHARPNESS = 10 # Numbers between 1 and 1000 are practical, give them a try!

0 commit comments

Comments
 (0)