Skip to content
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

Add Missing Type Annotations #5

Merged
merged 1 commit into from
Sep 12, 2022
Merged
Changes from all commits
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
32 changes: 23 additions & 9 deletions adafruit_st7565.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
from micropython import const
from adafruit_bus_device import spi_device

try:
from typing import Optional
from digitalio import DigitalInOut
from busio import SPI
except ImportError:
pass

try:
import framebuf
except ImportError:
Expand Down Expand Up @@ -81,8 +88,15 @@ class ST7565(framebuf.FrameBuffer):
CMD_SET_STATIC_REG = const(0x00)

def __init__(
self, spi, dc_pin, cs_pin, reset_pin=None, *, contrast=0, baudrate=1000000
):
self,
spi: SPI,
dc_pin: DigitalInOut,
cs_pin: DigitalInOut,
reset_pin: Optional[DigitalInOut] = None,
*,
contrast: int = 0,
baudrate: int = 1000000
) -> None:
self._dc_pin = dc_pin
dc_pin.switch_to_output(value=False)

Expand Down Expand Up @@ -127,7 +141,7 @@ def __init__(
# Contrast
self.contrast = contrast

def reset(self):
def reset(self) -> None:
"""Reset the display"""
if self._reset_pin:
# Toggle RST low to reset.
Expand All @@ -136,13 +150,13 @@ def reset(self):
self._reset_pin.value = True
time.sleep(0.5)

def write_cmd(self, cmd):
def write_cmd(self, cmd: int) -> None:
"""Send a command to the SPI device"""
self._dc_pin.value = False
with self.spi_device as spi:
spi.write(bytearray([cmd])) # pylint: disable=no-member

def show(self):
def show(self) -> None:
"""write out the frame buffer via SPI"""
for page in self.pagemap:
# Home cursor on the page
Expand All @@ -163,12 +177,12 @@ def show(self):
spi.write(self.buffer[row_start:row_stop]) # pylint: disable=no-member

@property
def invert(self):
def invert(self) -> bool:
"""Whether the display is inverted, cached value"""
return self._invert

@invert.setter
def invert(self, val):
def invert(self, val: bool) -> None:
"""Set invert on or normal display on"""
self._invert = val
if val:
Expand All @@ -177,12 +191,12 @@ def invert(self, val):
self.write_cmd(self.CMD_SET_DISP_NORMAL)

@property
def contrast(self):
def contrast(self) -> int:
"""The cached contrast value"""
return self._contrast

@contrast.setter
def contrast(self, val):
def contrast(self, val: int) -> None:
"""Set contrast to specified value (should be 0-127)."""
self._contrast = max(0, min(val, 0x7F)) # Clamp to values 0-0x7f
self.write_cmd(self.CMD_SET_VOLUME_FIRST)
Expand Down