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 type hints #14

Merged
merged 3 commits into from
Oct 31, 2021
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
84 changes: 53 additions & 31 deletions adafruit_ssd1305.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
except ImportError:
import adafruit_framebuf as framebuf

try:
from digitalio import DigitalInOut
tylercrumpton marked this conversation as resolved.
Show resolved Hide resolved
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"

Expand Down Expand Up @@ -72,15 +79,23 @@ 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
self.external_vcc = external_vcc
# 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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
)
Expand All @@ -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)