Skip to content

Commit e7b19f7

Browse files
authored
Merge pull request #24 from tekktrik/doc/add-typing
Add typing, correct advanced example filename
2 parents 44fcc65 + 0355cc0 commit e7b19f7

File tree

5 files changed

+58
-40
lines changed

5 files changed

+58
-40
lines changed

adafruit_cap1188/cap1188.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929

3030
from micropython import const
3131

32+
try:
33+
from typing import Tuple, Union
34+
except ImportError:
35+
pass
36+
3237
__version__ = "0.0.0-auto.0"
3338
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CAP1188.git"
3439

@@ -73,41 +78,41 @@ class CAP1188_Channel:
7378
"""Helper class to represent a touch channel on the CAP1188. Not meant to
7479
be used directly."""
7580

76-
def __init__(self, cap1188, pin):
81+
def __init__(self, cap1188: "CAP1188", pin: int) -> None:
7782
self._cap1188 = cap1188
7883
self._pin = pin
7984

8085
@property
81-
def value(self):
86+
def value(self) -> bool:
8287
"""Whether the pin is being touched or not."""
8388
return self._cap1188.touched() & (1 << self._pin - 1) != 0
8489

8590
@property
86-
def raw_value(self):
91+
def raw_value(self) -> int:
8792
"""The raw touch measurement."""
8893
return self._cap1188.delta_count(self._pin)
8994

9095
@property
91-
def threshold(self):
96+
def threshold(self) -> int:
9297
"""The touch threshold value."""
9398
return self._cap1188._read_register(_CAP1188_THESHOLD_1 + self._pin - 1)
9499

95100
@threshold.setter
96-
def threshold(self, value):
101+
def threshold(self, value: int) -> None:
97102
value = int(value)
98103
if not 0 <= value <= 127:
99104
raise ValueError("Threshold value must be in range 0 to 127.")
100105
self._cap1188._write_register(_CAP1188_THESHOLD_1 + self._pin - 1, value)
101106

102-
def recalibrate(self):
107+
def recalibrate(self) -> None:
103108
"""Perform a self recalibration."""
104109
self._cap1188.recalibrate_pins(1 << self._pin - 1)
105110

106111

107112
class CAP1188:
108113
"""CAP1188 driver base, must be extended for I2C/SPI interfacing."""
109114

110-
def __init__(self):
115+
def __init__(self) -> None:
111116
mid = self._read_register(_CAP1188_MANU_ID)
112117
if mid != _CAP1188_MID:
113118
raise RuntimeError(
@@ -124,7 +129,7 @@ def __init__(self):
124129
self._write_register(0x2F, 0x10) # turn off input-1-sets-all-inputs feature
125130
self.recalibrate()
126131

127-
def __getitem__(self, key):
132+
def __getitem__(self, key: int) -> CAP1188_Channel:
128133
pin = key
129134
index = key - 1
130135
if pin < 1 or pin > 8:
@@ -134,12 +139,12 @@ def __getitem__(self, key):
134139
return self._channels[index]
135140

136141
@property
137-
def touched_pins(self):
142+
def touched_pins(self) -> Tuple[bool, bool, bool, bool, bool, bool, bool, bool]:
138143
"""A tuple of touched state for all pins."""
139144
touched = self.touched()
140145
return tuple(bool(touched >> i & 1) for i in range(8))
141146

142-
def touched(self):
147+
def touched(self) -> int:
143148
"""Return 8 bit value representing touch state of all pins."""
144149
# clear the INT bit and any previously touched pins
145150
current = self._read_register(_CAP1188_MAIN_CONTROL)
@@ -148,20 +153,20 @@ def touched(self):
148153
return self._read_register(_CAP1188_INPUT_STATUS)
149154

150155
@property
151-
def sensitivity(self):
156+
def sensitivity(self) -> int:
152157
"""The sensitvity of touch detections. Range is 1 (least) to 128 (most)."""
153158
return _SENSITIVITY[self._read_register(_CAP1188_SENSITIVTY) >> 4 & 0x07]
154159

155160
@sensitivity.setter
156-
def sensitivity(self, value):
161+
def sensitivity(self, value: int) -> None:
157162
if value not in _SENSITIVITY:
158163
raise ValueError("Sensitivty must be one of: {}".format(_SENSITIVITY))
159164
value = _SENSITIVITY.index(value) << 4
160165
new_setting = self._read_register(_CAP1188_SENSITIVTY) & 0x8F | value
161166
self._write_register(_CAP1188_SENSITIVTY, new_setting)
162167

163168
@property
164-
def averaging(self):
169+
def averaging(self) -> int:
165170
"""Samples that are taken for all active channels during the
166171
sensor cycle. All samples are taken consecutively on
167172
the same channel before the next channel is sampled
@@ -179,7 +184,7 @@ def averaging(self):
179184
return _AVG[register >> 4 & 0x07]
180185

181186
@averaging.setter
182-
def averaging(self, value):
187+
def averaging(self, value: int) -> None:
183188
if value not in _AVG:
184189
raise ValueError("Avg must be one of: {}".format(_AVG))
185190
register = self._read_register(_CAP1188_AVERAGING)
@@ -189,7 +194,7 @@ def averaging(self, value):
189194
self._write_register(_CAP1188_AVERAGING, avg_value)
190195

191196
@property
192-
def sample(self):
197+
def sample(self) -> str:
193198
"""Determines the overall cycle time for all measured channels
194199
during normal operation. All measured channels are sampled at the
195200
beginning of the cycle time. If additional time is remaining, then
@@ -201,7 +206,7 @@ def sample(self):
201206
return _SAMP_TIME[register >> 2 & 0x03]
202207

203208
@sample.setter
204-
def sample(self, value):
209+
def sample(self, value: str) -> None:
205210
if value not in _SAMP_TIME:
206211
raise ValueError("Sample Time must be one of: {}".format(_SAMP_TIME))
207212
register = self._read_register(_CAP1188_AVERAGING)
@@ -211,7 +216,7 @@ def sample(self, value):
211216
self._write_register(_CAP1188_AVERAGING, sample_value)
212217

213218
@property
214-
def cycle(self):
219+
def cycle(self) -> str:
215220
"""The programmed cycle time is only maintained if
216221
the total averaging time for all samples is less
217222
than the programmed cycle. The AVG[2:0] bits will
@@ -226,7 +231,7 @@ def cycle(self):
226231
return _CYCLE_TIME[register & 0x03]
227232

228233
@cycle.setter
229-
def cycle(self, value):
234+
def cycle(self, value: str) -> None:
230235
if value not in _CYCLE_TIME:
231236
raise ValueError("Cycle Time must be one of: {}".format(_CYCLE_TIME))
232237
register = self._read_register(_CAP1188_AVERAGING)
@@ -236,26 +241,26 @@ def cycle(self, value):
236241
self._write_register(_CAP1188_AVERAGING, cycle_value)
237242

238243
@property
239-
def thresholds(self):
244+
def thresholds(self) -> Tuple[int, int, int, int, int, int, int, int]:
240245
"""Touch threshold value for all channels."""
241246
return self.threshold_values()
242247

243248
@thresholds.setter
244-
def thresholds(self, value):
249+
def thresholds(self, value: int) -> None:
245250
value = int(value)
246251
if not 0 <= value <= 127:
247252
raise ValueError("Threshold value must be in range 0 to 127.")
248253
self._write_block(_CAP1188_THESHOLD_1, bytearray((value,) * 8))
249254

250-
def threshold_values(self):
255+
def threshold_values(self) -> Tuple[int, int, int, int, int, int, int, int]:
251256
"""Return tuple of touch threshold values for all channels."""
252257
return tuple(self._read_block(_CAP1188_THESHOLD_1, 8))
253258

254-
def recalibrate(self):
259+
def recalibrate(self) -> None:
255260
"""Perform a self recalibration on all the pins."""
256261
self.recalibrate_pins(0xFF)
257262

258-
def delta_count(self, pin):
263+
def delta_count(self, pin: int) -> int:
259264
"""Return the 8 bit delta count value for the channel."""
260265
if pin < 1 or pin > 8:
261266
raise IndexError("Pin must be a value 1-8.")
@@ -264,22 +269,22 @@ def delta_count(self, pin):
264269
raw_value = raw_value - 256 if raw_value & 128 else raw_value
265270
return raw_value
266271

267-
def recalibrate_pins(self, mask):
272+
def recalibrate_pins(self, mask: int) -> None:
268273
"""Recalibrate pins specified by bit mask."""
269274
self._write_register(_CAP1188_CAL_ACTIVATE, mask)
270275

271-
def _read_register(self, address):
276+
def _read_register(self, address: int) -> int:
272277
"""Return 8 bit value of register at address."""
273278
raise NotImplementedError
274279

275-
def _write_register(self, address, value):
280+
def _write_register(self, address: int, value: int) -> None:
276281
"""Write 8 bit value to register at address."""
277282
raise NotImplementedError
278283

279-
def _read_block(self, start, length):
284+
def _read_block(self, start: int, length: int) -> bytearray:
280285
"""Return byte array of values from start address to length."""
281286
raise NotImplementedError
282287

283-
def _write_block(self, start, data):
288+
def _write_block(self, start: int, data: Union[bytearray, bytes]) -> None:
284289
"""Write out data beginning at start address."""
285290
raise NotImplementedError

adafruit_cap1188/i2c.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
from micropython import const
3333
from adafruit_cap1188.cap1188 import CAP1188
3434

35+
try:
36+
from typing import Union
37+
from busio import I2C
38+
except ImportError:
39+
pass
40+
3541
__version__ = "0.0.0-auto.0"
3642
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CAP1188.git"
3743

@@ -41,34 +47,34 @@
4147
class CAP1188_I2C(CAP1188):
4248
"""Driver for the CAP1188 connected over I2C."""
4349

44-
def __init__(self, i2c, address=_CAP1188_DEFAULT_ADDRESS):
50+
def __init__(self, i2c: I2C, address: int = _CAP1188_DEFAULT_ADDRESS) -> None:
4551
self._i2c = i2c_device.I2CDevice(i2c, address)
4652
self._buf = bytearray(2)
4753
super().__init__()
4854

49-
def _read_register(self, address):
55+
def _read_register(self, address: int) -> int:
5056
"""Return 8 bit value of register at address."""
5157
self._buf[0] = address
5258
with self._i2c as i2c:
5359
i2c.write_then_readinto(self._buf, self._buf, out_end=1, in_start=1)
5460
return self._buf[1]
5561

56-
def _write_register(self, address, value):
62+
def _write_register(self, address: int, value: int) -> None:
5763
"""Write 8 bit value to registter at address."""
5864
self._buf[0] = address
5965
self._buf[1] = value
6066
with self._i2c as i2c:
6167
i2c.write(self._buf)
6268

63-
def _read_block(self, start, length):
69+
def _read_block(self, start: int, length: int) -> bytearray:
6470
"""Return byte array of values from start address to length."""
6571
result = bytearray(length)
6672
with self._i2c as i2c:
6773
i2c.write(bytes((start,)))
6874
i2c.readinto(result)
6975
return result
7076

71-
def _write_block(self, start, data):
77+
def _write_block(self, start: int, data: Union[bytearray, bytes]) -> None:
7278
"""Write out data beginning at start address."""
7379
with self._i2c as i2c:
7480
i2c.write(bytes((start,)) + data)

adafruit_cap1188/spi.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,23 @@
3939
_CAP1188_SPI_WRITE_DATA = const(0x7E)
4040
_CAP1188_SPI_READ_DATA = const(0x7F)
4141

42+
try:
43+
from typing import Union
44+
from busio import SPI
45+
from digitalio import DigitalInOut
46+
except ImportError:
47+
pass
48+
4249

4350
class CAP1188_SPI(CAP1188):
4451
"""Driver for the CAP1188 connected over SPI."""
4552

46-
def __init__(self, spi, cs):
53+
def __init__(self, spi: SPI, cs: DigitalInOut) -> None:
4754
self._spi = spi_device.SPIDevice(spi, cs)
4855
self._buf = bytearray(4)
4956
super().__init__()
5057

51-
def _read_register(self, address):
58+
def _read_register(self, address: int) -> int:
5259
# pylint: disable=no-member
5360
"""Return 8 bit value of register at address."""
5461
self._buf[0] = _CAP1188_SPI_SET_ADDR
@@ -58,7 +65,7 @@ def _read_register(self, address):
5865
spi.write_readinto(self._buf, self._buf)
5966
return self._buf[3]
6067

61-
def _write_register(self, address, value):
68+
def _write_register(self, address: int, value: int) -> None:
6269
# pylint: disable=no-member
6370
"""Write 8 bit value to registter at address."""
6471
self._buf[0] = _CAP1188_SPI_SET_ADDR
@@ -68,7 +75,7 @@ def _write_register(self, address, value):
6875
with self._spi as spi:
6976
spi.write(self._buf)
7077

71-
def _read_block(self, start, length):
78+
def _read_block(self, start: int, length: int) -> bytearray:
7279
# pylint: disable=no-member
7380
"""Return byte array of values from start address to length."""
7481
self._buf[0] = _CAP1188_SPI_SET_ADDR
@@ -80,7 +87,7 @@ def _read_block(self, start, length):
8087
spi.write_readinto(result, result)
8188
return result
8289

83-
def _write_block(self, start, data):
90+
def _write_block(self, start: int, data: Union[bytearray, bytes]) -> None:
8491
# pylint: disable=no-member
8592
"""Write out data beginning at start address."""
8693
self._buf[0] = _CAP1188_SPI_SET_ADDR

docs/examples.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ Advance test
1313
This example show the new feature included in the library allowing the possibilite the
1414
configure the averaging, cycle and sample. For reference please see Sensor Datasheet.
1515

16-
.. literalinclude:: ../examples/cap118_advancedtest.py
17-
:caption: examples/cap118_advancedtest.py
16+
.. literalinclude:: ../examples/cap1188_advancedtest.py
17+
:caption: examples/cap1188_advancedtest.py
1818
:linenos:
File renamed without changes.

0 commit comments

Comments
 (0)