Created
November 27, 2021 14:11
-
-
Save rsbohn/8a3412e70f0f175437fdb290e145e04f to your computer and use it in GitHub Desktop.
Uptime QR Code
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
# SPDX-FileCopyrightText: 2021 Randall Bohn | |
# | |
# SPDX-Licence-Identifier: MIT | |
"Uptime QR Code" | |
import time | |
import displayio | |
from adafruit_featherwing import tft_featherwing_35 | |
from adafruit_bitmap_font.bitmap_font import load_font | |
from adafruit_display_text.label import Label | |
from adafruit_miniqr import QRCode | |
displayio.release_displays() | |
wing = tft_featherwing_35.TFTFeatherWing35() | |
display = wing.display | |
font = load_font("/fonts/Arial-12.pcf") | |
palette = displayio.Palette(2) | |
palette[0] = 0x888888 | |
def uptime(): | |
now = time.monotonic() | |
return b"%d:%02d:%02d" % (now // 3600, now % 3600 // 60, now % 60) | |
def qrnow(): | |
qr = QRCode(qr_type=2) | |
qr.add_data(uptime()) | |
qr.make() | |
return qr | |
def fit_bitmap(size): | |
return displayio.Bitmap(size,size,2) | |
def imprint(qrcode, bitmap=None): | |
border = 2 | |
m = qrcode.matrix | |
if bitmap == None: | |
print("creating bitmap") | |
bitmap = fit_bitmap(m.width+border*2) | |
for y in range(m.height): | |
for x in range(m.width): | |
bitmap[border+x,border+y] = m[x,y] != 0 | |
return bitmap | |
label = Label(font, text="Starting...", x=12, y=display.height * 3//8) | |
splash = displayio.Group(scale=2) | |
splash.append(label) | |
bitmap = imprint(qrnow()) | |
grid = displayio.TileGrid(bitmap, pixel_shader=palette, | |
x=display.width//4-bitmap.width//2, y=display.height * 1 // 8) | |
splash.append(grid) | |
display.show(splash) | |
while True: | |
now = time.monotonic() | |
if int(now % 5) == 0: | |
imprint(qrnow(), bitmap) | |
label.text = "Uptime: "+uptime().decode('utf-8') | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment