Skip to content

Commit 94c5d74

Browse files
committed
Stellar: Tweak & tidy examples.
1 parent 67152e3 commit 94c5d74

File tree

15 files changed

+126
-90
lines changed

15 files changed

+126
-90
lines changed

micropython/examples/stellar_unicorn/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Clock example with (optional) NTP synchronization. You can adjust the brightness
5454

5555
[eighties_super_computer.py](eighties_super_computer.py)
5656

57-
Random LEDs blink on and off mimicing the look of a movie super computer doing its work in the eighties. You can adjust the brightness with LUX + and -.
57+
Random LEDs blink on and off mimicking the look of a movie super computer doing its work in the eighties. You can adjust the brightness with LUX + and -.
5858

5959
### Feature Test
6060

micropython/examples/stellar_unicorn/cheerlights_history.py

Lines changed: 75 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,46 @@
1111
import uasyncio
1212
import urequests
1313
import time
14+
import random
1415
from machine import Timer, Pin
1516
from stellar import StellarUnicorn
16-
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY
17-
18-
URL = 'http://api.thingspeak.com/channels/1417/field/2/last.json'
19-
20-
UPDATE_INTERVAL = 113 # refresh interval in secs. Be nice to free APIs!
21-
# this esoteric number is used so that a column of LEDs equates (approximately) to an hour
17+
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY, PEN_P8 as PEN
18+
19+
URL = 'http://api.thingspeak.com/channels/1417/field/1/last.txt'
20+
21+
UPDATE_INTERVAL = 60 * 60 / 16 # refresh interval in secs. Be nice to free APIs!
22+
# Calculated as 60 minutes * 60 seconds divided by number of pixels per row
23+
# so that a row of LEDs equates (approximately) to an hour
24+
25+
CHEERLIGHTS_COLOR_VALUES = [
26+
(0x00, 0x00, 0x00), # Black/Unlit
27+
(0xFF, 0x00, 0x00),
28+
(0x00, 0x80, 0x00),
29+
(0x00, 0x00, 0xFF),
30+
(0x00, 0xFF, 0xFF),
31+
(0xFF, 0xFF, 0xFF),
32+
(0xFD, 0xF5, 0xE6),
33+
(0x80, 0x00, 0x80),
34+
(0xFF, 0x00, 0xFF),
35+
(0xFF, 0xFF, 0x00),
36+
(0xFF, 0xA5, 0x00),
37+
(0xFF, 0xC0, 0xCB),
38+
]
39+
40+
CHEERLIGHTS_COLOR_NAMES = [
41+
"black", # Black/Unlit, not part of cheerlights colours
42+
"red",
43+
"green",
44+
"blue",
45+
"cyan",
46+
"white",
47+
"oldlace",
48+
"purple",
49+
"magenta",
50+
"yellow",
51+
"orange",
52+
"pink"
53+
]
2254

2355

2456
def status_handler(mode, status, ip):
@@ -32,71 +64,62 @@ def status_handler(mode, status, ip):
3264
print('Wifi connection failed!')
3365

3466

35-
def hex_to_rgb(hex):
36-
# converts a hex colour code into RGB
37-
h = hex.lstrip('#')
38-
r, g, b = (int(h[i:i + 2], 16) for i in (0, 2, 4))
39-
return r, g, b
40-
41-
4267
def get_data():
68+
global index
4369
# open the json file
44-
print(f'Requesting URL: {URL}')
45-
r = urequests.get(URL)
46-
# open the json data
47-
j = r.json()
48-
print('Data obtained!')
49-
r.close()
70+
if UPDATE_INTERVAL >= 60:
71+
print(f'Requesting URL: {URL}')
72+
r = urequests.get(URL)
73+
name = r.content.decode("utf-8").strip()
74+
r.close()
75+
print('Data obtained!')
76+
77+
else:
78+
print("Random test colour!")
79+
# For sped-up testing we don't want to hit the API at all
80+
name = random.choice(CHEERLIGHTS_COLOR_NAMES[1:])
5081

5182
# flash the onboard LED after getting data
5283
pico_led.value(True)
5384
time.sleep(0.2)
5485
pico_led.value(False)
5586

56-
# extract hex colour from the json data
57-
hex = j['field2']
58-
5987
# add the new hex colour to the end of the array
60-
colour_array.append(hex)
61-
print(f'Colour added to array: {hex}')
62-
# remove the oldest colour in the array
63-
colour_array.pop(0)
64-
update_leds()
65-
66-
67-
def update_leds():
68-
# light up the LEDs
69-
# this step takes a second, it's doing a lot of hex_to_rgb calculations!
70-
print("Updating LEDs...")
71-
i = 0
72-
for x in range(width):
73-
for y in range(height):
74-
r, g, b = hex_to_rgb(colour_array[i])
75-
76-
current_colour = graphics.create_pen(r, g, b)
77-
graphics.set_pen(current_colour)
78-
graphics.pixel(x, y)
79-
i = i + 1
88+
if index == (width * height):
89+
index = 0
90+
graphics.clear()
91+
92+
colour_array[index] = CHEERLIGHTS_COLOR_NAMES.index(name)
93+
index += 1
94+
print(f'Colour added to array: {name}')
95+
8096
su.update(graphics)
8197
print("LEDs updated!")
8298

8399

84100
su = StellarUnicorn()
85-
graphics = PicoGraphics(DISPLAY)
86-
87101
width = StellarUnicorn.WIDTH
88102
height = StellarUnicorn.HEIGHT
89103

104+
# set up a buffer to store the colours
105+
colour_array = bytearray(width * height)
106+
107+
# We'll use palette mode, so just make the colour list the display buffer
108+
graphics = PicoGraphics(DISPLAY, pen_type=PEN, buffer=colour_array)
109+
110+
# Set up the palette with cheerlights colour values
111+
graphics.set_palette(CHEERLIGHTS_COLOR_VALUES)
112+
graphics.set_pen(0)
113+
graphics.clear()
114+
115+
# Keep track of the pixel we're lighting
116+
index = 0
117+
90118
su.set_brightness(0.5)
91119

92120
# set up the Pico W's onboard LED
93121
pico_led = Pin('LED', Pin.OUT)
94122

95-
current_colour = graphics.create_pen(0, 0, 0)
96-
97-
# set up an list to store the colours
98-
colour_array = ["#000000"] * 1024
99-
100123
# set up wifi
101124
try:
102125
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler)
@@ -109,19 +132,19 @@ def update_leds():
109132

110133
# start timer (the timer will call the function to update our data every UPDATE_INTERVAL)
111134
timer = Timer(-1)
112-
timer.init(period=UPDATE_INTERVAL * 1000, mode=Timer.PERIODIC, callback=lambda t: get_data())
135+
timer.init(period=int(UPDATE_INTERVAL * 1000), mode=Timer.PERIODIC, callback=lambda t: get_data())
113136

114137
while True:
115138
# adjust brightness with LUX + and -
116139
# LEDs take a couple of secs to update, so adjust in big (10%) steps
117140
if su.is_pressed(StellarUnicorn.SWITCH_BRIGHTNESS_UP):
118141
su.adjust_brightness(+0.1)
119-
update_leds()
142+
su.update(graphics)
120143
print(f"Brightness set to {su.get_brightness()}")
121144

122145
if su.is_pressed(StellarUnicorn.SWITCH_BRIGHTNESS_DOWN):
123146
su.adjust_brightness(-0.1)
124-
update_leds()
147+
su.update(graphics)
125148
print(f"Brightness set to {su.get_brightness()}")
126149

127150
# pause for a moment (important or the USB serial device will fail)

micropython/examples/stellar_unicorn/eighties_super_computer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY
55

66
'''
7-
Random LEDs blink on and off mimicing the look of a movie
7+
Random LEDs blink on and off mimicking the look of a movie
88
super computer doing its work in the eighties.
99
1010
You can adjust the brightness with LUX + and -.

micropython/examples/stellar_unicorn/exchange_ticker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
currency_rate = ""
2525
rate_keys = []
2626

27-
# diplay options
27+
# display options
2828
line_1_line = -2
2929
line_2_line = 9
3030
line_3_line = 20

micropython/examples/stellar_unicorn/launch/fire.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
def init():
16-
# a palette of five firey colours (white, yellow, orange, red, smoke)
16+
# a palette of five fiery colours (white, yellow, orange, red, smoke)
1717
global palette
1818
palette = [
1919
graphics.create_pen(0, 0, 0),

micropython/examples/stellar_unicorn/launch/main.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import math
23
import machine
34
from stellar import StellarUnicorn
45
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY
@@ -29,12 +30,21 @@ def pressed():
2930

3031
# wait for a button to be pressed and load that effect
3132
while True:
33+
b = int((math.sin(time.ticks_ms() / 200) + 1) / 2.0 * 255)
34+
b = max(60, b)
35+
3236
graphics.set_font("bitmap6")
3337
graphics.set_pen(graphics.create_pen(0, 0, 0))
3438
graphics.clear()
35-
graphics.set_pen(graphics.create_pen(155, 155, 155))
36-
graphics.text("PRESS", 3, 6, -1, 1)
37-
graphics.text("A B C OR D!", 5, 14, 16, 1, 0)
39+
40+
graphics.set_pen(graphics.create_pen(b, 0, 0))
41+
graphics.pixel(0, 3)
42+
graphics.set_pen(graphics.create_pen(0, b, 0))
43+
graphics.pixel(0, 5)
44+
graphics.set_pen(graphics.create_pen(0, 0, b))
45+
graphics.pixel(0, 7)
46+
graphics.set_pen(graphics.create_pen(b, 0, b))
47+
graphics.pixel(0, 9)
3848

3949
# brightness up/down
4050
if stellar.is_pressed(StellarUnicorn.SWITCH_BRIGHTNESS_UP):

micropython/examples/stellar_unicorn/launch/today.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
# You will need to create or update the file secrets.py with your network credentials using Thonny
77
# in order for the example to update using the NTP.
88

9-
# secrets.py should contain:
10-
# WIFI_SSID = ""
11-
# WIFI_PASSWORD = ""
9+
# WIFI_CONFIG.py should contain:
10+
# SSID = ""
11+
# PSK = ""
12+
# COUNTRY = ""
1213

1314
try:
14-
from secrets import WIFI_SSID, WIFI_PASSWORD
15+
from WIFI_CONFIG import SSID, PSK
1516
except ImportError:
16-
print("Create secrets.py with your WiFi credentials")
17+
print("Create WIFI_CONFIG.py with your WiFi credentials")
1718

1819
graphics = None
1920

@@ -22,22 +23,22 @@
2223

2324
rtc = machine.RTC()
2425

25-
DAYS = ["Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"]
26+
DAYS = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"]
2627

2728
# Enable the Wireless
2829
wlan = network.WLAN(network.STA_IF)
2930
wlan.active(True)
3031

3132

32-
def network_connect(SSID, PSK):
33+
def network_connect(ssid, psk):
3334

3435
# Number of attempts to make before timeout
3536
max_wait = 5
3637

3738
# Sets the Wireless LED pulsing and attempts to connect to your local network.
3839
print("connecting...")
3940
wlan.config(pm=0xa11140) # Turn WiFi power saving off for some slow APs
40-
wlan.connect(SSID, PSK)
41+
wlan.connect(ssid, psk)
4142

4243
while max_wait > 0:
4344
if wlan.status() < 0 or wlan.status() >= 3:
@@ -55,9 +56,9 @@ def network_connect(SSID, PSK):
5556
def sync_time():
5657

5758
try:
58-
network_connect(WIFI_SSID, WIFI_PASSWORD)
59+
network_connect(SSID, PSK)
5960
except NameError:
60-
print("Create secrets.py with your WiFi credentials")
61+
print("Create WIFI_CONFIG.py with your WiFi credentials")
6162

6263
if wlan.status() < 0 or wlan.status() >= 3:
6364
try:
@@ -67,12 +68,10 @@ def sync_time():
6768

6869

6970
def init():
70-
7171
sync_time()
7272

7373

7474
def draw():
75-
7675
# Pens
7776
RED = graphics.create_pen(120, 0, 0)
7877
WHITE = graphics.create_pen(255, 255, 255)
@@ -85,16 +84,16 @@ def draw():
8584

8685
# Measures the length of the text to help us with centring later.
8786
day_length = graphics.measure_text(DAYS[current_t[3]], 1)
88-
date_length = graphics.measure_text(str(current_t[2]), 3)
87+
date_length = graphics.measure_text(str(current_t[2]), 1)
8988

9089
graphics.set_font("bitmap6")
9190
graphics.set_pen(RED)
9291
graphics.rectangle(0, 0, WIDTH, 7)
9392
graphics.set_pen(WHITE)
94-
graphics.text(DAYS[current_t[3]], (WIDTH // 2) - (day_length // 2) - 1, 0, 16, 1)
93+
graphics.text(DAYS[current_t[3]], (WIDTH // 2) - (day_length // 2), 0, 16, 1)
9594

9695
graphics.set_pen(RED)
97-
graphics.set_font("bitmap8")
98-
graphics.text(str(current_t[2]), (WIDTH // 2) - (date_length // 2) + 1, 9, 16, 3)
96+
graphics.set_font("bitmap6")
97+
graphics.text(str(current_t[2]), (WIDTH // 2) - (date_length // 2) + 1, 8, 16, 1)
9998

10099
graphics.set_pen(graphics.create_pen(0, 0, 0))

micropython/examples/stellar_unicorn/numpy/eighties_super_computer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# machine.freq(250_000_000)
1515

1616
DAMPING_FACTOR = 0.95
17-
NUMBER_OF_LIGHTS = 10
17+
NUMBER_OF_LIGHTS = 4
1818
INTENSITY = 20
1919

2020
volume = 0.5

micropython/examples/stellar_unicorn/numpy/fire_effect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
graphics = PicoGraphics(DISPLAY_STELLAR_UNICORN, pen_type=PEN_P8)
1919

2020
# Number of random fire spawns
21-
FIRE_SPAWNS = 5
21+
FIRE_SPAWNS = 3
2222

2323
# Fire damping
2424
DAMPING_FACTOR = 0.98

micropython/examples/stellar_unicorn/numpy/lava_lamp.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# MAXIMUM OVERKILL
1414
# machine.freq(250_000_000)
1515

16+
NUM_BLOBS = 3
17+
1618
su = StellarUnicorn()
1719
graphics = PicoGraphics(DISPLAY_STELLAR_UNICORN, pen_type=PEN_P8)
1820
su.set_brightness(0.5)
@@ -45,7 +47,7 @@ def move(self):
4547
self.dy = -self.dy
4648

4749

48-
blobs = [Blob() for _ in range(10)]
50+
blobs = [Blob() for _ in range(NUM_BLOBS)]
4951

5052

5153
# Fill palette with a steep falloff from bright red to dark blue
@@ -60,7 +62,7 @@ def update():
6062
blob.move()
6163
lava[int(blob.y)][int(blob.x)] = blob.r
6264

63-
# Propogate the blobs outwards
65+
# Propagate the blobs outwards
6466
a = numpy.roll(lava, 1, axis=0)
6567
b = numpy.roll(lava, -1, axis=0)
6668
d = numpy.roll(lava, 1, axis=1)

0 commit comments

Comments
 (0)