diff --git a/luma/led_matrix/legacy.py b/luma/led_matrix/legacy.py index c6b14a7..ed60b46 100644 --- a/luma/led_matrix/legacy.py +++ b/luma/led_matrix/legacy.py @@ -1382,11 +1382,9 @@ def text(draw, xy, text, fill=None, font=None): x += 1 -def show_message(device, msg, y_offset=0, fill=None, font=None): +def show_message(device, msg, y_offset=0, delay=0.03, fill=None, font=None): font = font or DEFAULT_FONT - with canvas(device) as draw: - w, h = textsize(msg, font) - + w, h = textsize(msg, font) x = device.width virtual = viewport(device, width=w + x + x, height=h) @@ -1396,5 +1394,5 @@ def show_message(device, msg, y_offset=0, fill=None, font=None): i = 0 while i < w + x: virtual.set_position((i, 0)) + time.sleep(delay) i += 1 - time.sleep(0.03) diff --git a/tests/reference/show_message.gif b/tests/reference/show_message.gif new file mode 100644 index 0000000..76062a4 Binary files /dev/null and b/tests/reference/show_message.gif differ diff --git a/tests/test_legacy.py b/tests/test_legacy.py index ea9c530..2d00a18 100644 --- a/tests/test_legacy.py +++ b/tests/test_legacy.py @@ -4,12 +4,23 @@ # See LICENSE.rst for details. +import hashlib +import os.path +from tempfile import NamedTemporaryFile +from PIL import Image + try: from unittest.mock import call, Mock except ImportError: from mock import call, Mock -from luma.led_matrix.legacy import text, textsize, proportional, CP437_FONT, LCD_FONT +from luma.core.emulator import gifanim +from luma.led_matrix.legacy import text, show_message, textsize, proportional, CP437_FONT, LCD_FONT + + +def md5(fname): + with open(fname, 'rb') as fp: + return hashlib.md5(fp.read()).hexdigest() def test_textsize(): @@ -39,3 +50,28 @@ def test_text_char(): call((5, 8), fill='white'), call((6, 8), fill='white') ]) + + +def test_show_message(): + reference = os.path.abspath(os.path.join( + os.path.dirname(__file__), + 'reference', + 'show_message.gif')) + + fname = NamedTemporaryFile(suffix=".gif").name + device = gifanim(width=40, height=8, transform="none", filename=fname) + + show_message(device, "Hello world!", delay=0, fill="white") + + device.write_animation() + + ref = Image.open(reference) + act = Image.open(fname) + + try: + while True: + assert ref == act + ref.seek(ref.tell() + 1) + act.seek(act.tell() + 1) + except EOFError: + pass