Skip to content

Commit 28bd3dc

Browse files
author
brentru
committed
black, lint, add font!
1 parent 68fb289 commit 28bd3dc

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed
Binary file not shown.

Matrix_Portal_Learn_Stats/code.py

+34-26
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,75 @@
11
import time
2-
import board
32
from random import randrange
4-
import rtc
3+
import board
54
import terminalio
65
from adafruit_matrixportal.matrixportal import MatrixPortal
76

87
# --- Data Setup --- #
9-
GUIDE_INDEX = 0
108
# Number of guides to fetch and display from the Adafruit Learning System
119
DISPLAY_NUM_GUIDES = 5
12-
DATA_SOURCE = "https://learn.adafruit.com/api/guides/new.json?count=%d"%DISPLAY_NUM_GUIDES
10+
# Data source URL
11+
DATA_SOURCE = (
12+
"https://learn.adafruit.com/api/guides/new.json?count=%d" % DISPLAY_NUM_GUIDES
13+
)
1314
TITLE_DATA_LOCATION = ["guides"]
14-
TAGLINE_DATA_LOCATION = ["guides", GUIDE_INDEX, "guide", "tagline"]
15-
16-
# the current working directory (where this file is)
17-
cwd = ("/" + __file__).rsplit("/", 1)[0]
1815

1916
matrixportal = MatrixPortal(
2017
url=DATA_SOURCE,
2118
json_path=TITLE_DATA_LOCATION,
2219
status_neopixel=board.NEOPIXEL,
23-
debug=True
2420
)
2521

2622
# --- Display Setup --- #
2723

2824
# Colors for guide name
29-
colors = [0xff0000, 0xffa500, 0xffff00,
30-
0x008000, 0x0000ff, 0x4b0082,
31-
0xee82ee]
25+
colors = [0xFFA500, 0xFFFF00, 0x008000, 0x0000FF, 0x4B0082, 0xEE82EE]
3226

3327
# Delay for scrolling the text
3428
SCROLL_DELAY = 0.03
35-
# id = 0, title
29+
30+
FONT = "/IBMPlexMono-Medium-24_jep.bdf"
31+
32+
# Learn guide count (ID = 0)
3633
matrixportal.add_text(
37-
text_font=terminalio.FONT,
38-
text_position=((matrixportal.graphics.display.width // 3) - 1, (matrixportal.graphics.display.height // 3) - 1),
34+
text_font=FONT,
35+
text_position=(
36+
(matrixportal.graphics.display.width // 12) - 1,
37+
(matrixportal.graphics.display.height // 2) - 4,
38+
),
3939
text_color=0x800000,
40-
text_scale = 2
4140
)
41+
matrixportal.preload_font("0123456789")
4242

43-
# id = 1, author
43+
# Learn guide title (ID = 1)
4444
matrixportal.add_text(
4545
text_font=terminalio.FONT,
4646
text_position=(2, 25),
4747
text_color=0x000080,
48-
scrolling = True
48+
scrolling=True,
4949
)
5050

51+
5152
def get_guide_info(index):
53+
"""Parses JSON data returned by the DATA_SOURCE
54+
to obtain the ALS guide title and number of guides and
55+
sets the text labels.
56+
:param int index: Guide index to display
57+
58+
"""
5259
if index > DISPLAY_NUM_GUIDES:
5360
raise RuntimeError("Provided index may not be larger than DISPLAY_NUM_GUIDES.")
54-
print("Obtaining guide info for guide %d..."%index)
61+
print("Obtaining guide info for guide %d..." % index)
5562

5663
# Traverse JSON data for title
5764
guide_count = matrixportal.network.json_traverse(als_data.json(), ["guide_count"])
65+
5866
# Set guide count
5967
matrixportal.set_text(guide_count, 0)
6068

6169
guides = matrixportal.network.json_traverse(als_data.json(), TITLE_DATA_LOCATION)
6270
guide_title = guides[index]["guide"]["title"]
6371
print("Guide Title", guide_title)
64-
72+
6573
# Select color for title text
6674
color_index = randrange(0, len(colors))
6775

@@ -72,21 +80,21 @@ def get_guide_info(index):
7280
matrixportal.set_text(guide_title, 1)
7381

7482

83+
refresh_time = None
7584
guide_idx = 0
7685
prv_hour = 0
77-
refresh_time = None
7886
while True:
79-
80-
if (not refresh_time) or (time.monotonic() - refresh_time) > 3600:
87+
if (not refresh_time) or (time.monotonic() - refresh_time) > 900:
8188
try:
8289
print("obtaining time from adafruit.io server...")
8390
matrixportal.get_local_time()
8491
refresh_time = time.monotonic()
8592
except RuntimeError as e:
86-
print("Retrying! - ", e)
93+
print("Unable to obtain time from Adafruit IO, retrying - ", e)
8794
continue
8895

8996
if time.localtime()[3] != prv_hour:
97+
print("New Hour, fetching new data...")
9098
# Fetch and store guide info response
9199
als_data = matrixportal.network.fetch(DATA_SOURCE)
92100
prv_hour = time.localtime()[3]
@@ -95,9 +103,9 @@ def get_guide_info(index):
95103
if guide_idx < DISPLAY_NUM_GUIDES:
96104
get_guide_info(guide_idx)
97105

98-
# Scroll the scrollable text blocks
106+
# Scroll the scrollable text block
99107
matrixportal.scroll_text(SCROLL_DELAY)
100108
guide_idx += 1
101109
else:
102110
guide_idx = 0
103-
time.sleep(0.5)
111+
time.sleep(0.05)

0 commit comments

Comments
 (0)