forked from SolderedElectronics/Inkplate-micropython
/
app.py
281 lines (249 loc) · 8.51 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import time
import machine
import network
import ntptime
from calendar_api import Calendar
from config import (
CLIENT_ID,
CLIENT_SECRET,
DISCOVERY_ENDPOINT,
SAVED_LOCATION,
SCOPES,
REFRESH_INTERVAL,
WLAN_PASSWORD,
WLAN_SSID
)
from device import DeviceAuth
from images import CALENDAR_40_40
from inkplate import Inkplate
from layout import ALIGN_CENTER, ALIGN_RIGHT, Column, Row
from utils import DateTime
# Shell
'''
picocom /dev/ttyUSB0 -b115200
'''
# Copy files.
'''
python pyboard.py --device /dev/ttyUSB0 -f cp app.py boot.py config.py calendar_api.py device.py images.py layout.py path.py text.py utils.py :
python pyboard.py --device /dev/ttyUSB0 app.py
'''
class App:
'''
The Calendar App.
'''
def __init__(self):
self.display = Inkplate(Inkplate.INKPLATE_1BIT)
self.display.begin()
self.width = self.display.width()
self.height = self.display.height()
# Connection state.
self.connecting = False
self.connected = False
# Auth state
self.authorizing = False
self.authorized = False
# Calender
self.calendar = None
def connect_to_network(self, notify=True):
'''
Connects to WiFi, and sync-s Network time
'''
if notify:
self._notify('Initializing', messages=[
'Connecting to %s' % (WLAN_SSID)
])
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
delay = 0
if not wlan.isconnected():
self.connecting = True
wlan.connect(WLAN_SSID, WLAN_PASSWORD)
while not wlan.isconnected() and delay < 50:
time.sleep_ms(200)
delay += 1
if not wlan.isconnected():
message = 'Cannot connect to WiFi SSID %s' % (WLAN_SSID)
self._error(message)
return
config = wlan.ifconfig()
print('Connected with config', config)
if notify:
self._notify('Initializing', messages=[
'Sync-ing real time clocks with Network'
])
print('Sync-ing network time.')
delay = 0
time_set = False
while wlan.isconnected() and not time_set and delay < 10:
try:
ntptime.settime()
time_set = True
except Exception:
time.sleep_ms(200)
delay += 1
self.connecting = False
self.connected = wlan.isconnected() and time_set
def initialize(self, notify=True):
'''
Initialize App.
'''
self.connect_to_network(notify=notify)
if not self.connected:
return
self.device_auth = DeviceAuth.from_file(SAVED_LOCATION)
if not self.device_auth or self.device_auth.authorized == False:
# Initialize auth
self.device_auth = DeviceAuth(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
discovery_endpoint=DISCOVERY_ENDPOINT,
scopes=SCOPES,
saved_location=SAVED_LOCATION
)
self.device_auth.discover()
self.authorizing = True
self.device_auth.authorize()
user_code = self.device_auth.user_code
verification_url = self.device_auth.verification_url
current_attempt = 0
max_attempts = 20
while not self.device_auth.authorized and current_attempt < max_attempts:
messages = [
'%s to continue' % (verification_url),
'Attempt %s of %s' % (current_attempt + 1, max_attempts)
]
self._notify(user_code, messages=messages)
self.device_auth.check_authorization_complete(max_attempts=1)
time.sleep(5) # Sleep duration in seconds.
current_attempt += 1
if not self.device_auth.authorized:
message = 'Unable to authorize the application.'
self._error(message=message)
return
if notify:
self._notify('Syncing', messages=[
'Updating calendar events'
])
self.build_calendar_ui()
print('Entering deep sleep.')
machine.deepsleep(REFRESH_INTERVAL * 60 * 1000)
def on_wakeup(self):
'''
Execute this method after waking up from deep sleep.
'''
self.initialize(notify=False)
def build_calendar_ui(self):
'''
Builds the actual Calendar UI after making the RESTful request.
'''
if not self.device_auth.authorized:
self._error('Need to authorize first.')
if not self.calendar:
self.calendar = Calendar(self.device_auth)
events = self.calendar.events()
date_today = DateTime.today()
sync_at = date_today.formatted()
sync_at_message = 'Last updated at %s' % (sync_at)
if len(events) <= 0:
messages = [
sync_at_message
]
self._notify('No events.', messages=messages)
else:
root = Column(
layout_width=self.width,
layout_height=self.height,
padding=10
)
header = Row(
parent=root,
layout_height=40,
wrap_content=False
)
f_date_today = date_today.formatted(include_day=True, include_time=False)
header.add_text_content('Today - %s' % (f_date_today), text_size=4)
header.add_image(CALENDAR_40_40, 40, 40, align=ALIGN_RIGHT)
content_root = Row(
parent=root,
layout_height=480,
wrap_content=False,
outline=True
)
content = Column(
parent=content_root,
wrap_content=False
)
content.add_spacer(10, outline=True)
content.add_spacer(20)
for event in events:
summary = event.summary
duration_info = None
if event.start_at:
include_day = not event.start_at.is_today()
duration_info = 'At %s' % (
event.start_at.formatted(include_day=include_day)
)
elif event.end_at:
duration_info = 'Ends at %s' % (
event.end_at.formatted(include_day=True, include_time=False)
)
content.add_text_content(summary)
if duration_info:
content.add_text_content(duration_info)
content.add_spacer(height=15)
content_root.add_node(content)
status = Row(
parent=root,
layout_height=40,
wrap_content=False,
outline=True
)
status.add_text_content(sync_at_message, align=ALIGN_RIGHT)
root.add_node(header)
root.add_node(content_root)
root.add_node(status)
self._draw(root)
def _error(self, message):
messages = [message]
print(message)
self._notify('Error', messages=messages)
def _notify(self, title, messages=list()):
root = Column(
layout_width=self.width,
layout_height=self.height,
padding=10
)
header = Row(
parent=root,
layout_height=40,
wrap_content=False
)
header.add_text_content('Calendar', text_size=4)
header.add_image(CALENDAR_40_40, 40, 40, align=ALIGN_RIGHT)
content_root = Row(
parent=root,
layout_height=520,
wrap_content=False,
outline=True
)
content = Column(
parent=content_root,
wrap_content=False
)
content.add_spacer(10, outline=True)
content.add_spacer(self.width // 4)
content.add_text_content(title, text_size=6, align=ALIGN_CENTER)
for message in messages:
content.add_text_content(message, align=ALIGN_CENTER)
content_root.add_node(content)
root.add_node(header)
root.add_node(content_root)
self._draw(root)
def _draw(self, node):
self.display.clearDisplay()
node.draw(self.display, 0, 0)
self.display.display()
if __name__ == '__main__':
app = App()
print('Initializing app.')
app.initialize()