2
2
#
3
3
# SPDX-License-Identifier: Unlicense
4
4
import time
5
- import ssl
6
- import board
7
5
import rtc
8
- import wifi
9
- import socketpool
10
- import adafruit_requests as requests
11
6
from adafruit_oauth2 import OAuth2
12
7
from adafruit_display_shapes .line import Line
13
- from adafruit_bitmap_font import bitmap_font
14
- from adafruit_display_text import label
15
8
from adafruit_magtag .magtag import MagTag
16
9
17
10
# Calendar ID
59
52
print ("Credentials and tokens are kept in secrets.py, please add them there!" )
60
53
raise
61
54
62
- print ( "Connecting to %s" % secrets [ "ssid" ])
63
- wifi . radio . connect ( secrets [ "ssid" ], secrets [ "password" ] )
64
- print ( "Connected to %s!" % secrets [ "ssid" ] )
55
+ # Create a new MagTag object
56
+ magtag = MagTag ( )
57
+ r = rtc . RTC ( )
65
58
66
- pool = socketpool . SocketPool ( wifi . radio )
67
- requests = requests . Session ( pool , ssl . create_default_context ())
59
+ magtag . network . connect ( )
60
+ requests = magtag . network . requests
68
61
69
62
# Initialize an OAuth2 object with GCal API scope
70
63
scopes = ["https://www.googleapis.com/auth/calendar.readonly" ]
@@ -86,15 +79,17 @@ def get_current_time(time_max=False):
86
79
cur_time = r .datetime
87
80
if time_max : # maximum time to fetch events is midnight (4:59:59UTC)
88
81
cur_time_max = time .struct_time (
89
- cur_time [0 ],
90
- cur_time [1 ],
91
- cur_time [2 ] + 1 ,
92
- 4 ,
93
- 59 ,
94
- 59 ,
95
- cur_time [6 ],
96
- cur_time [7 ],
97
- cur_time [8 ],
82
+ (
83
+ cur_time [0 ],
84
+ cur_time [1 ],
85
+ cur_time [2 ] + 1 ,
86
+ 4 ,
87
+ 59 ,
88
+ 59 ,
89
+ cur_time [6 ],
90
+ cur_time [7 ],
91
+ cur_time [8 ],
92
+ )
98
93
)
99
94
cur_time = cur_time_max
100
95
cur_time = "{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}{:s}" .format (
@@ -186,76 +181,66 @@ def display_calendar_events(resp_events):
186
181
print ("Event Time:" , format_datetime (event_start ))
187
182
print ("-" * 40 )
188
183
# Generate labels holding event info
189
- label_event_time = label .Label (
190
- font_event ,
191
- x = 7 ,
192
- y = 40 + (event_idx * 35 ),
193
- color = 0x000000 ,
184
+ magtag .add_text (
185
+ text_font = font_event ,
186
+ text_position = (7 , 40 + (event_idx * 35 )),
187
+ text_color = 0x000000 ,
194
188
text = format_datetime (event_start ),
195
189
)
196
- magtag .splash .append (label_event_time )
197
-
198
- label_event_desc = label .Label (
199
- font_event ,
200
- x = 88 ,
201
- y = 40 + (event_idx * 35 ),
202
- color = 0x000000 ,
190
+ magtag .add_text (
191
+ text_font = font_event ,
192
+ text_position = (88 , 40 + (event_idx * 35 )),
193
+ text_color = 0x000000 ,
203
194
text = event_name ,
204
195
line_spacing = 0.65 ,
205
196
)
206
- magtag .splash .append (label_event_desc )
207
197
208
198
209
- # Create a new MagTag object
210
- magtag = MagTag ()
211
- r = rtc .RTC ()
212
-
213
199
# DisplayIO Setup
214
200
magtag .set_background (0xFFFFFF )
215
201
216
202
# Add the header
217
203
line_header = Line (0 , 30 , 320 , 30 , color = 0x000000 )
218
204
magtag .splash .append (line_header )
219
205
220
- font_h1 = bitmap_font .load_font ("fonts/Arial-18.pcf" )
221
- label_header = label .Label (font_h1 , x = 5 , y = 15 , color = 0x000000 )
222
- magtag .splash .append (label_header )
206
+ label_header = magtag .add_text (
207
+ text_font = "fonts/Arial-18.pcf" ,
208
+ text_position = (5 , 15 ),
209
+ text_color = 0x000000 ,
210
+ )
223
211
224
212
# Set up calendar event fonts
225
- font_event = bitmap_font . load_font ( "fonts/Arial-12.pcf" )
213
+ font_event = "fonts/Arial-12.pcf"
226
214
227
215
if not google_auth .refresh_access_token ():
228
216
raise RuntimeError ("Unable to refresh access token - has the token been revoked?" )
229
217
access_token_obtained = int (time .monotonic ())
230
218
231
- while True :
232
- # check if we need to refresh token
233
- if (
234
- int (time .monotonic ()) - access_token_obtained
235
- >= google_auth .access_token_expiration
236
- ):
237
- print ("Access token expired, refreshing..." )
238
- if not google_auth .refresh_access_token ():
239
- raise RuntimeError (
240
- "Unable to refresh access token - has the token been revoked?"
241
- )
242
- access_token_obtained = int (time .monotonic ())
219
+ # check if we need to refresh token
220
+ if int (time .monotonic ()) - access_token_obtained >= google_auth .access_token_expiration :
221
+ print ("Access token expired, refreshing..." )
222
+ if not google_auth .refresh_access_token ():
223
+ raise RuntimeError (
224
+ "Unable to refresh access token - has the token been revoked?"
225
+ )
226
+ access_token_obtained = int (time .monotonic ())
243
227
244
- # fetch calendar events!
245
- print ("fetching local time..." )
246
- now = get_current_time ()
228
+ # fetch calendar events!
229
+ print ("fetching local time..." )
230
+ now = get_current_time ()
247
231
248
- # setup header label
249
- label_header .text = format_datetime (now , pretty_date = True )
232
+ # setup header label
233
+ magtag .set_text (
234
+ format_datetime (now , pretty_date = True ), label_header , auto_refresh = False
235
+ )
250
236
251
- print ("fetching calendar events..." )
252
- events = get_calendar_events (CALENDAR_ID , MAX_EVENTS , now )
237
+ print ("fetching calendar events..." )
238
+ events = get_calendar_events (CALENDAR_ID , MAX_EVENTS , now )
253
239
254
- print ("displaying events" )
255
- display_calendar_events (events )
240
+ print ("displaying events" )
241
+ display_calendar_events (events )
256
242
257
- board .DISPLAY .show (magtag .splash )
258
- board .DISPLAY .refresh ()
243
+ magtag .graphics .display .refresh ()
259
244
260
- print ("Sleeping for %d minutes" % REFRESH_TIME )
261
- magtag .exit_and_deep_sleep (REFRESH_TIME * 60 )
245
+ print ("Sleeping for %d minutes" % REFRESH_TIME )
246
+ magtag .exit_and_deep_sleep (REFRESH_TIME * 60 )
0 commit comments