Skip to content

Commit 29ce41e

Browse files
authored
Merge pull request #38 from Neradoc/displayio-to-busdisplay
Update to CP 10 with busdisplay
2 parents f9b95d2 + 8967ac3 commit 29ce41e

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

adafruit_bitmapsaver.py

+18-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
================================================================================
99
1010
Save a displayio.Bitmap (and associated displayio.Palette) in a BMP file.
11-
Make a screenshot (the contents of a displayio.Display) and save in a BMP file.
11+
Make a screenshot (the contents of a busdisplay.BusDisplay) and save in a BMP file.
1212
1313
1414
* Author(s): Dave Astels, Matt Land
@@ -31,11 +31,14 @@
3131
import gc
3232
import struct
3333
import board
34-
from displayio import Bitmap, Palette, Display, ColorConverter
34+
from displayio import Bitmap, Palette, ColorConverter
35+
3536

3637
try:
3738
from typing import Tuple, Optional, Union
3839
from io import BufferedWriter
40+
from busdisplay import BusDisplay
41+
from framebufferio import FramebufferDisplay
3942
except ImportError:
4043
pass
4144

@@ -67,9 +70,11 @@ def _bytes_per_row(source_width: int) -> int:
6770
return pixel_bytes + padding_bytes
6871

6972

70-
def _rotated_height_and_width(pixel_source: Union[Bitmap, Display]) -> Tuple[int, int]:
73+
def _rotated_height_and_width(
74+
pixel_source: Union[Bitmap, BusDisplay, FramebufferDisplay]
75+
) -> Tuple[int, int]:
7176
# flip axis if the display is rotated
72-
if isinstance(pixel_source, Display) and (pixel_source.rotation % 180 != 0):
77+
if hasattr(pixel_source, "rotation") and (pixel_source.rotation % 180 != 0):
7378
return pixel_source.height, pixel_source.width
7479
return pixel_source.width, pixel_source.height
7580

@@ -111,7 +116,7 @@ def rgb565_to_rgb888(rgb565):
111116
# pylint:disable=too-many-locals
112117
def _write_pixels(
113118
output_file: BufferedWriter,
114-
pixel_source: Union[Bitmap, Display],
119+
pixel_source: Union[Bitmap, BusDisplay, FramebufferDisplay],
115120
palette: Optional[Union[Palette, ColorConverter]],
116121
) -> None:
117122
saving_bitmap = isinstance(pixel_source, Bitmap)
@@ -136,7 +141,7 @@ def _write_pixels(
136141
color >>= 8
137142
buffer_index += 1
138143
else:
139-
# pixel_source: Display
144+
# pixel_source: display
140145
result_buffer = bytearray(2048)
141146
data = pixel_source.fill_row(y - 1, result_buffer)
142147
for i in range(width):
@@ -156,15 +161,17 @@ def _write_pixels(
156161

157162
def save_pixels(
158163
file_or_filename: Union[str, BufferedWriter],
159-
pixel_source: Union[Display, Bitmap] = None,
164+
pixel_source: Union[BusDisplay, FramebufferDisplay, Bitmap] = None,
160165
palette: Optional[Union[Palette, ColorConverter]] = None,
161166
) -> None:
162167
"""Save pixels to a 24 bit per pixel BMP file.
163168
If pixel_source if a displayio.Bitmap, save it's pixels through palette.
164-
If it's a displayio.Display, a palette isn't required.
169+
If it's a displayio display, a palette isn't required. To be supported,
170+
a display must implement `busdisplay.BusDisplay.fill_row`. Known supported
171+
display types are `busdisplay.BusDisplay` and `framebufferio.FramebufferDisplay`.
165172
166173
:param file_or_filename: either the file to save to, or it's absolute name
167-
:param pixel_source: the Bitmap or Display to save
174+
:param pixel_source: the Bitmap or display to save
168175
:param palette: the Palette to use for looking up colors in the bitmap
169176
"""
170177
if not pixel_source:
@@ -177,8 +184,8 @@ def save_pixels(
177184
raise ValueError(
178185
"Third argument must be a Palette or ColorConverter for a Bitmap save"
179186
)
180-
elif not isinstance(pixel_source, Display):
181-
raise ValueError("Second argument must be a Bitmap or Display")
187+
elif not hasattr(pixel_source, "fill_row"):
188+
raise ValueError("Second argument must be a Bitmap or supported display type")
182189
try:
183190
if isinstance(file_or_filename, str):
184191
output_file = open( # pylint: disable=consider-using-with

docs/api.rst

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11

2-
.. If you created a package, create one automodule per module in the package.
3-
4-
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
5-
.. use this format as the module name: "adafruit_foo.foo"
2+
API Reference
3+
#############
64

75
.. automodule:: adafruit_bitmapsaver
86
:members:

docs/conf.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@
2727
# Uncomment the below if you use native CircuitPython modules such as
2828
# digitalio, micropython and busio. List the modules you use. Without it, the
2929
# autodoc module docs will fail to generate with a warning.
30-
autodoc_mock_imports = ["displayio", "digitalio", "busio", "board"]
30+
autodoc_mock_imports = [
31+
"busdisplay",
32+
"displayio",
33+
"digitalio",
34+
"busio",
35+
"board",
36+
"framebufferio",
37+
]
3138

3239

3340
intersphinx_mapping = {

0 commit comments

Comments
 (0)