-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from adafruit/clocking-fix
Fix clocking & add framebuffer mirroring examples
- Loading branch information
Showing
10 changed files
with
180 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/python3 | ||
""" | ||
Mirror a scaled copy of the framebuffer to a 64x32 matrix | ||
The upper left corner of the framebuffer is displayed until the user hits ctrl-c. | ||
The `/dev/fb0` special file will exist if a monitor is plugged in at boot time, | ||
or if `/boot/firmware/cmdline.txt` specifies a resolution such as | ||
`... video=HDMI-A-1:640x480M@60D`. | ||
""" | ||
|
||
|
||
import adafruit_raspberry_pi5_piomatter | ||
import numpy as np | ||
|
||
yoffset = 0 | ||
xoffset = 0 | ||
|
||
with open("/sys/class/graphics/fb0/virtual_size") as f: | ||
screenx, screeny = [int(word) for word in f.read().split(",")] | ||
|
||
with open("/sys/class/graphics/fb0/bits_per_pixel") as f: | ||
bits_per_pixel = int(f.read()) | ||
|
||
assert bits_per_pixel == 16 | ||
|
||
bytes_per_pixel = bits_per_pixel // 8 | ||
dtype = {2: np.uint16, 4: np.uint32}[bytes_per_pixel] | ||
|
||
with open("/sys/class/graphics/fb0/stride") as f: | ||
stride = int(f.read()) | ||
|
||
linux_framebuffer = np.memmap('/dev/fb0',mode='r', shape=(screeny, stride // bytes_per_pixel), dtype=dtype) | ||
|
||
width = 64 | ||
height = 32 | ||
geometry = adafruit_raspberry_pi5_piomatter.Geometry(width=width, height=height, n_addr_lines=4, rotation=adafruit_raspberry_pi5_piomatter.Orientation.Normal) | ||
matrix_framebuffer = np.zeros(shape=(geometry.height, geometry.width), dtype=dtype) | ||
matrix = adafruit_raspberry_pi5_piomatter.AdafruitMatrixBonnetRGB565(matrix_framebuffer, geometry) | ||
|
||
while True: | ||
matrix_framebuffer[:,:] = linux_framebuffer[yoffset:yoffset+height, xoffset:xoffset+width] | ||
matrix.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/python3 | ||
""" | ||
Mirror a scaled copy of the framebuffer to a 64x32 matrix | ||
The upper left corner of the framebuffer is displayed until the user hits ctrl-c. | ||
The `/dev/fb0` special file will exist if a monitor is plugged in at boot time, | ||
or if `/boot/firmware/cmdline.txt` specifies a resolution such as | ||
`... video=HDMI-A-1:640x480M@60D`. | ||
""" | ||
|
||
|
||
import adafruit_raspberry_pi5_piomatter | ||
import numpy as np | ||
import PIL.Image as Image | ||
|
||
with open("/sys/class/graphics/fb0/virtual_size") as f: | ||
screenx, screeny = [int(word) for word in f.read().split(",")] | ||
|
||
with open("/sys/class/graphics/fb0/bits_per_pixel") as f: | ||
bits_per_pixel = int(f.read()) | ||
|
||
assert bits_per_pixel == 16 | ||
|
||
bytes_per_pixel = bits_per_pixel // 8 | ||
dtype = {2: np.uint16, 4: np.uint32}[bytes_per_pixel] | ||
|
||
with open("/sys/class/graphics/fb0/stride") as f: | ||
stride = int(f.read()) | ||
|
||
linux_framebuffer = np.memmap('/dev/fb0',mode='r', shape=(screeny, stride // bytes_per_pixel), dtype=dtype) | ||
|
||
xoffset = 0 | ||
yoffset = 0 | ||
width = 64 | ||
height = 32 | ||
scale = 3 | ||
|
||
geometry = adafruit_raspberry_pi5_piomatter.Geometry(width=width, height=height, n_addr_lines=4, rotation=adafruit_raspberry_pi5_piomatter.Orientation.Normal) | ||
matrix_framebuffer = np.zeros(shape=(geometry.height, geometry.width, 3), dtype=np.uint8) | ||
matrix = adafruit_raspberry_pi5_piomatter.AdafruitMatrixBonnetRGB888Packed(matrix_framebuffer, geometry) | ||
|
||
while True: | ||
tmp = linux_framebuffer[yoffset:yoffset+height*scale, xoffset:xoffset+width*scale] | ||
# Convert the RGB565 framebuffer into RGB888Packed (so that we can use PIL image operations to rescale it) | ||
r = (tmp & 0xf800) >> 8 | ||
r = r | (r >> 5) | ||
r = r.astype(np.uint8) | ||
g = (tmp & 0x07e0) >> 3 | ||
g = g | (g >> 6) | ||
g = g.astype(np.uint8) | ||
b = (tmp & 0x001f) << 3 | ||
b = b | (b >> 5) | ||
b = b.astype(np.uint8) | ||
img = Image.fromarray(np.stack([r, g, b], -1)) | ||
img = img.resize((width, height)) | ||
matrix_framebuffer[:,:] = np.asarray(img) | ||
matrix.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,26 @@ | ||
import sys | ||
from contextlib import contextmanager | ||
import io | ||
import pathlib | ||
from contextlib import redirect_stdout | ||
|
||
import adafruit_pioasm | ||
import click | ||
|
||
|
||
@contextmanager | ||
def temporary_stdout(filename): | ||
old_stdout = sys.stdout | ||
try: | ||
with open(filename, "w", encoding="utf-8") as sys.stdout: | ||
yield sys.stdout | ||
finally: | ||
sys.stdout = old_stdout | ||
|
||
@click.command | ||
@click.argument("infile") | ||
@click.argument("outfile") | ||
def main(infile, outfile): | ||
program_name = infile.rpartition("/")[2].partition(".")[0] | ||
print(program_name) | ||
program_name = pathlib.Path(infile).stem | ||
program = adafruit_pioasm.Program.from_file(infile, build_debuginfo=True) | ||
|
||
with temporary_stdout(outfile): | ||
c_program = io.StringIO() | ||
with redirect_stdout(c_program): | ||
program.print_c_program(program_name) | ||
|
||
with open(outfile, "w", encoding="utf-8") as out: | ||
print("#pragma once", file=out) | ||
print("", file=out) | ||
print(c_program.getvalue().rstrip().replace("True", "true"), file=out) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.