38
38
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_STMPE610.git"
39
39
40
40
41
-
42
41
_STMPE_ADDR = const (0x41 )
43
42
_STMPE_VERSION = const (0x0811 )
44
43
60
59
_STMPE_INT_CTRL_DISABLE = const (0x00 )
61
60
62
61
63
-
64
62
_STMPE_INT_EN = const (0x0A )
65
63
_STMPE_INT_EN_TOUCHDET = const (0x01 )
66
64
_STMPE_INT_EN_FIFOTH = const (0x02 )
129
127
_STMPE_GPIO_ALT_FUNCT = const (0x17 )
130
128
131
129
132
-
133
130
class Adafruit_STMPE610 :
134
131
"""
135
132
A driver for the STMPE610 Resistive Touch sensor.
136
133
"""
134
+
137
135
def __init__ (self ):
138
136
"""Reset the controller"""
139
137
self ._write_register_byte (_STMPE_SYS_CTRL1 , _STMPE_SYS_CTRL1_RESET )
140
- time .sleep (.001 )
138
+ time .sleep (0 .001 )
141
139
142
-
143
- self ._write_register_byte (_STMPE_SYS_CTRL2 , 0x0 ) # turn on clocks!
140
+ self ._write_register_byte (_STMPE_SYS_CTRL2 , 0x0 ) # turn on clocks!
144
141
self ._write_register_byte (
145
- _STMPE_TSC_CTRL , _STMPE_TSC_CTRL_XYZ | _STMPE_TSC_CTRL_EN ) # XYZ and enable!
142
+ _STMPE_TSC_CTRL , _STMPE_TSC_CTRL_XYZ | _STMPE_TSC_CTRL_EN
143
+ ) # XYZ and enable!
146
144
self ._write_register_byte (_STMPE_INT_EN , _STMPE_INT_EN_TOUCHDET )
147
145
self ._write_register_byte (
148
- _STMPE_ADC_CTRL1 , _STMPE_ADC_CTRL1_10BIT | (0x6 << 4 )) # 96 clocks per conversion
146
+ _STMPE_ADC_CTRL1 , _STMPE_ADC_CTRL1_10BIT | (0x6 << 4 )
147
+ ) # 96 clocks per conversion
149
148
self ._write_register_byte (_STMPE_ADC_CTRL2 , _STMPE_ADC_CTRL2_6_5MHZ )
150
149
self ._write_register_byte (
151
- _STMPE_TSC_CFG , _STMPE_TSC_CFG_4SAMPLE | _STMPE_TSC_CFG_DELAY_1MS
152
- | _STMPE_TSC_CFG_SETTLE_5MS )
150
+ _STMPE_TSC_CFG ,
151
+ _STMPE_TSC_CFG_4SAMPLE
152
+ | _STMPE_TSC_CFG_DELAY_1MS
153
+ | _STMPE_TSC_CFG_SETTLE_5MS ,
154
+ )
153
155
self ._write_register_byte (_STMPE_TSC_FRACTION_Z , 0x6 )
154
156
self ._write_register_byte (_STMPE_FIFO_TH , 1 )
155
157
self ._write_register_byte (_STMPE_FIFO_STA , _STMPE_FIFO_STA_RESET )
156
- self ._write_register_byte (_STMPE_FIFO_STA , 0 ) # unreset
158
+ self ._write_register_byte (_STMPE_FIFO_STA , 0 ) # unreset
157
159
self ._write_register_byte (_STMPE_TSC_I_DRIVE , _STMPE_TSC_I_DRIVE_50MA )
158
- self ._write_register_byte (_STMPE_INT_STA , 0xFF ) # reset all ints
160
+ self ._write_register_byte (_STMPE_INT_STA , 0xFF ) # reset all ints
159
161
self ._write_register_byte (
160
- _STMPE_INT_CTRL , _STMPE_INT_CTRL_POL_HIGH | _STMPE_INT_CTRL_ENABLE )
162
+ _STMPE_INT_CTRL , _STMPE_INT_CTRL_POL_HIGH | _STMPE_INT_CTRL_ENABLE
163
+ )
161
164
162
165
def read_data (self ):
163
166
"""Request next stored reading - return tuple containing (x,y,pressure) """
@@ -188,7 +191,6 @@ def _write_register_byte(self, register, value):
188
191
# Subclasses MUST implement this!
189
192
raise NotImplementedError
190
193
191
-
192
194
@property
193
195
def touches (self ):
194
196
"""
@@ -198,27 +200,25 @@ def touches(self):
198
200
touchpoints = []
199
201
while (len (touchpoints ) < 4 ) and not self .buffer_empty :
200
202
(x_loc , y_loc , pressure ) = self .read_data ()
201
- point = {'x' : x_loc , 'y' : y_loc , ' pressure' : pressure }
203
+ point = {"x" : x_loc , "y" : y_loc , " pressure" : pressure }
202
204
touchpoints .append (point )
203
205
return touchpoints
204
206
205
-
206
207
@property
207
208
def get_version (self ):
208
209
"Read the version number from the sensosr"
209
210
v_1 = self ._read_byte (0 )
210
211
v_2 = self ._read_byte (1 )
211
- version = v_1 << 8 | v_2
212
- #print("version ",hex(version))
212
+ version = v_1 << 8 | v_2
213
+ # print("version ",hex(version))
213
214
return version
214
215
215
216
@property
216
217
def touched (self ):
217
218
"Report if any touches have been detectd"
218
- touch = self ._read_byte (_STMPE_TSC_CTRL )& 0x80
219
+ touch = self ._read_byte (_STMPE_TSC_CTRL ) & 0x80
219
220
return touch == 0x80
220
221
221
-
222
222
@property
223
223
def buffer_size (self ):
224
224
"The amount of touch data in the buffer"
@@ -230,33 +230,31 @@ def buffer_empty(self):
230
230
empty = self ._read_byte (_STMPE_FIFO_STA ) & _STMPE_FIFO_STA_EMPTY
231
231
return empty != 0
232
232
233
-
234
-
235
233
@property
236
234
def get_point (self ):
237
235
"Read one touch from the buffer"
238
236
(x_loc , y_loc , pressure ) = self .read_data ()
239
- point = {'x' :x_loc , 'y' :y_loc , 'pressure' :pressure }
240
- return point
241
-
242
-
237
+ point = {"x" : x_loc , "y" : y_loc , "pressure" : pressure }
238
+ return point
243
239
244
240
245
241
class Adafruit_STMPE610_I2C (Adafruit_STMPE610 ):
246
242
"""
247
243
I2C driver for the STMPE610 Resistive Touch sensor.
248
244
"""
245
+
249
246
def __init__ (self , i2c , address = _STMPE_ADDR ):
250
247
"""
251
248
Check the STMPE610 was founnd
252
249
Default address is 0x41 but another address can be passed in as an argument
253
250
"""
254
- import adafruit_bus_device .i2c_device as i2cdev
251
+ import adafruit_bus_device .i2c_device as i2cdev # pylint: disable=import-outside-toplevel
252
+
255
253
self ._i2c = i2cdev .I2CDevice (i2c , address )
256
254
# Check device version.
257
255
version = self .get_version
258
256
if _STMPE_VERSION != version :
259
- raise RuntimeError (' Failed to find STMPE610! Chip Version 0x%x' % version )
257
+ raise RuntimeError (" Failed to find STMPE610! Chip Version 0x%x" % version )
260
258
super ().__init__ ()
261
259
262
260
def _read_register (self , register , length ):
@@ -265,34 +263,40 @@ def _read_register(self, register, length):
265
263
i2c .write (bytearray ([register & 0xFF ]))
266
264
result = bytearray (length )
267
265
i2c .readinto (result )
268
- #print("$%02X => %s" % (register, [hex(i) for i in result]))
266
+ # print("$%02X => %s" % (register, [hex(i) for i in result]))
269
267
return result
270
268
271
269
def _write_register_byte (self , register , value ):
272
270
"""Low level register writing over I2C, writes one 8-bit value"""
273
271
with self ._i2c as i2c :
274
272
i2c .write (bytes ([register & 0xFF , value & 0xFF ]))
275
- #print("$%02X <= 0x%02X" % (register, value))
273
+ # print("$%02X <= 0x%02X" % (register, value))
276
274
277
275
278
276
class Adafruit_STMPE610_SPI (Adafruit_STMPE610 ):
279
277
"""
280
278
SPI driver for the STMPE610 Resistive Touch sensor.
281
279
"""
280
+
282
281
def __init__ (self , spi , cs , baudrate = 1000000 ):
283
282
"""
284
283
Check the STMPE610 was found,Default clock rate 1000000 - can be changed with 'baudrate'
285
284
"""
286
- import adafruit_bus_device .spi_device as spidev
285
+ import adafruit_bus_device .spi_device as spidev # pylint: disable=import-outside-toplevel
286
+
287
287
self ._spi = spidev .SPIDevice (spi , cs , baudrate = baudrate )
288
288
# Check device version.
289
289
version = self .get_version
290
290
if _STMPE_VERSION != version :
291
291
# if it fails try SPI MODE 1 -- that is what Arduino does
292
- self ._spi = spidev .SPIDevice (spi , cs , baudrate = baudrate , polarity = 0 , phase = 1 )
292
+ self ._spi = spidev .SPIDevice (
293
+ spi , cs , baudrate = baudrate , polarity = 0 , phase = 1
294
+ )
293
295
version = self .get_version
294
296
if _STMPE_VERSION != version :
295
- raise RuntimeError ('Failed to find STMPE610! Chip Version 0x%x' % version )
297
+ raise RuntimeError (
298
+ "Failed to find STMPE610! Chip Version 0x%x" % version
299
+ )
296
300
super ().__init__ ()
297
301
298
302
# pylint: disable=no-member
@@ -304,7 +308,7 @@ def _read_register(self, register, length):
304
308
spi .write (bytearray ([register ]))
305
309
result = bytearray (length )
306
310
spi .readinto (result )
307
- # print("$%02X => %s" % (register, [hex(i) for i in result]))
311
+ # print("$%02X => %s" % (register, [hex(i) for i in result]))
308
312
return result
309
313
310
314
def _write_register_byte (self , register , value ):
0 commit comments