41
41
42
42
from micropython import const
43
43
44
+ try :
45
+ from typing import List
46
+ except ImportError :
47
+ pass
44
48
45
- _FT6206_DEFAULT_I2C_ADDR = 0x38
46
49
47
- _FT6XXX_REG_DATA = const (0x00 )
48
- _FT6XXX_REG_NUMTOUCHES = const (0x02 )
49
- _FT6XXX_REG_THRESHHOLD = const (0x80 )
50
- _FT6XXX_REG_POINTRATE = const (0x88 )
51
- _FT6XXX_REG_LIBH = const (0xA1 )
52
- _FT6XXX_REG_LIBL = const (0xA2 )
53
- _FT6XXX_REG_CHIPID = const (0xA3 )
54
- _FT6XXX_REG_FIRMVERS = const (0xA6 )
55
- _FT6XXX_REG_VENDID = const (0xA8 )
56
- _FT6XXX_REG_RELEASE = const (0xAF )
50
+ _FT_DEFAULT_I2C_ADDR = 0x38
51
+
52
+ _FT_REG_DATA = const (0x00 )
53
+ _FT_REG_NUMTOUCHES = const (0x02 )
54
+ _FT_REG_THRESHHOLD = const (0x80 )
55
+ _FT_REG_POINTRATE = const (0x88 )
56
+ _FT_REG_LIBH = const (0xA1 )
57
+ _FT_REG_LIBL = const (0xA2 )
58
+ _FT_REG_CHIPID = const (0xA3 )
59
+ _FT_REG_FIRMVERS = const (0xA6 )
60
+ _FT_REG_VENDID = const (0xA8 )
61
+ _FT_REG_RELEASE = const (0xAF )
62
+
63
+ _FT6XXX_TOUCH_BUFFER_SIZE = 32
64
+ _FT6XXX_SCALE_FACTOR = (1.0 , 1.0 )
65
+
66
+ _FT5X06_TOUCH_BUFFER_SIZE = 63
67
+ _FT5X06_SCALE_FACTOR = (2.24 , 2.14 ) # (x,y) scaling factors
57
68
58
69
59
70
class Adafruit_FocalTouch :
60
71
"""
61
72
A driver for the FocalTech capacitive touch sensor.
62
73
"""
63
74
64
- _debug = False
65
- chip = None
66
-
67
- def __init__ (
68
- self , i2c , address = _FT6206_DEFAULT_I2C_ADDR , debug = False , irq_pin = None
69
- ):
75
+ def __init__ (self , i2c , address = _FT_DEFAULT_I2C_ADDR , debug = False , irq_pin = None ):
70
76
self ._i2c = I2CDevice (i2c , address )
71
77
self ._debug = debug
72
78
self ._irq_pin = irq_pin
73
79
74
- chip_data = self ._read (_FT6XXX_REG_LIBH , 8 ) # don't wait for IRQ
80
+ chip_data = self ._read (_FT_REG_LIBH , 8 ) # don't wait for IRQ
81
+ # print("chip_data: {%x}".format(chip_data))
75
82
lib_ver , chip_id , _ , _ , firm_id , _ , vend_id = struct .unpack (
76
83
">HBBBBBB" , chip_data
77
84
)
85
+ print (
86
+ "lib_ver: {:02X}, chip_id: {:02X}, firm_id: {:02X}, vend_id: {:02X}" .format (
87
+ lib_ver , chip_id , firm_id , vend_id
88
+ )
89
+ )
78
90
79
- if vend_id != 0x11 :
91
+ if vend_id not in ( 0x11 , 0x42 ) :
80
92
raise RuntimeError ("Did not find FT chip" )
81
93
82
94
if chip_id == 0x06 :
83
95
self .chip = "FT6206"
96
+ self ._touch_buffer_size = _FT6XXX_TOUCH_BUFFER_SIZE
97
+ self ._scale_factor = _FT6XXX_SCALE_FACTOR
84
98
elif chip_id == 0x64 :
85
99
self .chip = "FT6236"
100
+ self ._touch_buffer_size = _FT6XXX_TOUCH_BUFFER_SIZE
101
+ self ._scale_factor = _FT6XXX_SCALE_FACTOR
102
+ elif chip_id == 0x55 :
103
+ self .chip = "FT5x06"
104
+ self ._touch_buffer_size = _FT5X06_TOUCH_BUFFER_SIZE
105
+ self ._scale_factor = _FT5X06_SCALE_FACTOR
86
106
87
107
if debug :
88
108
print ("Library vers %04X" % lib_ver )
89
109
print ("Firmware ID %02X" % firm_id )
90
- print ("Point rate %d Hz" % self ._read (_FT6XXX_REG_POINTRATE , 1 )[0 ])
91
- print ("Thresh %d" % self ._read (_FT6XXX_REG_THRESHHOLD , 1 )[0 ])
110
+ print ("Point rate %d Hz" % self ._read (_FT_REG_POINTRATE , 1 )[0 ])
111
+ print ("Thresh %d" % self ._read (_FT_REG_THRESHHOLD , 1 )[0 ])
92
112
93
113
@property
94
- def touched (self ):
114
+ def touched (self ) -> int :
95
115
""" Returns the number of touches currently detected """
96
- return self ._read (_FT6XXX_REG_NUMTOUCHES , 1 , irq_pin = self ._irq_pin )[0 ]
116
+ return self ._read (_FT_REG_NUMTOUCHES , 1 , irq_pin = self ._irq_pin )[0 ]
97
117
98
118
# pylint: disable=unused-variable
99
119
@property
100
- def touches (self ):
120
+ def touches (self ) -> List [ dict ] :
101
121
"""
102
122
Returns a list of touchpoint dicts, with 'x' and 'y' containing the
103
123
touch coordinates, and 'id' as the touch # for multitouch tracking
104
124
"""
105
125
touchpoints = []
106
- data = self ._read (_FT6XXX_REG_DATA , 32 , irq_pin = self ._irq_pin )
126
+ data = self ._read (_FT_REG_DATA , self . _touch_buffer_size , irq_pin = self ._irq_pin )
107
127
108
- for i in range (2 ):
128
+ touchcount = data [_FT_REG_NUMTOUCHES - _FT_REG_DATA ]
129
+ if self ._debug :
130
+ print ("touchcount: {}" .format (touchcount ))
131
+
132
+ for i in range (touchcount ):
109
133
point_data = data [i * 6 + 3 : i * 6 + 9 ]
110
134
if all (i == 255 for i in point_data ):
111
135
continue
112
136
# print([hex(i) for i in point_data])
113
137
x , y , weight , misc = struct .unpack (">HHBB" , point_data )
114
138
# print(x, y, weight, misc)
115
139
touch_id = y >> 12
116
- x &= 0xFFF
117
- y &= 0xFFF
140
+ x = round (( x & 0xFFF ) / self . _scale_factor [ 0 ])
141
+ y = round (( y & 0xFFF ) / self . _scale_factor [ 1 ])
118
142
point = {"x" : x , "y" : y , "id" : touch_id }
143
+ if self ._debug :
144
+ print ("id: {}, x: {}, y: {}" .format (touch_id , x , y ))
119
145
touchpoints .append (point )
120
146
return touchpoints
121
147
122
- # pylint: enable=unused-variable
123
-
124
- def _read (self , register , length , irq_pin = None ):
148
+ def _read (self , register , length , irq_pin = None ) -> bytearray :
125
149
"""Returns an array of 'length' bytes from the 'register'"""
126
150
with self ._i2c as i2c :
127
151
@@ -137,10 +161,12 @@ def _read(self, register, length, irq_pin=None):
137
161
print ("\t $%02X => %s" % (register , [hex (i ) for i in result ]))
138
162
return result
139
163
140
- def _write (self , register , values ):
164
+ def _write (self , register , values ) -> None :
141
165
"""Writes an array of 'length' bytes to the 'register'"""
142
166
with self ._i2c as i2c :
143
167
values = [(v & 0xFF ) for v in [register ] + values ]
168
+ print ("register: %02X, value: %02X" % (values [0 ], values [1 ]))
144
169
i2c .write (bytes (values ))
170
+
145
171
if self ._debug :
146
172
print ("\t $%02X <= %s" % (values [0 ], [hex (i ) for i in values [1 :]]))
0 commit comments