Skip to content

Commit fcaf6a5

Browse files
authored
Merge pull request #69 from careyk007/add_missing_type_annotations
doc(mypy): Add type annotations to function defintions
2 parents 6a0ed1b + 9bea68c commit fcaf6a5

File tree

1 file changed

+52
-35
lines changed

1 file changed

+52
-35
lines changed

adafruit_ssd1306.py

+52-35
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
except ImportError:
2222
import adafruit_framebuf as framebuf
2323

24+
try:
25+
# Used only for typing
26+
from typing import Optional
27+
import busio
28+
import digitalio
29+
except ImportError:
30+
pass
31+
2432
__version__ = "0.0.0-auto.0"
2533
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SSD1306.git"
2634

@@ -49,7 +57,16 @@ class _SSD1306(framebuf.FrameBuffer):
4957
"""Base class for SSD1306 display driver"""
5058

5159
# pylint: disable-msg=too-many-arguments
52-
def __init__(self, buffer, width, height, *, external_vcc, reset, page_addressing):
60+
def __init__(
61+
self,
62+
buffer: memoryview,
63+
width: int,
64+
height: int,
65+
*,
66+
external_vcc: bool,
67+
reset: Optional[digitalio.DigitalInOut],
68+
page_addressing: bool
69+
):
5370
super().__init__(buffer, width, height)
5471
self.width = width
5572
self.height = height
@@ -67,9 +84,9 @@ def __init__(self, buffer, width, height, *, external_vcc, reset, page_addressin
6784
# Parameters for efficient Page Addressing Mode (typical of U8Glib libraries)
6885
# Important as not all screens appear to support Horizontal Addressing Mode
6986
if self.page_addressing:
70-
self.pagebuffer = bytearray(width + 1)
87+
self.pagebuffer = bytearray(width + 1) # type: Optional[bytearray]
7188
self.pagebuffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
72-
self.page_column_start = bytearray(2)
89+
self.page_column_start = bytearray(2) # type: Optional[bytearray]
7390
self.page_column_start[0] = self.width % 32
7491
self.page_column_start[1] = 0x10 + self.width // 32
7592
else:
@@ -80,11 +97,11 @@ def __init__(self, buffer, width, height, *, external_vcc, reset, page_addressin
8097
self.init_display()
8198

8299
@property
83-
def power(self):
100+
def power(self) -> bool:
84101
"""True if the display is currently powered on, otherwise False"""
85102
return self._power
86103

87-
def init_display(self):
104+
def init_display(self) -> None:
88105
"""Base class to initialize display"""
89106
# The various screen sizes available with the ssd1306 OLED driver
90107
# chip require differing configuration values for the display clock
@@ -136,36 +153,36 @@ def init_display(self):
136153
self.fill(0)
137154
self.show()
138155

139-
def poweroff(self):
156+
def poweroff(self) -> None:
140157
"""Turn off the display (nothing visible)"""
141158
self.write_cmd(SET_DISP)
142159
self._power = False
143160

144-
def contrast(self, contrast):
161+
def contrast(self, contrast: int) -> None:
145162
"""Adjust the contrast"""
146163
self.write_cmd(SET_CONTRAST)
147164
self.write_cmd(contrast)
148165

149-
def invert(self, invert):
166+
def invert(self, invert: bool) -> None:
150167
"""Invert all pixels on the display"""
151168
self.write_cmd(SET_NORM_INV | (invert & 1))
152169

153-
def rotate(self, rotate):
170+
def rotate(self, rotate: bool) -> None:
154171
"""Rotate the display 0 or 180 degrees"""
155172
self.write_cmd(SET_COM_OUT_DIR | ((rotate & 1) << 3))
156173
self.write_cmd(SET_SEG_REMAP | (rotate & 1))
157174
# com output (vertical mirror) is changed immediately
158175
# you need to call show() for the seg remap to be visible
159176

160-
def write_framebuf(self):
177+
def write_framebuf(self) -> None:
161178
"""Derived class must implement this"""
162179
raise NotImplementedError
163180

164-
def write_cmd(self, cmd):
181+
def write_cmd(self, cmd: int) -> None:
165182
"""Derived class must implement this"""
166183
raise NotImplementedError
167184

168-
def poweron(self):
185+
def poweron(self) -> None:
169186
"Reset device and turn on the display."
170187
if self.reset_pin:
171188
self.reset_pin.value = 1
@@ -177,7 +194,7 @@ def poweron(self):
177194
self.write_cmd(SET_DISP | 0x01)
178195
self._power = True
179196

180-
def show(self):
197+
def show(self) -> None:
181198
"""Update the display"""
182199
if not self.page_addressing:
183200
xpos0 = 0
@@ -210,14 +227,14 @@ class SSD1306_I2C(_SSD1306):
210227

211228
def __init__(
212229
self,
213-
width,
214-
height,
215-
i2c,
230+
width: int,
231+
height: int,
232+
i2c: busio.I2C,
216233
*,
217-
addr=0x3C,
218-
external_vcc=False,
219-
reset=None,
220-
page_addressing=False
234+
addr: int = 0x3C,
235+
external_vcc: bool = False,
236+
reset: Optional[digitalio.DigitalInOut] = None,
237+
page_addressing: bool = False
221238
):
222239
self.i2c_device = i2c_device.I2CDevice(i2c, addr)
223240
self.addr = addr
@@ -239,14 +256,14 @@ def __init__(
239256
page_addressing=self.page_addressing,
240257
)
241258

242-
def write_cmd(self, cmd):
259+
def write_cmd(self, cmd: int) -> None:
243260
"""Send a command to the I2C device"""
244261
self.temp[0] = 0x80 # Co=1, D/C#=0
245262
self.temp[1] = cmd
246263
with self.i2c_device:
247264
self.i2c_device.write(self.temp)
248265

249-
def write_framebuf(self):
266+
def write_framebuf(self) -> None:
250267
"""Blast out the frame buffer using a single I2C transaction to support
251268
hardware I2C interfaces."""
252269
if self.page_addressing:
@@ -281,18 +298,18 @@ class SSD1306_SPI(_SSD1306):
281298
# Disable should be reconsidered when refactor can be tested.
282299
def __init__(
283300
self,
284-
width,
285-
height,
286-
spi,
287-
dc,
288-
reset,
289-
cs,
301+
width: int,
302+
height: int,
303+
spi: busio.SPI,
304+
dc: digitalio.DigitalInOut,
305+
reset: Optional[digitalio.DigitalInOut],
306+
cs: digitalio.DigitalInOut,
290307
*,
291-
external_vcc=False,
292-
baudrate=8000000,
293-
polarity=0,
294-
phase=0,
295-
page_addressing=False
308+
external_vcc: bool = False,
309+
baudrate: int = 8000000,
310+
polarity: int = 0,
311+
phase: int = 0,
312+
page_addressing: bool = False
296313
):
297314
self.page_addressing = page_addressing
298315
if self.page_addressing:
@@ -316,13 +333,13 @@ def __init__(
316333
page_addressing=self.page_addressing,
317334
)
318335

319-
def write_cmd(self, cmd):
336+
def write_cmd(self, cmd: int) -> None:
320337
"""Send a command to the SPI device"""
321338
self.dc_pin.value = 0
322339
with self.spi_device as spi:
323340
spi.write(bytearray([cmd]))
324341

325-
def write_framebuf(self):
342+
def write_framebuf(self) -> None:
326343
"""write to the frame buffer via SPI"""
327344
self.dc_pin.value = 1
328345
with self.spi_device as spi:

0 commit comments

Comments
 (0)