26
26
import time
27
27
from micropython import const
28
28
29
+ try :
30
+ from typing import Dict , List , Optional , Tuple
31
+ from typing_extensions import Literal
32
+ from microcontroller import Pin
33
+ from busio import I2C , SPI
34
+ except ImportError :
35
+ pass
29
36
30
37
__version__ = "0.0.0+auto.0"
31
38
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_STMPE610.git"
32
39
33
40
34
- def map_range (x , in_min , in_max , out_min , out_max ):
41
+ def map_range (
42
+ x : float , in_min : float , in_max : float , out_min : float , out_max : float
43
+ ) -> float :
35
44
"""
36
45
Maps a value from one range to another. Values beyond the input minimum or
37
46
maximum will be limited to the minimum or maximum of the output range.
@@ -155,7 +164,7 @@ class Adafruit_STMPE610:
155
164
156
165
See the examples folder for instantiation kwargs and properties."""
157
166
158
- def __init__ (self ):
167
+ def __init__ (self ) -> None :
159
168
"""Reset the controller."""
160
169
self ._write_register_byte (_STMPE_SYS_CTRL1 , _STMPE_SYS_CTRL1_RESET )
161
170
time .sleep (0.001 )
@@ -185,7 +194,7 @@ def __init__(self):
185
194
_STMPE_INT_CTRL , _STMPE_INT_CTRL_POL_HIGH | _STMPE_INT_CTRL_ENABLE
186
195
)
187
196
188
- def read_data (self ):
197
+ def read_data (self ) -> Tuple [ int , int , int ] :
189
198
"""Request next stored reading - return tuple containing (x,y,pressure)."""
190
199
d_1 = self ._read_byte (0xD7 )
191
200
d_2 = self ._read_byte (0xD7 )
@@ -199,23 +208,23 @@ def read_data(self):
199
208
self ._write_register_byte (_STMPE_INT_STA , 0xFF )
200
209
return (x_loc , y_loc , pressure )
201
210
202
- def _read_byte (self , register ) :
211
+ def _read_byte (self , register : int ) -> int :
203
212
"""Read a byte register value and return it."""
204
213
return self ._read_register (register , 1 )[0 ]
205
214
206
- def _read_register (self , register , length ) :
215
+ def _read_register (self , register : int , length : int ) -> bytearray :
207
216
"""Read an arbitrarily long register (specified by length number of
208
217
bytes) and return a bytearray of the retrieved data.
209
218
Subclasses MUST implement this!"""
210
219
raise NotImplementedError
211
220
212
- def _write_register_byte (self , register , value ) :
221
+ def _write_register_byte (self , register : int , value : int ) -> None :
213
222
"""Write a single byte register at the specified register address.
214
223
Subclasses MUST implement this!"""
215
224
raise NotImplementedError
216
225
217
226
@property
218
- def touches (self ):
227
+ def touches (self ) -> List [ Dict [ str , int ]] :
219
228
"""Returns a list of touchpoint dicts, with 'x' and 'y' containing the
220
229
touch coordinates, and 'pressure'."""
221
230
touchpoints = []
@@ -226,7 +235,7 @@ def touches(self):
226
235
return touchpoints
227
236
228
237
@property
229
- def get_version (self ):
238
+ def get_version (self ) -> int :
230
239
"""Read the version number from the sensor."""
231
240
v_1 = self ._read_byte (0 )
232
241
v_2 = self ._read_byte (1 )
@@ -235,24 +244,24 @@ def get_version(self):
235
244
return version
236
245
237
246
@property
238
- def touched (self ):
247
+ def touched (self ) -> bool :
239
248
"""Report if any touches were detected."""
240
249
touch = self ._read_byte (_STMPE_TSC_CTRL ) & 0x80
241
250
return touch == 0x80
242
251
243
252
@property
244
- def buffer_size (self ):
253
+ def buffer_size (self ) -> int :
245
254
"""The amount of touch data in the buffer."""
246
255
return self ._read_byte (_STMPE_FIFO_SIZE )
247
256
248
257
@property
249
- def buffer_empty (self ):
258
+ def buffer_empty (self ) -> bool :
250
259
"""Buffer empty status."""
251
260
empty = self ._read_byte (_STMPE_FIFO_STA ) & _STMPE_FIFO_STA_EMPTY
252
261
return empty != 0
253
262
254
263
@property
255
- def get_point (self ):
264
+ def get_point (self ) -> Dict [ str , int ] :
256
265
"""Read one touch from the buffer."""
257
266
(x_loc , y_loc , pressure ) = self .read_data ()
258
267
point = {"x" : x_loc , "y" : y_loc , "pressure" : pressure }
@@ -291,13 +300,13 @@ class Adafruit_STMPE610_I2C(Adafruit_STMPE610):
291
300
292
301
def __init__ ( # pylint: disable=too-many-arguments
293
302
self ,
294
- i2c ,
295
- address = _STMPE_ADDR ,
296
- calibration = None ,
297
- size = None ,
298
- disp_rotation = 0 ,
299
- touch_flip = (False , False ),
300
- ):
303
+ i2c : I2C ,
304
+ address : int = _STMPE_ADDR ,
305
+ calibration : Optional [ Tuple [ Tuple [ int , int ], Tuple [ int , int ]]] = None ,
306
+ size : Optional [ Tuple [ Tuple [ int , int ], Tuple [ int , int ]]] = None ,
307
+ disp_rotation : Literal [ 0 , 90 , 180 , 270 ] = 0 ,
308
+ touch_flip : Tuple [ bool , bool ] = (False , False ),
309
+ ) -> None :
301
310
302
311
self ._calib = calibration
303
312
self ._disp_size = size
@@ -320,11 +329,15 @@ def __init__( # pylint: disable=too-many-arguments
320
329
# Check device version.
321
330
version = self .get_version
322
331
if _STMPE_VERSION != version :
323
- raise RuntimeError ("Failed to find STMPE610! Chip Version 0x%x" % version )
332
+ raise RuntimeError (
333
+ f"Failed to find STMPE610! Chip Version { hex (version ).upper ()} ."
334
+ )
324
335
super ().__init__ ()
325
336
326
337
@property
327
- def touch_point (self ): # pylint: disable=too-many-branches
338
+ def touch_point ( # pylint: disable=too-many-branches
339
+ self ,
340
+ ) -> Optional [Tuple [int , int , int ]]:
328
341
"""Read latest touched point value and convert to calibration-adjusted
329
342
and rotated display coordinates. Commpatible with Displayio Button.
330
343
:return: x, y, pressure
@@ -368,7 +381,7 @@ def touch_point(self): # pylint: disable=too-many-branches
368
381
return (x , y , pressure )
369
382
return None
370
383
371
- def _read_register (self , register , length ) :
384
+ def _read_register (self , register : int , length : int ) -> bytearray :
372
385
"""Low level register reading over I2C, returns a list of values."""
373
386
with self ._i2c as i2c :
374
387
i2c .write (bytearray ([register & 0xFF ]))
@@ -377,7 +390,7 @@ def _read_register(self, register, length):
377
390
# print("$%02X => %s" % (register, [hex(i) for i in result]))
378
391
return result
379
392
380
- def _write_register_byte (self , register , value ) :
393
+ def _write_register_byte (self , register : int , value : int ) -> None :
381
394
"""Low level register writing over I2C, writes one 8-bit value."""
382
395
with self ._i2c as i2c :
383
396
i2c .write (bytes ([register & 0xFF , value & 0xFF ]))
@@ -419,14 +432,14 @@ class Adafruit_STMPE610_SPI(Adafruit_STMPE610):
419
432
420
433
def __init__ ( # pylint: disable=too-many-arguments
421
434
self ,
422
- spi ,
423
- cs ,
424
- baudrate = 1000000 ,
425
- calibration = None ,
426
- size = None ,
427
- disp_rotation = 0 ,
428
- touch_flip = (False , False ),
429
- ):
435
+ spi : SPI ,
436
+ cs : Pin ,
437
+ baudrate : int = 1000000 ,
438
+ calibration : Optional [ Tuple [ Tuple [ int , int ], Tuple [ int , int ]]] = None ,
439
+ size : Optional [ Tuple [ int , int ]] = None ,
440
+ disp_rotation : Literal [ 0 , 90 , 180 , 270 ] = 0 ,
441
+ touch_flip : Tuple [ bool , bool ] = (False , False ),
442
+ ) -> None :
430
443
431
444
self ._calib = calibration
432
445
self ._disp_size = size
@@ -456,14 +469,16 @@ def __init__( # pylint: disable=too-many-arguments
456
469
version = self .get_version
457
470
if _STMPE_VERSION != version :
458
471
raise RuntimeError (
459
- "Failed to find STMPE610 controller! Chip Version 0x%x . "
472
+ f "Failed to find STMPE610 controller! Chip Version { hex ( version ). upper () } . "
460
473
"If you are using the breakout, verify you are in SPI mode."
461
474
% version
462
475
)
463
476
super ().__init__ ()
464
477
465
478
@property
466
- def touch_point (self ): # pylint: disable=too-many-branches
479
+ def touch_point ( # pylint: disable=too-many-branches
480
+ self ,
481
+ ) -> Optional [Tuple [int , int , int ]]:
467
482
"""Read latest touched point value and convert to calibration-adjusted
468
483
and rotated display coordinates. Commpatible with Displayio Button.
469
484
:return: x, y, pressure
@@ -509,7 +524,7 @@ def touch_point(self): # pylint: disable=too-many-branches
509
524
510
525
# pylint: disable=no-member
511
526
# Disable should be reconsidered when refactor can be tested.
512
- def _read_register (self , register , length ) :
527
+ def _read_register (self , register : int , length : int ) -> bytearray :
513
528
"""Low level register reading over SPI, returns a list of values."""
514
529
register = (register | 0x80 ) & 0xFF # Read single byte, bit 7 high.
515
530
with self ._spi as spi :
@@ -519,7 +534,7 @@ def _read_register(self, register, length):
519
534
# print("$%02X => %s" % (register, [hex(i) for i in result]))
520
535
return result
521
536
522
- def _write_register_byte (self , register , value ) :
537
+ def _write_register_byte (self , register : int , value : int ) -> None :
523
538
"""Low level register writing over SPI, writes one 8-bit value."""
524
539
register &= 0x7F # Write, bit 7 low.
525
540
with self ._spi as spi :
0 commit comments