@@ -100,17 +100,10 @@ class APDS9960:
100
100
APDS9900 provide basic driver services for the ASDS9960 breakout board
101
101
"""
102
102
103
- _enable = RWBit (APDS9960_ENABLE , 0 )
104
- _color_enable = RWBit (APDS9960_ENABLE , 1 )
105
- _proximity_enable = RWBit (APDS9960_ENABLE , 2 )
106
103
_gesture_enable = RWBit (APDS9960_ENABLE , 6 )
107
- _gesture_fifo_threshold = RWBits (2 , APDS9960_GCONF1 , 6 )
108
- _gesture_gain = RWBits (2 , APDS9960_GCONF2 , 5 )
109
- _color_gain = RWBits (2 , APDS9960_CONTROL , 0 )
110
104
_gesture_valid = RWBit (APDS9960_GSTATUS , 0 )
111
105
_gesture_mode = RWBit (APDS9960_GCONF4 , 0 )
112
106
_proximity_persistance = RWBits (4 , APDS9960_PERS , 4 )
113
- _proximity_enable_interrupt = RWBit (APDS9960_ENABLE , 5 )
114
107
115
108
def __init__ (self ,
116
109
i2c , * ,
@@ -124,23 +117,21 @@ def __init__(self,
124
117
if interrupt_pin :
125
118
self ._interrupt_pin .switch_to_input (pull = digitalio .Pull .UP )
126
119
127
- if self .read8 (APDS9960_ID ) != 0xAB :
120
+ if self ._read8 (APDS9960_ID ) != 0xAB :
128
121
raise RuntimeError ()
129
122
130
123
self .enable_gesture = False
131
124
self .enable_proximity = False
132
125
self .enable_color = False
133
- self ._enable_proximity_interrupt = False
134
- self .enable_color_interrupt = False
135
- self .proximity_interrupt = False
126
+ self .enable_proximity_interrupt = False
136
127
self .clear_interrupt ()
137
128
138
129
self .enable = False
139
130
time .sleep (0.010 )
140
131
self .enable = True
141
132
time .sleep (0.010 )
142
133
143
- self ._color_gain = gain
134
+ self .color_gain = gain
144
135
self .integration_time = integration_time
145
136
self .gesture_dimensions = 0x00 # all
146
137
self .gesture_fifo_threshold = 0x01 # fifo 4
@@ -149,32 +140,41 @@ def __init__(self,
149
140
self ._reset_counts ()
150
141
151
142
# gesture pulse length=0x2 pulse count=0x3
152
- self .write8 (APDS9960_GPULSE , (0x2 << 6 ) | 0x3 )
143
+ self ._write8 (APDS9960_GPULSE , (0x2 << 6 ) | 0x3 )
153
144
154
145
155
146
## BOARD
156
- @property
157
- def enable (self ):
158
- """Set or get board enable. True to enable, False to disable"""
159
- return self ._enable
160
-
161
- @enable .setter
162
- def enable (self , enable_flag ):
163
- self ._enable = enable_flag
164
-
165
147
def _reset_counts (self ):
166
148
"""Gesture detection internal counts"""
167
149
self ._saw_down_start = 0
168
150
self ._saw_up_start = 0
169
151
self ._saw_left_start = 0
170
152
self ._saw_right_start = 0
171
153
154
+
155
+ enable = RWBit (APDS9960_ENABLE , 0 )
156
+ """Board enable. True to enable, False to disable"""
157
+ enable_color = RWBit (APDS9960_ENABLE , 1 )
158
+ """Color detection enable flag.
159
+ True when color detection is enabled, else False"""
160
+ enable_proximity = RWBit (APDS9960_ENABLE , 2 )
161
+ """Enable of proximity mode"""
162
+ gesture_fifo_threshold = RWBits (2 , APDS9960_GCONF1 , 6 )
163
+ """Gesture fifo threshold value: range 0-3"""
164
+ gesture_gain = RWBits (2 , APDS9960_GCONF2 , 5 )
165
+ """Gesture gain value: range 0-3"""
166
+ color_gain = RWBits (2 , APDS9960_CONTROL , 0 )
167
+ """Color gain value"""
168
+ enable_proximity_interrupt = RWBit (APDS9960_ENABLE , 5 )
169
+ """Proximity interrupt enable flag. True if enabled,
170
+ False to disable"""
171
+
172
172
## GESTURE DETECTION
173
173
@property
174
174
def enable_gesture (self ):
175
- """Set ot get gesture detection enable flag
176
- Note that when disabled, the gesture mode is turned off"""
177
- return self ._gesture_mode , self . _gesture_enable
175
+ """Gesture detection enable flag. True to enable, False to disable.
176
+ Note that when disabled, gesture mode is turned off"""
177
+ return self ._gesture_enable
178
178
179
179
@enable_gesture .setter
180
180
def enable_gesture (self , enable_flag ):
@@ -183,7 +183,7 @@ def enable_gesture(self, enable_flag):
183
183
self ._gesture_enable = enable_flag
184
184
185
185
def gesture (self ): #pylint: disable-msg=too-many-branches
186
- """Return a gesture code if detected. =0 if no gesture
186
+ """Returns gesture code if detected. =0 if no gesture detected
187
187
=1 if an UP, =2 if a DOWN, =3 if an LEFT, =4 if a RIGHT
188
188
"""
189
189
# buffer to read of contents of device FIFO buffer
@@ -256,144 +256,95 @@ def gesture(self): #pylint: disable-msg=too-many-branches
256
256
257
257
return gesture_received
258
258
259
-
260
259
@property
261
260
def gesture_dimensions (self ):
262
- """Set of get gesture dimension value: range 0-3"""
263
- return self .read8 (APDS9960_GCONF3 )
261
+ """Gesture dimension value: range 0-3"""
262
+ return self ._read8 (APDS9960_GCONF3 )
264
263
265
264
@gesture_dimensions .setter
266
265
def gesture_dimensions (self , dims ):
267
- self .write8 (APDS9960_GCONF3 , dims & 0x03 )
268
-
269
- @property
270
- def gesture_fifo_threshold (self ):
271
- """Set or get gesture fifo threshold value: range 0-3"""
272
- return self ._gesture_fifo_threshold
273
-
274
- @gesture_fifo_threshold .setter
275
- def gesture_fifo_threshold (self , thresh ):
276
- self ._gesture_fifo_threshold = thresh & 0x03
277
-
278
- @property
279
- def gesture_gain (self ):
280
- """Set or get gesture gain value: range 0-3"""
281
- return self ._gesture_gain
282
-
283
- @gesture_gain .setter
284
- def gesture_gain (self , gain ):
285
- self ._gesture_gain = gain
286
-
287
-
288
- ## COLOR DETECTION
289
- @property
290
- def enable_color (self ):
291
- """Set of get color detection enable flag.
292
- True when color detection is enabled, else False"""
293
- return self ._color_enable
294
-
295
- @enable_color .setter
296
- def enable_color (self , enable_flag ):
297
- self ._color_enable = enable_flag
266
+ self ._write8 (APDS9960_GCONF3 , dims & 0x03 )
298
267
299
268
@property
300
269
def color_data_ready (self ):
301
- """Get color data ready flag. zero if not ready, 1 is ready"""
302
- return self .read8 (APDS9960_STATUS ) & 0x01
270
+ """Color data ready flag. zero if not ready, 1 is ready"""
271
+ return self ._read8 (APDS9960_STATUS ) & 0x01
303
272
304
273
@property
305
274
def color_data (self ):
306
- """Returns tuple containing r, g, b, c values"""
275
+ """Tuple containing r, g, b, c values"""
307
276
return self ._color_data16 (APDS9960_CDATAL + 2 ), \
308
277
self ._color_data16 (APDS9960_CDATAL + 4 ), \
309
278
self ._color_data16 (APDS9960_CDATAL + 6 ), \
310
279
self ._color_data16 (APDS9960_CDATAL )
311
- ### PROXIMITY
312
- @property
313
- def enable_proximity (self ):
314
- """Returns or sets the enablement of proximity mode"""
315
- return self ._proximity_enable
316
-
317
- @enable_proximity .setter
318
- def enable_proximity (self , enable_flag ):
319
- self ._proximity_enable = enable_flag
320
280
281
+ ### PROXIMITY
321
282
@property
322
283
def proximity_interrupt_threshold (self ):
323
- """Returns a tuple containing low and high threshold
284
+ """Tuple containing low and high threshold
324
285
followed by the proximity interrupt persistance.
325
- Set the proximity interrupt threshold values using a tuple of zero to
326
- three values: low threshold, high threshold, persistance.
286
+ When setting the proximity interrupt threshold values using a tuple of
287
+ zero to three values: low threshold, high threshold, persistance.
327
288
persistance defaults to 4 if not provided"""
328
- return self .read8 (APDS9960_PILT ), \
329
- self .read8 (APDS9960_PIHT ), \
289
+ return self ._read8 (APDS9960_PILT ), \
290
+ self ._read8 (APDS9960_PIHT ), \
330
291
self ._proximity_persistance
331
292
332
293
@proximity_interrupt_threshold .setter
333
294
def proximity_interrupt_threshold (self , setting_tuple ):
334
295
if setting_tuple :
335
- self .write8 (APDS9960_PILT , setting_tuple [0 ])
296
+ self ._write8 (APDS9960_PILT , setting_tuple [0 ])
336
297
if len (setting_tuple ) > 1 :
337
- self .write8 (APDS9960_PIHT , setting_tuple [1 ])
298
+ self ._write8 (APDS9960_PIHT , setting_tuple [1 ])
338
299
persist = 4 # default 4
339
300
if len (setting_tuple ) > 2 :
340
301
persist = min (setting_tuple [2 ], 7 )
341
302
self ._proximity_persistance = persist
342
303
343
304
344
- @property
345
- def enable_proximity_interrupt (self ):
346
- """Set or get proximity interrupt enable flag. True if enabled,
347
- False to disable"""
348
- return self ._proximity_enable_interrupt
349
-
350
- @enable_proximity_interrupt .setter
351
- def enable_proximity_interrupt (self , enable_flag ):
352
- self ._proximity_enable_interrupt = enable_flag
353
-
354
305
@property
355
306
def gesture_proximity_threshold (self ):
356
- """Set or get proximity threshold value: range 0-255"""
357
- return self .read8 (APDS9960_GPENTH )
307
+ """Proximity threshold value: range 0-255"""
308
+ return self ._read8 (APDS9960_GPENTH )
358
309
359
310
@gesture_proximity_threshold .setter
360
311
def gesture_proximity_threshold (self , thresh ):
361
- self .write8 (APDS9960_GPENTH , thresh & 0xff )
312
+ self ._write8 (APDS9960_GPENTH , thresh & 0xff )
362
313
363
314
def proximity (self ):
364
- """Set or get proximity value: range 0-255"""
365
- return self .read8 (APDS9960_PDATA )
315
+ """proximity value: range 0-255"""
316
+ return self ._read8 (APDS9960_PDATA )
366
317
367
318
def clear_interrupt (self ):
368
319
"""Clear all interrupts"""
369
- self .writecmdonly (APDS9960_AICLEAR )
320
+ self ._writecmdonly (APDS9960_AICLEAR )
370
321
371
322
@property
372
323
def integration_time (self ):
373
- """Set or get the proximity integration time: range 0-255"""
374
- return self .read8 (APDS9960_ATIME )
324
+ """Proximity integration time: range 0-255"""
325
+ return self ._read8 (APDS9960_ATIME )
375
326
376
327
@integration_time .setter
377
328
def integration_time (self , int_time ):
378
- self .write8 (APDS9960_ATIME , int_time & 0xff )
329
+ self ._write8 (APDS9960_ATIME , int_time & 0xff )
379
330
380
331
# method for reading and writing to I2C
381
- def write8 (self , command , abyte ):
332
+ def _write8 (self , command , abyte ):
382
333
"""Write a command and 1 byte of data to the I2C device"""
383
334
buf = bytearray (2 )
384
335
buf [0 ] = command
385
336
buf [1 ] = abyte
386
337
with self .i2c_device as i2c :
387
338
i2c .write (buf )
388
339
389
- def writecmdonly (self , command ):
340
+ def _writecmdonly (self , command ):
390
341
"""Writes a command and 0 bytes of data to the I2C device"""
391
342
buf = bytearray (1 )
392
343
buf [0 ] = command
393
344
with self .i2c_device as i2c :
394
345
i2c .write (buf )
395
346
396
- def read8 (self , command ):
347
+ def _read8 (self , command ):
397
348
"""Sends a command and reads 1 byte of data from the I2C device"""
398
349
buf = bytearray (1 )
399
350
buf [0 ] = command
@@ -403,7 +354,7 @@ def read8(self, command):
403
354
return buf [0 ]
404
355
405
356
def _color_data16 (self , command ):
406
- """Sends a command and reads 2 byte of data from the I2C device
357
+ """Sends a command and reads 2 bytes of data from the I2C device
407
358
The returned data is low byte first followed by high byte"""
408
359
buf = bytearray (2 )
409
360
buf [0 ] = command
0 commit comments