29
29
30
30
from micropython import const
31
31
32
+ try :
33
+ from typing import Tuple , Union
34
+ except ImportError :
35
+ pass
36
+
32
37
__version__ = "0.0.0-auto.0"
33
38
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CAP1188.git"
34
39
@@ -73,41 +78,41 @@ class CAP1188_Channel:
73
78
"""Helper class to represent a touch channel on the CAP1188. Not meant to
74
79
be used directly."""
75
80
76
- def __init__ (self , cap1188 , pin ) :
81
+ def __init__ (self , cap1188 : "CAP1188" , pin : int ) -> None :
77
82
self ._cap1188 = cap1188
78
83
self ._pin = pin
79
84
80
85
@property
81
- def value (self ):
86
+ def value (self ) -> bool :
82
87
"""Whether the pin is being touched or not."""
83
88
return self ._cap1188 .touched () & (1 << self ._pin - 1 ) != 0
84
89
85
90
@property
86
- def raw_value (self ):
91
+ def raw_value (self ) -> int :
87
92
"""The raw touch measurement."""
88
93
return self ._cap1188 .delta_count (self ._pin )
89
94
90
95
@property
91
- def threshold (self ):
96
+ def threshold (self ) -> int :
92
97
"""The touch threshold value."""
93
98
return self ._cap1188 ._read_register (_CAP1188_THESHOLD_1 + self ._pin - 1 )
94
99
95
100
@threshold .setter
96
- def threshold (self , value ) :
101
+ def threshold (self , value : int ) -> None :
97
102
value = int (value )
98
103
if not 0 <= value <= 127 :
99
104
raise ValueError ("Threshold value must be in range 0 to 127." )
100
105
self ._cap1188 ._write_register (_CAP1188_THESHOLD_1 + self ._pin - 1 , value )
101
106
102
- def recalibrate (self ):
107
+ def recalibrate (self ) -> None :
103
108
"""Perform a self recalibration."""
104
109
self ._cap1188 .recalibrate_pins (1 << self ._pin - 1 )
105
110
106
111
107
112
class CAP1188 :
108
113
"""CAP1188 driver base, must be extended for I2C/SPI interfacing."""
109
114
110
- def __init__ (self ):
115
+ def __init__ (self ) -> None :
111
116
mid = self ._read_register (_CAP1188_MANU_ID )
112
117
if mid != _CAP1188_MID :
113
118
raise RuntimeError (
@@ -124,7 +129,7 @@ def __init__(self):
124
129
self ._write_register (0x2F , 0x10 ) # turn off input-1-sets-all-inputs feature
125
130
self .recalibrate ()
126
131
127
- def __getitem__ (self , key ) :
132
+ def __getitem__ (self , key : int ) -> CAP1188_Channel :
128
133
pin = key
129
134
index = key - 1
130
135
if pin < 1 or pin > 8 :
@@ -134,12 +139,12 @@ def __getitem__(self, key):
134
139
return self ._channels [index ]
135
140
136
141
@property
137
- def touched_pins (self ):
142
+ def touched_pins (self ) -> Tuple [ bool , bool , bool , bool , bool , bool , bool , bool ] :
138
143
"""A tuple of touched state for all pins."""
139
144
touched = self .touched ()
140
145
return tuple (bool (touched >> i & 1 ) for i in range (8 ))
141
146
142
- def touched (self ):
147
+ def touched (self ) -> int :
143
148
"""Return 8 bit value representing touch state of all pins."""
144
149
# clear the INT bit and any previously touched pins
145
150
current = self ._read_register (_CAP1188_MAIN_CONTROL )
@@ -148,20 +153,20 @@ def touched(self):
148
153
return self ._read_register (_CAP1188_INPUT_STATUS )
149
154
150
155
@property
151
- def sensitivity (self ):
156
+ def sensitivity (self ) -> int :
152
157
"""The sensitvity of touch detections. Range is 1 (least) to 128 (most)."""
153
158
return _SENSITIVITY [self ._read_register (_CAP1188_SENSITIVTY ) >> 4 & 0x07 ]
154
159
155
160
@sensitivity .setter
156
- def sensitivity (self , value ) :
161
+ def sensitivity (self , value : int ) -> None :
157
162
if value not in _SENSITIVITY :
158
163
raise ValueError ("Sensitivty must be one of: {}" .format (_SENSITIVITY ))
159
164
value = _SENSITIVITY .index (value ) << 4
160
165
new_setting = self ._read_register (_CAP1188_SENSITIVTY ) & 0x8F | value
161
166
self ._write_register (_CAP1188_SENSITIVTY , new_setting )
162
167
163
168
@property
164
- def averaging (self ):
169
+ def averaging (self ) -> int :
165
170
"""Samples that are taken for all active channels during the
166
171
sensor cycle. All samples are taken consecutively on
167
172
the same channel before the next channel is sampled
@@ -179,7 +184,7 @@ def averaging(self):
179
184
return _AVG [register >> 4 & 0x07 ]
180
185
181
186
@averaging .setter
182
- def averaging (self , value ) :
187
+ def averaging (self , value : int ) -> None :
183
188
if value not in _AVG :
184
189
raise ValueError ("Avg must be one of: {}" .format (_AVG ))
185
190
register = self ._read_register (_CAP1188_AVERAGING )
@@ -189,7 +194,7 @@ def averaging(self, value):
189
194
self ._write_register (_CAP1188_AVERAGING , avg_value )
190
195
191
196
@property
192
- def sample (self ):
197
+ def sample (self ) -> str :
193
198
"""Determines the overall cycle time for all measured channels
194
199
during normal operation. All measured channels are sampled at the
195
200
beginning of the cycle time. If additional time is remaining, then
@@ -201,7 +206,7 @@ def sample(self):
201
206
return _SAMP_TIME [register >> 2 & 0x03 ]
202
207
203
208
@sample .setter
204
- def sample (self , value ) :
209
+ def sample (self , value : str ) -> None :
205
210
if value not in _SAMP_TIME :
206
211
raise ValueError ("Sample Time must be one of: {}" .format (_SAMP_TIME ))
207
212
register = self ._read_register (_CAP1188_AVERAGING )
@@ -211,7 +216,7 @@ def sample(self, value):
211
216
self ._write_register (_CAP1188_AVERAGING , sample_value )
212
217
213
218
@property
214
- def cycle (self ):
219
+ def cycle (self ) -> str :
215
220
"""The programmed cycle time is only maintained if
216
221
the total averaging time for all samples is less
217
222
than the programmed cycle. The AVG[2:0] bits will
@@ -226,7 +231,7 @@ def cycle(self):
226
231
return _CYCLE_TIME [register & 0x03 ]
227
232
228
233
@cycle .setter
229
- def cycle (self , value ) :
234
+ def cycle (self , value : str ) -> None :
230
235
if value not in _CYCLE_TIME :
231
236
raise ValueError ("Cycle Time must be one of: {}" .format (_CYCLE_TIME ))
232
237
register = self ._read_register (_CAP1188_AVERAGING )
@@ -236,26 +241,26 @@ def cycle(self, value):
236
241
self ._write_register (_CAP1188_AVERAGING , cycle_value )
237
242
238
243
@property
239
- def thresholds (self ):
244
+ def thresholds (self ) -> Tuple [ int , int , int , int , int , int , int , int ] :
240
245
"""Touch threshold value for all channels."""
241
246
return self .threshold_values ()
242
247
243
248
@thresholds .setter
244
- def thresholds (self , value ) :
249
+ def thresholds (self , value : int ) -> None :
245
250
value = int (value )
246
251
if not 0 <= value <= 127 :
247
252
raise ValueError ("Threshold value must be in range 0 to 127." )
248
253
self ._write_block (_CAP1188_THESHOLD_1 , bytearray ((value ,) * 8 ))
249
254
250
- def threshold_values (self ):
255
+ def threshold_values (self ) -> Tuple [ int , int , int , int , int , int , int , int ] :
251
256
"""Return tuple of touch threshold values for all channels."""
252
257
return tuple (self ._read_block (_CAP1188_THESHOLD_1 , 8 ))
253
258
254
- def recalibrate (self ):
259
+ def recalibrate (self ) -> None :
255
260
"""Perform a self recalibration on all the pins."""
256
261
self .recalibrate_pins (0xFF )
257
262
258
- def delta_count (self , pin ) :
263
+ def delta_count (self , pin : int ) -> int :
259
264
"""Return the 8 bit delta count value for the channel."""
260
265
if pin < 1 or pin > 8 :
261
266
raise IndexError ("Pin must be a value 1-8." )
@@ -264,22 +269,22 @@ def delta_count(self, pin):
264
269
raw_value = raw_value - 256 if raw_value & 128 else raw_value
265
270
return raw_value
266
271
267
- def recalibrate_pins (self , mask ) :
272
+ def recalibrate_pins (self , mask : int ) -> None :
268
273
"""Recalibrate pins specified by bit mask."""
269
274
self ._write_register (_CAP1188_CAL_ACTIVATE , mask )
270
275
271
- def _read_register (self , address ) :
276
+ def _read_register (self , address : int ) -> int :
272
277
"""Return 8 bit value of register at address."""
273
278
raise NotImplementedError
274
279
275
- def _write_register (self , address , value ) :
280
+ def _write_register (self , address : int , value : int ) -> None :
276
281
"""Write 8 bit value to register at address."""
277
282
raise NotImplementedError
278
283
279
- def _read_block (self , start , length ) :
284
+ def _read_block (self , start : int , length : int ) -> bytearray :
280
285
"""Return byte array of values from start address to length."""
281
286
raise NotImplementedError
282
287
283
- def _write_block (self , start , data ) :
288
+ def _write_block (self , start : int , data : Union [ bytearray , bytes ]) -> None :
284
289
"""Write out data beginning at start address."""
285
290
raise NotImplementedError
0 commit comments