-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunicorn_clock.py
84 lines (65 loc) · 1.86 KB
/
unicorn_clock.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
#!/usr/bin/env python3
import signal
import threading
import time
# See https://github.com/pimoroni/unicorn-hat/blob/master/examples/hat/clock.py
# Which was used as base and changed to fit my needs
from graphics import Drawing, Color
import unicornhathd as unicorn
class UnicornDrawing(Drawing):
def __init__(self):
Drawing.__init__(self,16,16)
'''
All drawing operations use the pixel method
so we override it for UnicornHat
'''
def pixel(self, x, y, col):
if x < 0 or x > 15 or y < 0 or y > 15:
return False
self.buffer[(x,y)] = col
unicorn.set_pixel(x, y, col.r, col.g, col.b)
def show(self):
unicorn.show()
d = UnicornDrawing()
# X offset of clock centre
O_X = 8
# Y offset of clock centre
O_Y = 8
# Radius of clock
R = 6
# Rotation offset of clock, set to 0, 90, 180, 270, etc
unicorn.rotation(0)
def setBrightness(currenttime):
currenthour = currenttime.tm_hour
# if it's between 10 am and 8 pm,
# use dimmer brightness
if(currenthour < 10 or currenthour > 20):
unicorn.brightness(0.5)
else:
unicorn.brightness(0.8)
def tick():
currenttime = time.localtime()
currenthour = currenttime.tm_hour
currentmin = currenttime.tm_min
currentsec = currenttime.tm_sec
# Set daytime or nighttime brightness
setBrightness(currenttime)
d.clear()
# Draw the circle around the clock
d.circle(O_X,O_Y,R,Color(255,0,255))
# Draw the clock hands
d.circle_line(O_X,O_Y,R-1,(-360.0*((currenthour % 12)/12.0)),Color(255,0,0))
d.circle_line(O_X,O_Y,R-1,(-360.0*(currentmin/60.0)), Color(0,255,0))
d.circle_line(O_X,O_Y,R-1,(-360.0*(currentsec/60.0)), Color(0,0,255))
# draw buffer to hardware
d.show()
def main(run, running):
running(True)
while run():
tick()
now = time.monotonic()
time.sleep(now %0.01)
unicorn.off()
running(False)
if __name__ == '__main__':
main()