-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathword_clock.py
More file actions
159 lines (116 loc) · 3.63 KB
/
word_clock.py
File metadata and controls
159 lines (116 loc) · 3.63 KB
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
# ICON schedule
# NAME Word Clock
# DESC No hands!
import time
import machine
import ntptime
import pngdec
from presto import Presto
# Setup for the Presto display
presto = Presto()
display = presto.display
WIDTH, HEIGHT = display.get_bounds()
BLACK = display.create_pen(0, 0, 0)
WHITE = display.create_pen(200, 200, 200)
GRAY = display.create_pen(30, 30, 30)
# Clear the screen before the network call is made
display.set_pen(BLACK)
display.clear()
presto.update()
# Length of time between updates in minutes.
UPDATE_INTERVAL = 15
rtc = machine.RTC()
time_string = None
words = ["it", "d", "is", "m", "about", "lv", "half", "c", "quarter", "b", "to", "past", "n", "one",
"two", "three", "four", "five", "six", "eleven", "ten", "d", "qdh", "eight", "seven", "rm", "twelve", "nine", "p", "ncsnheypresto", "O'Clock", "agrdsp"]
def show_message(text):
display.set_pen(BLACK)
display.clear()
display.set_pen(WHITE)
display.text(f"{text}", 5, 10, WIDTH, 2)
presto.update()
show_message("Connecting...")
try:
wifi = presto.connect()
except ValueError as e:
while True:
show_message(e)
except ImportError as e:
while True:
show_message(e)
# Set the correct time using the NTP service.
try:
ntptime.settime()
except OSError:
while True:
show_message("Unable to get time.\n\nCheck your network try again.")
def approx_time(hours, minutes):
nums = {0: "twelve", 1: "one", 2: "two",
3: "three", 4: "four", 5: "five", 6: "six",
7: "seven", 8: "eight", 9: "nine", 10: "ten",
11: "eleven", 12: "twelve"}
if hours == 12:
hours = 0
if minutes >= 0 and minutes < 8:
return "it is about " + nums[hours] + " O'Clock"
if minutes >= 8 and minutes < 23:
return "it is about quarter past " + nums[hours]
if minutes >= 23 and minutes < 38:
return "it is about half past " + nums[hours]
if minutes >= 38 and minutes < 53:
return "it is about quarter to " + nums[hours + 1]
return "it is about " + nums[hours + 1] + " O'Clock"
def update():
global time_string
# grab the current time from the ntp server and update the Pico RTC
try:
ntptime.settime()
except OSError:
print("Unable to contact NTP server")
current_t = rtc.datetime()
time_string = approx_time(current_t[4] - 12 if current_t[4] > 12 else current_t[4], current_t[5])
# Splits the string into an array of words for displaying later
time_string = time_string.split()
print(time_string)
def draw():
global time_string
display.set_font("bitmap8")
display.set_layer(1)
# Clear the screen
display.set_pen(BLACK)
display.clear()
default_x = 25
x = default_x
y = 35
line_space = 20
letter_space = 15
margin = 25
scale = 1
spacing = 1
for word in words:
if word in time_string:
display.set_pen(WHITE)
else:
display.set_pen(GRAY)
for letter in word:
text_length = display.measure_text(letter, scale, spacing)
if not x + text_length <= WIDTH - margin:
y += line_space
x = default_x
display.text(letter.upper(), x, y, WIDTH, scale=scale, spacing=spacing)
x += letter_space
presto.update()
# Set the background in layer 0
# This means we don't need to decode the image every frame
display.set_layer(0)
try:
p = pngdec.PNG(display)
p.open_file("wordclock_background.png")
p.decode(0, 0)
except OSError:
display.set_pen(BLACK)
display.clear()
while True:
update()
draw()
time.sleep(60 * UPDATE_INTERVAL)