|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # Display a runtext with double-buffering. |
3 | 3 | from samplebase import SampleBase |
| 4 | +from argparse import ArgumentTypeError |
4 | 5 | from rgbmatrix import graphics |
5 | 6 | import time |
6 | 7 |
|
| 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") |
7 | 15 |
|
8 | 16 | class RunText(SampleBase): |
9 | 17 | def __init__(self, *args, **kwargs): |
10 | 18 | super(RunText, self).__init__(*args, **kwargs) |
| 19 | + self.parser.add_argument("-f", "--font", help="Path to *.bdf-font to be used", default=DEFAULT_FONT) |
11 | 20 | self.parser.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default="Hello world!") |
12 | 21 |
|
| 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 | + |
13 | 30 | 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 | + |
15 | 37 | 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 |
27 | 40 |
|
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") |
30 | 44 |
|
| 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) |
31 | 74 |
|
32 | 75 | # Main function |
33 | 76 | if __name__ == "__main__": |
|
0 commit comments