From 0bf4bcdcc883db564e9ecd2fa6bc658deb532822 Mon Sep 17 00:00:00 2001 From: Tyler Crumpton Date: Fri, 29 Oct 2021 14:31:36 -0500 Subject: [PATCH 1/3] Add type hints --- adafruit_ssd1305.py | 84 ++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 31 deletions(-) diff --git a/adafruit_ssd1305.py b/adafruit_ssd1305.py index 515a1c0..c25e8e2 100644 --- a/adafruit_ssd1305.py +++ b/adafruit_ssd1305.py @@ -41,6 +41,13 @@ except ImportError: import adafruit_framebuf as framebuf +try: + from digitalio import DigitalInOut + from busio import I2C, SPI + from typing import Optional +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SSD1305.git" @@ -72,7 +79,15 @@ class _SSD1305(framebuf.FrameBuffer): """Base class for SSD1305 display driver""" # pylint: disable-msg=too-many-arguments - def __init__(self, buffer, width, height, *, external_vcc, reset): + def __init__( + self, + buffer: memoryview, + width: int, + height: int, + *, + external_vcc: bool, + reset: Optional[DigitalInOut] + ): super().__init__(buffer, width, height) self.width = width self.height = height @@ -80,7 +95,7 @@ def __init__(self, buffer, width, height, *, external_vcc, reset): # reset may be None if not needed self.reset_pin = reset if self.reset_pin: - self.reset_pin.switch_to_output(value=0) + self.reset_pin.switch_to_output(value=False) self.pages = self.height // 8 self._column_offset = 0 if self.height == 32: @@ -91,7 +106,7 @@ def __init__(self, buffer, width, height, *, external_vcc, reset): self.poweron() self.init_display() - def init_display(self): + def init_display(self) -> None: """Base class to initialize display""" for cmd in ( SET_DISP | 0x00, # off @@ -135,39 +150,39 @@ def init_display(self): self.fill(0) self.show() - def poweroff(self): + def poweroff(self) -> None: """Turn off the display (nothing visible)""" self.write_cmd(SET_DISP | 0x00) - def contrast(self, contrast): + def contrast(self, contrast: int) -> None: """Adjust the contrast""" self.write_cmd(SET_CONTRAST) self.write_cmd(contrast) - def invert(self, invert): + def invert(self, invert: bool) -> None: """Invert all pixels on the display""" self.write_cmd(SET_NORM_INV | (invert & 1)) - def write_framebuf(self): + def write_framebuf(self) -> None: """Derived class must implement this""" raise NotImplementedError - def write_cmd(self, cmd): + def write_cmd(self, cmd: int) -> None: """Derived class must implement this""" raise NotImplementedError - def poweron(self): + def poweron(self) -> None: "Reset device and turn on the display." if self.reset_pin: - self.reset_pin.value = 1 + self.reset_pin.value = True time.sleep(0.001) - self.reset_pin.value = 0 + self.reset_pin.value = False time.sleep(0.010) - self.reset_pin.value = 1 + self.reset_pin.value = True time.sleep(0.010) self.write_cmd(SET_DISP | 0x01) - def show(self): + def show(self) -> None: """Update the display""" xpos0 = 0 xpos1 = self.width - 1 @@ -197,7 +212,14 @@ class SSD1305_I2C(_SSD1305): """ def __init__( - self, width, height, i2c, *, addr=0x3C, external_vcc=False, reset=None + self, + width: int, + height: int, + i2c: I2C, + *, + addr: int = 0x3C, + external_vcc: bool = False, + reset: Optional[DigitalInOut] = None ): self.i2c_device = i2c_device.I2CDevice(i2c, addr) self.addr = addr @@ -217,14 +239,14 @@ def __init__( reset=reset, ) - def write_cmd(self, cmd): + def write_cmd(self, cmd: int) -> None: """Send a command to the SPI device""" self.temp[0] = 0x80 # Co=1, D/C#=0 self.temp[1] = cmd with self.i2c_device: self.i2c_device.write(self.temp) - def write_framebuf(self): + def write_framebuf(self) -> None: """Blast out the frame buffer using a single I2C transaction to support hardware I2C interfaces.""" with self.i2c_device: @@ -248,20 +270,20 @@ class SSD1305_SPI(_SSD1305): # Disable should be reconsidered when refactor can be tested. def __init__( self, - width, - height, - spi, - dc, - reset, - cs, + width: int, + height: int, + spi: SPI, + dc: DigitalInOut, + reset: DigitalInOut, + cs: DigitalInOut, *, - external_vcc=False, - baudrate=8000000, - polarity=0, - phase=0 + external_vcc: bool = False, + baudrate: int = 8000000, + polarity: int = 0, + phase: int = 0 ): self.rate = 10 * 1024 * 1024 - dc.switch_to_output(value=0) + dc.switch_to_output(value=False) self.spi_device = spi_device.SPIDevice( spi, cs, baudrate=baudrate, polarity=polarity, phase=phase ) @@ -275,14 +297,14 @@ def __init__( reset=reset, ) - def write_cmd(self, cmd): + def write_cmd(self, cmd: int) -> None: """Send a command to the SPI device""" - self.dc_pin.value = 0 + self.dc_pin.value = False with self.spi_device as spi: spi.write(bytearray([cmd])) - def write_framebuf(self): + def write_framebuf(self) -> None: """write to the frame buffer via SPI""" - self.dc_pin.value = 1 + self.dc_pin.value = True with self.spi_device as spi: spi.write(self.buffer) From 89c292dc82bfb47f11f94c4b37ebdf2b0c7ff3c7 Mon Sep 17 00:00:00 2001 From: Tyler Crumpton Date: Fri, 29 Oct 2021 15:00:25 -0500 Subject: [PATCH 2/3] Move typing import to top of block --- adafruit_ssd1305.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_ssd1305.py b/adafruit_ssd1305.py index c25e8e2..8c4cd3a 100644 --- a/adafruit_ssd1305.py +++ b/adafruit_ssd1305.py @@ -42,9 +42,9 @@ import adafruit_framebuf as framebuf try: + from typing import Optional from digitalio import DigitalInOut from busio import I2C, SPI - from typing import Optional except ImportError: pass From 803aba0a5b2d1dd7c7eefd0442d20d335c678e2b Mon Sep 17 00:00:00 2001 From: Tyler Crumpton Date: Fri, 29 Oct 2021 15:01:42 -0500 Subject: [PATCH 3/3] Add comment about typing imports --- adafruit_ssd1305.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_ssd1305.py b/adafruit_ssd1305.py index 8c4cd3a..39d4bfb 100644 --- a/adafruit_ssd1305.py +++ b/adafruit_ssd1305.py @@ -42,6 +42,7 @@ import adafruit_framebuf as framebuf try: + # Used only for typing from typing import Optional from digitalio import DigitalInOut from busio import I2C, SPI