Python Forum
help with scrolling text on RGB Matrix - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: help with scrolling text on RGB Matrix (/thread-41916.html)



help with scrolling text on RGB Matrix - Foutsy - Apr-09-2024

I am able to scroll text using Runtext.py but would like to have the text that is displayed, get it from a separate document. This would make it simpler for the end user to change the text being displayed without getting into the actual code.
Would anyone have any solutions for this?
Thanks


RE: help with scrolling text on RGB Matrix - deanhystad - Apr-09-2024

What is runtext.py? A google search came up with multiple example programs with this name. Can you post your code?


RE: help with scrolling text on RGB Matrix - Foutsy - Apr-09-2024

(Apr-09-2024, 05:47 PM)deanhystad Wrote: What is runtext.py? A google search came up with multiple example programs with this name. Can you post your code?

sorry, I'm very new to this. Below is the runtext.py code:

!/usr/bin/env python
# Display a runtext with double-buffering.
from samplebase import SampleBase
from rgbmatrix import graphics
import time


class RunText(SampleBase):
    def __init__(self, *args, **kwargs):
        super(RunText, self).__init__(*args, **kwargs)
        self.parser.add_argument("-t1", "--text", help="The text to scroll on the RGB LED panel", default="Hello world!") 

    def run(self):
        offscreen_canvas = self.matrix.CreateFrameCanvas()
        font = graphics.Font()
        font.LoadFont("../../../fonts/texgyre-27.bdf")
        textColor = graphics.Color(255,0,0)
        pos = offscreen_canvas.width
        my_text = self.args.text

        while True:
            offscreen_canvas.Clear()
            len = graphics.DrawText(offscreen_canvas, font, pos, 25, textColor, my_text)
            pos -= 1
            if (pos + len < 0):
                pos = offscreen_canvas.width

            time.sleep(0.015)
            offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)


# Main function
if __name__ == "__main__":
    run_text = RunText()
    if (not run_text.process()):
        run_text.print_help()



RE: help with scrolling text on RGB Matrix - deanhystad - Apr-09-2024

Looks like this one:

https://github.com/hzeller/rpi-rgb-led-matrix/blob/master/bindings/python/samples/samplebase.py

Your post was missing the last line. I corrected it.

The program already allows setting the message without any programming. Just supply the message in the command line.
Output:
python runtext.py -text "This is the message I want."
Changing the program to read the text from a file looks easy. You can reuse the "text" argument to be the name of a file that contains the text to display. You'll want to give text a default value that is a filename, not the text to display.
self.parser.add_argument("-t1", "--text", help="The text to scroll on the RGB LED panel", default=""message.txt") 
Only a few lines need to change in the run method.
    def run(self):
        offscreen_canvas = self.matrix.CreateFrameCanvas()
        font = graphics.Font()
        font.LoadFont("../../../fonts/texgyre-27.bdf")
        textColor = graphics.Color(255,0,0)
        pos = offscreen_canvas.width
        text_file = self.args.text

        # Read text from file
        with open(text_file, "r") as file:
            my_text = file.read()
 
        while True:
            offscreen_canvas.Clear()
            len = graphics.DrawText(offscreen_canvas, font, pos, 25, textColor, my_text)
            pos -= 1
            if (pos + len < 0):
                pos = offscreen_canvas.width
                # Each time we scrolled the entire message, read the text from the file.
                # To change text, modify the file, and when the current message is done,
                # the new message will be displayed.
                with open(text_file, "r") as file:
                    my_text = file.read()
 
            time.sleep(0.015)
            offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
I don't have a device for testing this code, so there may be bugs.