forked from pimoroni/unicorn-hat
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclock.py
executable file
·67 lines (53 loc) · 1.54 KB
/
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
#!/usr/bin/env python
import unicornhat as unicorn
from graphics import Drawing, Color
import time, signal, threading
class UnicornDrawing(Drawing):
def __init__(self):
Drawing.__init__(self,8,8)
'''
All drawing operations use the pixel method
so we override it for UnicornHat
'''
def pixel(self, x, y, col):
if x < 0 or x > 7 or y < 0 or y > 7:
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 = 3
# Y offset of clock centre
O_Y = 3
# Radius of clock
R = 3
# 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.15)
else:
unicorn.brightness(0.5)
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.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()
threading.Timer(1,tick).start()
tick()