Skip to content

Commit cd0f5a7

Browse files
committed
doc(mypy): Add type annotations to function defintions
1 parent 6a0ed1b commit cd0f5a7

File tree

1 file changed

+49
-35
lines changed

1 file changed

+49
-35
lines changed

adafruit_ssd1306.py

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
except ImportError:
2222
import adafruit_framebuf as framebuf
2323

24+
try:
25+
from typing import Any, Optional
26+
except ImportError:
27+
pass
28+
2429
__version__ = "0.0.0-auto.0"
2530
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SSD1306.git"
2631

@@ -49,7 +54,16 @@ class _SSD1306(framebuf.FrameBuffer):
4954
"""Base class for SSD1306 display driver"""
5055

5156
# pylint: disable-msg=too-many-arguments
52-
def __init__(self, buffer, width, height, *, external_vcc, reset, page_addressing):
57+
def __init__(
58+
self,
59+
buffer: memoryview,
60+
width: int,
61+
height: int,
62+
*,
63+
external_vcc: bool,
64+
reset: Any,
65+
page_addressing: bool
66+
):
5367
super().__init__(buffer, width, height)
5468
self.width = width
5569
self.height = height
@@ -67,9 +81,9 @@ def __init__(self, buffer, width, height, *, external_vcc, reset, page_addressin
6781
# Parameters for efficient Page Addressing Mode (typical of U8Glib libraries)
6882
# Important as not all screens appear to support Horizontal Addressing Mode
6983
if self.page_addressing:
70-
self.pagebuffer = bytearray(width + 1)
84+
self.pagebuffer = bytearray(width + 1) # type: Optional[bytearray]
7185
self.pagebuffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1
72-
self.page_column_start = bytearray(2)
86+
self.page_column_start = bytearray(2) # type: Optional[bytearray]
7387
self.page_column_start[0] = self.width % 32
7488
self.page_column_start[1] = 0x10 + self.width // 32
7589
else:
@@ -80,11 +94,11 @@ def __init__(self, buffer, width, height, *, external_vcc, reset, page_addressin
8094
self.init_display()
8195

8296
@property
83-
def power(self):
97+
def power(self) -> bool:
8498
"""True if the display is currently powered on, otherwise False"""
8599
return self._power
86100

87-
def init_display(self):
101+
def init_display(self) -> None:
88102
"""Base class to initialize display"""
89103
# The various screen sizes available with the ssd1306 OLED driver
90104
# chip require differing configuration values for the display clock
@@ -136,36 +150,36 @@ def init_display(self):
136150
self.fill(0)
137151
self.show()
138152

139-
def poweroff(self):
153+
def poweroff(self) -> None:
140154
"""Turn off the display (nothing visible)"""
141155
self.write_cmd(SET_DISP)
142156
self._power = False
143157

144-
def contrast(self, contrast):
158+
def contrast(self, contrast: int) -> None:
145159
"""Adjust the contrast"""
146160
self.write_cmd(SET_CONTRAST)
147161
self.write_cmd(contrast)
148162

149-
def invert(self, invert):
163+
def invert(self, invert: bool) -> None:
150164
"""Invert all pixels on the display"""
151165
self.write_cmd(SET_NORM_INV | (invert & 1))
152166

153-
def rotate(self, rotate):
167+
def rotate(self, rotate: bool) -> None:
154168
"""Rotate the display 0 or 180 degrees"""
155169
self.write_cmd(SET_COM_OUT_DIR | ((rotate & 1) << 3))
156170
self.write_cmd(SET_SEG_REMAP | (rotate & 1))
157171
# com output (vertical mirror) is changed immediately
158172
# you need to call show() for the seg remap to be visible
159173

160-
def write_framebuf(self):
174+
def write_framebuf(self) -> None:
161175
"""Derived class must implement this"""
162176
raise NotImplementedError
163177

164-
def write_cmd(self, cmd):
178+
def write_cmd(self, cmd: Any) -> None:
165179
"""Derived class must implement this"""
166180
raise NotImplementedError
167181

168-
def poweron(self):
182+
def poweron(self) -> None:
169183
"Reset device and turn on the display."
170184
if self.reset_pin:
171185
self.reset_pin.value = 1
@@ -177,7 +191,7 @@ def poweron(self):
177191
self.write_cmd(SET_DISP | 0x01)
178192
self._power = True
179193

180-
def show(self):
194+
def show(self) -> None:
181195
"""Update the display"""
182196
if not self.page_addressing:
183197
xpos0 = 0
@@ -210,14 +224,14 @@ class SSD1306_I2C(_SSD1306):
210224

211225
def __init__(
212226
self,
213-
width,
214-
height,
215-
i2c,
227+
width: int,
228+
height: int,
229+
i2c: Any,
216230
*,
217-
addr=0x3C,
218-
external_vcc=False,
219-
reset=None,
220-
page_addressing=False
231+
addr: int = 0x3C,
232+
external_vcc: bool = False,
233+
reset: Any = None,
234+
page_addressing: bool = False
221235
):
222236
self.i2c_device = i2c_device.I2CDevice(i2c, addr)
223237
self.addr = addr
@@ -239,14 +253,14 @@ def __init__(
239253
page_addressing=self.page_addressing,
240254
)
241255

242-
def write_cmd(self, cmd):
256+
def write_cmd(self, cmd: int) -> None:
243257
"""Send a command to the I2C device"""
244258
self.temp[0] = 0x80 # Co=1, D/C#=0
245259
self.temp[1] = cmd
246260
with self.i2c_device:
247261
self.i2c_device.write(self.temp)
248262

249-
def write_framebuf(self):
263+
def write_framebuf(self) -> None:
250264
"""Blast out the frame buffer using a single I2C transaction to support
251265
hardware I2C interfaces."""
252266
if self.page_addressing:
@@ -281,18 +295,18 @@ class SSD1306_SPI(_SSD1306):
281295
# Disable should be reconsidered when refactor can be tested.
282296
def __init__(
283297
self,
284-
width,
285-
height,
286-
spi,
287-
dc,
288-
reset,
289-
cs,
298+
width: int,
299+
height: int,
300+
spi: Any,
301+
dc: Any,
302+
reset: bool,
303+
cs: int,
290304
*,
291-
external_vcc=False,
292-
baudrate=8000000,
293-
polarity=0,
294-
phase=0,
295-
page_addressing=False
305+
external_vcc: bool = False,
306+
baudrate: int = 8000000,
307+
polarity: int = 0,
308+
phase: int = 0,
309+
page_addressing: bool = False
296310
):
297311
self.page_addressing = page_addressing
298312
if self.page_addressing:
@@ -316,13 +330,13 @@ def __init__(
316330
page_addressing=self.page_addressing,
317331
)
318332

319-
def write_cmd(self, cmd):
333+
def write_cmd(self, cmd: int) -> None:
320334
"""Send a command to the SPI device"""
321335
self.dc_pin.value = 0
322336
with self.spi_device as spi:
323337
spi.write(bytearray([cmd]))
324338

325-
def write_framebuf(self):
339+
def write_framebuf(self) -> None:
326340
"""write to the frame buffer via SPI"""
327341
self.dc_pin.value = 1
328342
with self.spi_device as spi:

0 commit comments

Comments
 (0)