Skip to content

Commit 17653a8

Browse files
authored
Merge pull request adafruit#1287 from brentru/brentru-matrix-portal-learn-stats
Matrix Portal Learn Guide Stats
2 parents e4427a0 + 086a532 commit 17653a8

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Binary file not shown.

Matrix_Portal_Learn_Stats/code.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import time
2+
from random import randrange
3+
import board
4+
import terminalio
5+
from adafruit_matrixportal.matrixportal import MatrixPortal
6+
7+
# --- Data Setup --- #
8+
# Number of guides to fetch and display from the Adafruit Learning System
9+
DISPLAY_NUM_GUIDES = 5
10+
# Data source URL
11+
DATA_SOURCE = (
12+
"https://learn.adafruit.com/api/guides/new.json?count=%d" % DISPLAY_NUM_GUIDES
13+
)
14+
TITLE_DATA_LOCATION = ["guides"]
15+
16+
matrixportal = MatrixPortal(
17+
url=DATA_SOURCE,
18+
json_path=TITLE_DATA_LOCATION,
19+
status_neopixel=board.NEOPIXEL,
20+
)
21+
22+
# --- Display Setup --- #
23+
24+
# Colors for guide name
25+
colors = [0xFFA500, 0xFFFF00, 0x008000, 0x0000FF, 0x4B0082, 0xEE82EE]
26+
27+
# Delay for scrolling the text
28+
SCROLL_DELAY = 0.03
29+
30+
FONT = "/IBMPlexMono-Medium-24_jep.bdf"
31+
32+
# Learn guide count (ID = 0)
33+
matrixportal.add_text(
34+
text_font=FONT,
35+
text_position=(
36+
(matrixportal.graphics.display.width // 12) - 1,
37+
(matrixportal.graphics.display.height // 2) - 4,
38+
),
39+
text_color=0x800000,
40+
)
41+
matrixportal.preload_font("0123456789")
42+
43+
# Learn guide title (ID = 1)
44+
matrixportal.add_text(
45+
text_font=terminalio.FONT,
46+
text_position=(2, 25),
47+
text_color=0x000080,
48+
scrolling=True,
49+
)
50+
51+
52+
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+
"""
59+
if index > DISPLAY_NUM_GUIDES:
60+
raise RuntimeError("Provided index may not be larger than DISPLAY_NUM_GUIDES.")
61+
print("Obtaining guide info for guide %d..." % index)
62+
63+
# Traverse JSON data for title
64+
guide_count = matrixportal.network.json_traverse(als_data.json(), ["guide_count"])
65+
66+
# Set guide count
67+
matrixportal.set_text(guide_count, 0)
68+
69+
guides = matrixportal.network.json_traverse(als_data.json(), TITLE_DATA_LOCATION)
70+
guide_title = guides[index]["guide"]["title"]
71+
print("Guide Title", guide_title)
72+
73+
# Select color for title text
74+
color_index = randrange(0, len(colors))
75+
76+
# Set the title text color
77+
matrixportal.set_text_color(colors[color_index], 1)
78+
79+
# Set the title text
80+
matrixportal.set_text(guide_title, 1)
81+
82+
83+
refresh_time = None
84+
guide_idx = 0
85+
prv_hour = 0
86+
while True:
87+
if (not refresh_time) or (time.monotonic() - refresh_time) > 900:
88+
try:
89+
print("obtaining time from adafruit.io server...")
90+
matrixportal.get_local_time()
91+
refresh_time = time.monotonic()
92+
except RuntimeError as e:
93+
print("Unable to obtain time from Adafruit IO, retrying - ", e)
94+
continue
95+
96+
if time.localtime()[3] != prv_hour:
97+
print("New Hour, fetching new data...")
98+
# Fetch and store guide info response
99+
als_data = matrixportal.network.fetch(DATA_SOURCE)
100+
prv_hour = time.localtime()[3]
101+
102+
# Cycle through guides retrieved
103+
if guide_idx < DISPLAY_NUM_GUIDES:
104+
get_guide_info(guide_idx)
105+
106+
# Scroll the scrollable text block
107+
matrixportal.scroll_text(SCROLL_DELAY)
108+
guide_idx += 1
109+
else:
110+
guide_idx = 0
111+
time.sleep(0.05)

0 commit comments

Comments
 (0)