Skip to content

Commit 67152e3

Browse files
committed
Copied Cosmic MPy examples to Stellar
1 parent 7aa75e5 commit 67152e3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+7163
-2
lines changed

micropython/examples/cosmic_unicorn/feature_test_with_audio.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ def outline_text(text):
152152
# The current synth beat
153153
beat = 0
154154

155-
text = ""
156-
157155

158156
def next_beat():
159157
global beat
@@ -312,6 +310,8 @@ def tick(timer):
312310
# print("white gradient")
313311
gradient(255, 255, 255)
314312

313+
text = ""
314+
315315
if cu.is_pressed(CosmicUnicorn.SWITCH_A):
316316
text = "PlaySyn"
317317

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# Stellar Unicorn MicroPython Examples <!-- omit in toc -->
2+
3+
- [About Stellar Unicorn](#about-stellar-unicorn)
4+
- [Stellar Unicorn and PicoGraphics](#stellar-unicorn-and-picographics)
5+
- [Examples](#examples)
6+
- [Clock](#clock)
7+
- [Eighties Super Computer](#eighties-super-computer)
8+
- [Feature Test](#feature-test)
9+
- [Feature Test With Audio](#feature-test-with-audio)
10+
- [Fire Effect](#fire-effect)
11+
- [Lava Lamp](#lava-lamp)
12+
- [Nostalgia Prompt](#nostalgia-prompt)
13+
- [Rainbow](#rainbow)
14+
- [Scrolling Text](#scrolling-text)
15+
- [Today](#today)
16+
- [Wireless Examples](#wireless-examples)
17+
- [Cheerlights History](#cheerlights-history)
18+
- [Stellar Paint](#stellar-paint)
19+
- [Exchange Ticker](#exchange-ticker)
20+
- [HTTP Text](#http-text)
21+
- [Weather](#weather)
22+
- [NumPy examples](#numpy-examples)
23+
- [Other Examples](#other-examples)
24+
- [Launch (Demo Reel)](#launch-demo-reel)
25+
- [Other Resources](#other-resources)
26+
27+
## About Stellar Unicorn
28+
29+
Stellar Unicorn offers 16x16 bright RGB LEDs driven by Pico W's PIO in addition to a 1W amplifier + speaker, a collection of system and user buttons, and two Qw/ST connectors for adding external sensors and devices. Woha!
30+
31+
- :link: [Stellar Unicorn store page](https://shop.pimoroni.com/products/stellar-unicorn)
32+
33+
Stellar Unicorn ships with MicroPython firmware pre-loaded, but you can download the most recent version at the link below (you'll want the `stellar-unicorn` image).
34+
35+
- [MicroPython releases](https://github.com/pimoroni/pimoroni-pico/releases)
36+
- [Installing MicroPython](../../../setting-up-micropython.md)
37+
38+
## Stellar Unicorn and PicoGraphics
39+
40+
The easiest way to start displaying cool stuff on Stellar Unicorn is using our Stellar Unicorn module (which contains a bunch of helpful functions for interacting with the buttons, adjusting brightness and suchlike) and our PicoGraphics library, which is chock full of useful functions for drawing on the LED matrix.
41+
42+
- [Stellar Unicorn function reference](../../modules/stellar_unicorn/README.md)
43+
- [PicoGraphics function reference](../../modules/picographics/README.md)
44+
45+
## Examples
46+
47+
### Clock
48+
49+
[clock.py](clock.py)
50+
51+
Clock example with (optional) NTP synchronization. You can adjust the brightness with LUX + and -, and resync the time by pressing A.
52+
53+
### Eighties Super Computer
54+
55+
[eighties_super_computer.py](eighties_super_computer.py)
56+
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 -.
58+
59+
### Feature Test
60+
61+
[feature_test.py](feature_test.py)
62+
63+
Displays some text, gradients and colours and demonstrates button use. You can adjust the brightness with LUX + and -.
64+
65+
### Feature Test With Audio
66+
67+
[feature_test_with_audio.py](feature_test_with_audio.py)
68+
69+
Displays some text, gradients and colours and demonstrates button use. Also demonstrates some of the audio / synth features.
70+
- Button A plays a synth tune
71+
- Button B plays a solo channel of the synth tune
72+
- Button C plays a sinewave (it's frequency can be adjusted with VOL + and -)
73+
- Button D plays a second sinewave (it's frequency can be adjusted with LUX + and -)
74+
- Sleep button stops the sounds
75+
76+
### Fire Effect
77+
78+
[fire_effect.py](fire_effect.py)
79+
80+
A pretty, procedural fire effect. Switch between landscape fire and vertical fire using the A and B buttons! You can adjust the brightness with LUX + and -.
81+
82+
### Lava Lamp
83+
84+
[lava_lamp.py](lava_lamp.py)
85+
86+
A 70s-tastic, procedural rainbow lava lamp. You can adjust the brightness with LUX + and -.
87+
88+
### Nostalgia Prompt
89+
90+
[nostalgia_prompt.py](nostalgia_prompt.py)
91+
92+
A collection of copies of classic terminal styles including C64, MS-DOS, Spectrum, and more. Images and text are drawn pixel by pixel from a pattern of Os and Xs. You can adjust the brightness with LUX + and -.
93+
94+
### Rainbow
95+
96+
[rainbow.py](rainbow.py)
97+
98+
Some good old fashioned rainbows! You can adjust the cycling speed with A and B, stripe width with C and D, hue with VOL + and -, and the brightness with LUX + and -. The sleep button stops the animation (can be started again with A or B).
99+
100+
### Scrolling Text
101+
102+
[scrolling_text.py](scrolling_text.py)
103+
104+
Display scrolling wisdom, quotes or greetz. You can adjust the brightness with LUX + and -.
105+
106+
### Today
107+
108+
[today.py](today.py)
109+
110+
Calendar example with (optional) NTP synchronization. You can adjust the brightness with LUX + and -, and resync the date by pressing C.
111+
112+
## Wireless Examples
113+
114+
These examples need `WIFI_CONFIG.py` and `network_manager.py` (from the `common` directory) to be saved to your Pico W. Open up `WIFI_CONFIG.py` in Thonny to add your wifi details (and save it when you're done).
115+
116+
- [micropython/examples/common](../../examples/common)
117+
118+
### Cheerlights History
119+
120+
[cheerlights_history.py](cheerlights_history.py)
121+
122+
Updates one pixel every two minutes to display the most recent #Cheerlights colour. Discover the most popular colours over time, or use it as an avant garde (but colourful) 16 hour clock! Find out more about the Cheerlights API at https://cheerlights.com/
123+
124+
You can adjust the brightness with LUX + and -.
125+
126+
### Stellar Paint
127+
128+
[stellar_paint](stellar_paint)
129+
130+
Draw on your Stellar Unicorn from another device in real time, over wifi!
131+
132+
This example needs the `micropython-phew` and `microdot` libraries (you can install these using Thonny's 'Tools > Manage Packages').
133+
134+
### Exchange Ticker
135+
136+
[exchange_ticker.py](exchange_ticker.py)
137+
138+
This example uses the Coinbase open API to collect the current exchange rates of various cryptocurrencies.
139+
140+
Press A to change to a different base exchange currency.
141+
142+
### HTTP Text
143+
144+
[http_text](http_text)
145+
146+
Display scrolling wisdom, quotes or greetz... from another computer or device!
147+
148+
You can adjust the brightness with LUX + and -.
149+
150+
Requires `logging.mpy` and `tinyweb` from [micropython/examples/common](../../examples/common) - copy these into the `lib` folder on your Pico W. You'll also need `index.html` to be saved alongside `html_text.py`.
151+
152+
### Weather
153+
154+
[weather](weather)
155+
156+
Display current weather data from the [Open-Meteo](https://open-meteo.com/) weather API.
157+
158+
## NumPy examples
159+
160+
[numpy](numpy)
161+
162+
The examples in the folder use `numpy`-like array functions contained in the `ulab` library for super fast graphical effects.
163+
164+
## Other Examples
165+
166+
### Launch (Demo Reel)
167+
168+
[launch](launch)
169+
170+
If you want to get the demo reel that Stellar Unicorn ships with back, copy the contents of this `launch` folder to your Pico W.
171+
172+
## Other Resources
173+
174+
Here are some cool Stellar Unicorn community projects and resources that you might find useful / inspirational! Note that code at the links below has not been tested by us and we're not able to offer support with it.
175+
176+
- :link: [Green Energy Display with Stellar Unicorn](https://www.hackster.io/andreas-motzek/clock-and-green-energy-display-with-stellar-unicorn-641dcb)
177+
- :link: [stellar-emoji-react - paint emojis from a computer, phone or tablet](https://github.com/chriscareycode/stellar-unicorn/tree/main/stellar-emoji-react)
178+
- :link: [stellar-paste - paste images from the clipboard to Stellar Unicorn](https://github.com/chriscareycode/stellar-unicorn/tree/main/stellar-paste)
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# This Stellar Unicorn example updates a pixel every two(ish) minutes
2+
# to display the most recent #cheerlights colour. Discover the most popular
3+
# colours over time, or use it as an avant garde (but colourful) 16 hour clock!
4+
# Find out more about the Cheerlights API at https://cheerlights.com/
5+
#
6+
# To run this example you'll need WIFI_CONFIG.py and network_manager.py from
7+
# the pimoroni-pico micropython/examples/common folder
8+
9+
import WIFI_CONFIG
10+
from network_manager import NetworkManager
11+
import uasyncio
12+
import urequests
13+
import time
14+
from machine import Timer, Pin
15+
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
22+
23+
24+
def status_handler(mode, status, ip):
25+
# reports wifi connection status
26+
print(mode, status, ip)
27+
print('Connecting to wifi...')
28+
if status is not None:
29+
if status:
30+
print('Wifi connection successful!')
31+
else:
32+
print('Wifi connection failed!')
33+
34+
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+
42+
def get_data():
43+
# 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()
50+
51+
# flash the onboard LED after getting data
52+
pico_led.value(True)
53+
time.sleep(0.2)
54+
pico_led.value(False)
55+
56+
# extract hex colour from the json data
57+
hex = j['field2']
58+
59+
# 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
80+
su.update(graphics)
81+
print("LEDs updated!")
82+
83+
84+
su = StellarUnicorn()
85+
graphics = PicoGraphics(DISPLAY)
86+
87+
width = StellarUnicorn.WIDTH
88+
height = StellarUnicorn.HEIGHT
89+
90+
su.set_brightness(0.5)
91+
92+
# set up the Pico W's onboard LED
93+
pico_led = Pin('LED', Pin.OUT)
94+
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+
100+
# set up wifi
101+
try:
102+
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler)
103+
uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK))
104+
except Exception as e:
105+
print(f'Wifi connection failed! {e}')
106+
107+
# get the first lot of data
108+
get_data()
109+
110+
# start timer (the timer will call the function to update our data every UPDATE_INTERVAL)
111+
timer = Timer(-1)
112+
timer.init(period=UPDATE_INTERVAL * 1000, mode=Timer.PERIODIC, callback=lambda t: get_data())
113+
114+
while True:
115+
# adjust brightness with LUX + and -
116+
# LEDs take a couple of secs to update, so adjust in big (10%) steps
117+
if su.is_pressed(StellarUnicorn.SWITCH_BRIGHTNESS_UP):
118+
su.adjust_brightness(+0.1)
119+
update_leds()
120+
print(f"Brightness set to {su.get_brightness()}")
121+
122+
if su.is_pressed(StellarUnicorn.SWITCH_BRIGHTNESS_DOWN):
123+
su.adjust_brightness(-0.1)
124+
update_leds()
125+
print(f"Brightness set to {su.get_brightness()}")
126+
127+
# pause for a moment (important or the USB serial device will fail)
128+
time.sleep(0.001)

0 commit comments

Comments
 (0)