diff --git a/lib/application.py b/lib/application.py new file mode 100644 index 0000000..df27aa6 --- /dev/null +++ b/lib/application.py @@ -0,0 +1,48 @@ +from PIL import Image,ImageDraw,ImageFont,ImageShow +from random import randint, choice +import time + +class Sheogorath: + def __init__(self): + self.width = 122 + self.height = 250 + try: + self.lyrics = open("lyrics.txt", "r").readlines() + except FileNotFoundError: + self.lyrics = ["Lyrics not found"] + try: + self.sheo = Image.open('res/sheo1.bmp') + except FileNotFoundError: + self.sheo = Image.new('1', (122, 122)) + try: + self.font15 = ImageFont.truetype('res/Font.ttc', 15) + self.font24 = ImageFont.truetype('res/Font.ttc', 24) + except FileNotFoundError: + self.font15 = None + self.font24 = None + + def get_lyric(self, max_length = 17): + lyrics = open("lyrics.txt", "r").readlines() + lyric = choice(self.lyrics) + out_lyric = "" + counter = 0 + for word in lyric.split(" "): + if counter + len(word) < max_length: + counter += len(word) + 1 + out_lyric += word + " " + else: + counter = len(word) + 1 + out_lyric += "\n" + word + " " + return out_lyric + + def get_frame(self): + time_image = Image.new('1', (self.height, self.width), 255) + time_draw = ImageDraw.Draw(time_image) + x = randint(0, 40) + lyric = self.get_lyric((300-x)//15) + time_image.paste(self.sheo, (x,randint(0, 10))) + time_draw.rectangle((160, 90, 250, 122), fill = 255) + if self.font15 and self.font24: + time_draw.text((185, 90), time.strftime('%H:%M'), font = self.font24, fill = 0) + time_draw.text((100+x, 5), lyric, font = self.font15, fill = 0) + return time_image diff --git a/lib/epdummy.py b/lib/epdummy.py new file mode 100644 index 0000000..235c106 --- /dev/null +++ b/lib/epdummy.py @@ -0,0 +1,20 @@ +from PIL import ImageShow + +class EPDummy: + def __init__(self): + self.width = 122 + self.height = 250 + FULL_UPDATE = 0 + PART_UPDATE = 1 + def display(self, image): + ImageShow.show(image) + def displayPartial(self, image): + ImageShow.show(image) + def displayPartBaseImage(self, image): + ImageShow.show(image) + def init(self, a): + pass + def Clear(self, a): + pass + def getbuffer(self, image): + return image diff --git a/sheogorath.py b/sheogorath.py index a7e9369..0bb52e3 100644 --- a/sheogorath.py +++ b/sheogorath.py @@ -2,105 +2,59 @@ # -*- coding:utf-8 -*- import sys import os -import logging import time import traceback import argparse from random import randint, choice from PIL import Image,ImageDraw,ImageFont,ImageShow +from lib import application +from lib import epdummy parser = argparse.ArgumentParser(description='e-Ink Sheogorath') parser.add_argument('--debug', action='store_true', help='If true, does not initialize e-Ink, shows images in window') +parser.add_argument('-d', '--delay', default=45, help='Delay between frames in seconds') +parser.add_argument('-r', '--refresh', default=10, help='Amount of frames after which to refresh the screen') args = parser.parse_args() -logging.basicConfig(level=logging.DEBUG) - -class EPDummy: - def __init__(self): - self.width = 122 - self.height = 250 - FULL_UPDATE = 0 - PART_UPDATE = 1 - def display(self, image): - ImageShow.show(image) - def displayPartial(self, image): - ImageShow.show(image) - def displayPartBaseImage(self, image): - ImageShow.show(image) - def init(self, a): - pass - def Clear(self, a): - pass - def getbuffer(self, image): - return image - -epd = EPDummy() +epd = epdummy.EPDummy() if not args.debug: from lib import epd2in13_V2 epd = epd2in13_V2.EPD() +app = application.Sheogorath() + +try: + DELAY = int(args.delay) +except ValueError: + DELAY = 45 +try: + N_REFRESH = int(args.refresh) +except ValueError: + N_REFRESH = 10 + try: - logging.info("Sheogorath.") epd.init(epd.FULL_UPDATE) epd.Clear(0xFF) - - font15 = ImageFont.truetype('res/Font.ttc', 15) - font24 = ImageFont.truetype('res/Font.ttc', 24) - image = Image.new('1', (epd.height, epd.width), 255) - draw = ImageDraw.Draw(image) - draw.rectangle([(0,0),(50,50)],outline = 0) - draw.rectangle([(55,0),(100,50)],fill = 0) - draw.line([(0,0),(50,50)], fill = 0,width = 1) - draw.line([(0,50),(50,0)], fill = 0,width = 1) - draw.chord((10, 60, 50, 100), 0, 360, fill = 0) - draw.ellipse((55, 60, 95, 100), outline = 0) - draw.pieslice((55, 60, 95, 100), 90, 180, outline = 0) - draw.pieslice((55, 60, 95, 100), 270, 360, fill = 0) - draw.polygon([(110,0),(110,50),(150,25)],outline = 0) - draw.polygon([(190,0),(190,50),(150,25)],fill = 0) - draw.text((120, 60), 'e-Paper demo', font = font15, fill = 0) - draw.text((110, 90), u'', font = font24, fill = 0) # read bmp file on window - logging.info("3.read bmp file on window...") epd.Clear(0xFF) - #image1 = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame - bmp = Image.open('res/sheo1.bmp') - - #epd.init(epd.FULL_UPDATE) - #epd.displayPartBaseImage(epd.getbuffer(time_image)) - - epd.init(epd.PART_UPDATE) - lyrics = open("lyrics.txt", "r").readlines() - def get_lyric(max_length = 17): - print(max_length) - lyric = choice(lyrics) - out_lyric = "" - counter = 0 - for word in lyric.split(" "): - if counter + len(word) < max_length: - counter += len(word) + 1 - out_lyric += word + " " - else: - counter = len(word) + 1 - out_lyric += "\n" + word + " " - return out_lyric + epd.init(epd.PART_UPDATE) + n = 0 while (True): - time_image = Image.new('1', (epd.height, epd.width), 255) - time_draw = ImageDraw.Draw(time_image) - x = randint(0, 40) - lyric = get_lyric((300-x)//15) - time_image.paste(bmp, (x,randint(0, 10))) - time_draw.rectangle((160, 90, 250, 122), fill = 255) - time_draw.text((185, 90), time.strftime('%H:%M'), font = font24, fill = 0) - time_draw.text((100+x, 5), lyric, font = font15, fill = 0) - epd.displayPartial(epd.getbuffer(time_image)) - epd.displayPartial(epd.getbuffer(time_image)) - time.sleep(30) -except IOError as e: - logging.info(e) + n += 1 + if n is N_REFRESH: + epd.init(epd.FULL_UPDATE) + epd.Clear(0xFF) + epd.Clear(0xFF) + n = 0 + time.sleep(1) + epd.init(epd.PART_UPDATE) + frame = epd.getbuffer(app.get_frame()) + epd.displayPartial(frame) + epd.displayPartial(frame) + time.sleep(DELAY) except KeyboardInterrupt: - logging.info("ctrl + c:") - epd2in13_V2.epdconfig.module_exit(cleanup=True) + if not args.debug: + epd2in13_V2.epdconfig.module_exit(cleanup=True) exit()