Skip to content

Commit 985d72c

Browse files
authored
Merge pull request #18 from FoamyGuy/updating_examples
Updating examples for newer init API
2 parents dd77450 + 248c59a commit 985d72c

10 files changed

+109
-98
lines changed

examples/fbmirror.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
linux_framebuffer = np.memmap('/dev/fb0',mode='r', shape=(screeny, stride // bytes_per_pixel), dtype=dtype)
3535

3636
@click.command
37-
@click.option("--x-offset", "xoffset", type=int, help="The x offset of top left corner of the region to mirror")
38-
@click.option("--y-offset", "yoffset", type=int, help="The y offset of top left corner of the region to mirror")
37+
@click.option("--x-offset", "xoffset", type=int, help="The x offset of top left corner of the region to mirror", default=0)
38+
@click.option("--y-offset", "yoffset", type=int, help="The y offset of top left corner of the region to mirror", default=0)
3939
@piomatter_click.standard_options
40-
def main(xoffset, yoffset, width, height, serpentine, rotation, colorspace, pinout, n_planes, n_addr_lines):
40+
def main(xoffset, yoffset, width, height, serpentine, rotation, pinout, n_planes, n_addr_lines):
4141
geometry = piomatter.Geometry(width=width, height=height, n_planes=n_planes, n_addr_lines=n_addr_lines, rotation=rotation)
4242
framebuffer = np.zeros(shape=(geometry.height, geometry.width), dtype=dtype)
43-
matrix = piomatter.PioMatter(colorspace=colorspace, pinout=pinout, framebuffer=framebuffer, geometry=geometry)
43+
matrix = piomatter.PioMatter(colorspace=piomatter.Colorspace.RGB565, pinout=pinout, framebuffer=framebuffer, geometry=geometry)
4444

4545
while True:
4646
framebuffer[:,:] = linux_framebuffer[yoffset:yoffset+height, xoffset:xoffset+width]

examples/fbmirror_scaled.py

+59-63
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,47 @@
11
#!/usr/bin/python3
22
"""
3-
Mirror a scaled copy of the framebuffer to 64x32 matrices,
3+
Mirror a scaled copy of the framebuffer to RGB matrices,
44
5-
The upper left corner of the framebuffer is displayed until the user hits ctrl-c.
5+
A portion of the framebuffer is displayed until the user hits ctrl-c.
66
77
Control scale, matrix size, and orientation with command line arguments.
88
9-
python fbmirror_scaled.py [scale] [width] [height] [orientation]
9+
Usage: fbmirror_scaled.py [OPTIONS]
10+
11+
Options:
12+
--x-offset INTEGER The x offset of top left corner of the
13+
region to mirror
14+
--y-offset INTEGER The y offset of top left corner of the
15+
region to mirror
16+
--scale INTEGER The scale factor to reduce the display down
17+
by.
18+
--num-address-lines INTEGER The number of address lines used by the
19+
panels
20+
--num-planes INTEGER The number of bit planes (color depth. Lower
21+
values can improve refresh rate in frames
22+
per second
23+
--orientation [Normal|R180|CCW|CW]
24+
The overall orientation (rotation) of the
25+
panels
26+
--pinout [AdafruitMatrixBonnet|AdafruitMatrixBonnetBGR|AdafruitMatrixHat|AdafruitMatrixHatBGR]
27+
The details of the electrical connection to
28+
the panels
29+
--serpentine / --no-serpentine The organization of multiple panels
30+
--height INTEGER The panel height in pixels
31+
--width INTEGER The panel width in pixels
32+
--help Show this message and exit.
1033
11-
scale int: How many times to scale down the display framebuffer. Default is 3.
12-
width int: Total width of matrices in pixels. Default is 64.
13-
height int: Total height of matrices in pixels. Default is 32.
14-
orientation int: Orientation in degrees, must be 0, 90, 180, or 270.
15-
Default is 0 or Normal orientation.
1634
1735
The `/dev/fb0` special file will exist if a monitor is plugged in at boot time,
1836
or if `/boot/firmware/cmdline.txt` specifies a resolution such as
1937
`... video=HDMI-A-1:640x480M@60D`.
2038
"""
21-
import sys
2239

23-
import adafruit_raspberry_pi5_piomatter
40+
import adafruit_raspberry_pi5_piomatter as piomatter
41+
import click
2442
import numpy as np
2543
import PIL.Image as Image
26-
27-
if len(sys.argv) >= 2:
28-
scale = int(sys.argv[1])
29-
else:
30-
scale = 3
31-
32-
if len(sys.argv) >= 3:
33-
width = int(sys.argv[2])
34-
else:
35-
width = 64
36-
37-
if len(sys.argv) >= 4:
38-
height = int(sys.argv[3])
39-
else:
40-
height = 32
41-
42-
if len(sys.argv) >= 5:
43-
rotation = int(sys.argv[4])
44-
if rotation == 90:
45-
rotation = adafruit_raspberry_pi5_piomatter.Orientation.CW
46-
elif rotation == 180:
47-
rotation = adafruit_raspberry_pi5_piomatter.Orientation.R180
48-
elif rotation == 270:
49-
rotation = adafruit_raspberry_pi5_piomatter.Orientation.CCW
50-
elif rotation == 0:
51-
rotation = adafruit_raspberry_pi5_piomatter.Orientation.Normal
52-
else:
53-
raise ValueError("Invalid rotation. Must be 0, 90, 180, or 270.")
54-
else:
55-
rotation = adafruit_raspberry_pi5_piomatter.Orientation.Normal
44+
import piomatter_click
5645

5746
with open("/sys/class/graphics/fb0/virtual_size") as f:
5847
screenx, screeny = [int(word) for word in f.read().split(",")]
@@ -70,26 +59,33 @@
7059

7160
linux_framebuffer = np.memmap('/dev/fb0',mode='r', shape=(screeny, stride // bytes_per_pixel), dtype=dtype)
7261

73-
xoffset = 0
74-
yoffset = 0
75-
76-
geometry = adafruit_raspberry_pi5_piomatter.Geometry(width=width, height=height, n_addr_lines=4, rotation=rotation)
77-
matrix_framebuffer = np.zeros(shape=(geometry.height, geometry.width, 3), dtype=np.uint8)
78-
matrix = adafruit_raspberry_pi5_piomatter.AdafruitMatrixBonnetRGB888Packed(matrix_framebuffer, geometry)
79-
80-
while True:
81-
tmp = linux_framebuffer[yoffset:yoffset+height*scale, xoffset:xoffset+width*scale]
82-
# Convert the RGB565 framebuffer into RGB888Packed (so that we can use PIL image operations to rescale it)
83-
r = (tmp & 0xf800) >> 8
84-
r = r | (r >> 5)
85-
r = r.astype(np.uint8)
86-
g = (tmp & 0x07e0) >> 3
87-
g = g | (g >> 6)
88-
g = g.astype(np.uint8)
89-
b = (tmp & 0x001f) << 3
90-
b = b | (b >> 5)
91-
b = b.astype(np.uint8)
92-
img = Image.fromarray(np.stack([r, g, b], -1))
93-
img = img.resize((width, height))
94-
matrix_framebuffer[:,:] = np.asarray(img)
95-
matrix.show()
62+
63+
@click.command
64+
@click.option("--x-offset", "xoffset", type=int, help="The x offset of top left corner of the region to mirror", default=0)
65+
@click.option("--y-offset", "yoffset", type=int, help="The y offset of top left corner of the region to mirror", default=0)
66+
@click.option("--scale", "scale", type=int, help="The scale factor to reduce the display down by.", default=3)
67+
@piomatter_click.standard_options
68+
def main(xoffset, yoffset, scale, width, height, serpentine, rotation, pinout, n_planes, n_addr_lines):
69+
geometry = piomatter.Geometry(width=width, height=height, n_planes=n_planes, n_addr_lines=n_addr_lines, rotation=rotation)
70+
matrix_framebuffer = np.zeros(shape=(geometry.height, geometry.width, 3), dtype=np.uint8)
71+
matrix = piomatter.PioMatter(colorspace=piomatter.Colorspace.RGB888Packed, pinout=pinout, framebuffer=matrix_framebuffer, geometry=geometry)
72+
73+
while True:
74+
tmp = linux_framebuffer[yoffset:yoffset + height * scale, xoffset:xoffset + width * scale]
75+
# Convert the RGB565 framebuffer into RGB888Packed (so that we can use PIL image operations to rescale it)
76+
r = (tmp & 0xf800) >> 8
77+
r = r | (r >> 5)
78+
r = r.astype(np.uint8)
79+
g = (tmp & 0x07e0) >> 3
80+
g = g | (g >> 6)
81+
g = g.astype(np.uint8)
82+
b = (tmp & 0x001f) << 3
83+
b = b | (b >> 5)
84+
b = b.astype(np.uint8)
85+
img = Image.fromarray(np.stack([r, g, b], -1))
86+
img = img.resize((width, height))
87+
matrix_framebuffer[:, :] = np.array(img)
88+
matrix.show()
89+
90+
if __name__ == '__main__':
91+
main()

examples/piomatter_click.py

-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def standard_options(
3232
height=32,
3333
serpentine=True,
3434
rotation=piomatter.Orientation.Normal,
35-
colorspace=piomatter.Colorspace.RGB888,
3635
pinout=piomatter.Pinout.AdafruitMatrixBonnet,
3736
n_planes=10,
3837
n_addr_lines=4,
@@ -58,13 +57,6 @@ def wrapper(f: click.decorators.FC):
5857
f = click.option("--height", default=height, help="The panel height in pixels")(f)
5958
if serpentine is not None:
6059
f = click.option("--serpentine/--no-serpentine", default=serpentine, help="The organization of multiple panels")(f)
61-
if colorspace is not None:
62-
f = click.option(
63-
"--colorspace",
64-
default=colorspace,
65-
type=PybindEnumChoice(piomatter.Colorspace),
66-
help="The memory organization of the framebuffer"
67-
)(f)
6860
if pinout is not None:
6961
f = click.option(
7062
"--pinout",

examples/play_gif.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import time
1313

14-
import adafruit_raspberry_pi5_piomatter
14+
import adafruit_raspberry_pi5_piomatter as piomatter
1515
import numpy as np
1616
import PIL.Image as Image
1717

@@ -21,9 +21,13 @@
2121
gif_file = "nyan.gif"
2222

2323
canvas = Image.new('RGB', (width, height), (0, 0, 0))
24-
geometry = adafruit_raspberry_pi5_piomatter.Geometry(width=width, height=height, n_addr_lines=4, rotation=adafruit_raspberry_pi5_piomatter.Orientation.Normal)
24+
geometry = piomatter.Geometry(width=width, height=height,
25+
n_addr_lines=4, rotation=piomatter.Orientation.Normal)
2526
framebuffer = np.asarray(canvas) + 0 # Make a mutable copy
26-
matrix = adafruit_raspberry_pi5_piomatter.AdafruitMatrixBonnetRGB888Packed(framebuffer, geometry)
27+
matrix = piomatter.PioMatter(colorspace=piomatter.Colorspace.RGB888Packed,
28+
pinout=piomatter.Pinout.AdafruitMatrixBonnet,
29+
framebuffer=framebuffer,
30+
geometry=geometry)
2731

2832
with Image.open(gif_file) as img:
2933
print(f"frames: {img.n_frames}")

examples/playframes.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@
1313
import sys
1414
import time
1515

16-
import adafruit_raspberry_pi5_piomatter
16+
import adafruit_raspberry_pi5_piomatter as piomatter
1717
import numpy as np
1818
import PIL.Image as Image
1919

2020
images = sorted(glob.glob(sys.argv[1]))
2121

22-
geometry = adafruit_raspberry_pi5_piomatter.Geometry(width=64, height=32, n_addr_lines=4, rotation=adafruit_raspberry_pi5_piomatter.Orientation.Normal)
22+
geometry = piomatter.Geometry(width=64, height=32, n_addr_lines=4, rotation=piomatter.Orientation.Normal)
2323
framebuffer = np.asarray(Image.open(images[0])) + 0 # Make a mutable copy
2424
nimages = len(images)
25-
matrix = adafruit_raspberry_pi5_piomatter.AdafruitMatrixBonnetRGB888Packed(framebuffer, geometry)
25+
matrix = piomatter.PioMatter(colorspace=piomatter.Colorspace.RGB888Packed,
26+
pinout=piomatter.Pinout.AdafruitMatrixBonnet,
27+
framebuffer=framebuffer,
28+
geometry=geometry)
2629

2730
while True:
2831
t0 = time.monotonic()

examples/quote_scroller.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
"""
1616

17-
import adafruit_raspberry_pi5_piomatter
17+
import adafruit_raspberry_pi5_piomatter as piomatter
1818
import numpy as np
1919
import requests
2020
from PIL import Image, ImageDraw, ImageFont
@@ -44,10 +44,14 @@
4444

4545
single_frame_img = Image.new("RGB", (total_width, total_height), (0, 0, 0))
4646

47-
geometry = adafruit_raspberry_pi5_piomatter.Geometry(width=total_width, height=total_height, n_addr_lines=4, rotation=adafruit_raspberry_pi5_piomatter.Orientation.Normal)
47+
geometry = piomatter.Geometry(width=total_width, height=total_height,
48+
n_addr_lines=4, rotation=piomatter.Orientation.Normal)
4849
framebuffer = np.asarray(single_frame_img) + 0 # Make a mutable copy
4950

50-
matrix = adafruit_raspberry_pi5_piomatter.AdafruitMatrixBonnetRGB888Packed(framebuffer, geometry)
51+
matrix = piomatter.PioMatter(colorspace=piomatter.Colorspace.RGB888Packed,
52+
pinout=piomatter.Pinout.AdafruitMatrixBonnet,
53+
framebuffer=framebuffer,
54+
geometry=geometry)
5155

5256
print("Ctrl-C to exit")
5357
while True:

examples/rainbow_spiral.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
#
44
# SPDX-License-Identifier: MIT
55
"""
6-
Display a simple test pattern of 3 shapes on a single 64x32 matrix panel.
6+
Display a spiral around the display drawn with a rainbow color.
77
88
Run like this:
99
10-
$ python simpletest.py
10+
$ python rainbow_spiral.py
1111
1212
"""
13-
import adafruit_raspberry_pi5_piomatter
13+
import adafruit_raspberry_pi5_piomatter as piomatter
1414
import numpy as np
1515
import rainbowio
1616
from PIL import Image, ImageDraw
@@ -23,10 +23,13 @@
2323
canvas = Image.new('RGB', (width, height), (0, 0, 0))
2424
draw = ImageDraw.Draw(canvas)
2525

26-
geometry = adafruit_raspberry_pi5_piomatter.Geometry(width=width, height=height, n_addr_lines=4,
27-
rotation=adafruit_raspberry_pi5_piomatter.Orientation.Normal)
26+
geometry = piomatter.Geometry(width=width, height=height, n_addr_lines=4,
27+
rotation=piomatter.Orientation.Normal)
2828
framebuffer = np.asarray(canvas) + 0 # Make a mutable copy
29-
matrix = adafruit_raspberry_pi5_piomatter.AdafruitMatrixBonnetRGB888Packed(framebuffer, geometry)
29+
matrix = piomatter.PioMatter(colorspace=piomatter.Colorspace.RGB888Packed,
30+
pinout=piomatter.Pinout.AdafruitMatrixBonnet,
31+
framebuffer=framebuffer,
32+
geometry=geometry)
3033

3134
color_index = 0
3235

examples/simpletest.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313

1414
import pathlib
1515

16-
import adafruit_raspberry_pi5_piomatter
16+
import adafruit_raspberry_pi5_piomatter as piomatter
1717
import numpy as np
1818
import PIL.Image as Image
1919

20-
geometry = adafruit_raspberry_pi5_piomatter.Geometry(width=64, height=64, n_addr_lines=4, rotation=adafruit_raspberry_pi5_piomatter.Orientation.Normal)
20+
geometry = piomatter.Geometry(width=64, height=64, n_addr_lines=4, rotation=piomatter.Orientation.Normal)
2121
framebuffer = np.asarray(Image.open(pathlib.Path(__file__).parent / "blinka64x64.png"))
22-
matrix = adafruit_raspberry_pi5_piomatter.AdafruitMatrixBonnetRGB888Packed(framebuffer, geometry)
22+
matrix = piomatter.PioMatter(colorspace=piomatter.Colorspace.RGB888Packed,
23+
pinout=piomatter.Pinout.AdafruitMatrixBonnet,
24+
framebuffer=framebuffer,
25+
geometry=geometry)
2326
matrix.show()
2427

2528
input("Hit enter to exit")

examples/single_panel_simpletest.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,27 @@
1111
1212
"""
1313

14-
import adafruit_raspberry_pi5_piomatter
14+
import adafruit_raspberry_pi5_piomatter as piomatter
1515
import numpy as np
1616
from PIL import Image, ImageDraw
1717

1818
width = 64
1919
height = 32
2020

21-
geometry = adafruit_raspberry_pi5_piomatter.Geometry(width=width, height=height, n_addr_lines=4, rotation=adafruit_raspberry_pi5_piomatter.Orientation.Normal)
21+
geometry = piomatter.Geometry(width=width, height=height, n_addr_lines=4,
22+
rotation=piomatter.Orientation.Normal)
2223

2324
canvas = Image.new('RGB', (width, height), (0, 0, 0))
2425
draw = ImageDraw.Draw(canvas)
2526

2627
framebuffer = np.asarray(canvas) + 0 # Make a mutable copy
27-
matrix = adafruit_raspberry_pi5_piomatter.AdafruitMatrixBonnetRGB888Packed(framebuffer, geometry)
28+
matrix = piomatter.PioMatter(colorspace=piomatter.Colorspace.RGB888Packed,
29+
pinout=piomatter.Pinout.AdafruitMatrixBonnet,
30+
framebuffer=framebuffer,
31+
geometry=geometry)
2832

29-
draw.rectangle((2,2, 10,10), fill=0x008800)
30-
draw.circle((18,6), 4, fill=0x880000)
33+
draw.rectangle((2, 2, 10, 10), fill=0x008800)
34+
draw.circle((18, 6), 4, fill=0x880000)
3135
draw.polygon([(28, 2), (32, 10), (24, 10)], fill=0x000088)
3236

3337
framebuffer[:] = np.asarray(canvas)

src/pymain.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ PYBIND11_MODULE(adafruit_raspberry_pi5_piomatter, m) {
112112
:toctree: _generate
113113
114114
Orientation
115+
Pinout
116+
Colorspace
115117
Geometry
116118
PioMatter
117119
AdafruitMatrixBonnetRGB888

0 commit comments

Comments
 (0)