-
Notifications
You must be signed in to change notification settings - Fork 1
/
APDS9960.py
2219 lines (1546 loc) · 66.8 KB
/
APDS9960.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
.. module:: Avago.apds9960
*************
apds9960 Module
*************
This module contains the driver for APDS-9960, It's features are Gesture detection, Proximity detection, Digital Ambient Light Sense (ALS) and Color Sense (RGBC).
The APDS-9960 is a serious little piece of hardware with built in UV and IR blocking filters, four separate diodes sensitive to different directions, and an I2C compatible interface
(`datasheet <https://cdn.sparkfun.com/datasheets/Sensors/Proximity/apds9960.pdf>`_).
"""
import i2c
import streams
streams.serial()
new_exception(RuntimeErrorSet,ValueError,'Can not override values')
new_exception(RuntimeErrorDel,ValueError,'Can not delete values')
new_exception(ErrorReadingRegister,RuntimeError,'It was an error while reading the register')
new_exception(ErrorWritingRegister,RuntimeError,'There was an error writing to the register')
new_exception(ErrorDevice,RuntimeError,'Error device 0xFF')
#Debug
DEBUG = 0
#APDS-9960 I2C address
APDS9960_I2C_ADDR = 0x39
#Gesture parameters
GESTURE_THRESHOLD_OUT = 10
GESTURE_SENSITIVITY_1 = 50
GESTURE_SENSITIVITY_2 = 20
#Error code for returned values
ERROR = 0xFF
#Acceptable device IDs
APDS9960_ID_1 = 0xAB
APDS9960_ID_2 = 0x9C
#Misc parameters
FIFO_PAUSE_TIME = 30 # Wait period (ms) between FIFO reads
#APDS-9960 register addresses
APDS9960_ENABLE = 0x80
APDS9960_ATIME = 0x81
APDS9960_WTIME = 0x83
APDS9960_AILTL = 0x84
APDS9960_AILTH = 0x85
APDS9960_AIHTL = 0x86
APDS9960_AIHTH = 0x87
APDS9960_PILT = 0x89
APDS9960_PIHT = 0x8B
APDS9960_PERS = 0x8C
APDS9960_CONFIG1 = 0x8D
APDS9960_PPULSE = 0x8E
APDS9960_CONTROL = 0x8F
APDS9960_CONFIG2 = 0x90
APDS9960_ID = 0x92
APDS9960_STATUS = 0x93
APDS9960_CDATAL = 0x94
APDS9960_CDATAH = 0x95
APDS9960_RDATAL = 0x96
APDS9960_RDATAH = 0x97
APDS9960_GDATAL = 0x98
APDS9960_GDATAH = 0x99
APDS9960_BDATAL = 0x9A
APDS9960_BDATAH = 0x9B
APDS9960_PDATA = 0x9C
APDS9960_POFFSET_UR = 0x9D
APDS9960_POFFSET_DL = 0x9E
APDS9960_CONFIG3 = 0x9F
APDS9960_GPENTH = 0xA0
APDS9960_GEXTH = 0xA1
APDS9960_GCONF1 = 0xA2
APDS9960_GCONF2 = 0xA3
APDS9960_GOFFSET_U = 0xA4
APDS9960_GOFFSET_D = 0xA5
APDS9960_GOFFSET_L = 0xA7
APDS9960_GOFFSET_R = 0xA9
APDS9960_GPULSE = 0xA6
APDS9960_GCONF3 = 0xAA
APDS9960_GCONF4 = 0xAB
APDS9960_GFLVL = 0xAE
APDS9960_GSTATUS = 0xAF
APDS9960_IFORCE = 0xE4
APDS9960_PICLEAR = 0xE5
APDS9960_CICLEAR = 0xE6
APDS9960_AICLEAR = 0xE7
APDS9960_GFIFO_U = 0xFC
APDS9960_GFIFO_D = 0xFD
APDS9960_GFIFO_L = 0xFE
APDS9960_GFIFO_R = 0xFF
#Bit fields
APDS9960_PON = 0b00000001
APDS9960_AEN = 0b00000010
APDS9960_PEN = 0b00000100
APDS9960_WEN = 0b00001000
APSD9960_AIEN = 0b00010000
APDS9960_PIEN = 0b00100000
APDS9960_GEN = 0b01000000
APDS9960_GVALID = 0b00000001
#On/Off definitions
OFF = 0
ON = 1
#Acceptable parameters for self.setMode
POWER = 0
AMBIENT_LIGHT = 1
PROXIMITY = 2
WAIT = 3
AMBIENT_LIGHT_INT = 4
PROXIMITY_INT = 5
GESTURE = 6
ALL = 7
#LED Drive values
LED_DRIVE_100MA = 0
LED_DRIVE_50MA = 1
LED_DRIVE_25MA = 2
LED_DRIVE_12_5MA = 3
#Proximity Gain (PGAIN) values
PGAIN_1X = 0
PGAIN_2X = 1
PGAIN_4X = 2
PGAIN_8X = 3
#ALS Gain (AGAIN) values
AGAIN_1X = 0
AGAIN_4X = 1
AGAIN_16X = 2
AGAIN_64X = 3
#Gesture Gain (GGAIN) values
GGAIN_1X = 0
GGAIN_2X = 1
GGAIN_4X = 2
GGAIN_8X = 3
#LED Boost values
LED_BOOST_100 = 0
LED_BOOST_150 = 1
LED_BOOST_200 = 2
LED_BOOST_300 = 3
#Gesture wait time values
GWTIME_0MS = 0
GWTIME_2_8MS = 1
GWTIME_5_6MS = 2
GWTIME_8_4MS = 3
GWTIME_14_0MS = 4
GWTIME_22_4MS = 5
GWTIME_30_8MS = 6
GWTIME_39_2MS = 7
#Default values
DEFAULT_ATIME = 219 # 103ms
DEFAULT_WTIME = 246 # 27ms
DEFAULT_PROX_PPULSE = 0x87 # 16us, 8 pulses
DEFAULT_GESTURE_PPULSE = 0x89 # 16us, 10 pulses
DEFAULT_POFFSET_UR = 0 # 0 offset
DEFAULT_POFFSET_DL = 0 # 0 offset
DEFAULT_CONFIG1 = 0x60 # No 12x wait (WTIME) factor
DEFAULT_LDRIVE = LED_DRIVE_100MA
DEFAULT_PGAIN = PGAIN_4X
DEFAULT_AGAIN = AGAIN_4X
DEFAULT_PILT = 0 # Low proximity threshold
DEFAULT_PIHT = 50 # High proximity threshold
DEFAULT_AILT = 0xFFFF # Force interrupt for calibration
DEFAULT_AIHT = 0
DEFAULT_PERS = 0x11 # 2 consecutive prox or ALS for int.
DEFAULT_CONFIG2 = 0x01 # No saturation interrupts or LED boost
DEFAULT_CONFIG3 = 0 # Enable all photodiodes, no SAI
DEFAULT_GPENTH = 40 # Threshold for entering gesture mode
DEFAULT_GEXTH = 30 # Threshold for exiting gesture mode
DEFAULT_GCONF1 = 0x40 # 4 gesture events for int., 1 for exit
DEFAULT_GGAIN = GGAIN_4X
DEFAULT_GLDRIVE = LED_DRIVE_100MA
DEFAULT_GWTIME = GWTIME_2_8MS
DEFAULT_GOFFSET = 0 # No offset scaling for gesture mode
DEFAULT_GPULSE = 0xC9 # 32us, 10 pulses
DEFAULT_GCONF3 = 0 # All photodiodes active during gesture
DEFAULT_GIEN = 0 # Disable gesture interrupts
#Direction definitions
DIR_NONE = 'DIR_NONE'
DIR_LEFT = 'DIR_LEFT'
DIR_RIGHT ='DIR_RIGHT'
DIR_UP = 'DIR_UP'
DIR_DOWN = 'DIR_DOWN'
DIR_NEAR = 'DIR_NEAR'
DIR_FAR= 'DIR_FAR'
DIR_ALL= 'DIR_ALL'
#State definitions
NA_STATE = 'NA_STATE'
NEAR_STATE = 'NEAR_STATE'
FAR_STATE = 'FAR_STATE'
ALL_STATE = 'ALL_STATE'
class gesture_data_type():
def __init__(self):
self.u_data=[0 for x in range(32)]
self.d_data=[0 for x in range(32)]
self.l_data=[0 for x in range(32)]
self.r_data=[0 for x in range(32)]
self.index = 0
self.total_gestures = 0
self.in_threshold = 0
self.out_threshold = 0
class APDS9960(i2c.I2C):
"""
==================
The APDS9960 class
==================
.. class:: APDS9960(drivername)
"""
def __init__(self, i2cdrv, addr=0x39, clk=100000):
try:
i2c.I2C.__init__(self,i2cdrv,addr,clk)
self._addr = addr
self.start()
self.gesture_ud_delta_ = 0
self.gesture_lr_delta_ = 0
self.gesture_ud_count_ = 0
self.gesture_lr_count_ = 0
self.gesture_near_count_ = 0
self.gesture_far_count_ = 0
self.gesture_state_ = 0
self.gesture_motion_ = DIR_NONE
self.gesture_data_= gesture_data_type()
except Exception as e:
print(e)
def printRegister(self):
try:
self._printDEBUG('APDS9960_ENABLE',self.write_read(APDS9960_ENABLE, 1)[0])
self._printDEBUG('APDS9960_CONFIG1',self.write_read(APDS9960_CONFIG1, 1)[0])
self._printDEBUG('APDS9960_CONTROL',self.write_read(APDS9960_CONTROL, 1)[0])
self._printDEBUG('APDS9960_CONFIG2',self.write_read(APDS9960_CONFIG2, 1)[0])
self._printDEBUG('APDS9960_STATUS',self.write_read(APDS9960_STATUS, 1)[0])
self._printDEBUG('APDS9960_CONFIG3',self.write_read(APDS9960_CONFIG3, 1)[0])
self._printDEBUG('APDS9960_GCONF3',self.write_read(APDS9960_GCONF3, 1)[0])
self._printDEBUG('APDS9960_GCONF4',self.write_read(APDS9960_GCONF4, 1)[0])
self._printDEBUG('APDS9960_GCONF2',self.write_read(APDS9960_GCONF2, 1)[0])
except:
raise ErrorReadingRegister
def initialize(self):
try:
# Read ID register and check against known values for APDS-9960 */
if (self.get_device_id()!= 0xAB):
return False
# Set ENABLE register to 0 (disable all features) */
self.setMode(ALL, OFF)
# Set default values for ambient light and proximity registers */
self._write_bytes(APDS9960_ATIME, DEFAULT_ATIME)
self._write_bytes(APDS9960_WTIME, DEFAULT_WTIME)
self._write_bytes(APDS9960_PPULSE, DEFAULT_PROX_PPULSE)
self._write_bytes(APDS9960_POFFSET_UR, DEFAULT_POFFSET_UR)#
self._write_bytes(APDS9960_POFFSET_DL, DEFAULT_POFFSET_DL)#
self._write_bytes(APDS9960_CONFIG1, DEFAULT_CONFIG1)#
self.setLEDDrive(DEFAULT_LDRIVE)#APDS9960_CONTROL
self.setProximityGain(DEFAULT_PGAIN)#APDS9960_CONTROL
self.setAmbientLightGain(DEFAULT_AGAIN)
self.setProxIntLowThresh(DEFAULT_PILT)
self.setProxIntHighThresh(DEFAULT_PIHT)
self.setLightIntLowThreshold(DEFAULT_AILT)
self.setLightIntHighThreshold(DEFAULT_AIHT)
self._write_bytes(APDS9960_PERS, DEFAULT_PERS)
self._write_bytes(APDS9960_CONFIG2, DEFAULT_CONFIG2)
self._write_bytes(APDS9960_CONFIG3, DEFAULT_CONFIG3)
# Set default values for gesture sense registers */
self.setGestureEnterThresh(DEFAULT_GPENTH)
self.setGestureExitThresh(DEFAULT_GEXTH)
self._write_bytes(APDS9960_GCONF1, DEFAULT_GCONF1)
self.setGestureGain(DEFAULT_GGAIN)
self.setGestureLEDDrive(DEFAULT_GLDRIVE)
self.setGestureWaitTime(DEFAULT_GWTIME)
self._write_bytes(APDS9960_GOFFSET_U, DEFAULT_GOFFSET)
self._write_bytes(APDS9960_GOFFSET_D, DEFAULT_GOFFSET)
self._write_bytes(APDS9960_GOFFSET_L, DEFAULT_GOFFSET)
self._write_bytes(APDS9960_GOFFSET_R, DEFAULT_GOFFSET)
self._write_bytes(APDS9960_GPULSE, DEFAULT_GPULSE)
self._write_bytes(APDS9960_GCONF3, DEFAULT_GCONF3)
self.setGestureIntEnable(DEFAULT_GIEN)
except Exception as e:
print(e)
def get_device_id(self):
n = self.write_read(APDS9960_ID, 1)
return n[0]
def getMode(self):
"""
.. method:: getMode()
Reads and returns the contents of the ENABLE register
"""
try:
enable_value = self.write_read(APDS9960_ENABLE, 1)[0]
except:
raise ErrorReadingRegister
return enable_value
def setMode(self, mode, enable):
"""
.. method:: setMode(mode, enable)
Enables or disables a feature in the APDS-9960
mode:
feature to enable
enable:
ON (1) or OFF (0)
"""
reg_val = self.getMode()
if reg_val == ERROR :
raise ErrorDevice
# Change bit(s) in ENABLE register */
enable = enable & 0x01
if mode >= 0 and mode <= 6 :
if (enable):
reg_val |= (1 << mode)
else:
reg_val &= ~(1 << mode)
elif mode == ALL:
if (enable):
reg_val = 0x7F
else:
reg_val = 0x00
self._write_bytes(APDS9960_ENABLE,reg_val)
def enableLightSensor(self, interrupts):
"""
.. method:: enableLightSensor(interrupts)
Starts the light (R/G/B/Ambient) sensor on the APDS-9960
interrupts:
True to enable hardware interrupt on high or low light
"""
# Set default gain, interrupts, enable power, and enable sensor */
self.setAmbientLightGain(DEFAULT_AGAIN)
if interrupts:
self.setAmbientLightIntEnable(1)
else:
self.setAmbientLightIntEnable(0)
self.enablePower()
self.setMode(AMBIENT_LIGHT, 1)
def disableLightSensor(self):
"""
.. method:: disableLightSensor()
Ends the light sensor on the APDS-9960
"""
self.setAmbientLightIntEnable(0)
self.setMode(AMBIENT_LIGHT, 0)
def enableProximitySensor(self, interrupts):
"""
.. method:: enableProximitySensor(interrupts)
Starts the proximity sensor on the APDS-9960
interrupts:
True to enable hardware external interrupt on proximity
"""
# Set default gain, LED, interrupts, enable power, and enable sensor */
self.setProximityGain(DEFAULT_PGAIN)
self.setLEDDrive(DEFAULT_LDRIVE)
if interrupts:
self.setProximityIntEnable(1)
else:
self.setProximityIntEnable(0)
self.enablePower()
self.setMode(PROXIMITY, 1)
def disableProximitySensor(self):
"""
.. method:: disableProximitySensor()
Ends the proximity sensor on the APDS-9960
"""
self.setProximityIntEnable(0)
self.setMode(PROXIMITY, 0)
def enableGestureSensor(self, interrupts):
"""
.. method:: enableGestureSensor(interrupts)
Starts the gesture recognition engine on the APDS-9960
* Enable gesture mode
* Set ENABLE to 0 (power off)
* Set WTIME to 0xFF
* Set AUX to LED_BOOST_300
* Enable PON, WEN, PEN, GEN in ENABLE
"""
try:
self._resetGestureParameters()
self._write_bytes(APDS9960_WTIME, 0xFF)
self._write_bytes(APDS9960_PPULSE, DEFAULT_GESTURE_PPULSE)
self.setLEDBoost(LED_BOOST_300)
if interrupts:
self.setGestureIntEnable(1)
else:
self.setGestureIntEnable(0)
self.setGestureMode(1)
self.enablePower(self)
self.setMode(WAIT, 1)
self.setMode(PROXIMITY, 1)
self.setMode(GESTURE, 1)
except Exception as e:
print(e)
def disableGestureSensor(self):
"""
.. method:: disableGestureSensor()
Ends the gesture recognition engine on the APDS-9960
"""
self._resetGestureParameters()
self.setGestureIntEnable(0)
self.setGestureMode(0)
self.setMode(GESTURE, 0)
def isGestureAvailable(self):
"""
.. method:: isGestureAvailable()
Determines if there is a gesture available for reading
return:
True if gesture available. False otherwise.
"""
#Read value from GSTATUS register
try:
val = self.write_read(APDS9960_GSTATUS, 1)[0]
except:
raise ErrorReadingRegister
# Shift and mask out GVALID bit */
val &= APDS9960_GVALID
# Return True/False based on GVALID bit */
if val == 1:
return True
else:
return False
def readGesture(self):
"""
.. method:: readGesture()
Processes a gesture event and returns best guessed gesture
return:
Number corresponding to gesture.
"""
fifo_level = 0
fifo_data =[]
# Make sure that power and gesture is on and data is valid */
mode= self.getMode() & 0b01000001
if not self.isGestureAvailable() or not mode:
self._printDEBUG(' Make sure that power and gesture is on and data is valid')
return DIR_NONE
# Keep looping as long as gesture data is valid */
while True:
# Wait some time to collect next batch of FIFO data */
sleep(FIFO_PAUSE_TIME)
# Get the contents of the STATUS register. Is data still valid? */
try:
gstatus = self.write_read(APDS9960_GSTATUS, 1)[0]
except:
raise ErrorReadingRegister
# If we have valid data, read in FIFO */
if (gstatus & APDS9960_GVALID) == APDS9960_GVALID:
#Read the current FIFO level
try:
fifo_level = self.write_read(APDS9960_GFLVL, 1)[0]
except:
raise ErrorReadingRegister
# If there's stuff in the FIFO, read it into our data block
self._printDEBUG("FIFO Level: ", fifo_level)
if fifo_level > 0:
try:
fifo_data = self.write_read(APDS9960_GFIFO_U, fifo_level * 4)
except:
raise ErrorReadingRegister
#self._printDEBUG("FIFO data: ", len(fifo_data))
#self._printDEBUG("FIFO Dump: ",fifo_data)
#sleep(1000)
# If at least 1 set of data, sort the data into U/D/L/R */
if len(fifo_data)>=4:
for i in range(0 ,len(fifo_data), 4):
self.gesture_data_.u_data[self.gesture_data_.index]=fifo_data[i + 0]
self.gesture_data_.d_data[self.gesture_data_.index]=fifo_data[i + 1]
self.gesture_data_.l_data[self.gesture_data_.index]=fifo_data[i + 2]
self.gesture_data_.r_data[self.gesture_data_.index]=fifo_data[i + 3]
self.gesture_data_.index+=1
self.gesture_data_.total_gestures+=1
self._printDEBUG("Finding First:","U:",fifo_data[i + 0],"D:",fifo_data[i + 1],"L:",fifo_data[i + 2],"R:",fifo_data[i + 3])
#self._printDEBUG("total_gestures: ", self.gesture_data_.total_gestures)
# # Filter and process gesture data. Decode near/far state */
if self._processGestureData():
if self._decodeGesture():
self._printDEBUG()
# Reset data */
self.gesture_data_.index = 0
self.gesture_data_.total_gestures = 0
# self.gesture_data_.u_data=[]
# self.gesture_data_.d_data=[]
# self.gesture_data_.l_data=[]
# self.gesture_data_.r_data=[]
else:
#Determine best guessed gesture and clean up */
sleep(FIFO_PAUSE_TIME)
if not self._decodeGesture():
self._printDEBUG('return decode False')
motion = self.gesture_motion_
self._printDEBUG("END: ")
self._printDEBUG(self.gesture_motion_)
self._printDEBUG(gstatus)
self._resetGestureParameters()
return motion
def enablePower(self):
"""
.. method::enablePower()
Turn the APDS-9960 on
"""
self.setMode(POWER, 1)
def disablePower(self):
"""
.. method::disablePower()
Turn the APDS-9960 off
"""
self.setMode(POWER, 0)
# #******************************************************************************
# * Ambient light and color sensor controls
# ******************************************************************************/
def readAmbientLight(self):
"""
.. method:: readAmbientLight()
Reads the ambient (clear) light level as a 16-bit value
return:
the value of the light sensor.
"""
try:
valLow = self.write_read(APDS9960_CDATAL, 1)[0]
valHight = self.write_read(APDS9960_CDATAH, 1)[0]
except:
raise ErrorReadingRegister
return valLow + (valHight << 8)
def readRedLight(self):
"""
.. method:: readRedLight()
Reads the red light level as a 16-bit value
return:
the value of the light sensor.
"""
try:
valLow = self.write_read(APDS9960_RDATAL, 1)[0]
valHight = self.write_read(APDS9960_RDATAH, 1)[0]
except:
raise ErrorReadingRegister
return valLow + (valHight << 8)
def readGreenLight(self):
"""
.. method:: readGreenLight()
Reads the red light level as a 16-bit value
return:
the value of the light sensor.
"""
try:
valLow = self.write_read(APDS9960_GDATAL, 1)[0]
valHight = self.write_read(APDS9960_GDATAH, 1)[0]
except:
raise ErrorReadingRegister
return valLow + (valHight << 8)
def readBlueLight(self):
"""
.. method:: readBlueLight()
Reads the red light level as a 16-bit value
return:
the value of the light sensor.
"""
try:
valLow = self.write_read(APDS9960_BDATAL, 1)[0]
valHight = self.write_read(APDS9960_BDATAH, 1)[0]
except:
raise ErrorReadingRegister
return valLow + (valHight << 8)
# ******************************************************************************
# * Proximity sensor controls
# ******************************************************************************/
def readProximity(self):
"""
.. method:: readProximity()
Reads the proximity level as an 8-bit value
return:
the value of the proximity sensor.
"""
try:
val = self.write_read(APDS9960_PDATA, 1)[0]
except:
raise ErrorReadingRegister
return val
def _processGestureData(self):
u_first = 0
d_first = 0
l_first = 0
r_first = 0
u_last = 0
d_last = 0
l_last = 0
r_last = 0
# If we have less than 4 total gestures, that's not enough */
if self.gesture_data_.total_gestures <= 4:
self._printDEBUG('Tot_Gest:',self.gesture_data_.total_gestures)
return False
# Check to make sure our data isn't out of bounds */
if self.gesture_data_.total_gestures <= 32 and self.gesture_data_.total_gestures > 0:
# Find the first value in U/D/L/R above the threshold */
for i in range(0, self.gesture_data_.total_gestures):
if (self.gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) and (self.gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) and (self.gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) and (self.gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT):
u_first = self.gesture_data_.u_data[i]
d_first = self.gesture_data_.d_data[i]
l_first = self.gesture_data_.l_data[i]
r_first = self.gesture_data_.r_data[i]
break
self._printDEBUG("Fist Values:","U:",u_first,"D:",d_first,"L:",l_first,"R:",r_first)
# If one of the _first values is 0, then there is no good data */
if (u_first == 0) or (d_first == 0) or (l_first == 0) or (r_first == 0):
return False
# Find the last value in U/D/L/R above the threshold */
#for( i = gesture_data_.total_gestures - 1 i >= 0 i-- )
l=range(self.gesture_data_.total_gestures)
l=l[::-1]
for i in l:
if (self.gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) and (self.gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) and (self.gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) and (self.gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) :
u_last = self.gesture_data_.u_data[i]
d_last = self.gesture_data_.d_data[i]
l_last = self.gesture_data_.l_data[i]
r_last = self.gesture_data_.r_data[i]
break
# Calculate the first vs. last ratio of up/down and left/right */
ud_ratio_first = ((u_first - d_first) * 100) / (u_first + d_first)
lr_ratio_first = ((l_first - r_first) * 100) / (l_first + r_first)
ud_ratio_last = ((u_last - d_last) * 100) / (u_last + d_last)
lr_ratio_last = ((l_last - r_last) * 100) / (l_last + r_last)
self._printDEBUG("Last Values:","U:", u_last,"D:",d_last,"L:", l_last,"R:", r_last)
self._printDEBUG("Ratios:","UD First:",ud_ratio_first,"UD Last:" , ud_ratio_last,"LR Fi:", lr_ratio_first,"LR La:", lr_ratio_last)
# Determine the difference between the first and last ratios */
ud_delta = ud_ratio_last - ud_ratio_first;
lr_delta = lr_ratio_last - lr_ratio_first;
self._printDEBUG("Deltas:","UD: " ,ud_delta,"LR: " , lr_delta)
#Accumulate the UD and LR delta values */
self.gesture_ud_delta_ += ud_delta;
self.gesture_lr_delta_ += lr_delta;
self._printDEBUG("Accumulations:","UD:" , self.gesture_ud_delta_,"LR:", self.gesture_lr_delta_)
# Determine U/D gesture */
if self.gesture_ud_delta_ >= GESTURE_SENSITIVITY_1:
self.gesture_ud_count_ = 1
elif self.gesture_ud_delta_ <= -GESTURE_SENSITIVITY_1:
self.gesture_ud_count_ = -1
else:
self.gesture_ud_count_ = 0
# Determine L/R gesture */
if self.gesture_lr_delta_ >= GESTURE_SENSITIVITY_1:
self.gesture_lr_count_ = 1
elif self.gesture_lr_delta_ <= -GESTURE_SENSITIVITY_1:
self.gesture_lr_count_ = -1
else:
self.gesture_lr_count_ = 0
# Determine Near/Far gesture */
if (self.gesture_ud_count_ == 0) and (self.gesture_lr_count_ == 0):
if (abs(ud_delta) < GESTURE_SENSITIVITY_2) and (abs(lr_delta) < GESTURE_SENSITIVITY_2):
if (ud_delta == 0) and (lr_delta == 0):
self.gesture_near_count_+=1
elif ud_delta != 0 or lr_delta != 0:
self.gesture_far_count_+=1
if (self.gesture_near_count_ >= 10) and (self.gesture_far_count_ >= 2):
if (ud_delta == 0) and (lr_delta == 0):
self.gesture_state_ = NEAR_STATE
elif ud_delta != 0 and lr_delta != 0:
self.gesture_state_ = FAR_STATE
return True
else:
if (abs(ud_delta) < GESTURE_SENSITIVITY_2) and (abs(lr_delta) < GESTURE_SENSITIVITY_2):
if (ud_delta == 0) and (lr_delta == 0):
self.gesture_near_count_+=1
if self.gesture_near_count_ >= 10:
self.gesture_ud_count_ = 0
self.gesture_lr_count_ = 0
self.gesture_ud_delta_ = 0
self.gesture_lr_delta_ = 0
#self._printDEBUG("UD_CT: " , self.gesture_ud_count_,"LR_CT:", self.gesture_lr_count_,"NEAR_CT:", self.gesture_near_count_,"FAR_CT:", self.gesture_far_count_)
self._printDEBUG("----------")
return False
def _decodeGesture(self):
# Determines swipe direction or near/far state
#return True if near/far event. False otherwise.
self._printDEBUG('gesture_ud_delta_',self.gesture_ud_delta_)
self._printDEBUG('gesture_lr_delta_',self.gesture_lr_delta_)
self._printDEBUG('gesture_ud_count_',self.gesture_ud_count_)
self._printDEBUG('gesture_lr_count_',self.gesture_lr_count_)
self._printDEBUG('gesture_near_count_',self.gesture_near_count_)
self._printDEBUG('gesture_far_count_',self.gesture_far_count_)
self._printDEBUG('gesture_state_',self.gesture_state_)
try:
# Return if near or far event is detected */
if self.gesture_state_ == NEAR_STATE:
self.gesture_motion_ = DIR_NEAR
return True
elif self.gesture_state_ == FAR_STATE:
self.gesture_motion_ = DIR_FAR
return True
# Determine swipe direction */
if (self.gesture_ud_count_ == -1) and (self.gesture_lr_count_ == 0):
self.gesture_motion_ = DIR_UP
elif (self.gesture_ud_count_ == 1) and (self.gesture_lr_count_ == 0):
self.gesture_motion_ = DIR_DOWN
elif (self.gesture_ud_count_ == 0) and (self.gesture_lr_count_ == 1):
self.gesture_motion_ = DIR_RIGHT
elif (self.gesture_ud_count_ == 0) and (self.gesture_lr_count_ == -1):
self.gesture_motion_ = DIR_LEFT
elif (self.gesture_ud_count_ == -1) and (self.gesture_lr_count_ == 1):
if abs(self.gesture_ud_delta_) > abs(self.gesture_lr_delta_):
self.gesture_motion_ = DIR_UP
else:
self.gesture_motion_ = DIR_RIGHT
elif (self.gesture_ud_count_ == 1) and (self.gesture_lr_count_ == -1):
if abs(self.gesture_ud_delta_) > abs(self.gesture_lr_delta_):
self.gesture_motion_ = DIR_DOWN
else:
self.gesture_motion_ = DIR_LEFT
elif (self.gesture_ud_count_ == -1) and (self.gesture_lr_count_ == -1):
if abs(self.gesture_ud_delta_) > abs(self.gesture_lr_delta_):
self.gesture_motion_ = DIR_UP
else:
self.gesture_motion_ = DIR_LEFT
elif (self.gesture_ud_count_ == 1) and (self.gesture_lr_count_ == 1):
if abs(self.gesture_ud_delta_) > abs(self.gesture_lr_delta_):
self.gesture_motion_ = DIR_DOWN
else:
self.gesture_motion_ = DIR_RIGHT
else:
self.gesture_motion_ = DIR_NONE
return False
return True
except Exception as e:
print(e)
return False
def _resetGestureParameters(self):
#Resets all the parameters in the gesture data member
self.gesture_data_.index = 0
self.gesture_data_.total_gestures = 0
self.gesture_ud_delta_ = 0
self.gesture_lr_delta_ = 0
self.gesture_ud_count_ = 0
self.gesture_lr_count_ = 0
self.gesture_near_count_ = 0
self.gesture_far_count_ = 0
self.gesture_state_ = 0