@@ -101,26 +101,19 @@ def __init__(self):
101
101
self ._sine_wave_sample = None
102
102
103
103
# Define touch:
104
- # We chose these verbose touch_A# names so that beginners could use it without understanding
105
- # lists and the capital A to match the pin name. The capitalization is not strictly Python
106
- # style, so everywhere we use these names, we whitelist the errors using:
107
- # pylint: disable=invalid-name
108
- self ._touch_A1 = None
109
- self ._touch_A2 = None
110
- self ._touch_A3 = None
111
- self ._touch_A4 = None
112
- self ._touch_A5 = None
113
- self ._touch_A6 = None
114
- self ._touch_A7 = None
104
+ # Initially, self._touches stores the pin used for a particular touch. When that touch is
105
+ # used for the first time, the pin is replaced with the corresponding TouchIn object.
106
+ # This saves a little RAM over using a separate read-only pin tuple.
107
+ # For example, after `cpx.touch_A2`, self._touches is equivalent to:
108
+ # [None, board.A1, touchio.TouchIn(board.A2), board.A3, ...]
109
+ # Slot 0 is not used (A0 is not allowed as a touch pin).
110
+ self ._touches = [None , board .A1 , board .A2 , board .A3 , board .A4 , board .A5 , board .A6 , board .A7 ]
115
111
self ._touch_threshold_adjustment = 0
116
112
117
113
# Define acceleration:
118
114
self ._i2c = busio .I2C (board .ACCELEROMETER_SCL , board .ACCELEROMETER_SDA )
119
115
self ._int1 = digitalio .DigitalInOut (board .ACCELEROMETER_INTERRUPT )
120
- try :
121
- self ._lis3dh = adafruit_lis3dh .LIS3DH_I2C (self ._i2c , address = 0x19 , int1 = self ._int1 )
122
- except TypeError :
123
- self ._lis3dh = adafruit_lis3dh .LIS3DH_I2C (self ._i2c , address = 0x19 )
116
+ self ._lis3dh = adafruit_lis3dh .LIS3DH_I2C (self ._i2c , address = 0x19 , int1 = self ._int1 )
124
117
self ._lis3dh .range = adafruit_lis3dh .RANGE_8_G
125
118
126
119
# Initialise tap:
@@ -149,13 +142,10 @@ def detect_taps(self):
149
142
@detect_taps .setter
150
143
def detect_taps (self , value ):
151
144
self ._detect_taps = value
152
- try :
153
- if value == 1 :
154
- self ._lis3dh .set_tap (value , 90 , time_limit = 4 , time_latency = 50 , time_window = 255 )
155
- if value == 2 :
156
- self ._lis3dh .set_tap (value , 60 , time_limit = 10 , time_latency = 50 , time_window = 255 )
157
- except AttributeError :
158
- pass
145
+ if value == 1 :
146
+ self ._lis3dh .set_tap (value , 90 , time_limit = 4 , time_latency = 50 , time_window = 255 )
147
+ if value == 2 :
148
+ self ._lis3dh .set_tap (value , 60 , time_limit = 10 , time_latency = 50 , time_window = 255 )
159
149
160
150
@property
161
151
def tapped (self ):
@@ -206,11 +196,7 @@ def tapped(self):
206
196
print("Done.")
207
197
208
198
"""
209
- try :
210
- return self ._lis3dh .tapped
211
- except AttributeError :
212
- raise RuntimeError ("Oops! You need a newer version of CircuitPython "
213
- "(2.2.0 or greater) to use this feature." )
199
+ return self ._lis3dh .tapped
214
200
215
201
@property
216
202
def acceleration (self ):
@@ -262,12 +248,19 @@ def shake(self, shake_threshold=30):
262
248
if cpx.shake(shake_threshold=20):
263
249
print("Shake detected more easily than before!")
264
250
"""
265
- try :
266
- return self ._lis3dh .shake (shake_threshold = shake_threshold )
267
- except AttributeError :
268
- raise RuntimeError ("Oops! You need a newer version of CircuitPython "
269
- "(2.2.0 or greater) to use this feature." )
270
-
251
+ return self ._lis3dh .shake (shake_threshold = shake_threshold )
252
+
253
+ def _touch (self , i ):
254
+ if not isinstance (self ._touches [i ], touchio .TouchIn ):
255
+ # First time referenced. Get the pin from the slot for this touch
256
+ # and replace it with a TouchIn object for the pin.
257
+ self ._touches [i ] = touchio .TouchIn (self ._touches [i ])
258
+ self ._touches [i ].threshold += self ._touch_threshold_adjustment
259
+ return self ._touches [i ].value
260
+
261
+ # We chose these verbose touch_A# names so that beginners could use it without understanding
262
+ # lists and the capital A to match the pin name. The capitalization is not strictly Python
263
+ # style, so everywhere we use these names, we whitelist the errors using:
271
264
@property
272
265
def touch_A1 (self ): # pylint: disable=invalid-name
273
266
"""Detect touch on capacitive touch pad A1.
@@ -283,10 +276,7 @@ def touch_A1(self): # pylint: disable=invalid-name
283
276
if cpx.touch_A1:
284
277
print('Touched pad A1')
285
278
"""
286
- if self ._touch_A1 is None :
287
- self ._touch_A1 = touchio .TouchIn (board .A1 )
288
- self ._touch_A1 .threshold += self ._touch_threshold_adjustment
289
- return self ._touch_A1 .value
279
+ return self ._touch (1 )
290
280
291
281
@property
292
282
def touch_A2 (self ): # pylint: disable=invalid-name
@@ -303,10 +293,7 @@ def touch_A2(self): # pylint: disable=invalid-name
303
293
if cpx.touch_A2:
304
294
print('Touched pad A2')
305
295
"""
306
- if self ._touch_A2 is None :
307
- self ._touch_A2 = touchio .TouchIn (board .A2 )
308
- self ._touch_A2 .threshold += self ._touch_threshold_adjustment
309
- return self ._touch_A2 .value
296
+ return self ._touch (2 )
310
297
311
298
@property
312
299
def touch_A3 (self ): # pylint: disable=invalid-name
@@ -323,10 +310,7 @@ def touch_A3(self): # pylint: disable=invalid-name
323
310
if cpx.touch_A3:
324
311
print('Touched pad A3')
325
312
"""
326
- if self ._touch_A3 is None :
327
- self ._touch_A3 = touchio .TouchIn (board .A3 )
328
- self ._touch_A3 .threshold += self ._touch_threshold_adjustment
329
- return self ._touch_A3 .value
313
+ return self ._touch (3 )
330
314
331
315
@property
332
316
def touch_A4 (self ): # pylint: disable=invalid-name
@@ -343,10 +327,7 @@ def touch_A4(self): # pylint: disable=invalid-name
343
327
if cpx.touch_A4:
344
328
print('Touched pad A4')
345
329
"""
346
- if self ._touch_A4 is None :
347
- self ._touch_A4 = touchio .TouchIn (board .A4 )
348
- self ._touch_A4 .threshold += self ._touch_threshold_adjustment
349
- return self ._touch_A4 .value
330
+ return self ._touch (4 )
350
331
351
332
@property
352
333
def touch_A5 (self ): # pylint: disable=invalid-name
@@ -363,10 +344,7 @@ def touch_A5(self): # pylint: disable=invalid-name
363
344
if cpx.touch_A5:
364
345
print('Touched pad A5')
365
346
"""
366
- if self ._touch_A5 is None :
367
- self ._touch_A5 = touchio .TouchIn (board .A5 )
368
- self ._touch_A5 .threshold += self ._touch_threshold_adjustment
369
- return self ._touch_A5 .value
347
+ return self ._touch (5 )
370
348
371
349
@property
372
350
def touch_A6 (self ): # pylint: disable=invalid-name
@@ -383,10 +361,7 @@ def touch_A6(self): # pylint: disable=invalid-name
383
361
if cpx.touch_A6:
384
362
print('Touched pad A6')
385
363
"""
386
- if self ._touch_A6 is None :
387
- self ._touch_A6 = touchio .TouchIn (board .A6 )
388
- self ._touch_A6 .threshold += self ._touch_threshold_adjustment
389
- return self ._touch_A6 .value
364
+ return self ._touch (6 )
390
365
391
366
@property
392
367
def touch_A7 (self ): # pylint: disable=invalid-name
@@ -403,10 +378,7 @@ def touch_A7(self): # pylint: disable=invalid-name
403
378
if cpx.touch_A7:
404
379
print('Touched pad A7')
405
380
"""
406
- if self ._touch_A7 is None :
407
- self ._touch_A7 = touchio .TouchIn (board .A7 )
408
- self ._touch_A7 .threshold += self ._touch_threshold_adjustment
409
- return self ._touch_A7 .value
381
+ return self ._touch (7 )
410
382
411
383
def adjust_touch_threshold (self , adjustment ):
412
384
"""Adjust the threshold needed to activate the capacitive touch pads.
@@ -427,9 +399,8 @@ def adjust_touch_threshold(self, adjustment):
427
399
if cpx.touch_A1:
428
400
print('Touched pad A1')
429
401
"""
430
- for pad_name in ["_touch_A" + str (x ) for x in range (1 , 8 )]:
431
- touch_in = getattr (self , pad_name )
432
- if touch_in :
402
+ for touch_in in self ._touches :
403
+ if isinstance (touch_in , touchio .TouchIn ):
433
404
touch_in .threshold += adjustment
434
405
self ._touch_threshold_adjustment += adjustment
435
406
@@ -595,12 +566,11 @@ def _generate_sample(self):
595
566
if self ._sample is not None :
596
567
return
597
568
length = 100
569
+ self ._sine_wave = array .array ("H" , Express ._sine_sample (length ))
598
570
if sys .implementation .version [0 ] >= 3 :
599
- self ._sine_wave = array .array ("H" , Express ._sine_sample (length ))
600
571
self ._sample = audioio .AudioOut (board .SPEAKER )
601
572
self ._sine_wave_sample = audioio .RawSample (self ._sine_wave )
602
573
else :
603
- self ._sine_wave = array .array ("H" , Express ._sine_sample (length ))
604
574
self ._sample = audioio .AudioOut (board .SPEAKER , self ._sine_wave )
605
575
606
576
0 commit comments