29
29
from adafruit_register .i2c_bits import ROBits
30
30
from adafruit_bus_device .i2c_device import I2CDevice
31
31
from micropython import const
32
+
32
33
try :
33
34
from typing import List , Tuple
34
35
except ImportError :
49
50
_TOUCH1_YH = const (0x05 )
50
51
_TOUCH1_YL = const (0x06 )
51
52
53
+
52
54
class Adafruit_FT5336 :
53
55
"""Adafruit FT5336 touch screen driver"""
56
+
54
57
# Define read-only register bits for vendor ID, chip ID, and number of touches.
55
58
_vend_id = ROBits (8 , _REG_VENDID , 0 ) # 8-bit read-only register for vendor ID
56
59
_chip_id = ROBits (8 , _REG_CHIPID , 0 ) # 8-bit read-only register for chip ID
57
- _num_touches = ROBits (8 , _REG_NUMTOUCHES , 0 ) # 8-bit read-only register for number of touches
60
+ _num_touches = ROBits (
61
+ 8 , _REG_NUMTOUCHES , 0
62
+ ) # 8-bit read-only register for number of touches
58
63
59
- def __init__ (self , i2c , i2c_addr : int = _DEFAULT_ADDR , max_touches : int = 5 ) -> None :
64
+ def __init__ (
65
+ self , i2c , i2c_addr : int = _DEFAULT_ADDR , max_touches : int = 5
66
+ ) -> None :
60
67
"""Initialization over I2C
61
68
62
69
:param int i2c_addr: I2C address (default 0x38)
@@ -68,9 +75,9 @@ def __init__(self, i2c, i2c_addr: int = _DEFAULT_ADDR, max_touches: int = 5) ->
68
75
self .max_touches = max_touches # Maximum number of touches to track
69
76
70
77
# Initialize touch point arrays
71
- self ._touchX : List [int ] = [0 ] * self .max_touches
72
- self ._touchY : List [int ] = [0 ] * self .max_touches
73
- self ._touchID : List [int ] = [0 ] * self .max_touches
78
+ self ._touch_x : List [int ] = [0 ] * self .max_touches
79
+ self ._touch_y : List [int ] = [0 ] * self .max_touches
80
+ self ._touch_id : List [int ] = [0 ] * self .max_touches
74
81
75
82
# Verify device identity by checking the vendor and chip IDs
76
83
if self ._vend_id != _VENDID :
@@ -88,44 +95,49 @@ def _read_data(self):
88
95
self ._touches = 0
89
96
90
97
for i in range (self ._touches ):
91
- self ._touchX [i ] = (buffer [_TOUCH1_XH + i * 6 ] & 0x0F ) << 8 | buffer [_TOUCH1_XL + i * 6 ]
92
- self ._touchY [i ] = (buffer [_TOUCH1_YH + i * 6 ] & 0x0F ) << 8 | buffer [_TOUCH1_YL + i * 6 ]
93
- self ._touchID [i ] = buffer [_TOUCH1_YH + i * 6 ] >> 4
98
+ self ._touch_x [i ] = (buffer [_TOUCH1_XH + i * 6 ] & 0x0F ) << 8 | buffer [
99
+ _TOUCH1_XL + i * 6
100
+ ]
101
+ self ._touch_y [i ] = (buffer [_TOUCH1_YH + i * 6 ] & 0x0F ) << 8 | buffer [
102
+ _TOUCH1_YL + i * 6
103
+ ]
104
+ self ._touch_id [i ] = buffer [_TOUCH1_YH + i * 6 ] >> 4
94
105
95
106
@property
96
107
def touched (self ) -> int :
97
108
"""Count of touch inputs detected
98
-
109
+
99
110
:return: Count of touch inputs detected (0-max_touches)
100
111
:rtype: int
101
112
"""
102
113
n = self ._num_touches
103
114
return 0 if n > self .max_touches else n
104
-
115
+
105
116
@property
106
117
def points (self ) -> List :
107
118
"""X, Y and Z values from each available touch input
108
-
119
+
109
120
:return: X, Y and Z values in a list
110
121
:rtype: List
111
122
"""
112
123
self ._read_data ()
113
124
points = []
114
125
for i in range (min (self ._touches , self .max_touches )):
115
- point = (self ._touchX [i ], self ._touchY [i ], 1 )
126
+ point = (self ._touch_x [i ], self ._touch_y [i ], 1 )
116
127
points .append (point )
117
128
118
129
return points
119
130
120
131
def point (self , point_index : int ) -> Tuple :
121
132
"""X, Y and Z value from a specified touch input
122
-
133
+
123
134
:param int point_index: Touch input to read (0 - max_touches)
124
135
:return: X, Y and Z values
125
136
:rtype: Tuple
126
137
"""
127
138
self ._read_data ()
128
139
if self ._touches == 0 or point_index >= self ._touches :
129
- return (0 , 0 , 0 )
140
+ value = (0 , 0 , 0 )
130
141
else :
131
- return (self ._touchX [point_index ], self ._touchY [point_index ], 1 )
142
+ value = (self ._touch_x [point_index ], self ._touch_y [point_index ], 1 )
143
+ return value
0 commit comments