Skip to content

Commit 9c7b6e6

Browse files
author
brentru
committed
Guide #, title, add aio time service
1 parent ec743a8 commit 9c7b6e6

File tree

1 file changed

+52
-28
lines changed

1 file changed

+52
-28
lines changed

Matrix_Portal_Learn_Stats/code.py

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import time
22
import board
3-
import terminalio
43
import rtc
4+
import terminalio
55
from adafruit_matrixportal.matrixportal import MatrixPortal
66

77
# --- Data Setup --- #
88
GUIDE_INDEX = 0
99
# Number of guides to fetch and display from the Adafruit Learning System
1010
DISPLAY_NUM_GUIDES = 5
1111
DATA_SOURCE = "https://learn.adafruit.com/api/guides/new.json?count=%d"%DISPLAY_NUM_GUIDES
12-
TITLE_DATA_LOCATION = ["guides", GUIDE_INDEX, "guide", "title"]
12+
TITLE_DATA_LOCATION = ["guides"]
1313
TAGLINE_DATA_LOCATION = ["guides", GUIDE_INDEX, "guide", "tagline"]
1414

1515
# the current working directory (where this file is)
@@ -22,44 +22,68 @@
2222
debug=True
2323
)
2424

25-
print(matrixportal.graphics.display.height)
25+
# --- Display Setup --- #
26+
# Delay for scrolling the text
27+
SCROLL_DELAY = 0.03
28+
# id = 0, title
29+
matrixportal.add_text(
30+
text_font=terminalio.FONT,
31+
text_position=((matrixportal.graphics.display.width // 3) - 1, (matrixportal.graphics.display.height // 3) - 1),
32+
text_color=0x800000,
33+
text_scale = 2
34+
)
35+
36+
# id = 1, author
2637
matrixportal.add_text(
2738
text_font=terminalio.FONT,
28-
text_position=(2, 4),
29-
text_color=0xFFFFFF,
30-
scrolling=True,
39+
text_position=(2, 25),
40+
text_color=0x000080,
41+
scrolling = True
3142
)
3243

44+
def get_guide_info(index):
45+
if index > DISPLAY_NUM_GUIDES:
46+
raise RuntimeError("Provided index may not be larger than DISPLAY_NUM_GUIDES.")
47+
print("Obtaining guide info for guide %d..."%index)
48+
# Traverse JSON data for title
49+
guide_count = matrixportal.network.json_traverse(als_data.json(), ["guide_count"])
50+
guides = matrixportal.network.json_traverse(als_data.json(), TITLE_DATA_LOCATION)
51+
guide_title = guides[index]["guide"]["title"]
52+
print("Guide Title", guide_title)
53+
return (guide_count, guide_title)
3354

3455

56+
idx = 0
57+
prv_hour = 0
3558
refresh_time = None
3659
while True:
37-
# Query local time every hour and on first run
60+
3861
if (not refresh_time) or (time.monotonic() - refresh_time) > 3600:
3962
try:
40-
print("Getting time from internet!")
63+
print("obtaining time from adafruit.io server...")
4164
matrixportal.get_local_time()
4265
refresh_time = time.monotonic()
4366
except RuntimeError as e:
44-
print("Some error occured, retrying! -", e)
67+
print("Retrying! - ", e)
4568
continue
46-
47-
the_time = time.localtime()
48-
print("Time: ", the_time)
4969

50-
try:
51-
print("Index is ", GUIDE_INDEX)
52-
# Update title location index
53-
TITLE_DATA_LOCATION = ["guides", GUIDE_INDEX, "guide", "title"]
54-
matrixportal.json_path=TITLE_DATA_LOCATION
55-
value = matrixportal.fetch()
56-
print("Response is", value)
57-
except (ValueError, RuntimeError) as e:
58-
print("Some error occured, retrying! -", e)
59-
matrixportal.scroll_text(0.01)
60-
if GUIDE_INDEX == DISPLAY_NUM_GUIDES - 1: # reached the end of the guides
61-
# reset the index
62-
print("Reached the end of the new guides, resetting!")
63-
GUIDE_INDEX = 0
64-
GUIDE_INDEX += 1
65-
time.sleep(0.5)
70+
if time.localtime()[3] != prv_hour:
71+
# Fetch and store guide info response
72+
als_data = matrixportal.network.fetch(DATA_SOURCE)
73+
prv_hour = time.localtime()[3]
74+
75+
# Cycle through guides retrieved
76+
if idx < DISPLAY_NUM_GUIDES:
77+
guide_count, guide_title = get_guide_info(idx)
78+
# Set title text
79+
matrixportal.set_text(guide_count, 0)
80+
81+
# Set author text
82+
matrixportal.set_text(guide_title, 1)
83+
84+
# Scroll the scrollable text blocks
85+
matrixportal.scroll_text(SCROLL_DELAY)
86+
idx += 1
87+
else:
88+
idx = 0
89+
time.sleep(0.5)

0 commit comments

Comments
 (0)