Skip to content

Commit 3cabc01

Browse files
authored
Merge pull request #8 from jsymons/typing
Add type hints
2 parents 8b0a931 + 5d3949e commit 3cabc01

File tree

5 files changed

+85
-37
lines changed

5 files changed

+85
-37
lines changed

adafruit_is31fl3741/__init__.py

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929
from adafruit_register.i2c_struct import ROUnaryStruct, UnaryStruct
3030
from adafruit_register.i2c_bit import RWBit
3131

32+
try:
33+
# Used only for typing
34+
from typing import Optional, Tuple, Union # pylint: disable=unused-import
35+
from PIL.ImageFile import ImageFile
36+
from adafruit_framebuf import FrameBuffer
37+
import busio
38+
except ImportError:
39+
pass
40+
3241
__version__ = "0.0.0-auto.0"
3342
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_IS31FL3741.git"
3443

@@ -77,7 +86,12 @@ class IS31FL3741:
7786
_shutdown_bit = RWBit(_IS3741_FUNCREG_CONFIG, 0)
7887
_pixel_buffer = None
7988

80-
def __init__(self, i2c, address=_IS3741_ADDR_DEFAULT, allocate=NO_BUFFER):
89+
def __init__(
90+
self,
91+
i2c: busio.I2C,
92+
address: int = _IS3741_ADDR_DEFAULT,
93+
allocate: int = NO_BUFFER,
94+
):
8195
if allocate >= PREFER_BUFFER:
8296
try:
8397
# Pixel buffer intentionally has an extra item at the start
@@ -95,16 +109,16 @@ def __init__(self, i2c, address=_IS3741_ADDR_DEFAULT, allocate=NO_BUFFER):
95109
self._page = None
96110
self.reset()
97111

98-
def reset(self):
112+
def reset(self) -> None:
99113
"""Reset"""
100114
self.page = 4
101115
self._reset_reg = 0xAE
102116

103-
def unlock(self):
117+
def unlock(self) -> None:
104118
"""Unlock"""
105119
self._lock_reg = 0xC5
106120

107-
def set_led_scaling(self, scale):
121+
def set_led_scaling(self, scale: int) -> None:
108122
"""Set scaling level for all LEDs.
109123
110124
:param scale: Scaling level from 0 (off) to 255 (brightest).
@@ -119,34 +133,34 @@ def set_led_scaling(self, scale):
119133
i2c.write(scalebuf, end=172) # 2nd page is smaller
120134

121135
@property
122-
def global_current(self):
136+
def global_current(self) -> int:
123137
"""Global current"""
124138
self.page = 4
125139
return self._gcurrent_reg
126140

127141
@global_current.setter
128-
def global_current(self, current):
142+
def global_current(self, current: int) -> None:
129143
self.page = 4
130144
self._gcurrent_reg = current
131145

132146
@property
133-
def enable(self):
147+
def enable(self) -> bool:
134148
"""Enable"""
135149
self.page = 4
136150
return self._shutdown_bit
137151

138152
@enable.setter
139-
def enable(self, enable):
153+
def enable(self, enable: bool) -> None:
140154
self.page = 4
141155
self._shutdown_bit = enable
142156

143157
@property
144-
def page(self):
158+
def page(self) -> Union[int, None]:
145159
"""Page"""
146160
return self._page
147161

148162
@page.setter
149-
def page(self, page_value):
163+
def page(self, page_value: int) -> None:
150164
if page_value == self._page:
151165
return # already set
152166
if page_value > 4:
@@ -155,7 +169,7 @@ def page(self, page_value):
155169
self.unlock()
156170
self._page_reg = page_value
157171

158-
def __getitem__(self, led):
172+
def __getitem__(self, led: int) -> int:
159173
if not 0 <= led <= 350:
160174
raise ValueError("LED must be 0 ~ 350")
161175
if self._pixel_buffer:
@@ -173,7 +187,7 @@ def __getitem__(self, led):
173187
)
174188
return self._buf[1]
175189

176-
def __setitem__(self, led, pwm):
190+
def __setitem__(self, led: int, pwm: int) -> None:
177191
if self._pixel_buffer:
178192
# Buffered version doesn't require range checks --
179193
# Python will throw its own IndexError/ValueError as needed.
@@ -195,7 +209,7 @@ def __setitem__(self, led, pwm):
195209
else:
196210
raise ValueError("LED must be 0 ~ 350")
197211

198-
def show(self):
212+
def show(self) -> None:
199213
"""Issue in-RAM pixel data to device. No effect if pixels are
200214
unbuffered.
201215
"""
@@ -256,12 +270,12 @@ class IS31FL3741_colorXY(IS31FL3741):
256270
# pylint: disable-msg=too-many-arguments
257271
def __init__(
258272
self,
259-
i2c,
260-
width,
261-
height,
262-
address=_IS3741_ADDR_DEFAULT,
263-
allocate=NO_BUFFER,
264-
order=IS3741_BGR,
273+
i2c: busio.I2C,
274+
width: int,
275+
height: int,
276+
address: int = _IS3741_ADDR_DEFAULT,
277+
allocate: int = NO_BUFFER,
278+
order: int = IS3741_BGR,
265279
):
266280
super().__init__(i2c, address=address, allocate=allocate)
267281
self.order = order
@@ -275,11 +289,11 @@ def __init__(
275289

276290
# This function must be replaced for each board
277291
@staticmethod
278-
def pixel_addrs(x, y):
292+
def pixel_addrs(x: int, y: int) -> Tuple[int, ...]:
279293
"""Calculate a device-specific LED offset for an X,Y 2D pixel."""
280294
raise NotImplementedError("Supported in subclasses only")
281295

282-
def fill(self, color=None):
296+
def fill(self, color: Optional[int] = None) -> None:
283297
"""Set all pixels to a given RGB color.
284298
285299
:param color: Packed 24-bit color value (0xRRGGBB).
@@ -294,7 +308,7 @@ def fill(self, color=None):
294308
self[addrs[self.g_offset]] = green
295309
self[addrs[self.b_offset]] = blue
296310

297-
def pixel(self, x, y, color=None):
311+
def pixel(self, x: int, y: int, color: Optional[int] = None) -> Union[int, None]:
298312
"""
299313
Set or retrieve RGB color of pixel at position (X,Y).
300314
@@ -321,7 +335,7 @@ def pixel(self, x, y, color=None):
321335
)
322336
return None
323337

324-
def image(self, img):
338+
def image(self, img: Union[FrameBuffer, ImageFile]) -> None:
325339
"""Copy an in-memory image to the LED matrix. Image should be in
326340
24-bit format (e.g. "RGB888") and dimensions should match matrix,
327341
this isn't super robust yet or anything.

adafruit_is31fl3741/adafruit_ledglasses.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
from adafruit_is31fl3741 import _IS3741_ADDR_DEFAULT, NO_BUFFER, IS3741_BGR
3030
from . import IS31FL3741_colorXY
3131

32+
try:
33+
# Used only for typing
34+
from typing import Tuple, Any # pylint: disable=unused-import
35+
import busio
36+
except ImportError:
37+
pass
38+
3239

3340
class Right_Ring:
3441
"""The right eye ring of the LED glasses"""
@@ -60,21 +67,21 @@ class Right_Ring:
6067
b"\x01\x28\x00\x3C\x00\x3D"
6168
)
6269

63-
def __init__(self, is31_controller, order):
70+
def __init__(self, is31_controller: IS31FL3741_colorXY, order: int):
6471
self._is31 = is31_controller
6572
self.r_offset = (order >> 4) & 3
6673
self.g_offset = (order >> 2) & 3
6774
self.b_offset = order & 3
6875

69-
def __setitem__(self, led, color):
76+
def __setitem__(self, led: int, color: int) -> None:
7077
if not 0 <= led <= 23:
7178
raise ValueError("led must be 0~23")
7279
offset = unpack_from(">HHH", self.ledmap_bytes, led * 6)
7380
self._is31[offset[self.r_offset]] = (color >> 16) & 0xFF
7481
self._is31[offset[self.g_offset]] = (color >> 8) & 0xFF
7582
self._is31[offset[self.b_offset]] = color & 0xFF
7683

77-
def __getitem__(self, led):
84+
def __getitem__(self, led: int) -> int:
7885
if not 0 <= led <= 23:
7986
raise ValueError("led must be 0~23")
8087
offset = unpack_from(">HHH", self.ledmap_bytes, led * 6)
@@ -84,7 +91,7 @@ def __getitem__(self, led):
8491
| self._is31[offset[self.b_offset]]
8592
)
8693

87-
def fill(self, color):
94+
def fill(self, color: int) -> None:
8895
"""Sets all LEDs in a ring to the same color.
8996
9097
:param color: Packed RGB color (0xRRGGBB).
@@ -129,21 +136,21 @@ class Left_Ring:
129136
b"\x01\x5E\x00\xF0\x00\xF1"
130137
)
131138

132-
def __init__(self, is31_controller, order):
139+
def __init__(self, is31_controller: IS31FL3741_colorXY, order: int):
133140
self._is31 = is31_controller
134141
self.r_offset = (order >> 4) & 3
135142
self.g_offset = (order >> 2) & 3
136143
self.b_offset = order & 3
137144

138-
def __setitem__(self, led, color):
145+
def __setitem__(self, led: int, color: int) -> None:
139146
if not 0 <= led <= 23:
140147
raise ValueError("led must be 0~23")
141148
offset = unpack_from(">HHH", self.ledmap_bytes, led * 6)
142149
self._is31[offset[self.r_offset]] = (color >> 16) & 0xFF
143150
self._is31[offset[self.g_offset]] = (color >> 8) & 0xFF
144151
self._is31[offset[self.b_offset]] = color & 0xFF
145152

146-
def __getitem__(self, led):
153+
def __getitem__(self, led: int) -> int:
147154
if not 0 <= led <= 23:
148155
raise ValueError("led must be 0~23")
149156
offset = unpack_from(">HHH", self.ledmap_bytes, led * 6)
@@ -153,7 +160,7 @@ def __getitem__(self, led):
153160
| self._is31[offset[self.b_offset]]
154161
)
155162

156-
def fill(self, color):
163+
def fill(self, color: int) -> None:
157164
"""Sets all LEDs in a ring to the same color.
158165
159166
:param color: Packed RGB color (0xRRGGBB).
@@ -268,7 +275,11 @@ class LED_Glasses(IS31FL3741_colorXY):
268275
)
269276

270277
def __init__(
271-
self, i2c, address=_IS3741_ADDR_DEFAULT, allocate=NO_BUFFER, order=IS3741_BGR
278+
self,
279+
i2c: busio.I2C,
280+
address: int = _IS3741_ADDR_DEFAULT,
281+
allocate: int = NO_BUFFER,
282+
order: int = IS3741_BGR,
272283
):
273284
super().__init__(i2c, 18, 5, address=address, allocate=allocate, order=order)
274285

@@ -281,5 +292,5 @@ def __init__(
281292
self.grid = self
282293

283294
@staticmethod
284-
def pixel_addrs(x, y):
295+
def pixel_addrs(x: int, y: int) -> Tuple[Any, ...]:
285296
return unpack_from(">HHH", LED_Glasses.ledmap_bytes, ((x * 5) + y) * 6)

adafruit_is31fl3741/adafruit_rgbmatrixqt.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,30 @@
2727
from adafruit_is31fl3741 import _IS3741_ADDR_DEFAULT, NO_BUFFER, IS3741_BGR
2828
from . import IS31FL3741_colorXY
2929

30+
try:
31+
# Used only for typing
32+
from typing import Tuple
33+
import busio
34+
except ImportError:
35+
pass
36+
3037

3138
class Adafruit_RGBMatrixQT(IS31FL3741_colorXY):
3239
"""Supports the Adafruit STEMMA QT IS31FL3741 RGB LED matrix."""
3340

3441
rowmap = [8, 5, 4, 3, 2, 1, 0, 7, 6]
3542

3643
def __init__(
37-
self, i2c, address=_IS3741_ADDR_DEFAULT, allocate=NO_BUFFER, order=IS3741_BGR
44+
self,
45+
i2c: busio.I2C,
46+
address: int = _IS3741_ADDR_DEFAULT,
47+
allocate: int = NO_BUFFER,
48+
order: int = IS3741_BGR,
3849
):
3950
super().__init__(i2c, 13, 9, address=address, allocate=allocate, order=order)
4051

4152
@staticmethod
42-
def pixel_addrs(x, y):
53+
def pixel_addrs(x: int, y: int) -> Tuple[int, int, int]:
4354
"""Calulate the RGB offsets into the device array for x,y pixel"""
4455
y = Adafruit_RGBMatrixQT.rowmap[y] # Reorder rows
4556

adafruit_is31fl3741/issi_evb.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,28 @@
3030
from adafruit_is31fl3741 import _IS3741_ADDR_DEFAULT, NO_BUFFER, IS3741_BGR
3131
from adafruit_is31fl3741 import IS31FL3741_colorXY
3232

33+
try:
34+
# Used only for typing
35+
from typing import Tuple # pylint: disable=unused-import
36+
import busio
37+
except ImportError:
38+
pass
39+
3340

3441
class ISSI_EVB(IS31FL3741_colorXY):
3542
"""Supports the ISSI IS31FL3741 eval board"""
3643

3744
def __init__(
38-
self, i2c, address=_IS3741_ADDR_DEFAULT, allocate=NO_BUFFER, order=IS3741_BGR
45+
self,
46+
i2c: busio.I2C,
47+
address: int = _IS3741_ADDR_DEFAULT,
48+
allocate: int = NO_BUFFER,
49+
order: int = IS3741_BGR,
3950
):
4051
super().__init__(i2c, 9, 13, address=address, allocate=allocate, order=order)
4152

4253
@staticmethod
43-
def pixel_addrs(x, y):
54+
def pixel_addrs(x: int, y: int) -> Tuple[int, int, int]:
4455
"""Calulate the RGB offsets into the device array for x,y pixel"""
4556
if y > 2:
4657
offset = (x * 10 + 12 - y) * 3

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
Adafruit-Blinka
66
adafruit-circuitpython-framebuf
77
adafruit-circuitpython-register
8+
Pillow

0 commit comments

Comments
 (0)