-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelloweenCountdown.py
executable file
·112 lines (95 loc) · 2.68 KB
/
HelloweenCountdown.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
import time
import board
from adafruit_matrixportal.matrixportal import MatrixPortal
EVENT_YEAR = 2021
EVENT_MONTH = 10
EVENT_DAY = 31
EVENT_HOUR = 17
EVENT_MINUTE = 0
FRAME_DURATION = 3
FRAMES = (
"bmps/jack.bmp",
"DAYS",
"bmps/ghost.bmp",
"HOURS",
"bmps/bats.bmp",
"MINUTES",
"bmps/skull.bmp",
"bmps/halloween.bmp",
)
EVENT_DAY_IMAGE = "bmps/happy_halloween.bmp"
SYNCHRONIZE_CLOCK = True
# --- Display setup ---
matrixportal = MatrixPortal(status_neopixel=board.NEOPIXEL, debug=True)
current_frame = None
# Create a new label with the color and text selected
matrixportal.add_text(
text_font="fonts/Arial-12.bdf",
text_position=(4, (matrixportal.graphics.display.height // 2) - 1),
text_color=0xEF7F31,
)
def set_time_until(unit=None):
event_time = time.struct_time(
(
EVENT_YEAR,
EVENT_MONTH,
EVENT_DAY,
EVENT_HOUR,
EVENT_MINUTE,
0, # we don't track seconds
-1,
-1,
False,
)
)
remaining = time.mktime(event_time) - time.mktime(time.localtime())
if remaining <= 0:
# oh, its event time!
matrixportal.set_background(EVENT_DAY_IMAGE)
return
remaining //= 60
mins_remaining = remaining % 60
remaining //= 60
hours_remaining = remaining % 24
remaining //= 24
days_remaining = remaining
if unit == "DAYS":
text = "{} day".format(days_remaining)
if days_remaining != 1:
text += "s"
if unit == "HOURS":
text = "{} hour".format(hours_remaining)
if hours_remaining != 1:
text += "s"
if unit == "MINUTES":
text = "{} min".format(mins_remaining)
if mins_remaining != 1:
text += "s"
matrixportal.set_text(text)
matrixportal.set_background(0)
def set_next_frame():
# pylint: disable=global-statement
global current_frame
# Advance to next frame if we already have one
if current_frame is not None:
current_frame += 1
# Loop back or set initial frame
if current_frame is None or current_frame >= len(FRAMES):
current_frame = 0
# Check if Picture or Text
print(FRAMES[current_frame])
if FRAMES[current_frame][-4:] == ".bmp":
matrixportal.set_background(FRAMES[current_frame])
matrixportal.set_text("")
else:
set_time_until(FRAMES[current_frame])
# Simulate the delay in case fetching time is fast
set_next_frame()
start_time = time.monotonic()
if SYNCHRONIZE_CLOCK:
matrixportal.get_local_time()
while time.monotonic() < start_time + FRAME_DURATION:
pass
while True:
set_next_frame()
time.sleep(FRAME_DURATION)