Created
October 25, 2022 22:54
-
-
Save Corteil/2a52914dda2cd65438c92efda1c10884 to your computer and use it in GitHub Desktop.
CheerLights MicroPython client for the Pimoroni Galactic Unicorn. You will need the secrets file with your network details, in the same folder as cheerlights.py to run on start rename cheerlights.py to main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# cheerlights | |
from time import sleep | |
from galactic import GalacticUnicorn | |
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY | |
# setup graphics | |
gu = GalacticUnicorn() | |
graphics = PicoGraphics(DISPLAY) | |
width = GalacticUnicorn.WIDTH | |
height = GalacticUnicorn.HEIGHT | |
# fill display | |
def draw(colour): | |
for x in range(width): | |
for y in range(height): | |
graphics.set_pen(colour) | |
graphics.pixel(x, y) | |
gu.update(graphics) | |
#setup network connection | |
import network | |
try: | |
from secrets import WIFI_SSID, WIFI_PASS | |
except ImportError: | |
WIFI_SSID = None | |
WIFI_PASS = None | |
wlan = network.WLAN(network.STA_IF) | |
wlan.active(True) | |
wlan.connect(WIFI_SSID, WIFI_PASS) | |
# Wait for connect or fail | |
max_wait = 10 | |
while max_wait > 0: | |
if wlan.status() < 0 or wlan.status() >= 3: | |
break | |
max_wait -= 1 | |
print('waiting for connection...') | |
sleep(1) | |
# Handle connection error | |
if wlan.status() != 3: | |
raise RuntimeError('network connection failed') | |
else: | |
print('connected') | |
status = wlan.ifconfig() | |
print( f'ip = {status[0]}' ) | |
# cheerlights | |
# cheerlights colours | |
colours = {b'red': graphics.create_pen(0xff, 0x00, 0x00), b'green': graphics.create_pen(0x00, 0x80, 0x00), b'blue': graphics.create_pen(0x00, 0x00, 0xff), | |
b'cyan': graphics.create_pen(0x00, 0xff, 0xff), b'white': graphics.create_pen(0xff, 0xff, 0xff), b'oldlace': graphics.create_pen(0xfd, 0xf5, 0xe6), | |
b'purple': graphics.create_pen(0x80, 0x00, 0x80), b'magenta': graphics.create_pen(0xff, 0x00, 0xff), b'yellow': graphics.create_pen(0xff, 0xff, 0x00), | |
b'orange': graphics.create_pen(0xff, 0xa5, 0x00), b'pink':graphics.create_pen(0xff, 0xc0, 0xcb)} | |
# get request | |
import urequests | |
while True: | |
r = urequests.get('http://api.thingspeak.com/channels/1417/field/1/last.txt') | |
print(f'Colour: {r.content}') | |
if r.content in colours: | |
draw(colours[r.content]) | |
r.close() | |
sleep(15) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WIFI_SSID = "name" | |
WIFI_PASS = "password" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment