mirror of
https://github.com/Hopiu/rpi-rgb-led-matrix.git
synced 2026-03-16 22:10:27 +00:00
Moved -t argument from SampleBase to RunText, let SampleBase inherit from object instead of ArgumentParser, code cleanup
This commit is contained in:
parent
6e41022019
commit
f0fb434916
10 changed files with 102 additions and 94 deletions
|
|
@ -1,14 +1,15 @@
|
|||
#!/usr/bin/env python
|
||||
from samplebase import SampleBase
|
||||
from rgbmatrix import graphics
|
||||
import time;
|
||||
import time
|
||||
|
||||
|
||||
class GraphicsTest(SampleBase):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(GraphicsTest, self).__init__(*args, **kwargs)
|
||||
|
||||
def Run(self):
|
||||
canvas = self.matrix;
|
||||
def run(self):
|
||||
canvas = self.matrix
|
||||
font = graphics.Font()
|
||||
font.LoadFont("../../fonts/7x13.bdf")
|
||||
|
||||
|
|
@ -26,6 +27,6 @@ class GraphicsTest(SampleBase):
|
|||
|
||||
# Main function
|
||||
if __name__ == "__main__":
|
||||
parser = GraphicsTest()
|
||||
if (not parser.process()):
|
||||
parser.print_help()
|
||||
graphics_test = GraphicsTest()
|
||||
if (not graphics_test.process()):
|
||||
graphics_test.print_help()
|
||||
|
|
|
|||
|
|
@ -2,11 +2,12 @@
|
|||
from samplebase import SampleBase
|
||||
import time
|
||||
|
||||
|
||||
class GrayscaleBlock(SampleBase):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(GrayscaleBlock, self).__init__(*args, **kwargs)
|
||||
|
||||
def Run(self):
|
||||
def run(self):
|
||||
sub_blocks = 16
|
||||
width = self.matrix.width
|
||||
height = self.matrix.height
|
||||
|
|
@ -33,6 +34,6 @@ class GrayscaleBlock(SampleBase):
|
|||
|
||||
# Main function
|
||||
if __name__ == "__main__":
|
||||
parser = GrayscaleBlock()
|
||||
if (not parser.process()):
|
||||
parser.print_help()
|
||||
grayscale_block = GrayscaleBlock()
|
||||
if (not grayscale_block.process()):
|
||||
grayscale_block.print_help()
|
||||
|
|
|
|||
|
|
@ -26,17 +26,17 @@ matrix = RGBMatrix(32, 1, 1)
|
|||
|
||||
# RGB example w/graphics prims.
|
||||
# Note, only "RGB" mode is supported currently.
|
||||
image = Image.new("RGB", (32, 32)) # Can be larger than matrix if wanted!!
|
||||
draw = ImageDraw.Draw(image) # Declare Draw instance before prims
|
||||
image = Image.new("RGB", (32, 32)) # Can be larger than matrix if wanted!!
|
||||
draw = ImageDraw.Draw(image) # Declare Draw instance before prims
|
||||
# Draw some shapes into image (no immediate effect on matrix)...
|
||||
draw.rectangle((0, 0, 31, 31), fill=(0, 0, 0), outline=(0,0,255))
|
||||
draw.rectangle((0, 0, 31, 31), fill=(0, 0, 0), outline=(0, 0, 255))
|
||||
draw.line((0, 0, 31, 31), fill=(255, 0, 0))
|
||||
draw.line((0, 31, 31, 0), fill=(0, 255, 0))
|
||||
|
||||
# Then scroll image across matrix...
|
||||
for n in range(-32, 33): # Start off top-left, move off bottom-right
|
||||
matrix.Clear()
|
||||
matrix.SetImage(image, n, n)
|
||||
time.sleep(0.05)
|
||||
for n in range(-32, 33): # Start off top-left, move off bottom-right
|
||||
matrix.Clear()
|
||||
matrix.SetImage(image, n, n)
|
||||
time.sleep(0.05)
|
||||
|
||||
matrix.Clear()
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
#!/usr/bin/env python
|
||||
import time
|
||||
from samplebase import SampleBase
|
||||
from rgbmatrix import RGBMatrix
|
||||
from PIL import Image
|
||||
|
||||
|
||||
class ImageScroller(SampleBase):
|
||||
def __init__(self, image_file, *args, **kwargs):
|
||||
super(ImageScroller, self).__init__(*args, **kwargs)
|
||||
self.image = Image.open(image_file)
|
||||
|
||||
def Run(self):
|
||||
def run(self):
|
||||
self.image.resize((self.matrix.width, self.matrix.height), Image.ANTIALIAS)
|
||||
|
||||
doubleBuffer = self.matrix.CreateFrameCanvas()
|
||||
double_buffer = self.matrix.CreateFrameCanvas()
|
||||
img_width, img_height = self.image.size
|
||||
|
||||
# let's scroll
|
||||
|
|
@ -22,10 +22,10 @@ class ImageScroller(SampleBase):
|
|||
if (xpos > img_width):
|
||||
xpos = 0
|
||||
|
||||
doubleBuffer.SetImage(self.image, -xpos)
|
||||
doubleBuffer.SetImage(self.image, -xpos + img_width)
|
||||
double_buffer.SetImage(self.image, -xpos)
|
||||
double_buffer.SetImage(self.image, -xpos + img_width)
|
||||
|
||||
doubleBuffer = self.matrix.SwapOnVSync(doubleBuffer)
|
||||
double_buffer = self.matrix.SwapOnVSync(double_buffer)
|
||||
time.sleep(0.01)
|
||||
|
||||
# Main function
|
||||
|
|
@ -33,6 +33,6 @@ class ImageScroller(SampleBase):
|
|||
# sudo ./image-scroller.py --chain=4
|
||||
# if you have a chain of four
|
||||
if __name__ == "__main__":
|
||||
scroller = ImageScroller(image_file = "../../examples-api-use/runtext.ppm")
|
||||
if (not scroller.process()):
|
||||
scroller.print_help()
|
||||
image_scroller = ImageScroller(image_file="../../examples-api-use/runtext.ppm")
|
||||
if (not image_scroller.process()):
|
||||
image_scroller.print_help()
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
#!/usr/bin/env python
|
||||
from samplebase import SampleBase
|
||||
import time
|
||||
|
||||
|
||||
class GrayscaleBlock(SampleBase):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(GrayscaleBlock, self).__init__(*args, **kwargs)
|
||||
|
||||
def Run(self):
|
||||
def run(self):
|
||||
max_brightness = self.matrix.brightness
|
||||
count = 0
|
||||
c = 255
|
||||
|
|
@ -31,6 +31,6 @@ class GrayscaleBlock(SampleBase):
|
|||
|
||||
# Main function
|
||||
if __name__ == "__main__":
|
||||
parser = GrayscaleBlock()
|
||||
if (not parser.process()):
|
||||
parser.print_help()
|
||||
grayscale_block = GrayscaleBlock()
|
||||
if (not grayscale_block.process()):
|
||||
grayscale_block.print_help()
|
||||
|
|
|
|||
|
|
@ -1,12 +1,13 @@
|
|||
#!/usr/bin/env python
|
||||
from samplebase import SampleBase
|
||||
|
||||
|
||||
class PulsingColors(SampleBase):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(PulsingColors, self).__init__(*args, **kwargs)
|
||||
|
||||
def Run(self):
|
||||
self.offscreenCanvas = self.matrix.CreateFrameCanvas()
|
||||
def run(self):
|
||||
self.offscreen_canvas = self.matrix.CreateFrameCanvas()
|
||||
continuum = 0
|
||||
|
||||
while True:
|
||||
|
|
@ -31,11 +32,11 @@ class PulsingColors(SampleBase):
|
|||
green = 255 - c
|
||||
blue = c
|
||||
|
||||
self.offscreenCanvas.Fill(red, green, blue)
|
||||
self.offscreenCanvas = self.matrix.SwapOnVSync(self.offscreenCanvas)
|
||||
self.offscreen_canvas.Fill(red, green, blue)
|
||||
self.offscreen_canvas = self.matrix.SwapOnVSync(self.offscreen_canvas)
|
||||
|
||||
# Main function
|
||||
if __name__ == "__main__":
|
||||
parser = PulsingColors()
|
||||
if (not parser.process()):
|
||||
parser.print_help()
|
||||
pulsing_colors = PulsingColors()
|
||||
if (not pulsing_colors.process()):
|
||||
pulsing_colors.print_help()
|
||||
|
|
|
|||
|
|
@ -2,11 +2,12 @@
|
|||
from samplebase import SampleBase
|
||||
import math
|
||||
|
||||
|
||||
class RotatingBlockGenerator(SampleBase):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(RotatingBlockGenerator, self).__init__(*args, **kwargs)
|
||||
|
||||
def Rotate(self, x, y, angle):
|
||||
def rotate(self, x, y, angle):
|
||||
return {
|
||||
"new_x": x * math.cos(angle) - y * math.sin(angle),
|
||||
"new_y": x * math.sin(angle) + y * math.cos(angle)
|
||||
|
|
@ -19,7 +20,7 @@ class RotatingBlockGenerator(SampleBase):
|
|||
return 255
|
||||
return 255 * (val - lo) / (hi - lo)
|
||||
|
||||
def Run(self):
|
||||
def run(self):
|
||||
cent_x = self.matrix.width / 2
|
||||
cent_y = self.matrix.height / 2
|
||||
|
||||
|
|
@ -33,7 +34,7 @@ class RotatingBlockGenerator(SampleBase):
|
|||
|
||||
deg_to_rad = 2 * 3.14159265 / 360
|
||||
rotation = 0
|
||||
offsetCanvas = self.matrix.CreateFrameCanvas()
|
||||
offset_canvas = self.matrix.CreateFrameCanvas()
|
||||
|
||||
while True:
|
||||
rotation += 1
|
||||
|
|
@ -41,19 +42,19 @@ class RotatingBlockGenerator(SampleBase):
|
|||
|
||||
for x in range(int(min_rotate), int(max_rotate)):
|
||||
for y in range(int(min_rotate), int(max_rotate)):
|
||||
ret = self.Rotate(x - cent_x, y - cent_x, deg_to_rad * rotation)
|
||||
ret = self.rotate(x - cent_x, y - cent_x, deg_to_rad * rotation)
|
||||
rot_x = ret["new_x"]
|
||||
rot_y = ret["new_y"]
|
||||
|
||||
if x >= min_display and x < max_display and y >= min_display and y < max_display:
|
||||
offsetCanvas.SetPixel(rot_x + cent_x, rot_y + cent_y, self.scale_col(x, min_display, max_display), 255 - self.scale_col(y, min_display, max_display), self.scale_col(y, min_display, max_display))
|
||||
if x >= min_display and x < max_display and y >= min_display and y < max_display:
|
||||
offset_canvas.SetPixel(rot_x + cent_x, rot_y + cent_y, self.scale_col(x, min_display, max_display), 255 - self.scale_col(y, min_display, max_display), self.scale_col(y, min_display, max_display))
|
||||
else:
|
||||
offsetCanvas.SetPixel(rot_x + cent_x, rot_y + cent_y, 0, 0, 0)
|
||||
offset_canvas.SetPixel(rot_x + cent_x, rot_y + cent_y, 0, 0, 0)
|
||||
|
||||
offsetCanvas = self.matrix.SwapOnVSync(offsetCanvas)
|
||||
offset_canvas = self.matrix.SwapOnVSync(offset_canvas)
|
||||
|
||||
# Main function
|
||||
if __name__ == "__main__":
|
||||
parser = RotatingBlockGenerator()
|
||||
if (not parser.process()):
|
||||
parser.print_help()
|
||||
rotating_block_generator = RotatingBlockGenerator()
|
||||
if (not rotating_block_generator.process()):
|
||||
rotating_block_generator.print_help()
|
||||
|
|
|
|||
|
|
@ -4,31 +4,33 @@ 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("-t", "--text", help="The text to scroll on the RGB LED panel", default="Hello world!")
|
||||
|
||||
def Run(self):
|
||||
offscreenCanvas = self.matrix.CreateFrameCanvas()
|
||||
def run(self):
|
||||
offscreen_canvas = self.matrix.CreateFrameCanvas()
|
||||
font = graphics.Font()
|
||||
font.LoadFont("../../fonts/7x13.bdf")
|
||||
textColor = graphics.Color(255, 255, 0)
|
||||
pos = offscreenCanvas.width
|
||||
myText = self.args["text"]
|
||||
pos = offscreen_canvas.width
|
||||
my_text = self.args.text
|
||||
|
||||
while True:
|
||||
offscreenCanvas.Clear()
|
||||
len = graphics.DrawText(offscreenCanvas, font, pos, 10, textColor, myText)
|
||||
offscreen_canvas.Clear()
|
||||
len = graphics.DrawText(offscreen_canvas, font, pos, 10, textColor, my_text)
|
||||
pos -= 1
|
||||
if (pos + len < 0):
|
||||
pos = offscreenCanvas.width
|
||||
pos = offscreen_canvas.width
|
||||
|
||||
time.sleep(0.05)
|
||||
offscreenCanvas = self.matrix.SwapOnVSync(offscreenCanvas)
|
||||
offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
|
||||
|
||||
|
||||
# Main function
|
||||
if __name__ == "__main__":
|
||||
parser = RunText()
|
||||
if (not parser.process()):
|
||||
parser.print_help()
|
||||
run_text = RunText()
|
||||
if (not run_text.process()):
|
||||
run_text.print_help()
|
||||
|
|
|
|||
41
python/samples/samplebase.py
Normal file → Executable file
41
python/samples/samplebase.py
Normal file → Executable file
|
|
@ -1,45 +1,46 @@
|
|||
import argparse, time, sys, os
|
||||
import argparse
|
||||
import time
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/..'))
|
||||
from rgbmatrix import RGBMatrix
|
||||
|
||||
class SampleBase(argparse.ArgumentParser):
|
||||
|
||||
class SampleBase(object):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(SampleBase, self).__init__(*args, **kwargs)
|
||||
self.parser = argparse.ArgumentParser()
|
||||
|
||||
# TODO: could this fill RGBMatrix::Options instead ?
|
||||
self.add_argument("-r", "--rows", action = "store", help = "Display rows. 16 for 16x32, 32 for 32x32. Default: 32", default = 32, type = int)
|
||||
self.add_argument("-P", "--parallel", action = "store", help = "For Plus-models or RPi2: parallel chains. 1..3. Default: 1", default = 1, type = int)
|
||||
self.add_argument("-c", "--chain", action = "store", help = "Daisy-chained boards. Default: 1.", default = 1, type = int)
|
||||
self.add_argument("-p", "--pwmbits", action = "store", help = "Bits used for PWM. Something between 1..11. Default: 11", default = 11, type = int)
|
||||
self.add_argument("-l", "--luminance", action = "store_true", help = "Don't do luminance correction (CIE1931)")
|
||||
self.add_argument("-b", "--brightness", action = "store", help = "Sets brightness level. Default: 100. Range: 1..100", default = 100, type = int)
|
||||
self.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default = "Hello world!")
|
||||
|
||||
self.args = {}
|
||||
self.parser.add_argument("-r", "--rows", action="store", help="Display rows. 16 for 16x32, 32 for 32x32. Default: 32", default=32, type=int)
|
||||
self.parser.add_argument("-P", "--parallel", action="store", help="For Plus-models or RPi2: parallel chains. 1..3. Default: 1", default=1, type=int)
|
||||
self.parser.add_argument("-c", "--chain", action="store", help="Daisy-chained boards. Default: 1.", default=1, type=int)
|
||||
self.parser.add_argument("-p", "--pwmbits", action="store", help="Bits used for PWM. Something between 1..11. Default: 11", default=11, type=int)
|
||||
self.parser.add_argument("-l", "--luminance", action="store_true", help="Don't do luminance correction (CIE1931)")
|
||||
self.parser.add_argument("-b", "--brightness", action="store", help="Sets brightness level. Default: 100. Range: 1..100", default=100, type=int)
|
||||
|
||||
def usleep(self, value):
|
||||
time.sleep(value / 1000000.0)
|
||||
|
||||
def Run(self):
|
||||
def run(self):
|
||||
print("Running")
|
||||
|
||||
def process(self):
|
||||
self.args = vars(self.parse_args())
|
||||
|
||||
self.args = self.parser.parse_args()
|
||||
|
||||
# TODO: validate values with RGBmatrix::Options::Validate().
|
||||
|
||||
self.matrix = RGBMatrix(self.args["rows"], self.args["chain"], self.args["parallel"])
|
||||
self.matrix.pwmBits = self.args["pwmbits"]
|
||||
self.matrix.brightness = self.args["brightness"]
|
||||
self.matrix = RGBMatrix(self.args.rows, self.args.chain, self.args.parallel)
|
||||
self.matrix.pwmBits = self.args.pwmbits
|
||||
self.matrix.brightness = self.args.brightness
|
||||
|
||||
if self.args["luminance"]:
|
||||
if self.args.luminance:
|
||||
self.matrix.luminanceCorrect = False
|
||||
|
||||
try:
|
||||
# Start loop
|
||||
print("Press CTRL-C to stop sample")
|
||||
self.Run()
|
||||
self.run()
|
||||
except KeyboardInterrupt:
|
||||
print("Exiting\n")
|
||||
sys.exit(0)
|
||||
|
|
|
|||
|
|
@ -1,29 +1,30 @@
|
|||
#!/usr/bin/env python
|
||||
from samplebase import SampleBase
|
||||
|
||||
|
||||
class SimpleSquare(SampleBase):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(SimpleSquare, self).__init__(*args, **kwargs)
|
||||
|
||||
def Run(self):
|
||||
offsetCanvas = self.matrix.CreateFrameCanvas()
|
||||
def run(self):
|
||||
offset_canvas = self.matrix.CreateFrameCanvas()
|
||||
while True:
|
||||
for x in range(0, self.matrix.width):
|
||||
offsetCanvas.SetPixel(x, x, 255, 255, 255)
|
||||
offsetCanvas.SetPixel(offsetCanvas.height - 1 - x, x, 255, 0, 255)
|
||||
offset_canvas.SetPixel(x, x, 255, 255, 255)
|
||||
offset_canvas.SetPixel(offset_canvas.height - 1 - x, x, 255, 0, 255)
|
||||
|
||||
for x in range(0, offsetCanvas.width):
|
||||
offsetCanvas.SetPixel(x, 0, 255, 0, 0)
|
||||
offsetCanvas.SetPixel(x, offsetCanvas.height - 1, 255, 255, 0)
|
||||
for x in range(0, offset_canvas.width):
|
||||
offset_canvas.SetPixel(x, 0, 255, 0, 0)
|
||||
offset_canvas.SetPixel(x, offset_canvas.height - 1, 255, 255, 0)
|
||||
|
||||
for y in range(0, offsetCanvas.height):
|
||||
offsetCanvas.SetPixel(0, y, 0, 0, 255)
|
||||
offsetCanvas.SetPixel(offsetCanvas.width - 1, y, 0, 255, 0)
|
||||
offsetCanvas = self.matrix.SwapOnVSync(offsetCanvas)
|
||||
for y in range(0, offset_canvas.height):
|
||||
offset_canvas.SetPixel(0, y, 0, 0, 255)
|
||||
offset_canvas.SetPixel(offset_canvas.width - 1, y, 0, 255, 0)
|
||||
offset_canvas = self.matrix.SwapOnVSync(offset_canvas)
|
||||
|
||||
|
||||
# Main function
|
||||
if __name__ == "__main__":
|
||||
parser = SimpleSquare()
|
||||
if (not parser.process()):
|
||||
parser.print_help()
|
||||
simple_square = SimpleSquare()
|
||||
if (not simple_square.process()):
|
||||
simple_square.print_help()
|
||||
|
|
|
|||
Loading…
Reference in a new issue