1
1
import time
2
- import board
3
2
from random import randrange
4
- import rtc
3
+ import board
5
4
import terminalio
6
5
from adafruit_matrixportal .matrixportal import MatrixPortal
7
6
8
7
# --- Data Setup --- #
9
- GUIDE_INDEX = 0
10
8
# Number of guides to fetch and display from the Adafruit Learning System
11
9
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
+ )
13
14
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 ]
18
15
19
16
matrixportal = MatrixPortal (
20
17
url = DATA_SOURCE ,
21
18
json_path = TITLE_DATA_LOCATION ,
22
19
status_neopixel = board .NEOPIXEL ,
23
- debug = True
24
20
)
25
21
26
22
# --- Display Setup --- #
27
23
28
24
# Colors for guide name
29
- colors = [0xff0000 , 0xffa500 , 0xffff00 ,
30
- 0x008000 , 0x0000ff , 0x4b0082 ,
31
- 0xee82ee ]
25
+ colors = [0xFFA500 , 0xFFFF00 , 0x008000 , 0x0000FF , 0x4B0082 , 0xEE82EE ]
32
26
33
27
# Delay for scrolling the text
34
28
SCROLL_DELAY = 0.03
35
- # id = 0, title
29
+
30
+ FONT = "/IBMPlexMono-Medium-24_jep.bdf"
31
+
32
+ # Learn guide count (ID = 0)
36
33
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
+ ),
39
39
text_color = 0x800000 ,
40
- text_scale = 2
41
40
)
41
+ matrixportal .preload_font ("0123456789" )
42
42
43
- # id = 1, author
43
+ # Learn guide title (ID = 1)
44
44
matrixportal .add_text (
45
45
text_font = terminalio .FONT ,
46
46
text_position = (2 , 25 ),
47
47
text_color = 0x000080 ,
48
- scrolling = True
48
+ scrolling = True ,
49
49
)
50
50
51
+
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
+ """
52
59
if index > DISPLAY_NUM_GUIDES :
53
60
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 )
55
62
56
63
# Traverse JSON data for title
57
64
guide_count = matrixportal .network .json_traverse (als_data .json (), ["guide_count" ])
65
+
58
66
# Set guide count
59
67
matrixportal .set_text (guide_count , 0 )
60
68
61
69
guides = matrixportal .network .json_traverse (als_data .json (), TITLE_DATA_LOCATION )
62
70
guide_title = guides [index ]["guide" ]["title" ]
63
71
print ("Guide Title" , guide_title )
64
-
72
+
65
73
# Select color for title text
66
74
color_index = randrange (0 , len (colors ))
67
75
@@ -72,21 +80,21 @@ def get_guide_info(index):
72
80
matrixportal .set_text (guide_title , 1 )
73
81
74
82
83
+ refresh_time = None
75
84
guide_idx = 0
76
85
prv_hour = 0
77
- refresh_time = None
78
86
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 :
81
88
try :
82
89
print ("obtaining time from adafruit.io server..." )
83
90
matrixportal .get_local_time ()
84
91
refresh_time = time .monotonic ()
85
92
except RuntimeError as e :
86
- print ("Retrying! - " , e )
93
+ print ("Unable to obtain time from Adafruit IO, retrying - " , e )
87
94
continue
88
95
89
96
if time .localtime ()[3 ] != prv_hour :
97
+ print ("New Hour, fetching new data..." )
90
98
# Fetch and store guide info response
91
99
als_data = matrixportal .network .fetch (DATA_SOURCE )
92
100
prv_hour = time .localtime ()[3 ]
@@ -95,9 +103,9 @@ def get_guide_info(index):
95
103
if guide_idx < DISPLAY_NUM_GUIDES :
96
104
get_guide_info (guide_idx )
97
105
98
- # Scroll the scrollable text blocks
106
+ # Scroll the scrollable text block
99
107
matrixportal .scroll_text (SCROLL_DELAY )
100
108
guide_idx += 1
101
109
else :
102
110
guide_idx = 0
103
- time .sleep (0.5 )
111
+ time .sleep (0.05 )
0 commit comments