Create cheerlights.py

Updated to use the Pimoroni standard Wi-Fi method to be inline with other examples and rename filed to cheerlights.py so can be import from REPL
pull/546/head
Brian Corteil 2023-02-14 23:07:35 +00:00
rodzic 4082d99186
commit 47a8373bbb
1 zmienionych plików z 65 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,65 @@
# 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):
for x in range(width):
for y in range(height):
graphics.set_pen(colour)
graphics.pixel(x, y)
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)