@@ -233,28 +233,6 @@ def __new__(cls, net_type):
233233 return board
234234
235235
236- class SensorVL6180X :
237- def __new__ (cls ):
238- try : # Python@RPi
239- import busio
240- import adafruit_vl6180x
241- import board
242- # Create I2C bus
243- i2c = busio .I2C (board .SCL , board .SDA )
244- # Create sensor instance.
245- return adafruit_vl6180x .VL6180X (i2c )
246- # You can add an offset to distance measurements here (e.g. calibration)
247- # Swapping for the following would add a +10 millimeter offset to measurements:
248- # sensor = adafruit_vl6180x.VL6180X(i2c, offset=10)
249- except ImportError : # microPython
250- import vl6180x
251- i2c = get_i2c ()
252- return vl6180x .Sensor (i2c , address = 0x29 )
253- except NotImplementedError : # if no I2C device
254- print ("No GPIO present." )
255- sys .exit ()
256-
257-
258236MAX_DEV_NB = 20
259237
260238
@@ -264,11 +242,109 @@ def get_i2c():
264242 for gpio in typical_gpio :
265243 scl , sda = gpio
266244 i2c = None
267- try :
245+ try : # MicroPython 1.19.1 20220618-v1.19.1
268246 i2c = machine .SoftI2C (scl = machine .Pin (scl ), sda = machine .Pin (sda ), freq = 100000 )
269247 if i2c .scan () and len (i2c .scan ()) < MAX_DEV_NB :
270248 return i2c
271- except :
272- pass
249+ except AttributeError : # Pycom MicroPython 1.20.2.r6 [v1.11-c5a0a97] on 2021-10-28
250+ i2c = machine .I2C (0 )
251+ return i2c
273252 del i2c
274253 raise RuntimeError ("No I2C devices found. Check I2C lines." )
254+
255+
256+ class SensorVL6180X :
257+ def __new__ (cls ):
258+ try : # Python@RPi
259+ import busio
260+ import adafruit_vl6180x
261+ import board
262+ i2c = busio .I2C (board .SCL , board .SDA )
263+ return adafruit_vl6180x .VL6180X (i2c )
264+
265+ except ImportError : # microPython
266+ import vl6180x
267+
268+ class ModSensor (vl6180x .Sensor ):
269+ def myRead16 (self , register , nb_bytes = 1 ):
270+ value = int .from_bytes (
271+ self .i2c .readfrom_mem (self ._address , register , nb_bytes , addrsize = 16 ),
272+ 'big'
273+ )
274+ return value & 0xFFFF
275+
276+ def read_lux (self , gain ):
277+ """Read the lux (light value) from the sensor and return it. Must
278+ specify the gain value to use for the lux reading:
279+
280+ ================= =====
281+ Setting Value
282+ ================= =====
283+ ``ALS_GAIN_1`` 1x
284+ ``ALS_GAIN_1_25`` 1.25x
285+ ``ALS_GAIN_1_67`` 1.67x
286+ ``ALS_GAIN_2_5`` 2.5x
287+ ``ALS_GAIN_5`` 5x
288+ ``ALS_GAIN_10`` 10x
289+ ``ALS_GAIN_20`` 20x
290+ ``ALS_GAIN_40`` 40x
291+ ================= =====
292+
293+ :param int gain: The gain value to use
294+
295+ """
296+
297+ reg = self .myRead16 (0x0014 )
298+ reg &= ~ 0x38
299+ reg |= 0x4 << 3 # IRQ on ALS ready
300+ self .myWrite16 (0x0014 , reg )
301+ # 100 ms integration period
302+ self .myWrite16 (0x0040 , 0 )
303+ self .myWrite16 (0x0041 , 100 )
304+ # analog gain
305+ gain = min (gain , 0x07 )
306+ self .myWrite16 (0x003F , 0x40 | gain )
307+ # start ALS
308+ self .myWrite16 (0x0038 , 1 )
309+ # Poll until "New Sample Ready threshold event" is set
310+ while (
311+ (self .myRead16 (0x004F ) >> 3 ) & 0x7
312+ ) != 4 :
313+ pass
314+ # read lux!
315+ lux = self .myRead16 (0x0050 , 2 )
316+ # clear interrupt
317+ self .myWrite16 (0x0015 , 0x07 )
318+ lux *= 0.32 # calibrated count/lux
319+
320+ if gain == 0x06 : # ALS_GAIN_1:
321+ pass
322+ elif gain == 0x05 : # ALS_GAIN_1_25:
323+ lux /= 1.25
324+ elif gain == 0x04 : # ALS_GAIN_1_67:
325+ lux /= 1.67
326+ elif gain == 0x03 : # ALS_GAIN_2_5:
327+ lux /= 2.5
328+ elif gain == 0x02 : # ALS_GAIN_5:
329+ lux /= 5
330+ elif gain == 0x01 : # ALS_GAIN_10:
331+ lux /= 10
332+ elif gain == 0x00 : # ALS_GAIN_20:
333+ lux /= 20
334+ elif gain == 0x07 : # ALS_GAIN_40:
335+ lux /= 40
336+ lux *= 100
337+ lux /= 100 # integration time in ms
338+ return lux
339+
340+ @property
341+ def range (self ):
342+ range_ = super ().range ()
343+ return range_
344+
345+ i2c = get_i2c ()
346+ return ModSensor (i2c , address = 0x29 )
347+
348+ except NotImplementedError : # if no I2C device
349+ print ("No GPIO present." )
350+ sys .exit ()
0 commit comments