25
25
https://github.com/adafruit/circuitpython/releases
26
26
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
27
27
"""
28
+ import struct
28
29
from micropython import const
29
30
30
31
from adafruit_bus_device import i2c_device
31
32
33
+ try :
34
+ import typing # pylint: disable=unused-import
35
+ from busio import I2C
36
+ except ImportError :
37
+ pass
38
+
32
39
33
40
__version__ = "0.0.0-auto.0"
34
41
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VL6180X.git"
49
56
_VL6180X_REG_RESULT_RANGE_VAL = const (0x062 )
50
57
_VL6180X_REG_RESULT_RANGE_STATUS = const (0x04D )
51
58
_VL6180X_REG_RESULT_INTERRUPT_STATUS_GPIO = const (0x04F )
59
+ _VL6180X_REG_SYSRANGE_PART_TO_PART_RANGE_OFFSET = const (0x024 )
52
60
53
61
# User-facing constants:
54
62
ALS_GAIN_1 = const (0x06 )
@@ -85,15 +93,18 @@ class VL6180X:
85
93
default value will be assumed.
86
94
"""
87
95
88
- def __init__ (self , i2c , address = _VL6180X_DEFAULT_I2C_ADDR ):
96
+ def __init__ (
97
+ self , i2c : I2C , address : int = _VL6180X_DEFAULT_I2C_ADDR , offset : int = 0
98
+ ) -> None :
89
99
self ._device = i2c_device .I2CDevice (i2c , address )
90
100
if self ._read_8 (_VL6180X_REG_IDENTIFICATION_MODEL_ID ) != 0xB4 :
91
101
raise RuntimeError ("Could not find VL6180X, is it connected and powered?" )
92
102
self ._load_settings ()
93
103
self ._write_8 (_VL6180X_REG_SYSTEM_FRESH_OUT_OF_RESET , 0x00 )
104
+ self .offset = offset
94
105
95
106
@property
96
- def range (self ):
107
+ def range (self ) -> int :
97
108
"""Read the range of an object in front of sensor and return it in mm."""
98
109
# wait for device to be ready for range measurement
99
110
while not self ._read_8 (_VL6180X_REG_RESULT_RANGE_STATUS ) & 0x01 :
@@ -109,7 +120,19 @@ def range(self):
109
120
self ._write_8 (_VL6180X_REG_SYSTEM_INTERRUPT_CLEAR , 0x07 )
110
121
return range_
111
122
112
- def read_lux (self , gain ):
123
+ @property
124
+ def offset (self ) -> int :
125
+ """Read and sets the manual offset for the sensor, in millimeters"""
126
+ return self ._offset
127
+
128
+ @offset .setter
129
+ def offset (self , offset : int ) -> None :
130
+ self ._write_8 (
131
+ _VL6180X_REG_SYSRANGE_PART_TO_PART_RANGE_OFFSET , struct .pack ("b" , offset )[0 ]
132
+ )
133
+ self ._offset = offset
134
+
135
+ def read_lux (self , gain : int ) -> float :
113
136
"""Read the lux (light value) from the sensor and return it. Must
114
137
specify the gain value to use for the lux reading:
115
138
- ALS_GAIN_1 = 1x
@@ -164,7 +187,7 @@ def read_lux(self, gain):
164
187
return lux
165
188
166
189
@property
167
- def range_status (self ):
190
+ def range_status (self ) -> int :
168
191
"""Retrieve the status/error from a previous range read. This will
169
192
return a constant value such as:
170
193
@@ -182,7 +205,7 @@ def range_status(self):
182
205
"""
183
206
return self ._read_8 (_VL6180X_REG_RESULT_RANGE_STATUS ) >> 4
184
207
185
- def _load_settings (self ):
208
+ def _load_settings (self ) -> None :
186
209
# private settings from page 24 of app note
187
210
self ._write_8 (0x0207 , 0x01 )
188
211
self ._write_8 (0x0208 , 0x01 )
@@ -238,12 +261,12 @@ def _load_settings(self):
238
261
self ._write_8 (0x0014 , 0x24 ) # Configures interrupt on 'New Sample
239
262
# Ready threshold event'
240
263
241
- def _write_8 (self , address , data ) :
264
+ def _write_8 (self , address : int , data : int ) -> None :
242
265
# Write 1 byte of data from the specified 16-bit register address.
243
266
with self ._device :
244
267
self ._device .write (bytes ([(address >> 8 ) & 0xFF , address & 0xFF , data ]))
245
268
246
- def _write_16 (self , address , data ) :
269
+ def _write_16 (self , address : int , data : int ) -> None :
247
270
# Write a 16-bit big endian value to the specified 16-bit register
248
271
# address.
249
272
with self ._device as i2c :
@@ -258,15 +281,15 @@ def _write_16(self, address, data):
258
281
)
259
282
)
260
283
261
- def _read_8 (self , address ) :
284
+ def _read_8 (self , address : int ) -> int :
262
285
# Read and return a byte from the specified 16-bit register address.
263
286
with self ._device as i2c :
264
287
result = bytearray (1 )
265
288
i2c .write (bytes ([(address >> 8 ) & 0xFF , address & 0xFF ]))
266
289
i2c .readinto (result )
267
290
return result [0 ]
268
291
269
- def _read_16 (self , address ) :
292
+ def _read_16 (self , address : int ) -> int :
270
293
# Read and return a 16-bit unsigned big endian value read from the
271
294
# specified 16-bit register address.
272
295
with self ._device as i2c :
0 commit comments