Skip to content

Instantly share code, notes, and snippets.

@terryspitz
Created September 8, 2018 23:24
Show Gist options
  • Save terryspitz/003873a09e66ec95dc1e988678a5135b to your computer and use it in GitHub Desktop.
Save terryspitz/003873a09e66ec95dc1e988678a5135b to your computer and use it in GitHub Desktop.
# Terry Spitz 2018
# word clock for waveshare ePaper
# based on https://github.com/mattohagan/word-clock/blob/master/word_clock.py
try:
import epd7in5
except:
#continue without ePaper display for debugging
epd7in5 = None
from PIL import Image, ImageDraw, ImageFont
from datetime import datetime
from time import sleep
EPD_WIDTH = 640
EPD_HEIGHT = 384
s1 = 'IT IS HALFTEN'
s2 = 'QUARTERTWENTY'
s3 = 'FIVE MINUTES'
s4 = 'PAST TO '
s5 = 'ONE TWO THREE'
s6 = 'FOURFIVESEVEN'
s7 = 'SIXEIGHT NINE'
s8 = 'TENELEVENAMPM'
s9 = 'TWELVE OCLOCK'
words = [s1, s2, s3, s4, s5, s6, s7, s8, s9]
def main():
if epd7in5:
epd = epd7in5.EPD()
epd.init()
# For simplicity, the arguments are explicit numerical coordinates
image = Image.new('1', (EPD_WIDTH, EPD_HEIGHT), 0) # 1: clear the frame
if epd7in5:
epd.display_frame(epd.get_frame_buffer(image))
sleep(1)
imagedraw = ImageDraw.Draw(image)
if epd7in5:
font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 36)
else:
font = ImageFont.truetype('consolab.ttf', 36)
class Clock():
pass
clock = Clock()
while(True):
# reset all
grey=0
clock.itis=grey
clock.half=grey
clock.ten1=grey
clock.quarter=grey
clock.twenty=grey
clock.five1=grey
clock.minutes=grey
clock.past=grey
clock.to=grey
clock.one=grey
clock.two=grey
clock.three=grey
clock.four=grey
clock.five2=grey
clock.six=grey
clock.seven=grey
clock.eight=grey
clock.nine=grey
clock.ten2=grey
clock.eleven=grey
clock.twelve=grey
clock.am=grey
clock.pm=grey
clock.oclock=grey
# check time
now = datetime.now()
hour = now.hour
minute = now.minute
second = now.second
highlight = 1
clock.itis = highlight
# highlight minutes
if minute >= 55:
clock.five1 = highlight
clock.minutes = highlight
clock.to = highlight
hour += 1
elif minute >= 50:
clock.ten1 = highlight
clock.minutes = highlight
clock.to = highlight
hour += 1
elif minute >= 45:
clock.quarter = highlight
clock.to = highlight
hour += 1
elif minute >= 40:
clock.twenty = highlight
clock.minutes = highlight
clock.to = highlight
hour += 1
elif minute >= 30:
clock.half = highlight
clock.past = highlight
elif minute >= 25:
clock.twenty = highlight
clock.five1 = highlight
clock.minutes = highlight
clock.past = highlight
elif minute >= 20:
clock.twenty = highlight
clock.minutes = highlight
clock.past = highlight
elif minute >= 15:
clock.quarter = highlight
clock.past = highlight
elif minute >= 10:
clock.ten1 = highlight
clock.minutes = highlight
clock.past = highlight
elif minute >= 5:
clock.five1 = highlight
clock.minutes = highlight
clock.past = highlight
elif minute >= 0:
clock.oclock = highlight
# hours
if hour == 1 or hour == 13:
clock.one = highlight
elif hour == 2 or hour == 14:
clock.two = highlight
elif hour == 3 or hour == 15:
clock.three = highlight
elif hour == 4 or hour == 16:
clock.four = highlight
elif hour == 5 or hour == 17:
clock.five2 = highlight
elif hour == 6 or hour == 18:
clock.six = highlight
elif hour == 7 or hour == 19:
clock.seven = highlight
elif hour == 8 or hour == 20:
clock.eight = highlight
elif hour == 9 or hour == 21:
clock.nine = highlight
elif hour == 10 or hour == 22:
clock.ten2 = highlight
elif hour == 11 or hour == 23:
clock.eleven = highlight
elif hour == 12 or hour == 24 or hour == 0:
clock.twelve = highlight
# am/pm/oclock
if hour > 12:
clock.pm = highlight
elif hour == 12:
clock.oclock = highlight
else:
clock.am = highlight
height = 36
width = 17
def draw(w,h,color, text):
w += 150
if color == grey:
# outline text
imagedraw.text((w - 1, h), text, font=font, fill=1)
imagedraw.text((w + 1, h), text, font=font, fill=1)
imagedraw.text((w, h - 1), text, font=font, fill=1)
imagedraw.text((w, h + 1), text, font=font, fill=1)
# imagedraw.text((w - 1, h - 1), text, font=font, fill=1)
# imagedraw.text((w + 1, h - 1), text, font=font, fill=1)
# imagedraw.text((w - 1, h + 1), text, font=font, fill=1)
# imagedraw.text((w + 1, h + 1), text, font=font, fill=1)
imagedraw.text((w, h), text, font=font, fill=color)
# first row
draw(width, height,clock.itis, text=words[0][:5])
draw(width * 7, height,clock.half ,text=words[0][5:10])
draw(width * 13, height,clock.ten1 , text=words[0][10:])
# second row
draw(width, height * 2,clock.quarter , text=words[1][:7])
draw(width * 9.4, height * 2,clock.twenty , text=words[1][7:])
# third row
draw(width, height * 3,clock.five1 , text=words[2][:4])
draw(width * 8.2, height * 3,clock.minutes , text=words[2][6:])
# fourth row
draw(width, height * 4,clock.past , text=words[3][:4])
draw(width * 11.75, height * 4,clock.to, text=words[3][9:])
# fifth row
draw(width, height * 5,clock.one , text=words[4][:3])
draw(width * 5.8, height * 5,clock.two , text=words[4][4:7])
draw(width * 10.6, height * 5,clock.three , text=words[4][8:])
# sixth row
draw(width, height * 6,clock.four , text=words[5][:4])
draw(width * 5.8, height * 6,clock.five2 , text=words[5][4:8])
draw(width * 10.6, height * 6,clock.seven , text=words[5][8:])
# seventh row
draw(width, height * 7,clock.six , text=words[6][:3])
draw(width * 4.6, height * 7, clock.eight , text=words[6][3:9])
draw(width * 11.8, height * 7,clock.nine , text=words[6][9:])
# eigth row
draw(width, height * 8,clock.ten2 , text=words[7][:3])
draw(width * 4.6, height * 8,clock.eleven , text=words[7][3:9])
draw(width * 11.8, height * 8,clock.am , text=words[7][9:11])
draw(width * 14.1, height * 8, clock.pm , text=words[7][11:])
# ninth row
draw(width, height * 9,clock.twelve , text=words[8][:6])
draw(width * 9.4, height * 9,clock.oclock , text=words[8][7:])
# extra letters
draw(width * 3.45, height, grey, text='V T')
draw(width * 5.75, height * 3, grey, text='GO')
draw(width * 5.79, height * 4, grey, text='NOLES MO')
draw(width * 4.58, height * 5, grey, text='S C')
draw(width * 10.5, height * 7, grey, text='Z')
draw(width * 8.15, height * 9, grey, text='P')
if epd7in5:
epd.display_frame(epd.get_frame_buffer(image))
else:
image.show()
sleep(30)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment