2727import site
2828import glob
2929
30+
3031class VL53L1xError (RuntimeError ):
3132 pass
3233
34+
3335class VL53L1xDistanceMode :
36+ NONE = 0
3437 SHORT = 1
3538 MEDIUM = 2
3639 LONG = 3
3740
41+
3842# Read/write function pointer types.
3943_I2C_MULTI_FUNC = CFUNCTYPE (c_int , c_ubyte , c_ubyte )
4044_I2C_READ_FUNC = CFUNCTYPE (c_int , c_ubyte , c_ubyte , POINTER (c_ubyte ), c_ubyte )
@@ -59,10 +63,10 @@ class VL53L1xDistanceMode:
5963 lib_file = files [0 ]
6064 try :
6165 _TOF_LIBRARY = CDLL (lib_file )
62- #print("Using: " + lib_location + "/vl51l1x_python.so")
66+ # print("Using: " + lib_location + "/vl51l1x_python.so")
6367 break
6468 except OSError :
65- #print(lib_location + "/vl51l1x_python.so not found")
69+ # print(lib_location + "/vl51l1x_python.so not found")
6670 pass
6771else :
6872 raise OSError ('Could not find vl53l1x_python.so' )
@@ -85,11 +89,11 @@ def __init__(self, i2c_bus=1, i2c_address=0x29, tca9548a_num=255, tca9548a_addr=
8589
8690 self ._dev = None
8791 # Register Address
88- self .ADDR_UNIT_ID_HIGH = 0x16 # Serial number high byte
89- self .ADDR_UNIT_ID_LOW = 0x17 # Serial number low byte
90- self .ADDR_I2C_ID_HIGH = 0x18 # Write serial number high byte for I2C address unlock
91- self .ADDR_I2C_ID_LOW = 0x19 # Write serial number low byte for I2C address unlock
92- self .ADDR_I2C_SEC_ADDR = 0x8a # Write new I2C address after unlock
92+ self .ADDR_UNIT_ID_HIGH = 0x16 # Serial number high byte
93+ self .ADDR_UNIT_ID_LOW = 0x17 # Serial number low byte
94+ self .ADDR_I2C_ID_HIGH = 0x18 # Write serial number high byte for I2C address unlock
95+ self .ADDR_I2C_ID_LOW = 0x19 # Write serial number low byte for I2C address unlock
96+ self .ADDR_I2C_SEC_ADDR = 0x8a # Write new I2C address after unlock
9397
9498 def open (self , reset = False ):
9599 self ._i2c .open (bus = self ._i2c_bus )
@@ -149,6 +153,14 @@ def start_ranging(self, mode=VL53L1xDistanceMode.LONG):
149153 """Start VL53L1X ToF Sensor Ranging"""
150154 _TOF_LIBRARY .startRanging (self ._dev , mode )
151155
156+ def set_distance_mode (self , mode ):
157+ """Set distance mode
158+
159+ :param mode: One of 1 = Short, 2 = Medium or 3 = Long
160+
161+ """
162+ _TOF_LIBRARY .setDistanceMode (self ._dev , mode )
163+
152164 def stop_ranging (self ):
153165 """Stop VL53L1X ToF Sensor Ranging"""
154166 _TOF_LIBRARY .stopRanging (self ._dev )
@@ -157,6 +169,33 @@ def get_distance(self):
157169 """Get distance from VL53L1X ToF Sensor"""
158170 return _TOF_LIBRARY .getDistance (self ._dev )
159171
172+ def set_timing (self , timing_budget , inter_measurement_period ):
173+ """Set the timing budget and inter measurement period.
174+
175+ A higher timing budget results in greater measurement accuracy,
176+ but also a higher power consumption.
177+
178+ The inter measurement period must be >= the timing budget, otherwise
179+ it will be double the expected value.
180+
181+ :param timing_budget: Timing budget in microseconds
182+ :param inter_measurement_period: Inter Measurement Period in milliseconds
183+
184+ """
185+ if (inter_measurement_period * 1000 ) < timing_budget :
186+ raise ValueError ("The Inter Measurement Period must be >= Timing Budget" )
187+
188+ self .set_timing_budget (timing_budget )
189+ self .set_inter_measurement_period (inter_measurement_period )
190+
191+ def set_timing_budget (self , timing_budget ):
192+ """Set the timing budget in microseocnds"""
193+ _TOF_LIBRARY .setMeasurementTimingBudgetMicroSeconds (self ._dev , timing_budget )
194+
195+ def set_inter_measurement_period (self , period ):
196+ """Set the inter-measurement period in milliseconds"""
197+ _TOF_LIBRARY .setInterMeasurementPeriodMilliSeconds (self ._dev , period )
198+
160199 # This function included to show how to access the ST library directly
161200 # from python instead of through the simplified interface
162201 def get_timing (self ):
0 commit comments