Created
October 24, 2023 11:17
-
-
Save dasmikko/fbd9473f2d65406e67d1d1a32d78fb9e to your computer and use it in GitHub Desktop.
Inky phat clock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from PIL import Image, ImageFont, ImageDraw | |
from font_hanken_grotesk import HankenGroteskBold, HankenGroteskMedium | |
from inky.auto import auto | |
import time | |
from datetime import datetime | |
# Setup display | |
inky_display = auto() | |
hour = time.strftime("%H", time.localtime()) | |
minute = time.strftime("%M", time.localtime()) | |
def update_display(): | |
# Params | |
scale_size = 1.5 | |
date_scale_size = 1.8 | |
padding = 0 | |
# Add border | |
inky_display.set_border(inky_display.RED) | |
# Prepare for drawing | |
img = Image.new("P", inky_display.resolution) | |
draw = ImageDraw.Draw(img) | |
hanken_bold_font = ImageFont.truetype(HankenGroteskBold, int(35 * scale_size)) | |
hanken_medium_font = ImageFont.truetype(HankenGroteskMedium, int(16 * date_scale_size)) | |
time_string = hour + ':' + minute | |
date_string = datetime.today().strftime('%d-%m-%Y') | |
# Draw the time | |
time_w, time_h = hanken_bold_font.getsize(time_string) | |
time_x = int((inky_display.width - time_w) / 2) | |
time_y = 10 + padding | |
draw.text((time_x, time_y), time_string, inky_display.BLACK, font=hanken_bold_font) | |
# Draw the date | |
date_w, date_h = hanken_medium_font.getsize(date_string) | |
date_x = int((inky_display.width - date_w) / 2) | |
date_y = 65 + padding | |
draw.text((date_x, date_y), date_string, inky_display.BLACK, font=hanken_medium_font) | |
# Update the display | |
inky_display.set_image(img) | |
inky_display.show() | |
update_display() | |
while True: | |
current_hour = time.strftime("%H", time.localtime()) | |
current_minute = time.strftime("%M", time.localtime()) | |
# Updat the display if the time has changed! | |
if current_hour != hour or current_minute != minute: | |
print("Time change, update display...") | |
hour = current_hour | |
minute = current_minute | |
update_display() | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment