23
23
from myDevices .devices .i2c import I2C
24
24
from myDevices .devices .sensor import Luminosity , Distance
25
25
from myDevices .utils .types import toint
26
- from myDevices .utils .logger import debug
26
+ from myDevices .utils .logger import debug
27
27
28
28
class VCNL4000 (I2C , Luminosity , Distance ):
29
29
REG_COMMAND = 0x80
@@ -52,8 +52,10 @@ class VCNL4000(I2C, Luminosity, Distance):
52
52
MASK_PROX_READY = 0b00100000
53
53
MASK_AMB_READY = 0b01000000
54
54
55
- def __init__ (self , slave = 0b0010011 , current = 20 , frequency = 781 , prox_threshold = 15 , prox_cycles = 10 , cal_cycles = 5 ):
56
- I2C .__init__ (self , toint (slave ))
55
+ def __init__ (self , slave = 0b0010011 , current = 20 , frequency = 781 , prox_threshold = 15 , prox_cycles = 10 , cal_cycles = 5 , luminosity = True , distance = True ):
56
+ self .luminosity = luminosity
57
+ self .distance = distance
58
+ I2C .__init__ (self , toint (slave ), True )
57
59
self .setCurrent (toint (current ))
58
60
self .setFrequency (toint (frequency ))
59
61
self .prox_threshold = toint (prox_threshold )
@@ -68,7 +70,12 @@ def __str__(self):
68
70
return "VCNL4000(slave=0x%02X)" % self .slave
69
71
70
72
def __family__ (self ):
71
- return [Luminosity .__family__ (self ), Distance .__family__ (self )]
73
+ family = []
74
+ if self .luminosity :
75
+ family .append (Luminosity .__family__ (self ))
76
+ if self .distance :
77
+ family .append (Distance .__family__ (self ))
78
+ return family
72
79
73
80
def __setProximityTiming__ (self ):
74
81
self .writeRegister (self .REG_PROX_ADJUST , self .VAL_MOD_TIMING_DEF )
@@ -85,12 +92,10 @@ def calibrate(self):
85
92
self .offset = self .__measureOffset__ ()
86
93
debug ("VCNL4000: offset = %d" % (self .offset ))
87
94
return self .offset
88
-
89
95
90
96
def setCurrent (self , current ):
91
97
self .current = current
92
98
self .__setCurrent__ ()
93
-
94
99
95
100
def getCurrent (self ):
96
101
return self .__getCurrent__ ()
@@ -141,9 +146,20 @@ def __getCurrent__(self):
141
146
return bits_current * 10
142
147
143
148
def __getLux__ (self ):
144
- self .writeRegister (self .REG_COMMAND , self .VAL_START_AMB )
145
- while not (self .readRegister (self .REG_COMMAND ) & self .MASK_AMB_READY ):
146
- time .sleep (0.001 )
149
+ count = 0
150
+ while count < 5 :
151
+ # This try catch loop is a hacky fix for issues when making an initial ambient light reading
152
+ # using a VCNL4010 sensor. It should be removed when true VCNL4010 support is added.
153
+ try :
154
+ self .writeRegister (self .REG_COMMAND , self .VAL_START_AMB )
155
+ while not (self .readRegister (self .REG_COMMAND ) & self .MASK_AMB_READY ):
156
+ time .sleep (0.001 )
157
+ count = 10
158
+ except OSError as ex :
159
+ count = count + 1
160
+ time .sleep (0.05 )
161
+ if count >= 5 :
162
+ raise ex
147
163
light_bytes = self .readRegisters (self .REG_AMB_RESULT_HIGH , 2 )
148
164
light_word = light_bytes [0 ] << 8 | light_bytes [1 ]
149
165
return self .__calculateLux__ (light_word )
@@ -206,6 +222,19 @@ def __readProximityCounts__(self):
206
222
while not (self .readRegister (self .REG_COMMAND ) & self .MASK_PROX_READY ):
207
223
time .sleep (0.001 )
208
224
proximity_bytes = self .readRegisters (self .REG_PROX_RESULT_HIGH , 2 )
209
- debug ("VCNL4000: prox raw value = %d" % (proximity_bytes [0 ] << 8 | proximity_bytes [1 ]))
225
+ debug ("VCNL4000: prox raw value = %d" % (proximity_bytes [0 ] << 8 | proximity_bytes [1 ]))
210
226
return (proximity_bytes [0 ] << 8 | proximity_bytes [1 ])
211
-
227
+
228
+ class VCNL4000_LUMINOSITY (VCNL4000 ):
229
+ def __init__ (self ):
230
+ VCNL4000 .__init__ (self , luminosity = True , distance = False )
231
+
232
+ def __str__ (self ):
233
+ return "VCNL4000_LUMINOSITY"
234
+
235
+ class VCNL4000_DISTANCE (VCNL4000 ):
236
+ def __init__ (self ):
237
+ VCNL4000 .__init__ (self , luminosity = False , distance = True )
238
+
239
+ def __str__ (self ):
240
+ return "VCNL4000_DISTANCE"
0 commit comments