-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #546 from Corteil/Galactic-Unicorn-CheerLights
CheerLights example for the Galactic Unicorn
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# cheerlights | ||
from time import sleep | ||
from galactic import GalacticUnicorn | ||
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY | ||
import uasyncio | ||
from network_manager import NetworkManager | ||
import urequests | ||
import WIFI_CONFIG | ||
|
||
|
||
# setup graphics | ||
gu = GalacticUnicorn() | ||
graphics = PicoGraphics(DISPLAY) | ||
width = GalacticUnicorn.WIDTH | ||
height = GalacticUnicorn.HEIGHT | ||
|
||
|
||
# fill display | ||
def draw(colour): | ||
|
||
graphics.set_pen(colour) | ||
graphics.clear() | ||
gu.update(graphics) | ||
|
||
|
||
def status_handler(mode, status, ip): | ||
# reports wifi connection status | ||
print(mode, status, ip) | ||
print('Connecting to wifi...') | ||
if status is not None: | ||
if status: | ||
print('Wifi connection successful!') | ||
else: | ||
print('Wifi connection failed!') | ||
|
||
|
||
# set up wifi | ||
try: | ||
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler) | ||
uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK)) | ||
except Exception as e: | ||
print(f'Wifi connection failed! {e}') | ||
|
||
|
||
# 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 | ||
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) |