2222# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323# SOFTWARE.
2424from ctypes import CDLL , CFUNCTYPE , POINTER , c_int , c_uint , pointer , c_ubyte , c_uint8 , c_uint32
25- import pkg_resources
26- SMBUS = 'smbus'
27- for dist in pkg_resources .working_set :
28- #print(dist.project_name, dist.version)
29- if dist .project_name == 'smbus' :
30- break
31- if dist .project_name == 'smbus2' :
32- SMBUS = 'smbus2'
33- break
34- if SMBUS == 'smbus' :
35- import smbus
36- elif SMBUS == 'smbus2' :
37- import smbus2 as smbus
25+ from smbus2 import SMBus , i2c_msg
3826import site
3927
40-
41- class Vl53l0xError (RuntimeError ):
28+ class VL53L1xError (RuntimeError ):
4229 pass
4330
44-
45- class Vl53l0xAccuracyMode :
46- GOOD = 0 # 33 ms timing budget 1.2m range
47- BETTER = 1 # 66 ms timing budget 1.2m range
48- BEST = 2 # 200 ms 1.2m range
49- LONG_RANGE = 3 # 33 ms timing budget 2m range
50- HIGH_SPEED = 4 # 20 ms timing budget 1.2m range
51-
52-
53- class Vl53l0xDeviceMode :
54- SINGLE_RANGING = 0
55- CONTINUOUS_RANGING = 1
56- SINGLE_HISTOGRAM = 2
57- CONTINUOUS_TIMED_RANGING = 3
58- SINGLE_ALS = 10
59- GPIO_DRIVE = 20
60- GPIO_OSC = 21
61-
62-
63- class Vl53l0xGpioAlarmType :
64- OFF = 0
65- THRESHOLD_CROSSED_LOW = 1
66- THRESHOLD_CROSSED_HIGH = 2
67- THRESHOLD_CROSSED_OUT = 3
68- NEW_MEASUREMENT_READY = 4
69-
70-
71- class Vl53l0xInterruptPolarity :
72- LOW = 0
73- HIGH = 1
74-
31+ class VL53L1xDistanceMode :
32+ SHORT = 1
33+ MEDIUM = 2
34+ LONG = 3
7535
7636# Read/write function pointer types.
7737_I2C_READ_FUNC = CFUNCTYPE (c_int , c_ubyte , c_ubyte , POINTER (c_ubyte ), c_ubyte )
@@ -97,7 +57,7 @@ def __init__(self, i2c_bus=1, i2c_address=0x29, tca9548a_num=255, tca9548a_addr=
9757 self .i2c_address = i2c_address
9858 self ._tca9548a_num = tca9548a_num
9959 self ._tca9548a_addr = tca9548a_addr
100- self ._i2c = smbus . SMBus ()
60+ self ._i2c = SMBus (1 )
10161 self ._dev = None
10262 # Resgiter Address
10363 self .ADDR_UNIT_ID_HIGH = 0x16 # Serial number high byte
@@ -119,22 +79,15 @@ def _configure_i2c_library_functions(self):
11979 # I2C bus read callback for low level library.
12080 def _i2c_read (address , reg , data_p , length ):
12181 ret_val = 0
122- result = []
123-
124- print ("Read {} bytes from {}." .format (length , reg ))
12582
126- try :
127- for i in range (0 , length , 32 ):
128- read_length = min (length - i , 32 )
129- result += self ._i2c .read_i2c_block_data (address , reg + i , read_length )
130- except IOError :
131- ret_val = - 1
83+ msg_w = i2c_msg .write (address , [reg >> 8 , reg & 0xff ])
84+ msg_r = i2c_msg .read (address , length )
13285
133- print ( result )
86+ self . _i2c . i2c_rdwr ( msg_w , msg_r )
13487
13588 if ret_val == 0 :
13689 for index in range (length ):
137- data_p [index ] = result [index ]
90+ data_p [index ] = ord ( msg_r . buf [index ])
13891
13992 return ret_val
14093
@@ -143,16 +96,12 @@ def _i2c_write(address, reg, data_p, length):
14396 ret_val = 0
14497 data = []
14598
146- print ("Write {} bytes to {}." .format (length , reg ))
147-
14899 for index in range (length ):
149100 data .append (data_p [index ])
150- print (data )
151- try :
152- for i in range (0 , len (data ), 32 ):
153- self ._i2c .write_i2c_block_data (address , reg + i , data [i :i + 32 ])
154- except IOError :
155- ret_val = - 1
101+
102+ msg_w = i2c_msg .write (address , [reg >> 8 , reg & 0xff ] + data )
103+
104+ self ._i2c .i2c_rdwr (msg_w )
156105
157106 return ret_val
158107
@@ -161,7 +110,7 @@ def _i2c_write(address, reg, data_p, length):
161110 self ._i2c_write_func = _I2C_WRITE_FUNC (_i2c_write )
162111 _TOF_LIBRARY .VL53L1_set_i2c (self ._i2c_read_func , self ._i2c_write_func )
163112
164- def start_ranging (self , mode = Vl53l0xAccuracyMode . GOOD ):
113+ def start_ranging (self , mode = VL53L1xDistanceMode . LONG ):
165114 """Start VL53L1X ToF Sensor Ranging"""
166115 _TOF_LIBRARY .startRanging (self ._dev , mode )
167116
@@ -184,57 +133,5 @@ def get_timing(self):
184133 else :
185134 return 0
186135
187- def configure_gpio_interrupt (
188- self , proximity_alarm_type = Vl53l0xGpioAlarmType .THRESHOLD_CROSSED_LOW ,
189- interrupt_polarity = Vl53l0xInterruptPolarity .HIGH , threshold_low_mm = 250 , threshold_high_mm = 500 ):
190- """
191- Configures a GPIO interrupt from device, be sure to call "clear_interrupt" after interrupt is processed.
192- """
193- pin = c_uint8 (0 ) # 0 is only GPIO pin.
194- device_mode = c_uint8 (Vl53l0xDeviceMode .CONTINUOUS_RANGING )
195- functionality = c_uint8 (proximity_alarm_type )
196- polarity = c_uint8 (interrupt_polarity )
197- status = _TOF_LIBRARY .VL53L1X_SetGpioConfig (self ._dev , pin , device_mode , functionality , polarity )
198- if status != 0 :
199- raise Vl53l0xError ('Error setting VL53L1X GPIO config' )
200-
201- threshold_low = c_uint32 (threshold_low_mm << 16 )
202- threshold_high = c_uint32 (threshold_high_mm << 16 )
203- status = _TOF_LIBRARY .VL53L1X_SetInterruptThresholds (self ._dev , device_mode , threshold_low , threshold_high )
204- if status != 0 :
205- raise Vl53l0xError ('Error setting VL53L1X thresholds' )
206-
207- # Ensure any pending interrupts are cleared.
208- self .clear_interrupt ()
209-
210- def clear_interrupt (self ):
211- mask = c_uint32 (0 )
212- status = _TOF_LIBRARY .VL53L1X_ClearInterruptMask (self ._dev , mask )
213- if status != 0 :
214- raise Vl53l0xError ('Error clearing VL53L1X interrupt' )
215-
216136 def change_address (self , new_address ):
217- if self ._dev is not None :
218- raise Vl53l0xError ('Error changing VL53L1X address' )
219-
220- self ._i2c .open (bus = self ._i2c_bus )
221-
222- if new_address == None :
223- return
224- elif new_address == self .i2c_address :
225- return
226- else :
227- # read value from 0x16,0x17
228- high = self ._i2c .read_byte_data (self .i2c_address , self .ADDR_UNIT_ID_HIGH )
229- low = self ._i2c .read_byte_data (self .i2c_address , self .ADDR_UNIT_ID_LOW )
230-
231- # write value to 0x18,0x19
232- self ._i2c .write_byte_data (self .i2c_address , self .ADDR_I2C_ID_HIGH , high )
233- self ._i2c .write_byte_data (self .i2c_address , self .ADDR_I2C_ID_LOW , low )
234-
235- # write new_address to 0x1a
236- self ._i2c .write_byte_data (self .i2c_address , self .ADDR_I2C_SEC_ADDR , new_address )
237-
238- self .i2c_address = new_address
239-
240- self ._i2c .close ()
137+ _TOF_LIBRARY .setDeviceAddress (self ._dev , new_address )
0 commit comments