1
1
import time
2
2
import board
3
- import terminalio
4
3
import rtc
4
+ import terminalio
5
5
from adafruit_matrixportal .matrixportal import MatrixPortal
6
6
7
7
# --- Data Setup --- #
8
8
GUIDE_INDEX = 0
9
9
# Number of guides to fetch and display from the Adafruit Learning System
10
10
DISPLAY_NUM_GUIDES = 5
11
11
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" ]
13
13
TAGLINE_DATA_LOCATION = ["guides" , GUIDE_INDEX , "guide" , "tagline" ]
14
14
15
15
# the current working directory (where this file is)
22
22
debug = True
23
23
)
24
24
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
26
37
matrixportal .add_text (
27
38
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
31
42
)
32
43
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 )
33
54
34
55
56
+ idx = 0
57
+ prv_hour = 0
35
58
refresh_time = None
36
59
while True :
37
- # Query local time every hour and on first run
60
+
38
61
if (not refresh_time ) or (time .monotonic () - refresh_time ) > 3600 :
39
62
try :
40
- print ("Getting time from internet! " )
63
+ print ("obtaining time from adafruit.io server... " )
41
64
matrixportal .get_local_time ()
42
65
refresh_time = time .monotonic ()
43
66
except RuntimeError as e :
44
- print ("Some error occured, retrying ! -" , e )
67
+ print ("Retrying ! - " , e )
45
68
continue
46
-
47
- the_time = time .localtime ()
48
- print ("Time: " , the_time )
49
69
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