From 79b4a27b11d54855ab19fb81f99492ae1ed42216 Mon Sep 17 00:00:00 2001 From: Scott Dutton Date: Wed, 6 Sep 2023 20:16:29 +0100 Subject: [PATCH 1/3] Add support to partial rendering of text --- .../galactic_unicorn/scrolling_text.py | 48 +++++++++++++++---- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/micropython/examples/galactic_unicorn/scrolling_text.py b/micropython/examples/galactic_unicorn/scrolling_text.py index 40d8c7645..9563c78ff 100644 --- a/micropython/examples/galactic_unicorn/scrolling_text.py +++ b/micropython/examples/galactic_unicorn/scrolling_text.py @@ -1,6 +1,7 @@ import time from galactic import GalacticUnicorn from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY +# import random ''' Display scrolling wisdom, quotes or greetz. @@ -27,15 +28,16 @@ # function for drawing outlined text def outline_text(text, x, y): - graphics.set_pen(graphics.create_pen(int(OUTLINE_COLOUR[0]), int(OUTLINE_COLOUR[1]), int(OUTLINE_COLOUR[2]))) - graphics.text(text, x - 1, y - 1, -1, 1) - graphics.text(text, x, y - 1, -1, 1) - graphics.text(text, x + 1, y - 1, -1, 1) - graphics.text(text, x - 1, y, -1, 1) - graphics.text(text, x + 1, y, -1, 1) - graphics.text(text, x - 1, y + 1, -1, 1) - graphics.text(text, x, y + 1, -1, 1) - graphics.text(text, x + 1, y + 1, -1, 1) + if (OUTLINE_COLOUR != BACKGROUND_COLOUR): + graphics.set_pen(graphics.create_pen(int(OUTLINE_COLOUR[0]), int(OUTLINE_COLOUR[1]), int(OUTLINE_COLOUR[2]))) + graphics.text(text, x - 1, y - 1, -1, 1) + graphics.text(text, x, y - 1, -1, 1) + graphics.text(text, x + 1, y - 1, -1, 1) + graphics.text(text, x - 1, y, -1, 1) + graphics.text(text, x + 1, y, -1, 1) + graphics.text(text, x - 1, y + 1, -1, 1) + graphics.text(text, x, y + 1, -1, 1) + graphics.text(text, x + 1, y + 1, -1, 1) graphics.set_pen(graphics.create_pen(int(MESSAGE_COLOUR[0]), int(MESSAGE_COLOUR[1]), int(MESSAGE_COLOUR[2]))) graphics.text(text, x, y, -1, 1) @@ -47,6 +49,9 @@ def outline_text(text, x, y): STATE_PRE_SCROLL = 0 STATE_SCROLLING = 1 STATE_POST_SCROLL = 2 +LENGTH_TO_TRIGGER_PARTIAL_RENDER = 50 +CHARS_TO_RENDER = 30 +CHARS_LEFT_RESET = 5 shift = 0 state = STATE_PRE_SCROLL @@ -59,6 +64,14 @@ def outline_text(text, x, y): last_time = time.ticks_ms() +message_display = MESSAGE +partial_text = 0 +if (len(message_display) > LENGTH_TO_TRIGGER_PARTIAL_RENDER): + partial_text = 1 + message_display = MESSAGE[0:CHARS_TO_RENDER] + current_char = 0 +msg_width = graphics.measure_text(message_display, 1) + while True: time_ms = time.ticks_ms() @@ -77,17 +90,32 @@ def outline_text(text, x, y): shift += 1 if shift >= (msg_width + PADDING * 2) - width - 1: state = STATE_POST_SCROLL + current_char = 0 + message_display = MESSAGE[0:CHARS_TO_RENDER] last_time = time_ms if state == STATE_POST_SCROLL and time_ms - last_time > HOLD_TIME * 1000: state = STATE_PRE_SCROLL + # MESSAGE_COLOUR = (random.randint(0,255), random.randint(0,255), random.randint(0,255)) # Uncomment this and the random import to make the display a new colour each rotation shift = 0 last_time = time_ms + + if (partial_text == 1 and PADDING - shift < -10): + char_size = graphics.measure_text(message_display[0:1], 1) + if (message_display[0:1] == ' '): + char_size -= 1 + shift -= char_size + current_char += 1 + + message_display = MESSAGE[current_char:current_char+CHARS_TO_RENDER] + if (len(message_display) < CHARS_LEFT_RESET): + state = STATE_POST_SCROLL + last_time = time_ms graphics.set_pen(graphics.create_pen(int(BACKGROUND_COLOUR[0]), int(BACKGROUND_COLOUR[1]), int(BACKGROUND_COLOUR[2]))) graphics.clear() - outline_text(MESSAGE, x=PADDING - shift, y=2) + outline_text(message_display, x=PADDING - shift, y=2) # update the display gu.update(graphics) From 13e4b092fe4e9fb0d73edbf30c686726a136c3cf Mon Sep 17 00:00:00 2001 From: Scott Dutton Date: Mon, 11 Sep 2023 19:17:39 +0100 Subject: [PATCH 2/3] Update scrolling_text.py --- micropython/examples/galactic_unicorn/scrolling_text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/micropython/examples/galactic_unicorn/scrolling_text.py b/micropython/examples/galactic_unicorn/scrolling_text.py index 9563c78ff..d4ab61041 100644 --- a/micropython/examples/galactic_unicorn/scrolling_text.py +++ b/micropython/examples/galactic_unicorn/scrolling_text.py @@ -107,7 +107,7 @@ def outline_text(text, x, y): shift -= char_size current_char += 1 - message_display = MESSAGE[current_char:current_char+CHARS_TO_RENDER] + message_display = MESSAGE[current_char:current_char + CHARS_TO_RENDER] if (len(message_display) < CHARS_LEFT_RESET): state = STATE_POST_SCROLL last_time = time_ms From 3d2fa5d8a594aa8d5c1ed3112ec8aecd88a5eeef Mon Sep 17 00:00:00 2001 From: Scott Dutton Date: Fri, 15 Sep 2023 19:58:57 +0100 Subject: [PATCH 3/3] Update scrolling_text.py --- micropython/examples/galactic_unicorn/scrolling_text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/micropython/examples/galactic_unicorn/scrolling_text.py b/micropython/examples/galactic_unicorn/scrolling_text.py index d4ab61041..916c9b20c 100644 --- a/micropython/examples/galactic_unicorn/scrolling_text.py +++ b/micropython/examples/galactic_unicorn/scrolling_text.py @@ -99,7 +99,7 @@ def outline_text(text, x, y): # MESSAGE_COLOUR = (random.randint(0,255), random.randint(0,255), random.randint(0,255)) # Uncomment this and the random import to make the display a new colour each rotation shift = 0 last_time = time_ms - + if (partial_text == 1 and PADDING - shift < -10): char_size = graphics.measure_text(message_display[0:1], 1) if (message_display[0:1] == ' '):