Created
December 22, 2023 16:03
-
-
Save jepler/0388f08328ec1bc77ceaa64bca2d7b2c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import io | |
import os | |
import ssl | |
import time | |
import displayio | |
import adafruit_requests | |
import board | |
import jpegio | |
import socketpool | |
import wifi | |
class ResponseStreamAdapter(io.IOBase): | |
def __init__(self, response): | |
self._response = response | |
def readinto(self, buf): | |
result = self._response._readinto(buf) | |
return result | |
t0 = time.monotonic_ns() | |
def tprint(msg): | |
t1 = time.monotonic_ns() | |
ms = (t1 - t0) // 1_000_000 | |
s = ms // 1000 | |
ms = ms % 1000 | |
print(f"{s:+4d}.{ms:04d}: {msg}") | |
tprint("connecting to wifi") | |
wifi.radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD")) | |
pool = socketpool.SocketPool(wifi.radio) | |
requests = adafruit_requests.Session(pool, ssl.create_default_context()) | |
# image_url = "https://cdn-shop.adafruit.com/970x728/5843-05.jpg" | |
image_url = "https://timeguy.com/images/avatar.jpg" | |
j = jpegio.JpegDecoder() | |
tprint("fetching image") | |
r = requests.get(image_url) | |
tprint(f"status {r.status_code}") | |
if r.status_code == 200: | |
stream = ResponseStreamAdapter(r) | |
w, h = j.open(stream) | |
tprint(f"Image size is {w}x{h}") | |
tprint("Decoding image") | |
b = displayio.Bitmap(w, h, 65535) | |
j.decode(b) | |
tprint("Decoding complete") | |
gr = displayio.Group() | |
tg = displayio.TileGrid( | |
b, | |
pixel_shader=displayio.ColorConverter( | |
input_colorspace=displayio.Colorspace.RGB565_SWAPPED | |
), | |
) | |
gr.append(tg) | |
board.DISPLAY.root_group = gr | |
while True: | |
pass | |
else: | |
print(r.status_code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment