WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit b502604

Browse files
authored
Merge pull request #1829 from ty-porter/issue-1619
Update runtext.py with more parameters
2 parents 591aa1f + a5a1113 commit b502604

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

bindings/python/samples/runtext.py

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,76 @@
11
#!/usr/bin/env python
22
# Display a runtext with double-buffering.
33
from samplebase import SampleBase
4+
from argparse import ArgumentTypeError
45
from rgbmatrix import graphics
56
import time
67

8+
DEFAULT_FONT = "../../../fonts/7x13.bdf"
9+
10+
def color(value):
11+
try:
12+
return graphics.Color(*[int(v) for v in value.split(",")])
13+
except:
14+
raise ArgumentTypeError(f"{value} is an invalid color value. Expected R,G,B values between 0-255, ex: 255,255,255")
715

816
class RunText(SampleBase):
917
def __init__(self, *args, **kwargs):
1018
super(RunText, self).__init__(*args, **kwargs)
19+
self.parser.add_argument("-f", "--font", help="Path to *.bdf-font to be used", default=DEFAULT_FONT)
1120
self.parser.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default="Hello world!")
1221

22+
self.parser.add_argument("-y", type=int, help="Shift Y-Origin of displaying text (Default: 10)", default=10)
23+
self.parser.add_argument("-l", "--loop", type=int, help="Number of loops through the text")
24+
self.parser.add_argument("-k", "--blink", help="Blink while scrolling. Keep on and off for these amount of scrolled pixels. Ex: 10,5", default=None)
25+
26+
self.parser.add_argument("-C", "--text-color", type=color, help="Text color. Default 255,255,255 (white)", default="255,255,255")
27+
self.parser.add_argument("-B", "--background-color", type=color, help="Background color. Default 0,0,0 (white)", default="0,0,0")
28+
29+
1330
def run(self):
14-
offscreen_canvas = self.matrix.CreateFrameCanvas()
31+
bg_color = self.args.background_color
32+
33+
main_canvas = self.matrix.CreateFrameCanvas()
34+
bg_canvas = self.matrix.CreateFrameCanvas()
35+
bg_canvas.Fill(bg_color.red, bg_color.green, bg_color.blue)
36+
1537
font = graphics.Font()
16-
font.LoadFont("../../../fonts/7x13.bdf")
17-
textColor = graphics.Color(255, 255, 0)
18-
pos = offscreen_canvas.width
19-
my_text = self.args.text
20-
21-
while True:
22-
offscreen_canvas.Clear()
23-
len = graphics.DrawText(offscreen_canvas, font, pos, 10, textColor, my_text)
24-
pos -= 1
25-
if (pos + len < 0):
26-
pos = offscreen_canvas.width
38+
font.LoadFont(self.args.font)
39+
x_pos = main_canvas.width
2740

28-
time.sleep(0.05)
29-
offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
41+
# Looping params
42+
i = 0
43+
loop_max = self.args.loop or float("inf")
3044

45+
# Blinking params
46+
blink_on_for, blink_off_for = [int(v) for v in self.args.blink.split(",")] if self.args.blink else [float("inf"), 0]
47+
blink_ct = 0
48+
blink_on = True
49+
50+
while i < loop_max:
51+
x_pos -= 1
52+
53+
if blink_on:
54+
if blink_ct >= blink_on_for:
55+
blink_on = False
56+
blink_ct = 0
57+
58+
main_canvas.Fill(bg_color.red, bg_color.green, bg_color.blue)
59+
len = graphics.DrawText(main_canvas, font, x_pos, self.args.y, self.args.text_color, self.args.text)
60+
main_canvas = self.matrix.SwapOnVSync(main_canvas)
61+
62+
if (x_pos + len < 0):
63+
i += 1
64+
x_pos = main_canvas.width
65+
else:
66+
if blink_ct >= blink_off_for:
67+
blink_on = True
68+
blink_ct = 0
69+
self.matrix.SwapOnVSync(bg_canvas)
70+
71+
blink_ct += 1
72+
73+
time.sleep(0.05)
3174

3275
# Main function
3376
if __name__ == "__main__":

0 commit comments

Comments
 (0)