-
-
Save zackkitzmiller/7352344f7347835560ea570a1f4c8ab1 to your computer and use it in GitHub Desktop.
CircuitPython Countdown for MagTag Demo
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 sys | |
import time | |
import board | |
import wifi | |
import socketpool | |
import adafruit_requests | |
import ssl | |
import rtc | |
import displayio | |
import terminalio | |
import adafruit_il0373 | |
import adafruit_miniqr | |
from adafruit_display_text import label | |
from adafruit_bitmap_font import bitmap_font | |
# The time of the thing! | |
EVENT_YEAR = 2021 | |
EVENT_MONTH = 1 | |
EVENT_DAY = 20 | |
EVENT_HOUR = 12 | |
EVENT_MINUTE = 00 | |
# we'll make a python-friendly structure | |
event_time = time.struct_time((EVENT_YEAR, EVENT_MONTH, EVENT_DAY, | |
EVENT_HOUR, EVENT_MINUTE, 0, # we don't track seconds | |
-1, -1, False)) # we dont know day of week/year or DST | |
displayio.release_displays() | |
display_bus = displayio.FourWire(board.SPI(), command=board.EPD_DC, | |
chip_select=board.EPD_CS, | |
reset=board.EPD_RESET, baudrate=1000000) | |
time.sleep(1) | |
display = adafruit_il0373.IL0373( | |
display_bus, | |
width=296, | |
height=128, | |
rotation=270, | |
black_bits_inverted=False, | |
color_bits_inverted=False, | |
grayscale=True, | |
refresh_time=1, | |
seconds_per_frame=5 | |
) | |
def bitmap_QR(matrix): | |
# monochome (2 color) palette | |
BORDER_PIXELS = 2 | |
# bitmap the size of the screen, monochrome (2 colors) | |
bitmap = displayio.Bitmap( | |
matrix.width + 2 * BORDER_PIXELS, matrix.height + 2 * BORDER_PIXELS, 2 | |
) | |
# raster the QR code | |
for y in range(matrix.height): # each scanline in the height | |
for x in range(matrix.width): | |
if matrix[x, y]: | |
bitmap[x + BORDER_PIXELS, y + BORDER_PIXELS] = 1 | |
else: | |
bitmap[x + BORDER_PIXELS, y + BORDER_PIXELS] = 0 | |
return bitmap | |
qr = adafruit_miniqr.QRCode(qr_type=3, error_correct=adafruit_miniqr.L) | |
qr.add_data(b"https://buildbackbetter.com/") | |
qr.make() | |
# generate the 1-pixel-per-bit bitmap | |
qr_bitmap = bitmap_QR(qr.matrix) | |
# We'll draw with a classic black/white palette | |
palette = displayio.Palette(2) | |
palette[0] = 0xFFFFFF | |
palette[1] = 0x000000 | |
# we'll scale the QR code as big as the display can handle | |
scale = min( | |
display.width // qr_bitmap.width, display.height // qr_bitmap.height | |
) | |
# then put it on the right | |
pos_x = int((display.width / scale) - qr_bitmap.width) | |
pos_y = int(((display.height / scale) - qr_bitmap.height) / 2) | |
qr_img = displayio.TileGrid(qr_bitmap, pixel_shader=palette, x=pos_x, y=pos_y) | |
qr_group = displayio.Group(scale=scale) | |
qr_group.append(qr_img) | |
# bg color | |
bg_bm = displayio.Bitmap(display.width, display.height, 1) | |
bg_sprite = displayio.TileGrid(bg_bm, pixel_shader=palette, x = 0, y = 0) | |
# text label | |
font = bitmap_font.load_font("Arial-Bold-24.bdf") | |
text_area = label.Label(font, text="Loading...", max_glyphs=40, | |
anchor_point = (0, 0.5), line_spacing=0.75, | |
x = 10, y = display.height//2, color = 0x0) | |
splash = displayio.Group() | |
splash.append(bg_sprite) | |
splash.append(qr_group) | |
splash.append(text_area) | |
display.show(splash) | |
display.refresh() | |
wifi.radio.connect("adafruit", "ffffffff") | |
print("ip", wifi.radio.ipv4_address) | |
#ipv4 = ipaddress.ip_address("8.8.4.4") | |
#print(ipv4) | |
#print("ping", wifi.radio.ping(ipv4)) | |
pool = socketpool.SocketPool(wifi.radio) | |
requests = adafruit_requests.Session(pool, ssl.create_default_context()) | |
response = requests.get("http://worldtimeapi.org/api/timezone/America/New_York") | |
#print(response.status_code) | |
datetime_str = response.json()['datetime'] | |
#print(datetime_str) | |
datesplit = datetime_str.split("-") | |
year = int(datesplit[0]) | |
month = int(datesplit[1]) | |
timesplit = datesplit[2].split("T") | |
mday = int(timesplit[0]) | |
timesplit = timesplit[1].split(":") | |
hours = int(timesplit[0]) | |
minutes = int(timesplit[1]) | |
seconds = int(float(timesplit[2].split("-")[0])) | |
rtc.RTC().datetime = time.struct_time((year, month, mday, hours, minutes, seconds, 0, 0, False)) | |
while True: | |
now = time.localtime() | |
print("Current time:", now) | |
remaining = time.mktime(event_time) - time.mktime(now) | |
print("Time remaining (s):", remaining) | |
if remaining < 0: | |
print("EVENT TIME") | |
while True: # that's all folks | |
pass | |
secs_remaining = remaining % 60 | |
remaining //= 60 | |
mins_remaining = remaining % 60 | |
remaining //= 60 | |
hours_remaining = remaining % 24 | |
remaining //= 24 | |
days_remaining = remaining | |
print("%d days, %d hours, %d minutes and %s seconds" % | |
(days_remaining, hours_remaining, mins_remaining, secs_remaining)) | |
text_area.text = "%d Days\n%d Hours\n%d Mins" % (days_remaining, hours_remaining, mins_remaining) | |
display.refresh() | |
time.sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment