From 76a0bf8c94174bb72626cb09193fde132576a77d Mon Sep 17 00:00:00 2001 From: Ross Kukard <75049118+RossK1@users.noreply.github.com> Date: Mon, 24 Apr 2023 09:56:57 -0700 Subject: [PATCH 1/2] Adding type annotations --- adafruit_pixel_framebuf.py | 54 ++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/adafruit_pixel_framebuf.py b/adafruit_pixel_framebuf.py index 3409e19..fb039da 100755 --- a/adafruit_pixel_framebuf.py +++ b/adafruit_pixel_framebuf.py @@ -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): @@ -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). @@ -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 @@ -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() From b432829aa1bb01c3fa0083eeb1e3326f646b0c32 Mon Sep 17 00:00:00 2001 From: Ross Kukard <75049118+RossK1@users.noreply.github.com> Date: Mon, 24 Apr 2023 11:56:23 -0700 Subject: [PATCH 2/2] Linting and adding typing package to requirements file --- adafruit_pixel_framebuf.py | 23 ++++++++++++++--------- requirements.txt | 1 + 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/adafruit_pixel_framebuf.py b/adafruit_pixel_framebuf.py index fb039da..184109d 100755 --- a/adafruit_pixel_framebuf.py +++ b/adafruit_pixel_framebuf.py @@ -39,10 +39,7 @@ # imports try: - from typing import Union - - from adafruit_dotstar import DotStar - from neopixel import NeoPixel + from circuitpython_typing.led import FillBasedColorUnion except ImportError: pass @@ -77,9 +74,10 @@ class PixelFramebuffer(adafruit_framebuf.FrameBuffer): """ + # pylint: disable=too-many-arguments def __init__( self, - pixels: Union[NeoPixel, DotStar], + pixels: FillBasedColorUnion, width: int, height: int, orientation: int = HORIZONTAL, @@ -89,7 +87,7 @@ def __init__( top: int = 0, bottom: int = 0, rotation: int = 0, - ) -> None: # pylint: disable=too-many-arguments + ) -> None: self._width = width self._height = height @@ -107,7 +105,9 @@ 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) -> None: @@ -119,7 +119,12 @@ def display(self) -> None: 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() diff --git a/requirements.txt b/requirements.txt index f40001b..d1041e9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ Adafruit-Blinka adafruit-circuitpython-framebuf>=1.4.2 adafruit-circuitpython-led-animation +adafruit-circuitpython-typing ~=1.4