Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quarantine clock
# Requirement:
# MagTag: https://www.adafruit.com/product/4800 (or 4819 for the kit)
#
# CIRCUITPY drive:
# /slides/background.bmp
# /fonts/EffectsEighty-24.bdf
# /fonts/EffectsEighty-32.bdf
# /fonts/EffectsEighty-48.bdf
#
# Libraries
# adafruit_il0373.mpy mandatory
# adafruit_requests.mpy mandatory
# adafruit_bitmap_font mandatory
# adafruit_display_text mandatory
# adafruit_imageload mandatory
import sys
import time
import board
import wifi
import socketpool
import adafruit_requests
import ssl
import rtc
import displayio
import terminalio
import adafruit_il0373
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
import adafruit_imageload
import secrets
# Based on:
# Base MagTag support code, without using MagTag library:
# https://gist.github.com/ladyada/2f117c1c83ed78e02c0f324724474698
# Quarantine Clock inspiration:
# https://github.com/mwfisher3/QuarantineClock/blob/master/today.html
# Learn guide for the same thing on PyPortal
# https://learn.adafruit.com/pyportal-quarantine-clock/quarantine-month-clock
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
)
# We'll draw with a classic black/white palette
palette = displayio.Palette(2)
palette[0] = 0xFFFFFF
palette[1] = 0x000000
splash = displayio.Group()
board.DISPLAY.show(splash)
grey_pal = displayio.Palette(4)
grey_pal[0] = 0x000000
grey_pal[1] = 0x555555
grey_pal[2] = 0xaaaaaa
grey_pal[3] = 0xFFFFFF
image, palette = adafruit_imageload.load(
"/slides/background.bmp",
bitmap=displayio.Bitmap,
palette=displayio.Palette
)
bg_sprite = displayio.TileGrid(image, pixel_shader=palette, x = 0, y = 0)
# Descriptions of each hour
hours_names = [
"midnight-ish",
"late night",
"late",
"super late",
"super early",
"really early",
"dawn",
"morning",
"morning",
"mid-morning",
"mid-morning",
"late morning",
"noon-ish",
"afternoon",
"afternoon",
"mid-afternoon",
"late afternoon",
"early evening",
"early evening",
"dusk-ish",
"evening",
"evening",
"late evening",
"late evening"]
# Dictionary of tm_mon and month name.
# note: tm_mon structure in CircuitPython ranges from [1,12]
months = {
1: "January",
2: "February",
3: "March",
4: "April",
5: "May",
6: "June",
7: "July",
8: "August",
9: "September",
10: "October",
11: "November",
12: "December"
}
# Dictionary of tm_mon and month name.
# note: tm_wday structure in CircuitPython ranges from [0,6]
days = {
0: "Monday",
1: "Tuesday",
2: "Wednesday",
3: "Thursday",
4: "Friday",
5: "Saturday",
6: "Sunday",
}
# Set the font
font_three = bitmap_font.load_font("/fonts/EffectsEighty-24.bdf")
#font_three = bitmap_font.load_font("/fonts/EffectsEighty-32.bdf")
text_three = label.Label(font_three,
text="",
max_glyphs=40,
anchor_point = (0.5, 0.5),
x = display.width//2,
y = display.height//4,
color = 0xaaaaaa)
font_two = bitmap_font.load_font("/fonts/EffectsEighty-32.bdf")
#font_two = bitmap_font.load_font("/fonts/EffectsEighty-48.bdf")
text_two = label.Label(font_two,
text="",
max_glyphs=40,
anchor_point = (0.5, 0.5),
x = display.width//2,
y = display.height//2,
color = 0x555555)
font_one = bitmap_font.load_font("/fonts/EffectsEighty-48.bdf")
#font_one = bitmap_font.load_font("/fonts/EffectsEightyBold-68.bdf")
text_one = label.Label(font_one,
text="",
max_glyphs=40,
anchor_point = (0.5, 0.5),
x = display.width//2,
y = (display.height*3)//4,
color = 0x000000)
splash = displayio.Group()
splash.append(bg_sprite)
splash.append(text_one)
splash.append(text_two)
splash.append(text_three)
display.show(splash)
display.refresh()
ssid = secrets.secrets['ssid']
password = secrets.secrets['password']
connected = False
try:
wifi.radio.connect(ssid, password)
connected = True
except:
print("Trying the alt SSID and password.")
ssid = secrets.secrets['alt_ssid']
password = secrets.secrets['alt_password']
try:
wifi.radio.connect(ssid, password)
connected = True
except:
print("Could not connect at all.")
print("ip", wifi.radio.ipv4_address)
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
# Still set to New York timing
response = requests.get("http://worldtimeapi.org/api/timezone/America/New_York")
#response = requests.get("http://worldtimeapi.org/api/timezone/CET")
print(response.status_code)
print(response)
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)
# convert tm_mon value to month name
tmp_month = months[now.tm_mon]
# determine and display how far we are in the month
if 1 <= now.tm_mday <= 14:
month = "Early %s-ish"%tmp_month
elif 15 <= now.tm_mday <= 24:
month = "Mid %s-ish"%tmp_month
else:
month = "Late %s-ish"%tmp_month
day = days[now.tm_wday]
hour = hours_names[now.tm_hour]
text_one.text = hour
text_two.text = day
text_three.text = month
display.refresh()
time.sleep(300)
@dglaude

This comment has been minimized.

Copy link
Owner Author

@dglaude dglaude commented Nov 23, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment