Skip to content

Commit b77e6cb

Browse files
authored
Merge pull request #14 from tylercrumpton/add-type-hints
Add type hints
2 parents 7704a5f + 803aba0 commit b77e6cb

File tree

1 file changed

+54
-31
lines changed

1 file changed

+54
-31
lines changed

adafruit_ssd1305.py

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

44+
try:
45+
# Used only for typing
46+
from typing import Optional
47+
from digitalio import DigitalInOut
48+
from busio import I2C, SPI
49+
except ImportError:
50+
pass
51+
4452
__version__ = "0.0.0-auto.0"
4553
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SSD1305.git"
4654

@@ -72,15 +80,23 @@ class _SSD1305(framebuf.FrameBuffer):
7280
"""Base class for SSD1305 display driver"""
7381

7482
# pylint: disable-msg=too-many-arguments
75-
def __init__(self, buffer, width, height, *, external_vcc, reset):
83+
def __init__(
84+
self,
85+
buffer: memoryview,
86+
width: int,
87+
height: int,
88+
*,
89+
external_vcc: bool,
90+
reset: Optional[DigitalInOut]
91+
):
7692
super().__init__(buffer, width, height)
7793
self.width = width
7894
self.height = height
7995
self.external_vcc = external_vcc
8096
# reset may be None if not needed
8197
self.reset_pin = reset
8298
if self.reset_pin:
83-
self.reset_pin.switch_to_output(value=0)
99+
self.reset_pin.switch_to_output(value=False)
84100
self.pages = self.height // 8
85101
self._column_offset = 0
86102
if self.height == 32:
@@ -91,7 +107,7 @@ def __init__(self, buffer, width, height, *, external_vcc, reset):
91107
self.poweron()
92108
self.init_display()
93109

94-
def init_display(self):
110+
def init_display(self) -> None:
95111
"""Base class to initialize display"""
96112
for cmd in (
97113
SET_DISP | 0x00, # off
@@ -135,39 +151,39 @@ def init_display(self):
135151
self.fill(0)
136152
self.show()
137153

138-
def poweroff(self):
154+
def poweroff(self) -> None:
139155
"""Turn off the display (nothing visible)"""
140156
self.write_cmd(SET_DISP | 0x00)
141157

142-
def contrast(self, contrast):
158+
def contrast(self, contrast: int) -> None:
143159
"""Adjust the contrast"""
144160
self.write_cmd(SET_CONTRAST)
145161
self.write_cmd(contrast)
146162

147-
def invert(self, invert):
163+
def invert(self, invert: bool) -> None:
148164
"""Invert all pixels on the display"""
149165
self.write_cmd(SET_NORM_INV | (invert & 1))
150166

151-
def write_framebuf(self):
167+
def write_framebuf(self) -> None:
152168
"""Derived class must implement this"""
153169
raise NotImplementedError
154170

155-
def write_cmd(self, cmd):
171+
def write_cmd(self, cmd: int) -> None:
156172
"""Derived class must implement this"""
157173
raise NotImplementedError
158174

159-
def poweron(self):
175+
def poweron(self) -> None:
160176
"Reset device and turn on the display."
161177
if self.reset_pin:
162-
self.reset_pin.value = 1
178+
self.reset_pin.value = True
163179
time.sleep(0.001)
164-
self.reset_pin.value = 0
180+
self.reset_pin.value = False
165181
time.sleep(0.010)
166-
self.reset_pin.value = 1
182+
self.reset_pin.value = True
167183
time.sleep(0.010)
168184
self.write_cmd(SET_DISP | 0x01)
169185

170-
def show(self):
186+
def show(self) -> None:
171187
"""Update the display"""
172188
xpos0 = 0
173189
xpos1 = self.width - 1
@@ -197,7 +213,14 @@ class SSD1305_I2C(_SSD1305):
197213
"""
198214

199215
def __init__(
200-
self, width, height, i2c, *, addr=0x3C, external_vcc=False, reset=None
216+
self,
217+
width: int,
218+
height: int,
219+
i2c: I2C,
220+
*,
221+
addr: int = 0x3C,
222+
external_vcc: bool = False,
223+
reset: Optional[DigitalInOut] = None
201224
):
202225
self.i2c_device = i2c_device.I2CDevice(i2c, addr)
203226
self.addr = addr
@@ -217,14 +240,14 @@ def __init__(
217240
reset=reset,
218241
)
219242

220-
def write_cmd(self, cmd):
243+
def write_cmd(self, cmd: int) -> None:
221244
"""Send a command to the SPI device"""
222245
self.temp[0] = 0x80 # Co=1, D/C#=0
223246
self.temp[1] = cmd
224247
with self.i2c_device:
225248
self.i2c_device.write(self.temp)
226249

227-
def write_framebuf(self):
250+
def write_framebuf(self) -> None:
228251
"""Blast out the frame buffer using a single I2C transaction to support
229252
hardware I2C interfaces."""
230253
with self.i2c_device:
@@ -248,20 +271,20 @@ class SSD1305_SPI(_SSD1305):
248271
# Disable should be reconsidered when refactor can be tested.
249272
def __init__(
250273
self,
251-
width,
252-
height,
253-
spi,
254-
dc,
255-
reset,
256-
cs,
274+
width: int,
275+
height: int,
276+
spi: SPI,
277+
dc: DigitalInOut,
278+
reset: DigitalInOut,
279+
cs: DigitalInOut,
257280
*,
258-
external_vcc=False,
259-
baudrate=8000000,
260-
polarity=0,
261-
phase=0
281+
external_vcc: bool = False,
282+
baudrate: int = 8000000,
283+
polarity: int = 0,
284+
phase: int = 0
262285
):
263286
self.rate = 10 * 1024 * 1024
264-
dc.switch_to_output(value=0)
287+
dc.switch_to_output(value=False)
265288
self.spi_device = spi_device.SPIDevice(
266289
spi, cs, baudrate=baudrate, polarity=polarity, phase=phase
267290
)
@@ -275,14 +298,14 @@ def __init__(
275298
reset=reset,
276299
)
277300

278-
def write_cmd(self, cmd):
301+
def write_cmd(self, cmd: int) -> None:
279302
"""Send a command to the SPI device"""
280-
self.dc_pin.value = 0
303+
self.dc_pin.value = False
281304
with self.spi_device as spi:
282305
spi.write(bytearray([cmd]))
283306

284-
def write_framebuf(self):
307+
def write_framebuf(self) -> None:
285308
"""write to the frame buffer via SPI"""
286-
self.dc_pin.value = 1
309+
self.dc_pin.value = True
287310
with self.spi_device as spi:
288311
spi.write(self.buffer)

0 commit comments

Comments
 (0)