Skip to content

Commit

Permalink
Add Color Type, change Image type, remove mypy pre-commit hook, remov…
Browse files Browse the repository at this point in the history
…e comment
  • Loading branch information
matt-land committed Apr 24, 2023
1 parent 79eb6fc commit 5aa898f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
14 changes: 0 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# SPDX-FileCopyrightText: 2020 Diego Elio Pettenò
# SPDX-FileCopyrightText: 2023 Matt Land
#
# SPDX-License-Identifier: Unlicense

Expand Down Expand Up @@ -41,16 +40,3 @@ repos:
files: "^tests/"
args:
- --disable=missing-docstring,consider-using-f-string,duplicate-code
- repo: local
hooks:
- id: mypy
name: mypy (library code)
entry: "mypy adafruit_rgb_display"
language: python
additional_dependencies: ["mypy==1.2.0"]
types: [python]
exclude: "^(docs/|examples/|tests/|setup.py$)"
# use require_serial so that script
# is only called once per commit
require_serial: true
pass_filenames: false
18 changes: 10 additions & 8 deletions adafruit_rgb_display/rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import digitalio
import busio

Image = Any # from PIL import Image
from circuitpython_typing.pil import Image
except ImportError:
pass

Expand Down Expand Up @@ -176,15 +176,17 @@ def _encode_pos(self, x: int, y: int) -> bytes:
"""Encode a position into bytes."""
return struct.pack(self._ENCODE_POS, x, y)

def _encode_pixel(self, color: Any) -> bytes:
def _encode_pixel(self, color: Union[int, Tuple]) -> bytes:
"""Encode a pixel color into bytes."""
return struct.pack(self._ENCODE_PIXEL, color)

def _decode_pixel(self, data: Union[bytes, Union[bytearray, memoryview]]) -> int:
"""Decode bytes into a pixel color."""
return color565(*struct.unpack(self._DECODE_PIXEL, data))

def pixel(self, x: int, y: int, color: Optional[Any] = None) -> Optional[int]:
def pixel(
self, x: int, y: int, color: Optional[Union[int, Tuple]] = None
) -> Optional[int]:
"""Read or write a pixel at a given position."""
if color is None:
return self._decode_pixel(self._block(x, y, x, y)) # type: ignore[arg-type]
Expand Down Expand Up @@ -232,7 +234,7 @@ def image(

# pylint: disable-msg=too-many-arguments
def fill_rectangle(
self, x: int, y: int, width: int, height: int, color: Any
self, x: int, y: int, width: int, height: int, color: Union[int, Tuple]
) -> None:
"""Draw a rectangle at specified position with specified width and
height, and fill it with the specified color."""
Expand All @@ -251,15 +253,15 @@ def fill_rectangle(

# pylint: enable-msg=too-many-arguments

def fill(self, color: Any = 0) -> None:
def fill(self, color: Union[int, Tuple] = 0) -> None:
"""Fill the whole display with the specified color."""
self.fill_rectangle(0, 0, self.width, self.height, color)

def hline(self, x: int, y: int, width: int, color: Any) -> None:
def hline(self, x: int, y: int, width: int, color: Union[int, Tuple]) -> None:
"""Draw a horizontal line."""
self.fill_rectangle(x, y, width, 1, color)

def vline(self, x: int, y: int, height: int, color: Any) -> None:
def vline(self, x: int, y: int, height: int, color: Union[int, Tuple]) -> None:
"""Draw a vertical line."""
self.fill_rectangle(x, y, 1, height, color)

Expand Down Expand Up @@ -339,7 +341,7 @@ def read(self, command: Optional[int] = None, count: int = 0) -> ByteString:
self.dc_pin.value = 0
with self.spi_device as spi:
if command is not None:
spi.write(bytearray([command])) # change to self.write()
spi.write(bytearray([command]))
if count:
spi.readinto(data)
return data

0 comments on commit 5aa898f

Please sign in to comment.