Skip to content

Commit 0bf4bcd

Browse files
committed
Add type hints
1 parent 7704a5f commit 0bf4bcd

File tree

1 file changed

+53
-31
lines changed

1 file changed

+53
-31
lines changed

adafruit_ssd1305.py

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@
4141
except ImportError:
4242
import adafruit_framebuf as framebuf
4343

44+
try:
45+
from digitalio import DigitalInOut
46+
from busio import I2C, SPI
47+
from typing import Optional
48+
except ImportError:
49+
pass
50+
4451
__version__ = "0.0.0-auto.0"
4552
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SSD1305.git"
4653

@@ -72,15 +79,23 @@ class _SSD1305(framebuf.FrameBuffer):
7279
"""Base class for SSD1305 display driver"""
7380

7481
# pylint: disable-msg=too-many-arguments
75-
def __init__(self, buffer, width, height, *, external_vcc, reset):
82+
def __init__(
83+
self,
84+
buffer: memoryview,
85+
width: int,
86+
height: int,
87+
*,
88+
external_vcc: bool,
89+
reset: Optional[DigitalInOut]
90+
):
7691
super().__init__(buffer, width, height)
7792
self.width = width
7893
self.height = height
7994
self.external_vcc = external_vcc
8095
# reset may be None if not needed
8196
self.reset_pin = reset
8297
if self.reset_pin:
83-
self.reset_pin.switch_to_output(value=0)
98+
self.reset_pin.switch_to_output(value=False)
8499
self.pages = self.height // 8
85100
self._column_offset = 0
86101
if self.height == 32:
@@ -91,7 +106,7 @@ def __init__(self, buffer, width, height, *, external_vcc, reset):
91106
self.poweron()
92107
self.init_display()
93108

94-
def init_display(self):
109+
def init_display(self) -> None:
95110
"""Base class to initialize display"""
96111
for cmd in (
97112
SET_DISP | 0x00, # off
@@ -135,39 +150,39 @@ def init_display(self):
135150
self.fill(0)
136151
self.show()
137152

138-
def poweroff(self):
153+
def poweroff(self) -> None:
139154
"""Turn off the display (nothing visible)"""
140155
self.write_cmd(SET_DISP | 0x00)
141156

142-
def contrast(self, contrast):
157+
def contrast(self, contrast: int) -> None:
143158
"""Adjust the contrast"""
144159
self.write_cmd(SET_CONTRAST)
145160
self.write_cmd(contrast)
146161

147-
def invert(self, invert):
162+
def invert(self, invert: bool) -> None:
148163
"""Invert all pixels on the display"""
149164
self.write_cmd(SET_NORM_INV | (invert & 1))
150165

151-
def write_framebuf(self):
166+
def write_framebuf(self) -> None:
152167
"""Derived class must implement this"""
153168
raise NotImplementedError
154169

155-
def write_cmd(self, cmd):
170+
def write_cmd(self, cmd: int) -> None:
156171
"""Derived class must implement this"""
157172
raise NotImplementedError
158173

159-
def poweron(self):
174+
def poweron(self) -> None:
160175
"Reset device and turn on the display."
161176
if self.reset_pin:
162-
self.reset_pin.value = 1
177+
self.reset_pin.value = True
163178
time.sleep(0.001)
164-
self.reset_pin.value = 0
179+
self.reset_pin.value = False
165180
time.sleep(0.010)
166-
self.reset_pin.value = 1
181+
self.reset_pin.value = True
167182
time.sleep(0.010)
168183
self.write_cmd(SET_DISP | 0x01)
169184

170-
def show(self):
185+
def show(self) -> None:
171186
"""Update the display"""
172187
xpos0 = 0
173188
xpos1 = self.width - 1
@@ -197,7 +212,14 @@ class SSD1305_I2C(_SSD1305):
197212
"""
198213

199214
def __init__(
200-
self, width, height, i2c, *, addr=0x3C, external_vcc=False, reset=None
215+
self,
216+
width: int,
217+
height: int,
218+
i2c: I2C,
219+
*,
220+
addr: int = 0x3C,
221+
external_vcc: bool = False,
222+
reset: Optional[DigitalInOut] = None
201223
):
202224
self.i2c_device = i2c_device.I2CDevice(i2c, addr)
203225
self.addr = addr
@@ -217,14 +239,14 @@ def __init__(
217239
reset=reset,
218240
)
219241

220-
def write_cmd(self, cmd):
242+
def write_cmd(self, cmd: int) -> None:
221243
"""Send a command to the SPI device"""
222244
self.temp[0] = 0x80 # Co=1, D/C#=0
223245
self.temp[1] = cmd
224246
with self.i2c_device:
225247
self.i2c_device.write(self.temp)
226248

227-
def write_framebuf(self):
249+
def write_framebuf(self) -> None:
228250
"""Blast out the frame buffer using a single I2C transaction to support
229251
hardware I2C interfaces."""
230252
with self.i2c_device:
@@ -248,20 +270,20 @@ class SSD1305_SPI(_SSD1305):
248270
# Disable should be reconsidered when refactor can be tested.
249271
def __init__(
250272
self,
251-
width,
252-
height,
253-
spi,
254-
dc,
255-
reset,
256-
cs,
273+
width: int,
274+
height: int,
275+
spi: SPI,
276+
dc: DigitalInOut,
277+
reset: DigitalInOut,
278+
cs: DigitalInOut,
257279
*,
258-
external_vcc=False,
259-
baudrate=8000000,
260-
polarity=0,
261-
phase=0
280+
external_vcc: bool = False,
281+
baudrate: int = 8000000,
282+
polarity: int = 0,
283+
phase: int = 0
262284
):
263285
self.rate = 10 * 1024 * 1024
264-
dc.switch_to_output(value=0)
286+
dc.switch_to_output(value=False)
265287
self.spi_device = spi_device.SPIDevice(
266288
spi, cs, baudrate=baudrate, polarity=polarity, phase=phase
267289
)
@@ -275,14 +297,14 @@ def __init__(
275297
reset=reset,
276298
)
277299

278-
def write_cmd(self, cmd):
300+
def write_cmd(self, cmd: int) -> None:
279301
"""Send a command to the SPI device"""
280-
self.dc_pin.value = 0
302+
self.dc_pin.value = False
281303
with self.spi_device as spi:
282304
spi.write(bytearray([cmd]))
283305

284-
def write_framebuf(self):
306+
def write_framebuf(self) -> None:
285307
"""write to the frame buffer via SPI"""
286-
self.dc_pin.value = 1
308+
self.dc_pin.value = True
287309
with self.spi_device as spi:
288310
spi.write(self.buffer)

0 commit comments

Comments
 (0)