-
Notifications
You must be signed in to change notification settings - Fork 776
/
Copy pathcode.py
executable file
·141 lines (112 loc) · 5.29 KB
/
code.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
# SPDX-FileCopyrightText: 2020 Phillip Burgess for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Google Sheets to MagTag example: Weekly Planner.
Gets tab-separated-value (TSV) spreadsheet from Google, displays task list
from today's column. This example does NOT deep sleep, a USB power connection
is recommended.
Fonts from Xorg project.
"""
# pylint: disable=import-error, line-too-long
import time
import rtc
from adafruit_display_shapes.rect import Rect
from adafruit_magtag.magtag import MagTag
# CONFIGURABLE SETTINGS and ONE-TIME INITIALIZATION ------------------------
TSV_URL = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vR1WjUKz35-ek6SiR5droDfvPp51MTds4wUs57vEZNh2uDfihSTPhTaiiRovLbNe1mkeRgurppRJ_Zy/pub?output=tsv'
TWELVE_HOUR = True # If set, show 12-hour vs 24-hour (e.g. 3:00 vs 15:00)
DD_MM = False # If set, show DD/MM instead of MM/DD dates
DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday']
MAGTAG = MagTag(rotation=0) # Portrait (vertical) display
MAGTAG.network.connect()
# SOME UTILITY FUNCTIONS ---------------------------------------------------
def hh_mm(time_struct, twelve_hour=True):
""" Given a time.struct_time, return a string as H:MM or HH:MM, either
12- or 24-hour style depending on twelve_hour flag.
"""
if twelve_hour:
if time_struct.tm_hour > 12:
hour_string = str(time_struct.tm_hour - 12) # 13-23 -> 1-11 (pm)
elif time_struct.tm_hour > 0:
hour_string = str(time_struct.tm_hour) # 1-12
else:
hour_string = '12' # 0 -> 12 (am)
else:
hour_string = '{hh:02d}'.format(hh=time_struct.tm_hour)
return hour_string + ':{mm:02d}'.format(mm=time_struct.tm_min)
# GRAPHICS INITIALIZATION --------------------------------------------------
# First text label (index 0) is day of week -- empty for now, is set later
MAGTAG.add_text(
text_font='/fonts/helvB24.pcf',
text_position=(MAGTAG.graphics.display.width // 2, 4),
line_spacing=1.0,
text_anchor_point=(0.5, 0), # Center top
is_data=False, # Text will be set manually
)
# Second (index 1) is task list -- again, empty on start, is set later
MAGTAG.add_text(
text_font='/fonts/ncenR14.pcf',
text_position=(3, 36),
line_spacing=1.0,
text_anchor_point=(0, 0), # Top left
is_data=False, # Text will be set manually
)
# Add 14-pixel-tall black bar at bottom of display. It's a distinct layer
# (not just background) to appear on top of task list if it runs long.
MAGTAG.graphics.splash.append(Rect(0, MAGTAG.graphics.display.height - 14,
MAGTAG.graphics.display.width,
MAGTAG.graphics.display.height, fill=0x0))
# Center white text (index 2) over black bar to show last update time
MAGTAG.add_text(
text_font='/fonts/helvB12.pcf',
text_position=(MAGTAG.graphics.display.width // 2,
MAGTAG.graphics.display.height - 1),
text_color=0xFFFFFF,
text_anchor_point=(0.5, 1), # Center bottom
is_data=False, # Text will be set manually
)
# MAIN LOOP ----------------------------------------------------------------
PRIOR_LIST = '' # Initialize these to nonsense values
PRIOR_DAY = -1 # so the list or day change always triggers on first pass
while True:
try:
print('Updating time')
MAGTAG.get_local_time()
NOW = rtc.RTC().datetime
print('Updating tasks')
RESPONSE = MAGTAG.network.fetch(TSV_URL)
if RESPONSE.status_code == 200:
TSV_DATA = RESPONSE.text
print('OK')
# Split text response into separate lines
LINES = TSV_DATA.split('\r\n')
# tm_wday uses 0-6 for Mon-Sun, we want 1-7 for Sun-Sat
COLUMN = (NOW.tm_wday + 1) % 7 + 1
TASK_LIST = '' # Clear task list string
for line in LINES[1:]: # Skip first line -- days of week in sheet
cells = line.split("\t") # Tab-separated!
if len(cells) >= COLUMN:
TASK_LIST += cells[COLUMN - 1] + '\n'
# Refreshing the display is jarring, so only do it if the task list
# or day has changed. This requires preserving state between passes,
# and is why this code doesn't deep sleep (which is like a reset).
if TASK_LIST != PRIOR_LIST or PRIOR_DAY != NOW.tm_wday:
# Set the day-of-week label at top
MAGTAG.set_text(DAYS[COLUMN - 1], auto_refresh=False)
# Set the "Updated" date and time label
if DD_MM:
DATE = '%d/%d' % (NOW.tm_mday, NOW.tm_mon)
else:
DATE = '%d/%d' % (NOW.tm_mon, NOW.tm_mday)
MAGTAG.set_text('Updated %s %s' % (DATE, hh_mm(NOW, TWELVE_HOUR)),
2, auto_refresh=False)
MAGTAG.set_text(TASK_LIST, 1) # Update list, refresh display
PRIOR_LIST = TASK_LIST # Save list state for next pass
PRIOR_DAY = NOW.tm_wday # Save day-of-week for next pass
except RuntimeError as error:
# If there's an error above, no harm, just try again in ~15 minutes.
# Usually it's a common network issue or time server hiccup.
print('Retrying in 15 min - ', error)
time.sleep(15 * 60) # Whether OK or error, wait 15 mins for next pass