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 ()
68
60
69
61
# Initialize an OAuth2 object with GCal API scope
70
62
scopes = ["https://www.googleapis.com/auth/calendar.readonly" ]
71
63
google_auth = OAuth2 (
72
- requests ,
64
+ magtag . network . requests ,
73
65
secrets ["google_client_id" ],
74
66
secrets ["google_client_secret" ],
75
67
scopes ,
@@ -86,15 +78,17 @@ def get_current_time(time_max=False):
86
78
cur_time = r .datetime
87
79
if time_max : # maximum time to fetch events is midnight (4:59:59UTC)
88
80
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 ],
81
+ (
82
+ cur_time [0 ],
83
+ cur_time [1 ],
84
+ cur_time [2 ] + 1 ,
85
+ 4 ,
86
+ 59 ,
87
+ 59 ,
88
+ cur_time [6 ],
89
+ cur_time [7 ],
90
+ cur_time [8 ],
91
+ )
98
92
)
99
93
cur_time = cur_time_max
100
94
cur_time = "{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}{:s}" .format (
@@ -126,7 +120,7 @@ def get_calendar_events(calendar_id, max_events, time_min):
126
120
"/events?maxResults={1}&timeMin={2}&timeMax={3}&orderBy=startTime"
127
121
"&singleEvents=true" .format (calendar_id , max_events , time_min , time_max )
128
122
)
129
- resp = requests .get (url , headers = headers )
123
+ resp = magtag . network . requests .get (url , headers = headers )
130
124
resp_json = resp .json ()
131
125
if "error" in resp_json :
132
126
raise RuntimeError ("Error:" , resp_json )
@@ -186,76 +180,66 @@ def display_calendar_events(resp_events):
186
180
print ("Event Time:" , format_datetime (event_start ))
187
181
print ("-" * 40 )
188
182
# 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 ,
183
+ magtag .add_text (
184
+ text_font = font_event ,
185
+ text_position = (7 , 40 + (event_idx * 35 )),
186
+ text_color = 0x000000 ,
194
187
text = format_datetime (event_start ),
195
188
)
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 ,
189
+ magtag .add_text (
190
+ text_font = font_event ,
191
+ text_position = (88 , 40 + (event_idx * 35 )),
192
+ text_color = 0x000000 ,
203
193
text = event_name ,
204
194
line_spacing = 0.65 ,
205
195
)
206
- magtag .splash .append (label_event_desc )
207
196
208
197
209
- # Create a new MagTag object
210
- magtag = MagTag ()
211
- r = rtc .RTC ()
212
-
213
198
# DisplayIO Setup
214
199
magtag .set_background (0xFFFFFF )
215
200
216
201
# Add the header
217
202
line_header = Line (0 , 30 , 320 , 30 , color = 0x000000 )
218
203
magtag .splash .append (line_header )
219
204
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 )
205
+ label_header = magtag .add_text (
206
+ text_font = "fonts/Arial-18.pcf" ,
207
+ text_position = (5 , 15 ),
208
+ text_color = 0x000000 ,
209
+ )
223
210
224
211
# Set up calendar event fonts
225
- font_event = bitmap_font . load_font ( "fonts/Arial-12.pcf" )
212
+ font_event = "fonts/Arial-12.pcf"
226
213
227
214
if not google_auth .refresh_access_token ():
228
215
raise RuntimeError ("Unable to refresh access token - has the token been revoked?" )
229
216
access_token_obtained = int (time .monotonic ())
230
217
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 ())
218
+ # check if we need to refresh token
219
+ if int (time .monotonic ()) - access_token_obtained >= google_auth .access_token_expiration :
220
+ print ("Access token expired, refreshing..." )
221
+ if not google_auth .refresh_access_token ():
222
+ raise RuntimeError (
223
+ "Unable to refresh access token - has the token been revoked?"
224
+ )
225
+ access_token_obtained = int (time .monotonic ())
243
226
244
- # fetch calendar events!
245
- print ("fetching local time..." )
246
- now = get_current_time ()
227
+ # fetch calendar events!
228
+ print ("fetching local time..." )
229
+ now = get_current_time ()
247
230
248
- # setup header label
249
- label_header .text = format_datetime (now , pretty_date = True )
231
+ # setup header label
232
+ magtag .set_text (
233
+ format_datetime (now , pretty_date = True ), label_header , auto_refresh = False
234
+ )
250
235
251
- print ("fetching calendar events..." )
252
- events = get_calendar_events (CALENDAR_ID , MAX_EVENTS , now )
236
+ print ("fetching calendar events..." )
237
+ events = get_calendar_events (CALENDAR_ID , MAX_EVENTS , now )
253
238
254
- print ("displaying events" )
255
- display_calendar_events (events )
239
+ print ("displaying events" )
240
+ display_calendar_events (events )
256
241
257
- board .DISPLAY .show (magtag .splash )
258
- board .DISPLAY .refresh ()
242
+ magtag .graphics .display .refresh ()
259
243
260
- print ("Sleeping for %d minutes" % REFRESH_TIME )
261
- magtag .exit_and_deep_sleep (REFRESH_TIME * 60 )
244
+ print ("Sleeping for %d minutes" % REFRESH_TIME )
245
+ magtag .exit_and_deep_sleep (REFRESH_TIME * 60 )
0 commit comments