Skip to content

Commit e4f301f

Browse files
committed
Added requested type annotations.
1 parent 5a89c94 commit e4f301f

File tree

1 file changed

+75
-29
lines changed

1 file changed

+75
-29
lines changed

adafruit_epd/epd.py

Lines changed: 75 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@
1111

1212
import time
1313
from micropython import const
14-
from digitalio import Direction
14+
from digitalio import Direction, DigitalInOut
1515
from adafruit_epd import mcp_sram
1616

17+
try:
18+
"""Needed for type annotations"""
19+
from typing import Any, Union, Callable
20+
from busio import SPI
21+
from PIL.Image import Image
22+
except ImportError:
23+
pass
24+
1725
__version__ = "0.0.0+auto.0"
1826
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_EPD.git"
1927

@@ -29,8 +37,16 @@ class Adafruit_EPD: # pylint: disable=too-many-instance-attributes, too-many-pu
2937
LIGHT = const(5)
3038

3139
def __init__(
32-
self, width, height, spi, cs_pin, dc_pin, sramcs_pin, rst_pin, busy_pin
33-
): # pylint: disable=too-many-arguments
40+
self,
41+
width: int,
42+
height: int,
43+
spi: SPI,
44+
cs_pin: DigitalInOut,
45+
dc_pin: DigitalInOut,
46+
sramcs_pin: DigitalInOut,
47+
rst_pin: DigitalInOut,
48+
busy_pin: DigitalInOut,
49+
) -> None: # pylint: disable=too-many-arguments
3450
self._width = width
3551
self._height = height
3652

@@ -76,7 +92,7 @@ def __init__(
7692
self._black_inverted = self._color_inverted = True
7793
self.hardware_reset()
7894

79-
def display(self): # pylint: disable=too-many-branches
95+
def display(self) -> None: # pylint: disable=too-many-branches
8096
"""show the contents of the display buffer"""
8197
self.power_up()
8298

@@ -149,15 +165,15 @@ def display(self): # pylint: disable=too-many-branches
149165

150166
self.update()
151167

152-
def hardware_reset(self):
168+
def hardware_reset(self) -> None:
153169
"""If we have a reset pin, do a hardware reset by toggling it"""
154170
if self._rst:
155171
self._rst.value = False
156172
time.sleep(0.1)
157173
self._rst.value = True
158174
time.sleep(0.1)
159175

160-
def command(self, cmd, data=None, end=True):
176+
def command(self, cmd: Any, data: Any = None, end: bool = True) -> Any:
161177
"""Send command byte to display."""
162178
self._cs.value = True
163179
self._dc.value = False
@@ -176,7 +192,7 @@ def command(self, cmd, data=None, end=True):
176192

177193
return ret
178194

179-
def _spi_transfer(self, data):
195+
def _spi_transfer(self, data: Any) -> Any:
180196
"""Transfer one byte or bytearray, toggling the cs pin if required by the EPD chipset"""
181197
if isinstance(data, int): # single byte!
182198
self._spibuf[0] = data
@@ -203,30 +219,30 @@ def _spi_transfer(self, data):
203219
self._spi_transfer(x)
204220
return None
205221

206-
def power_up(self):
222+
def power_up(self) -> None:
207223
"""Power up the display in preparation for writing RAM and updating.
208224
must be implemented in subclass"""
209225
raise NotImplementedError()
210226

211-
def power_down(self):
227+
def power_down(self) -> None:
212228
"""Power down the display, must be implemented in subclass"""
213229
raise NotImplementedError()
214230

215-
def update(self):
231+
def update(self) -> None:
216232
"""Update the display from internal memory, must be implemented in subclass"""
217233
raise NotImplementedError()
218234

219-
def write_ram(self, index):
235+
def write_ram(self, index: int) -> None:
220236
"""Send the one byte command for starting the RAM write process. Returns
221237
the byte read at the same time over SPI. index is the RAM buffer, can be
222238
0 or 1 for tri-color displays. must be implemented in subclass"""
223239
raise NotImplementedError()
224240

225-
def set_ram_address(self, x, y):
241+
def set_ram_address(self, x: int, y: int) -> None:
226242
"""Set the RAM address location, must be implemented in subclass"""
227243
raise NotImplementedError()
228244

229-
def set_black_buffer(self, index, inverted):
245+
def set_black_buffer(self, index: Union[0, 1], inverted: bool) -> None:
230246
"""Set the index for the black buffer data (0 or 1) and whether its inverted"""
231247
if index == 0:
232248
self._blackframebuf = self._framebuf1
@@ -236,7 +252,7 @@ def set_black_buffer(self, index, inverted):
236252
raise RuntimeError("Buffer index must be 0 or 1")
237253
self._black_inverted = inverted
238254

239-
def set_color_buffer(self, index, inverted):
255+
def set_color_buffer(self, index: Union[0, 1], inverted: bool) -> None:
240256
"""Set the index for the color buffer data (0 or 1) and whether its inverted"""
241257
if index == 0:
242258
self._colorframebuf = self._framebuf1
@@ -246,7 +262,12 @@ def set_color_buffer(self, index, inverted):
246262
raise RuntimeError("Buffer index must be 0 or 1")
247263
self._color_inverted = inverted
248264

249-
def _color_dup(self, func, args, color):
265+
def _color_dup(
266+
self,
267+
func: Callable,
268+
args: Any,
269+
color: Union[0, 1, 2, 3, 4, 5],
270+
) -> None:
250271
black = getattr(self._blackframebuf, func)
251272
red = getattr(self._colorframebuf, func)
252273
if self._blackframebuf is self._colorframebuf: # monochrome
@@ -255,11 +276,11 @@ def _color_dup(self, func, args, color):
255276
black(*args, color=(color == Adafruit_EPD.BLACK) != self._black_inverted)
256277
red(*args, color=(color == Adafruit_EPD.RED) != self._color_inverted)
257278

258-
def pixel(self, x, y, color):
279+
def pixel(self, x: int, y: int, color: int) -> None:
259280
"""draw a single pixel in the display buffer"""
260281
self._color_dup("pixel", (x, y), color)
261282

262-
def fill(self, color):
283+
def fill(self, color: int) -> None:
263284
"""fill the screen with the passed color"""
264285
red_fill = ((color == Adafruit_EPD.RED) != self._color_inverted) * 0xFF
265286
black_fill = ((color == Adafruit_EPD.BLACK) != self._black_inverted) * 0xFF
@@ -271,21 +292,34 @@ def fill(self, color):
271292
self._blackframebuf.fill(black_fill)
272293
self._colorframebuf.fill(red_fill)
273294

274-
def rect(self, x, y, width, height, color): # pylint: disable=too-many-arguments
295+
def rect(
296+
self, x: int, y: int, width: int, height: int, color: int
297+
) -> None: # pylint: disable=too-many-arguments
275298
"""draw a rectangle"""
276299
self._color_dup("rect", (x, y, width, height), color)
277300

278301
def fill_rect(
279-
self, x, y, width, height, color
280-
): # pylint: disable=too-many-arguments
302+
self, x: int, y: int, width: int, height: int, color: int
303+
) -> None: # pylint: disable=too-many-arguments
281304
"""fill a rectangle with the passed color"""
282305
self._color_dup("fill_rect", (x, y, width, height), color)
283306

284-
def line(self, x_0, y_0, x_1, y_1, color): # pylint: disable=too-many-arguments
307+
def line(
308+
self, x_0: int, y_0: int, x_1: int, y_1: int, color: int
309+
) -> None: # pylint: disable=too-many-arguments
285310
"""Draw a line from (x_0, y_0) to (x_1, y_1) in passed color"""
286311
self._color_dup("line", (x_0, y_0, x_1, y_1), color)
287312

288-
def text(self, string, x, y, color, *, font_name="font5x8.bin", size=1):
313+
def text(
314+
self,
315+
string: str,
316+
x: int,
317+
y: int,
318+
color: int,
319+
*,
320+
font_name: str = "font5x8.bin",
321+
size: int = 1
322+
) -> None:
289323
"""Write text string at location (x, y) in given color, using font file"""
290324
if self._blackframebuf is self._colorframebuf: # monochrome
291325
self._blackframebuf.text(
@@ -315,39 +349,51 @@ def text(self, string, x, y, color, *, font_name="font5x8.bin", size=1):
315349
)
316350

317351
@property
318-
def width(self):
352+
def width(self) -> int:
319353
"""The width of the display, accounting for rotation"""
320354
if self.rotation in (0, 2):
321355
return self._width
322356
return self._height
323357

324358
@property
325-
def height(self):
359+
def height(self) -> int:
326360
"""The height of the display, accounting for rotation"""
327361
if self.rotation in (0, 2):
328362
return self._height
329363
return self._width
330364

331365
@property
332-
def rotation(self):
366+
def rotation(self) -> Union[0, 1, 2, 3]:
333367
"""The rotation of the display, can be one of (0, 1, 2, 3)"""
334368
return self._framebuf1.rotation
335369

336370
@rotation.setter
337-
def rotation(self, val):
371+
def rotation(self, val: int) -> None:
338372
self._framebuf1.rotation = val
339373
if self._framebuf2:
340374
self._framebuf2.rotation = val
341375

342-
def hline(self, x, y, width, color):
376+
def hline(
377+
self,
378+
x: int,
379+
y: int,
380+
width: int,
381+
color: int,
382+
) -> None:
343383
"""draw a horizontal line"""
344384
self.fill_rect(x, y, width, 1, color)
345385

346-
def vline(self, x, y, height, color):
386+
def vline(
387+
self,
388+
x: int,
389+
y: int,
390+
height: int,
391+
color: int,
392+
) -> None:
347393
"""draw a vertical line"""
348394
self.fill_rect(x, y, 1, height, color)
349395

350-
def image(self, image):
396+
def image(self, image: Image) -> None:
351397
"""Set buffer to value of Python Imaging Library image. The image should
352398
be in RGB mode and a size equal to the display size.
353399
"""

0 commit comments

Comments
 (0)