-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
347 lines (310 loc) · 10.6 KB
/
main.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# Adapted from https://github.com/muccc/flipdots/blob/master/scripts/clock.py
import display
from utime import sleep
import utime
import math
import leds
import buttons
import ujson
import os
import leds
import power
CONFIG_NAME = "clock.json"
def update_charging_indicator():
chargeVoltage = power.read_chargein_voltage()
if chargeVoltage < 1:
leds.set(10, [ 0, 0, 0 ])
else:
batteryVoltage = power.read_battery_voltage()
if batteryVoltage < 4.15:
leds.set(10, [ 42, 0, 0 ])
else:
leds.set(10, [ 0, 42, 0 ])
utime.sleep_ms(250)
class Time:
def __init__(self, start=0):
self.time = start
self.wait_time = 0.95
def tick(self):
sleep(self.wait_time)
self.time += 1
@property
def second(self):
return self.time % 60
@property
def minute(self):
return (self.time / 60) % 60
@property
def hour(self):
return (self.time / 3600) % 24
class Clock:
def __init__(
self,
sizex=80,
sizey=80,
radius=38,
offsetx=30,
hour_hand=True,
minute_hand=True,
second_hand=True,
console_out=False,
run_once=False,
update_interval=0,
):
self.sizex = sizex
self.sizey = sizey
self.radius = radius
self.center = (int(self.sizex / 2), int(self.sizey / 2))
self.hour_hand = hour_hand
self.minute_hand = minute_hand
self.second_hand = second_hand
self.console_out = console_out
self.update_interval = (
update_interval if update_interval != 0 else (1 if self.second_hand else 30)
)
self.run_once = run_once
self.offsetx = offsetx
self.time = Time()
self.theme = 0
self.default_themes = [
{
"background": [0, 0, 0],
"center": [255, 255, 255],
"m1": [255, 255, 255],
"m5": [255, 255, 255],
"hour_hand": [255, 255, 255],
"minute_hand": [255, 255, 255],
"second_hand": [255, 255, 255],
},
{
"background": [130, 30, 70],
"center": [255, 255, 255],
"m1": [255, 255, 255],
"m5": [255, 255, 255],
"hour_hand": [255, 255, 255],
"minute_hand": [255, 255, 255],
"second_hand": [255, 255, 255],
},
{
"background": [0, 80, 0],
"center": [255, 255, 255],
"m1": [255, 255, 255],
"m5": [255, 255, 255],
"hour_hand": [255, 255, 255],
"minute_hand": [255, 255, 255],
"second_hand": [255, 255, 255],
},
{
"background": [0, 80, 80],
"center": [255, 255, 255],
"m1": [255, 255, 255],
"m5": [255, 255, 255],
"hour_hand": [255, 255, 255],
"minute_hand": [255, 255, 255],
"second_hand": [255, 255, 255],
},
{
"background": [255, 255, 255],
"center": [0, 0, 0],
"m1": [0, 0, 0],
"m5": [0, 0, 0],
"hour_hand": [0, 0, 0],
"minute_hand": [0, 0, 0],
"second_hand": [0, 0, 0],
},
]
self.themes = self.default_themes
# check for config file
if CONFIG_NAME in os.listdir("."):
self.readConfig()
else:
self.writeConfig()
# load colors
self.setTheme(self.theme)
def readConfig(self):
with open(CONFIG_NAME, "r") as f:
try:
c = ujson.loads(f.read())
if (
"themes" in c
and len(c["themes"]) > 0
and isinstance(c["themes"], list)
):
self.themes = c["themes"]
if "theme" and isinstance(c["theme"], int):
self.theme = c["theme"]
except ValueError:
print("parsing %s failed" % CONFIG_NAME)
def writeConfig(self):
with open(CONFIG_NAME, "w") as f:
f.write(ujson.dumps({"theme": self.theme, "themes": self.themes}))
def setTheme(self, theme):
self.theme = theme % len(self.themes)
self.background_col = (
self.themes[self.theme]["background"]
if "background" in self.themes[self.theme]
else self.default_themes[0]["background"]
)
self.center_col = (
self.themes[self.theme]["center"]
if "center" in self.themes[self.theme]
else self.default_themes[0]["center"]
)
self.m1_col = (
self.themes[self.theme]["m1"]
if "m1" in self.themes[self.theme]
else self.default_themes[0]["m1"]
)
self.m5_col = (
self.themes[self.theme]["m5"]
if "m5" in self.themes[self.theme]
else self.default_themes[0]["m5"]
)
self.hour_hand_col = (
self.themes[self.theme]["hour_hand"]
if "hour_hand" in self.themes[self.theme]
else self.default_themes[0]["hour_hand"]
)
self.minute_hand_col = (
self.themes[self.theme]["minute_hand"]
if "minute_hand" in self.themes[self.theme]
else self.default_themes[0]["minute_hand"]
)
self.second_hand_col = (
self.themes[self.theme]["second_hand"]
if "second_hand" in self.themes[self.theme]
else self.default_themes[0]["second_hand"]
)
def loop(self):
colored = False
try:
with display.open() as disp:
button_pressed = False
while True:
self.updateClock(disp)
update_charging_indicator()
if self.run_once:
break
# check for button presses
v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT)
if v == 0:
button_pressed = False
if not button_pressed and v & buttons.BOTTOM_LEFT != 0:
button_pressed = True
self.setTheme(self.theme - 1)
self.writeConfig()
elif not button_pressed and v & buttons.BOTTOM_RIGHT != 0:
button_pressed = True
self.setTheme(self.theme + 1)
self.writeConfig()
except KeyboardInterrupt:
for i in range(11):
leds.set(i, (0, 0, 0))
return
def drawImage(self, image):
with display.open() as d:
d.clear()
for x in range(len(image)):
for y in range(len(image[x])):
d.pixel(
x + self.offsetx,
y,
col=(255, 255, 255) if image[x][y] else (0, 0, 0),
)
d.update()
def updateClock(self, disp):
disp.clear(self.background_col)
localtime = utime.localtime()
disp.pixel(self.center[0] + self.offsetx, self.center[1], col=self.center_col)
hour_coords = self.circlePoint(
math.radians(
(((localtime[3] % 12) / 12.0) if localtime[3] else 0) * 360
+ 270
+ (localtime[4] / 2)
)
)
minute_coords = self.circlePoint(math.radians(localtime[4] * 6 + 270))
second_coords = self.circlePoint(math.radians(localtime[5] * 6 + 270))
for i in range(60):
degree = i * 6 + 90
radian = -math.radians(degree)
coords = self.circlePoint(radian)
if not i % 5:
self.addLine(disp, coords, self.center, 3, 1, col=self.m5_col)
else:
self.addLine(disp, coords, self.center, 1, col=self.m1_col)
if self.hour_hand:
self.addLine(
disp,
self.center,
hour_coords,
int(self.radius / 3),
1,
col=self.hour_hand_col,
)
if self.minute_hand:
self.addLine(
disp,
self.center,
minute_coords,
int(self.radius / 2),
col=self.minute_hand_col,
)
if self.second_hand:
self.addLine(
disp,
self.center,
second_coords,
self.radius - int(self.radius / 8.0),
col=self.second_hand_col,
)
if self.console_out:
for y in range(self.radius * 2):
line = ""
for x in range(self.radius * 2):
line = line + (
"."
if image[(self.center[1] - self.radius) + y][
(self.center[0] - self.radius) + x
]
else " "
)
print(line)
disp.update()
def circlePoint(self, t):
return (
int(round(self.radius * math.cos(t))) + self.center[0],
int(round(self.radius * math.sin(t))) + self.center[1],
)
def addLine(self, disp, source, aim, length, thickness=1, col=(255, 255, 255)):
vector = self.subVector(aim, source)
vector = self.normVector(vector)
destination = self.addVector(source, self.multiplyVector(vector, length))
disp.line(
round(source[0]) + self.offsetx,
round(source[1]),
round(destination[0]) + self.offsetx,
round(destination[1]),
col=col,
size=thickness,
)
def normVector(self, v):
length = math.sqrt(sum([i ** 2 for i in v]))
new_v = []
for i in range(len(v)):
new_v.append(v[i] / length)
return tuple(new_v)
def subVector(self, v1, v2):
res = []
for i in range(len(v1)):
res.append(v1[i] - v2[i])
return tuple(res)
def addVector(self, v1, v2):
res = []
for i in range(len(v1)):
res.append(v1[i] + v2[i])
return tuple(res)
def multiplyVector(self, v, multiplier):
return tuple([i * multiplier for i in v])
clock = Clock()
clock.loop()