]> vault307.fbx.one Git - Clock-time.git/blob - clock-time.py
clock script and service
[Clock-time.git] / clock-time.py
1 #!/usr/bin/env python
2
3 import colorsys
4 import datetime
5 import time
6 import random
7 from sys import exit
8 from PIL import Image, ImageDraw, ImageFont
9 import unicornhathd
10
11
12 #FONT = ('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 10)
13 FONT = ('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf', 10)
14
15 width, height = unicornhathd.get_shape()
16
17 unicornhathd.rotation(270)
18 unicornhathd.brightness(0.25)
19
20 text_x = 0
21 text_y = -2
22 offset = 8
23
24 font_file, font_size = FONT
25
26 SAMPLE_CHARACTER = '0'
27
28 font = ImageFont.truetype(font_file, font_size)
29 text_width, text_height = font.getsize(SAMPLE_CHARACTER)
30
31 text_width += width + text_x
32
33 try:
34 while True:
35 current_hour = datetime.datetime.now().strftime('%I')
36 current_minute = datetime.datetime.now().strftime('%M')
37 current_seconds = int(datetime.datetime.now().strftime('%S')) * 2
38
39 try:
40 active_minute
41 except NameError:
42 active_minute = int(current_minute)
43 r_value = random.randint(50, 255)
44 g_value = random.randint(50, 255)
45 b_value = random.randint(50, 255)
46
47 if int(current_minute) != active_minute:
48 active_minute = int(current_minute)
49 if active_minute % 5 == 0:
50 r_value = random.randint(50, 255)
51 g_value = random.randint(50, 255)
52 b_value = random.randint(50, 255)
53
54 rgb_values = [r_value, g_value, b_value]
55 fill_values = (rgb_values[0], rgb_values[1], rgb_values[2])
56
57 TEXT_UL = current_hour[0]
58 TEXT_UR = current_hour[1]
59 TEXT_LL = current_minute[0]
60 TEXT_LR = current_minute[1]
61
62 image = Image.new('RGB', (text_width, max(height, text_height)), (0, 0, 0))
63
64 draw = ImageDraw.Draw(image)
65 draw.text((text_x, text_y), TEXT_UL, fill=fill_values, font=font)
66 draw.text((text_x + offset, text_y), TEXT_UR, fill=fill_values, font=font)
67 draw.text((text_x, text_y + offset), TEXT_LL, fill=fill_values, font=font)
68 draw.text((text_x + offset, text_y + offset), TEXT_LR, fill=fill_values, font=font)
69
70 unicornhathd.clear()
71
72 for x in range(width):
73 for y in range(height):
74 pixel = image.getpixel((x, y))
75 r, g, b = [int(n) for n in pixel]
76 unicornhathd.set_pixel(width - 1 - x, y, r, g, b)
77
78 unicornhathd.show()
79
80 time.sleep(1)
81
82 except KeyboardInterrupt:
83 unicornhathd.off()