Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Galactic Unicorn] Add support to partial rendering of text #836

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions micropython/examples/galactic_unicorn/scrolling_text.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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()

Expand All @@ -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)
Expand Down