-
Notifications
You must be signed in to change notification settings - Fork 776
/
code.py
executable file
·74 lines (63 loc) · 2.26 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
# SPDX-FileCopyrightText: 2020 Collin Cunningham for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import time
from adafruit_magtag.magtag import MagTag
USE_AMPM_TIME = True
weekdays = ("mon", "tue", "wed", "thur", "fri", "sat", "sun")
last_sync = None
last_minute = None
magtag = MagTag()
magtag.graphics.set_background("/background.bmp")
mid_x = magtag.graphics.display.width // 2 - 1
magtag.add_text(
text_font="Lato-Regular-74.bdf",
text_position=(mid_x,10),
text_anchor_point=(0.5,0),
is_data=False,
)
magtag.set_text("00:00a", auto_refresh = False)
magtag.add_text(
text_font="/BebasNeueRegular-41.bdf",
text_position=(126,86), #was 141
text_anchor_point=(0,0),
is_data=False,
)
magtag.set_text("DAY 00:00a", index = 1, auto_refresh = False)
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.
"""
postfix = ""
if twelve_hour:
if time_struct.tm_hour > 12:
hour_string = str(time_struct.tm_hour - 12) # 13-23 -> 1-11 (pm)
postfix = "p"
elif time_struct.tm_hour > 0:
hour_string = str(time_struct.tm_hour) # 1-12
postfix = "a"
if time_struct.tm_hour == 12:
postfix = "p" # 12 -> 12 (pm)
else:
hour_string = '12' # 0 -> 12 (am)
postfix = "a"
else:
hour_string = '{hh:02d}'.format(hh=time_struct.tm_hour)
return hour_string + ':{mm:02d}'.format(mm=time_struct.tm_min) + postfix
while True:
if not last_sync or (time.monotonic() - last_sync) > 3600:
# at start or once an hour
magtag.network.get_local_time()
last_sync = time.monotonic()
# get current time
now = time.localtime()
# minute updated, refresh display!
if not last_minute or (last_minute != now.tm_min): # minute has updated
magtag.set_text(hh_mm(now, USE_AMPM_TIME), index = 0)
last_minute = now.tm_min
# timestamp
if magtag.peripherals.button_a_pressed:
out = weekdays[now.tm_wday] + " " + hh_mm(now, USE_AMPM_TIME)
magtag.set_text(out, index = 1)
while magtag.peripherals.button_a_pressed: # wait till released
pass