Stellar: Tweak & tidy examples.

This commit is contained in:
Phil Howard 2023-05-31 13:47:24 +01:00
parent 67152e32e5
commit 94c5d74894
15 changed files with 122 additions and 86 deletions

View File

@ -54,7 +54,7 @@ Clock example with (optional) NTP synchronization. You can adjust the brightness
[eighties_super_computer.py](eighties_super_computer.py)
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 -.
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 -.
### Feature Test

View File

@ -11,14 +11,46 @@ from network_manager import NetworkManager
import uasyncio
import urequests
import time
import random
from machine import Timer, Pin
from stellar import StellarUnicorn
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY, PEN_P8 as PEN
URL = 'http://api.thingspeak.com/channels/1417/field/2/last.json'
URL = 'http://api.thingspeak.com/channels/1417/field/1/last.txt'
UPDATE_INTERVAL = 113 # refresh interval in secs. Be nice to free APIs!
# this esoteric number is used so that a column of LEDs equates (approximately) to an hour
UPDATE_INTERVAL = 60 * 60 / 16 # refresh interval in secs. Be nice to free APIs!
# Calculated as 60 minutes * 60 seconds divided by number of pixels per row
# so that a row of LEDs equates (approximately) to an hour
CHEERLIGHTS_COLOR_VALUES = [
(0x00, 0x00, 0x00), # Black/Unlit
(0xFF, 0x00, 0x00),
(0x00, 0x80, 0x00),
(0x00, 0x00, 0xFF),
(0x00, 0xFF, 0xFF),
(0xFF, 0xFF, 0xFF),
(0xFD, 0xF5, 0xE6),
(0x80, 0x00, 0x80),
(0xFF, 0x00, 0xFF),
(0xFF, 0xFF, 0x00),
(0xFF, 0xA5, 0x00),
(0xFF, 0xC0, 0xCB),
]
CHEERLIGHTS_COLOR_NAMES = [
"black", # Black/Unlit, not part of cheerlights colours
"red",
"green",
"blue",
"cyan",
"white",
"oldlace",
"purple",
"magenta",
"yellow",
"orange",
"pink"
]
def status_handler(mode, status, ip):
@ -32,71 +64,62 @@ def status_handler(mode, status, ip):
print('Wifi connection failed!')
def hex_to_rgb(hex):
# converts a hex colour code into RGB
h = hex.lstrip('#')
r, g, b = (int(h[i:i + 2], 16) for i in (0, 2, 4))
return r, g, b
def get_data():
global index
# open the json file
print(f'Requesting URL: {URL}')
r = urequests.get(URL)
# open the json data
j = r.json()
print('Data obtained!')
r.close()
if UPDATE_INTERVAL >= 60:
print(f'Requesting URL: {URL}')
r = urequests.get(URL)
name = r.content.decode("utf-8").strip()
r.close()
print('Data obtained!')
else:
print("Random test colour!")
# For sped-up testing we don't want to hit the API at all
name = random.choice(CHEERLIGHTS_COLOR_NAMES[1:])
# flash the onboard LED after getting data
pico_led.value(True)
time.sleep(0.2)
pico_led.value(False)
# extract hex colour from the json data
hex = j['field2']
# add the new hex colour to the end of the array
colour_array.append(hex)
print(f'Colour added to array: {hex}')
# remove the oldest colour in the array
colour_array.pop(0)
update_leds()
if index == (width * height):
index = 0
graphics.clear()
colour_array[index] = CHEERLIGHTS_COLOR_NAMES.index(name)
index += 1
print(f'Colour added to array: {name}')
def update_leds():
# light up the LEDs
# this step takes a second, it's doing a lot of hex_to_rgb calculations!
print("Updating LEDs...")
i = 0
for x in range(width):
for y in range(height):
r, g, b = hex_to_rgb(colour_array[i])
current_colour = graphics.create_pen(r, g, b)
graphics.set_pen(current_colour)
graphics.pixel(x, y)
i = i + 1
su.update(graphics)
print("LEDs updated!")
su = StellarUnicorn()
graphics = PicoGraphics(DISPLAY)
width = StellarUnicorn.WIDTH
height = StellarUnicorn.HEIGHT
# set up a buffer to store the colours
colour_array = bytearray(width * height)
# We'll use palette mode, so just make the colour list the display buffer
graphics = PicoGraphics(DISPLAY, pen_type=PEN, buffer=colour_array)
# Set up the palette with cheerlights colour values
graphics.set_palette(CHEERLIGHTS_COLOR_VALUES)
graphics.set_pen(0)
graphics.clear()
# Keep track of the pixel we're lighting
index = 0
su.set_brightness(0.5)
# set up the Pico W's onboard LED
pico_led = Pin('LED', Pin.OUT)
current_colour = graphics.create_pen(0, 0, 0)
# set up an list to store the colours
colour_array = ["#000000"] * 1024
# set up wifi
try:
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler)
@ -109,19 +132,19 @@ get_data()
# start timer (the timer will call the function to update our data every UPDATE_INTERVAL)
timer = Timer(-1)
timer.init(period=UPDATE_INTERVAL * 1000, mode=Timer.PERIODIC, callback=lambda t: get_data())
timer.init(period=int(UPDATE_INTERVAL * 1000), mode=Timer.PERIODIC, callback=lambda t: get_data())
while True:
# adjust brightness with LUX + and -
# LEDs take a couple of secs to update, so adjust in big (10%) steps
if su.is_pressed(StellarUnicorn.SWITCH_BRIGHTNESS_UP):
su.adjust_brightness(+0.1)
update_leds()
su.update(graphics)
print(f"Brightness set to {su.get_brightness()}")
if su.is_pressed(StellarUnicorn.SWITCH_BRIGHTNESS_DOWN):
su.adjust_brightness(-0.1)
update_leds()
su.update(graphics)
print(f"Brightness set to {su.get_brightness()}")
# pause for a moment (important or the USB serial device will fail)

View File

@ -4,7 +4,7 @@ from stellar import StellarUnicorn
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY
'''
Random LEDs blink on and off mimicing the look of a movie
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 -.

View File

@ -24,7 +24,7 @@ currency_symbol = ""
currency_rate = ""
rate_keys = []
# diplay options
# display options
line_1_line = -2
line_2_line = 9
line_3_line = 20

View File

@ -13,7 +13,7 @@ damping_factor = 0.97
def init():
# a palette of five firey colours (white, yellow, orange, red, smoke)
# a palette of five fiery colours (white, yellow, orange, red, smoke)
global palette
palette = [
graphics.create_pen(0, 0, 0),

View File

@ -1,4 +1,5 @@
import time
import math
import machine
from stellar import StellarUnicorn
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY
@ -29,12 +30,21 @@ def pressed():
# wait for a button to be pressed and load that effect
while True:
b = int((math.sin(time.ticks_ms() / 200) + 1) / 2.0 * 255)
b = max(60, b)
graphics.set_font("bitmap6")
graphics.set_pen(graphics.create_pen(0, 0, 0))
graphics.clear()
graphics.set_pen(graphics.create_pen(155, 155, 155))
graphics.text("PRESS", 3, 6, -1, 1)
graphics.text("A B C OR D!", 5, 14, 16, 1, 0)
graphics.set_pen(graphics.create_pen(b, 0, 0))
graphics.pixel(0, 3)
graphics.set_pen(graphics.create_pen(0, b, 0))
graphics.pixel(0, 5)
graphics.set_pen(graphics.create_pen(0, 0, b))
graphics.pixel(0, 7)
graphics.set_pen(graphics.create_pen(b, 0, b))
graphics.pixel(0, 9)
# brightness up/down
if stellar.is_pressed(StellarUnicorn.SWITCH_BRIGHTNESS_UP):

View File

@ -6,14 +6,15 @@ import machine
# You will need to create or update the file secrets.py with your network credentials using Thonny
# in order for the example to update using the NTP.
# secrets.py should contain:
# WIFI_SSID = ""
# WIFI_PASSWORD = ""
# WIFI_CONFIG.py should contain:
# SSID = ""
# PSK = ""
# COUNTRY = ""
try:
from secrets import WIFI_SSID, WIFI_PASSWORD
from WIFI_CONFIG import SSID, PSK
except ImportError:
print("Create secrets.py with your WiFi credentials")
print("Create WIFI_CONFIG.py with your WiFi credentials")
graphics = None
@ -22,14 +23,14 @@ HEIGHT = 16 # StellarUnicorn.HEIGHT
rtc = machine.RTC()
DAYS = ["Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"]
DAYS = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"]
# Enable the Wireless
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
def network_connect(SSID, PSK):
def network_connect(ssid, psk):
# Number of attempts to make before timeout
max_wait = 5
@ -37,7 +38,7 @@ def network_connect(SSID, PSK):
# Sets the Wireless LED pulsing and attempts to connect to your local network.
print("connecting...")
wlan.config(pm=0xa11140) # Turn WiFi power saving off for some slow APs
wlan.connect(SSID, PSK)
wlan.connect(ssid, psk)
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
@ -55,9 +56,9 @@ def network_connect(SSID, PSK):
def sync_time():
try:
network_connect(WIFI_SSID, WIFI_PASSWORD)
network_connect(SSID, PSK)
except NameError:
print("Create secrets.py with your WiFi credentials")
print("Create WIFI_CONFIG.py with your WiFi credentials")
if wlan.status() < 0 or wlan.status() >= 3:
try:
@ -67,12 +68,10 @@ def sync_time():
def init():
sync_time()
def draw():
# Pens
RED = graphics.create_pen(120, 0, 0)
WHITE = graphics.create_pen(255, 255, 255)
@ -85,16 +84,16 @@ def draw():
# Measures the length of the text to help us with centring later.
day_length = graphics.measure_text(DAYS[current_t[3]], 1)
date_length = graphics.measure_text(str(current_t[2]), 3)
date_length = graphics.measure_text(str(current_t[2]), 1)
graphics.set_font("bitmap6")
graphics.set_pen(RED)
graphics.rectangle(0, 0, WIDTH, 7)
graphics.set_pen(WHITE)
graphics.text(DAYS[current_t[3]], (WIDTH // 2) - (day_length // 2) - 1, 0, 16, 1)
graphics.text(DAYS[current_t[3]], (WIDTH // 2) - (day_length // 2), 0, 16, 1)
graphics.set_pen(RED)
graphics.set_font("bitmap8")
graphics.text(str(current_t[2]), (WIDTH // 2) - (date_length // 2) + 1, 9, 16, 3)
graphics.set_font("bitmap6")
graphics.text(str(current_t[2]), (WIDTH // 2) - (date_length // 2) + 1, 8, 16, 1)
graphics.set_pen(graphics.create_pen(0, 0, 0))

View File

@ -14,7 +14,7 @@ Experiment with the damping, number of spawns and intensity to change the effect
# machine.freq(250_000_000)
DAMPING_FACTOR = 0.95
NUMBER_OF_LIGHTS = 10
NUMBER_OF_LIGHTS = 4
INTENSITY = 20
volume = 0.5

View File

@ -18,7 +18,7 @@ su.set_brightness(0.5)
graphics = PicoGraphics(DISPLAY_STELLAR_UNICORN, pen_type=PEN_P8)
# Number of random fire spawns
FIRE_SPAWNS = 5
FIRE_SPAWNS = 3
# Fire damping
DAMPING_FACTOR = 0.98

View File

@ -13,6 +13,8 @@ A lava lamp effect, created by blurred, moving particles.
# MAXIMUM OVERKILL
# machine.freq(250_000_000)
NUM_BLOBS = 3
su = StellarUnicorn()
graphics = PicoGraphics(DISPLAY_STELLAR_UNICORN, pen_type=PEN_P8)
su.set_brightness(0.5)
@ -45,7 +47,7 @@ class Blob():
self.dy = -self.dy
blobs = [Blob() for _ in range(10)]
blobs = [Blob() for _ in range(NUM_BLOBS)]
# Fill palette with a steep falloff from bright red to dark blue
@ -60,7 +62,7 @@ def update():
blob.move()
lava[int(blob.y)][int(blob.x)] = blob.r
# Propogate the blobs outwards
# Propagate the blobs outwards
a = numpy.roll(lava, 1, axis=0)
b = numpy.roll(lava, -1, axis=0)
d = numpy.roll(lava, 1, axis=1)

View File

@ -15,9 +15,9 @@ Press "A" to manually re-seed.
# MAXIMUM OVERKILL
# machine.freq(250_000_000)
INITIAL_LIFE = 500 # Number of live cells to seed
INITIAL_LIFE = 128 # Number of live cells to seed
GENERATION_TIME_MS = 50 # MS between generations
MINIMUM_LIFE = 10 # Auto reseed when only this many alive cells remain
MINIMUM_LIFE = 15 # Auto reseed when only this many alive cells remain
SMOOTHED = True # Enable for a more organic if somewhat unsettling feel
DECAY = 0.90 # Rate at which smoothing effect decays, higher number = more persistent, 1.0 = no decay

View File

@ -12,6 +12,8 @@ HELLO NEO.
# MAXIMUM OVERKILL
# machine.freq(250_000_000)
NUM_SPARKLES = 1
su = StellarUnicorn()
su.set_brightness(1.0)
graphics = PicoGraphics(DISPLAY_STELLAR_UNICORN, pen_type=PEN_P8)
@ -29,7 +31,7 @@ for g in range(128):
def update():
trippy[:] *= 0.65
for _ in range(2):
for _ in range(NUM_SPARKLES):
x = random.randint(0, width - 1)
y = random.randint(0, height // 2)
trippy[y][x] = random.randint(128, 255) / 255.0

View File

@ -6,7 +6,7 @@ from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN, PEN_P8
from ulab import numpy
"""
THIS IS FINE!
THI IS FIN!
"""
# MAXIMUM OVERKILL
@ -17,7 +17,7 @@ su.set_brightness(0.5)
graphics = PicoGraphics(DISPLAY_STELLAR_UNICORN, pen_type=PEN_P8)
# Number of random fire spawns
FIRE_SPAWNS = 5
FIRE_SPAWNS = 4
# Fire damping
DAMPING_FACTOR = 0.98
@ -81,9 +81,9 @@ def draw():
# Draw text over the top
graphics.set_pen(0)
graphics.text("This", 6, 1, 1, 1)
graphics.text("is", 11, 9, 1, 1)
graphics.text("fine", 6, 17, 1, 1)
graphics.text("This", 1, 0, 1, 1)
graphics.text("is", 1, 3, 1, 1)
graphics.text("fine", 1, 9, 1, 1)
su.update(graphics)

View File

@ -19,7 +19,7 @@ graphics = PicoGraphics(DISPLAY_STELLAR_UNICORN, pen_type=PEN_P8)
DAMPING_FACTOR = 0.8
NUMBER_OF_DROPS = 5
NUMBER_OF_DROPS = 2
INTENSITY = 10
OFFSET = 0.0 # Try 0.5

View File

@ -30,7 +30,7 @@ HEIGHT = StellarUnicorn.HEIGHT
rtc = machine.RTC()
DAYS = ["Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"]
DAYS = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"]
# Enable the Wireless
wlan = network.WLAN(network.STA_IF)
@ -99,7 +99,7 @@ def draw():
graphics.set_pen(RED)
graphics.rectangle(0, 0, WIDTH, 7)
graphics.set_pen(WHITE)
graphics.text(DAYS[current_t[3]], (WIDTH // 2) - (day_length // 2) - 1, 0, 16, 1)
graphics.text(DAYS[current_t[3]], (WIDTH // 2) - (day_length // 2), 0, 16, 1)
graphics.set_pen(RED)
graphics.set_font("bitmap6")