diff --git a/display_controller/__init__.py b/display_controller/__init__.py index 122bdbf..e69de29 100644 --- a/display_controller/__init__.py +++ b/display_controller/__init__.py @@ -1,7 +0,0 @@ -import os -import sys - -picdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "pic") -libdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "lib") -if os.path.exists(libdir): - sys.path.append(libdir) diff --git a/display_controller/__main__.py b/display_controller/__main__.py index cdc1398..72ae635 100644 --- a/display_controller/__main__.py +++ b/display_controller/__main__.py @@ -2,14 +2,15 @@ # -*- coding:utf-8 -*- import os import logging -from . import picdir -from .lib import parse_api_result +from .lib import create_to_display_image, parse_api_result, font from waveshare_epd import epd4in2 -from PIL import Image, ImageDraw, ImageFont logging.basicConfig(level=logging.DEBUG) +TEST_IMAGE_GEN = False RESULT_FILENAME = "api_result.tsv" +if TEST_IMAGE_GEN: + RESULT_FILENAME = "api_result_test.tsv" result_filepath = os.path.join( os.path.dirname(os.path.dirname(os.path.realpath(__file__))), @@ -31,33 +32,9 @@ epd.init() epd.Clear() - font31 = ImageFont.truetype(os.path.join(picdir, "Menlo.ttc"), 31) - - # Drawing the next transport departures - logging.info("Drawing the next transport departures...") - Bimage = Image.new("1", (epd.width, epd.height), 255) # 255: clear the frame - draw = ImageDraw.Draw(Bimage) - LINE_HEIGHT = 2 - PADDING_WITH_LINE = 16 - FONT_SIZE = 31 - for i, s in enumerate(to_display): - draw.text( - (0, i * (FONT_SIZE + LINE_HEIGHT + PADDING_WITH_LINE * 2)), - s, - font=font31, - fill=0, - ) - draw.rectangle( - ( - 20, - (FONT_SIZE + PADDING_WITH_LINE) - + i * (LINE_HEIGHT + PADDING_WITH_LINE * 2 + FONT_SIZE), - 400 - 20, - (FONT_SIZE + PADDING_WITH_LINE + LINE_HEIGHT) - + i * (LINE_HEIGHT + PADDING_WITH_LINE * 2 + FONT_SIZE), - ), - fill=0, - ) + Bimage = create_to_display_image(to_display, epd.width, epd.height, font) + if TEST_IMAGE_GEN: + Bimage.save("display_controller/test_display.bmp") epd.display(epd.getbuffer(Bimage)) logging.info("Goto Sleep...") diff --git a/display_controller/lib/__init__.py b/display_controller/lib/__init__.py index d89ad13..2bf878a 100644 --- a/display_controller/lib/__init__.py +++ b/display_controller/lib/__init__.py @@ -1,10 +1,23 @@ from typing import List import logging import os +from PIL import Image, ImageFont, ImageDraw +import sys MAX_NB_COLS = 4 MAX_DISPLAYED_LINES = 5 +picdir = os.path.join( + os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "pic" +) +font = ImageFont.truetype(os.path.join(picdir, "Menlo.ttc"), 31) + +libdir = os.path.join( + os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "lib" +) +if os.path.exists(libdir): + sys.path.append(libdir) + def parse_api_result(result_filepath: str) -> List[str]: if not os.path.isfile(result_filepath): @@ -25,6 +38,7 @@ def parse_api_result(result_filepath: str) -> List[str]: line_direction = cols[1][:3] + ".." + cols[1][-3:] to_append = ( cols[0] + + " " * (3 - len(cols[0])) + " " + line_direction + " " @@ -34,3 +48,34 @@ def parse_api_result(result_filepath: str) -> List[str]: logging.info(f"appending {to_append}") to_display.append(to_append) return to_display + + +def create_to_display_image( + text_to_display: List[str], width: int, height: int, font: ImageFont.FreeTypeFont +) -> Image.Image: + # Drawing the next transport departures + logging.info("Drawing the next transport departures...") + Bimage = Image.new("1", (width, height), 255) # 255: clear the frame + draw = ImageDraw.Draw(Bimage) + LINE_HEIGHT = 2 + PADDING_WITH_LINE = 16 + FONT_SIZE = font.size + for i, s in enumerate(text_to_display): + draw.text( + (0, i * (FONT_SIZE + LINE_HEIGHT + PADDING_WITH_LINE * 2)), + s, + font=font, + fill=0, + ) + draw.rectangle( + ( + 20, + (FONT_SIZE + PADDING_WITH_LINE) + + i * (LINE_HEIGHT + PADDING_WITH_LINE * 2 + FONT_SIZE), + 400 - 20, + (FONT_SIZE + PADDING_WITH_LINE + LINE_HEIGHT) + + i * (LINE_HEIGHT + PADDING_WITH_LINE * 2 + FONT_SIZE), + ), + fill=0, + ) + return Bimage diff --git a/display_controller/test_display.bmp b/display_controller/test_display.bmp new file mode 100644 index 0000000..67d329a Binary files /dev/null and b/display_controller/test_display.bmp differ diff --git a/display_controller/tests.py b/display_controller/tests.py index 8d46f23..843c034 100644 --- a/display_controller/tests.py +++ b/display_controller/tests.py @@ -1,26 +1,43 @@ import unittest import os -from lib import parse_api_result +from lib import create_to_display_image, parse_api_result, font +from lib.waveshare_epd import epd4in2 +from PIL import Image, ImageChops TEST_RESULT_FILENAME = "api_result_test.tsv" +TEST_IMAGE_FILENAME = "test_display.bmp" +test_result_filepath = os.path.join( + os.path.dirname(os.path.dirname(os.path.realpath(__file__))), + "api_fetcher", + TEST_RESULT_FILENAME, +) -class TestParse(unittest.TestCase): +test_image_filepath = os.path.join( + os.path.dirname(os.path.realpath(__file__)), TEST_IMAGE_FILENAME +) + + +class TestDisplayController(unittest.TestCase): def test_parse_result(self): - test_result_filepath = os.path.join( - os.path.dirname(os.path.dirname(os.path.realpath(__file__))), - "api_fetcher", - TEST_RESULT_FILENAME, - ) expected = [ - "6 Gen..age 10:46+1", - "3 Gra..tti 10:46+1", - "9 Ver..urs 10:46", - "6 Ver..age 10:47", - "10 Gen..ive 10:47", + "6 Gen..age 10:46+1", + "3 Gra..tti 10:46+1", + "9 Ver..urs 10:46", + "6 Ver..age 10:47", + "10 Gen..ive 10:47", ] self.assertEqual(parse_api_result(test_result_filepath), expected) + def test_generate_image(self): + to_display = parse_api_result(test_result_filepath) + epd = epd4in2.EPD() + + test_image = create_to_display_image(to_display, epd.width, epd.height, font) + with Image.open(test_image_filepath) as expected_image: + diff = ImageChops.difference(test_image, expected_image) + self.assertIsNone(diff.getbbox()) + if __name__ == "__main__": unittest.main()