Skip to content

Commit

Permalink
refactors and adds tests for display
Browse files Browse the repository at this point in the history
  • Loading branch information
Robb-Fr committed Apr 23, 2024
1 parent 059284f commit 3d89743
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 49 deletions.
7 changes: 0 additions & 7 deletions display_controller/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
37 changes: 7 additions & 30 deletions display_controller/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__))),
Expand All @@ -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...")
Expand Down
45 changes: 45 additions & 0 deletions display_controller/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
+ " "
Expand All @@ -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
Binary file added display_controller/test_display.bmp
Binary file not shown.
41 changes: 29 additions & 12 deletions display_controller/tests.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 3d89743

Please sign in to comment.