Skip to content

Adding type annotations #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions adafruit_pixel_framebuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,24 @@
"""

# imports
try:
from typing import Union

from adafruit_dotstar import DotStar
from neopixel import NeoPixel
except ImportError:
pass

from micropython import const
import adafruit_framebuf
from adafruit_led_animation.grid import PixelGrid
from micropython import const

__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Pixel_Framebuf.git"

HORIZONTAL = const(1)
VERTICAL = const(2)
HORIZONTAL: int = const(1)
VERTICAL: int = const(2)


# pylint: disable=too-many-function-args
class PixelFramebuffer(adafruit_framebuf.FrameBuffer):
Expand All @@ -59,6 +67,7 @@ class PixelFramebuffer(adafruit_framebuf.FrameBuffer):
:param width: Framebuffer width.
:param height: Framebuffer height.
:param orientation: Orientation of the strip pixels - HORIZONTAL (default) or VERTICAL.
HORIZONTAL and VERTICAL are primitive integers created by micropython.const(x).
:param alternating: Whether the strip alternates direction from row to row (default True).
:param reverse_x: Whether the strip X origin is on the right side (default False).
:param reverse_y: Whether the strip Y origin is on the bottom (default False).
Expand All @@ -70,17 +79,17 @@ class PixelFramebuffer(adafruit_framebuf.FrameBuffer):

def __init__(
self,
pixels,
width,
height,
orientation=HORIZONTAL,
alternating=True,
reverse_x=False,
reverse_y=False,
top=0,
bottom=0,
rotation=0,
): # pylint: disable=too-many-arguments
pixels: Union[NeoPixel, DotStar],
width: int,
height: int,
orientation: int = HORIZONTAL,
alternating: bool = True,
reverse_x: bool = False,
reverse_y: bool = False,
top: int = 0,
bottom: int = 0,
rotation: int = 0,
) -> None: # pylint: disable=too-many-arguments
self._width = width
self._height = height

Expand All @@ -98,26 +107,19 @@ def __init__(

self._buffer = bytearray(width * height * 3)
self._double_buffer = bytearray(width * height * 3)
super().__init__(
self._buffer, width, height, buf_format=adafruit_framebuf.RGB888
)
super().__init__(self._buffer, width, height, buf_format=adafruit_framebuf.RGB888)
self.rotation = rotation

def blit(self):
def blit(self) -> None:
"""blit is not yet implemented"""
raise NotImplementedError()

def display(self):
def display(self) -> None:
"""Copy the raw buffer changes to the grid and show"""
for _y in range(self._height):
for _x in range(self._width):
index = (_y * self.stride + _x) * 3
if (
self._buffer[index : index + 3]
!= self._double_buffer[index : index + 3]
):
if self._buffer[index : index + 3] != self._double_buffer[index : index + 3]:
self._grid[(_x, _y)] = tuple(self._buffer[index : index + 3])
self._double_buffer[index : index + 3] = self._buffer[
index : index + 3
]
self._double_buffer[index : index + 3] = self._buffer[index : index + 3]
self._grid.show()