black, lint, add font!

This commit is contained in:
brentru 2020-10-26 16:15:10 -04:00
parent 68fb289d2c
commit 28bd3dc93a
2 changed files with 37 additions and 29 deletions

View file

@ -1,67 +1,75 @@
import time import time
import board
from random import randrange from random import randrange
import rtc import board
import terminalio import terminalio
from adafruit_matrixportal.matrixportal import MatrixPortal from adafruit_matrixportal.matrixportal import MatrixPortal
# --- Data Setup --- # # --- Data Setup --- #
GUIDE_INDEX = 0
# Number of guides to fetch and display from the Adafruit Learning System # Number of guides to fetch and display from the Adafruit Learning System
DISPLAY_NUM_GUIDES = 5 DISPLAY_NUM_GUIDES = 5
DATA_SOURCE = "https://learn.adafruit.com/api/guides/new.json?count=%d"%DISPLAY_NUM_GUIDES # Data source URL
DATA_SOURCE = (
"https://learn.adafruit.com/api/guides/new.json?count=%d" % DISPLAY_NUM_GUIDES
)
TITLE_DATA_LOCATION = ["guides"] TITLE_DATA_LOCATION = ["guides"]
TAGLINE_DATA_LOCATION = ["guides", GUIDE_INDEX, "guide", "tagline"]
# the current working directory (where this file is)
cwd = ("/" + __file__).rsplit("/", 1)[0]
matrixportal = MatrixPortal( matrixportal = MatrixPortal(
url=DATA_SOURCE, url=DATA_SOURCE,
json_path=TITLE_DATA_LOCATION, json_path=TITLE_DATA_LOCATION,
status_neopixel=board.NEOPIXEL, status_neopixel=board.NEOPIXEL,
debug=True
) )
# --- Display Setup --- # # --- Display Setup --- #
# Colors for guide name # Colors for guide name
colors = [0xff0000, 0xffa500, 0xffff00, colors = [0xFFA500, 0xFFFF00, 0x008000, 0x0000FF, 0x4B0082, 0xEE82EE]
0x008000, 0x0000ff, 0x4b0082,
0xee82ee]
# Delay for scrolling the text # Delay for scrolling the text
SCROLL_DELAY = 0.03 SCROLL_DELAY = 0.03
# id = 0, title
matrixportal.add_text(
text_font=terminalio.FONT,
text_position=((matrixportal.graphics.display.width // 3) - 1, (matrixportal.graphics.display.height // 3) - 1),
text_color=0x800000,
text_scale = 2
)
# id = 1, author FONT = "/IBMPlexMono-Medium-24_jep.bdf"
# Learn guide count (ID = 0)
matrixportal.add_text(
text_font=FONT,
text_position=(
(matrixportal.graphics.display.width // 12) - 1,
(matrixportal.graphics.display.height // 2) - 4,
),
text_color=0x800000,
)
matrixportal.preload_font("0123456789")
# Learn guide title (ID = 1)
matrixportal.add_text( matrixportal.add_text(
text_font=terminalio.FONT, text_font=terminalio.FONT,
text_position=(2, 25), text_position=(2, 25),
text_color=0x000080, text_color=0x000080,
scrolling = True scrolling=True,
) )
def get_guide_info(index): def get_guide_info(index):
"""Parses JSON data returned by the DATA_SOURCE
to obtain the ALS guide title and number of guides and
sets the text labels.
:param int index: Guide index to display
"""
if index > DISPLAY_NUM_GUIDES: if index > DISPLAY_NUM_GUIDES:
raise RuntimeError("Provided index may not be larger than DISPLAY_NUM_GUIDES.") raise RuntimeError("Provided index may not be larger than DISPLAY_NUM_GUIDES.")
print("Obtaining guide info for guide %d..."%index) print("Obtaining guide info for guide %d..." % index)
# Traverse JSON data for title # Traverse JSON data for title
guide_count = matrixportal.network.json_traverse(als_data.json(), ["guide_count"]) guide_count = matrixportal.network.json_traverse(als_data.json(), ["guide_count"])
# Set guide count # Set guide count
matrixportal.set_text(guide_count, 0) matrixportal.set_text(guide_count, 0)
guides = matrixportal.network.json_traverse(als_data.json(), TITLE_DATA_LOCATION) guides = matrixportal.network.json_traverse(als_data.json(), TITLE_DATA_LOCATION)
guide_title = guides[index]["guide"]["title"] guide_title = guides[index]["guide"]["title"]
print("Guide Title", guide_title) print("Guide Title", guide_title)
# Select color for title text # Select color for title text
color_index = randrange(0, len(colors)) color_index = randrange(0, len(colors))
@ -72,21 +80,21 @@ def get_guide_info(index):
matrixportal.set_text(guide_title, 1) matrixportal.set_text(guide_title, 1)
refresh_time = None
guide_idx = 0 guide_idx = 0
prv_hour = 0 prv_hour = 0
refresh_time = None
while True: while True:
if (not refresh_time) or (time.monotonic() - refresh_time) > 900:
if (not refresh_time) or (time.monotonic() - refresh_time) > 3600:
try: try:
print("obtaining time from adafruit.io server...") print("obtaining time from adafruit.io server...")
matrixportal.get_local_time() matrixportal.get_local_time()
refresh_time = time.monotonic() refresh_time = time.monotonic()
except RuntimeError as e: except RuntimeError as e:
print("Retrying! - ", e) print("Unable to obtain time from Adafruit IO, retrying - ", e)
continue continue
if time.localtime()[3] != prv_hour: if time.localtime()[3] != prv_hour:
print("New Hour, fetching new data...")
# Fetch and store guide info response # Fetch and store guide info response
als_data = matrixportal.network.fetch(DATA_SOURCE) als_data = matrixportal.network.fetch(DATA_SOURCE)
prv_hour = time.localtime()[3] prv_hour = time.localtime()[3]
@ -95,9 +103,9 @@ while True:
if guide_idx < DISPLAY_NUM_GUIDES: if guide_idx < DISPLAY_NUM_GUIDES:
get_guide_info(guide_idx) get_guide_info(guide_idx)
# Scroll the scrollable text blocks # Scroll the scrollable text block
matrixportal.scroll_text(SCROLL_DELAY) matrixportal.scroll_text(SCROLL_DELAY)
guide_idx += 1 guide_idx += 1
else: else:
guide_idx = 0 guide_idx = 0
time.sleep(0.5) time.sleep(0.05)