forked from rdagger/Pi-ST7565
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_clock.py
52 lines (47 loc) · 1.51 KB
/
example_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
import st7565
import xglcd_font as font
import math
import time
neato = font.XglcdFont('/home/pi/Pi-ST7565/fonts/Neato5x7.c', 5, 7)
glcd = st7565.Glcd(rgb=[21, 20, 16])
glcd.init()
x0, y0 = 63, 31
def get_face_xy(angle, radius):
"""
Get x,y coordinates on face at specified angle and radius
"""
theta = math.radians(angle)
x = int(x0 + radius * math.cos(theta))
y = int(y0 + radius * math.sin(theta))
return x, y
def draw_face():
# Outline
glcd.draw_circle(x0, y0, 31)
# Ticks
for angle in range(30, 331, 30):
glcd.draw_line(x0, y0, *get_face_xy(angle, 29))
# Clear center of circle
glcd.fill_circle(x0, y0, 25, color=0)
# Numbers
glcd.draw_string("12", neato, x0 - 5, y0 - 29, spacing=0)
glcd.draw_letter("3", neato, x0 + 25, y0 - 3)
glcd.draw_letter("6", neato, x0 - 2, y0 + 23)
glcd.draw_letter("9", neato, x0 - 29, y0 - 3)
# Date
glcd.draw_string(time.strftime("%b").upper(), neato, 0,0)
glcd.draw_string(time.strftime(" %d"), neato, 0, 8)
try:
while True:
glcd.clear_back_buffer()
draw_face()
minute = int(time.strftime("%M"))
hour = int(time.strftime("%H"))
glcd.draw_line(x0, y0, *get_face_xy(minute * 6 + 270, 29))
glcd.draw_line(x0, y0, *get_face_xy(hour * 30 - 90, 20))
glcd.flip()
while minute == int(time.strftime("%M")):
time.sleep(1)
except KeyboardInterrupt:
print('\nCtrl-C pressed. Cleaning up and exiting...')
finally:
glcd.cleanup()