5
5
`adafruit_ft5336`
6
6
================================================================================
7
7
8
- Touchscreen driver for the FT5336 touch controller
8
+ CircuitPython driver for the FT5336 touch screen controller
9
9
10
10
11
11
* Author(s): Liz Clark
15
15
16
16
**Hardware:**
17
17
18
- .. todo:: Add links to any specific hardware product page(s), or category page(s).
19
- Use unordered list & hyperlink rST inline format: "* `Link Text <url>`_"
18
+ * `Adafruit 3.5" TFT 320x480 with Capacitive Touch Breakout: <https://adafruit.com/product/5846>`_
20
19
21
20
**Software and Dependencies:**
22
21
23
22
* Adafruit CircuitPython firmware for the supported boards:
24
23
https://circuitpython.org/downloads
25
24
26
- .. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies
27
- based on the library's use of either.
28
-
29
25
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
30
26
* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
31
27
"""
34
30
from adafruit_bus_device .i2c_device import I2CDevice
35
31
from micropython import const
36
32
try :
37
- from typing import List
33
+ from typing import List , Tuple
38
34
except ImportError :
39
35
pass
40
36
54
50
_TOUCH1_YL = const (0x06 )
55
51
56
52
class Adafruit_FT5336 :
53
+ """Adafruit FT5336 touch screen driver"""
57
54
# Define read-only register bits for vendor ID, chip ID, and number of touches.
58
55
_vend_id = ROBits (8 , _REG_VENDID , 0 ) # 8-bit read-only register for vendor ID
59
56
_chip_id = ROBits (8 , _REG_CHIPID , 0 ) # 8-bit read-only register for chip ID
60
57
_num_touches = ROBits (8 , _REG_NUMTOUCHES , 0 ) # 8-bit read-only register for number of touches
61
58
62
- def __init__ (self , i2c , i2c_addr : int = _DEFAULT_ADDR , max_touches : int = 5 ):
63
- """
64
- Initializes the FT5336 touchscreen driver.
59
+ def __init__ (self , i2c , i2c_addr : int = _DEFAULT_ADDR , max_touches : int = 5 ) -> None :
60
+ """Initialization over I2C
65
61
66
- Args:
67
- i2c: The I2C bus object.
68
- i2c_addr (int): The I2C address of the device. Defaults to _DEFAULT_ADDR.
69
- max_touches (int): Maximum number of touch points to track. Defaults to 5.
70
-
71
- Raises:
72
- ValueError: If the detected vendor ID or chip ID does not match the expected values.
62
+ :param int i2c_addr: I2C address (default 0x38)
63
+ :param int max_touches: Maximum number of touch points to track. Defaults to 5.
73
64
"""
74
65
self .i2c_device = I2CDevice (i2c , i2c_addr ) # I2C device instance
75
66
self .i2c_addr = i2c_addr # Store the I2C address
@@ -87,37 +78,52 @@ def __init__(self, i2c, i2c_addr: int = _DEFAULT_ADDR, max_touches: int = 5):
87
78
if self ._chip_id != _CHIPID :
88
79
raise ValueError ("Incorrect chip ID" )
89
80
90
- @property
91
- def touched (self ):
92
- n = self ._num_touches
93
- return 0 if n > 5 else n
94
-
95
81
def _read_data (self ):
96
82
buffer = bytearray (32 )
97
83
with self .i2c_device as i2c :
98
84
i2c .write_then_readinto (bytearray ([0 ]), buffer , in_end = 32 )
99
85
100
86
self ._touches = buffer [_TD_STATUS ]
101
- if self ._touches > 5 or self ._touches == 0 :
87
+ if self ._touches > self . max_touches or self ._touches == 0 :
102
88
self ._touches = 0
103
89
104
90
for i in range (self ._touches ):
105
91
self ._touchX [i ] = (buffer [_TOUCH1_XH + i * 6 ] & 0x0F ) << 8 | buffer [_TOUCH1_XL + i * 6 ]
106
92
self ._touchY [i ] = (buffer [_TOUCH1_YH + i * 6 ] & 0x0F ) << 8 | buffer [_TOUCH1_YL + i * 6 ]
107
93
self ._touchID [i ] = buffer [_TOUCH1_YH + i * 6 ] >> 4
94
+
95
+ @property
96
+ def touched (self ) -> int :
97
+ """Count of touch inputs detected
98
+
99
+ :return: Count of touch inputs detected (0-max_touches)
100
+ :rtype: int
101
+ """
102
+ n = self ._num_touches
103
+ return 0 if n > self .max_touches else n
108
104
109
105
@property
110
- def points (self ):
106
+ def points (self ) -> List :
107
+ """X, Y and Z values from each available touch input
108
+
109
+ :return: X, Y and Z values in a list
110
+ :rtype: List
111
+ """
111
112
self ._read_data ()
112
-
113
113
points = []
114
114
for i in range (min (self ._touches , self .max_touches )):
115
- point = (self ._touchX [i ], self ._touchY [i ], 1 ) # 1 indicates touch is active
115
+ point = (self ._touchX [i ], self ._touchY [i ], 1 )
116
116
points .append (point )
117
117
118
118
return points
119
119
120
- def point (self , point_index : int ):
120
+ def point (self , point_index : int ) -> Tuple :
121
+ """X, Y and Z value from a specified touch input
122
+
123
+ :param int point_index: Touch input to read (0 - max_touches)
124
+ :return: X, Y and Z values
125
+ :rtype: Tuple
126
+ """
121
127
self ._read_data ()
122
128
if self ._touches == 0 or point_index >= self ._touches :
123
129
return (0 , 0 , 0 )
0 commit comments