2121# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2222# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323# SOFTWARE.
24-
25- import time
2624from ctypes import *
2725import smbus2 as smbus
26+ import site
2827
29- VL53L0X_GOOD_ACCURACY_MODE = 0 # Good Accuracy mode
30- VL53L0X_BETTER_ACCURACY_MODE = 1 # Better Accuracy mode
31- VL53L0X_BEST_ACCURACY_MODE = 2 # Best Accuracy mode
32- VL53L0X_LONG_RANGE_MODE = 3 # Longe Range mode
33- VL53L0X_HIGH_SPEED_MODE = 4 # High Speed mode
28+ VL53L0X_GOOD_ACCURACY_MODE = 0 # Good Accuracy mode
29+ VL53L0X_BETTER_ACCURACY_MODE = 1 # Better Accuracy mode
30+ VL53L0X_BEST_ACCURACY_MODE = 2 # Best Accuracy mode
31+ VL53L0X_LONG_RANGE_MODE = 3 # Longe Range mode
32+ VL53L0X_HIGH_SPEED_MODE = 4 # High Speed mode
3433
3534i2cbus = smbus .SMBus (1 )
3635
36+
3737# i2c bus read callback
3838def i2c_read (address , reg , data_p , length ):
39- ret_val = 0 ;
39+ ret_val = 0
4040 result = []
4141
4242 try :
4343 result = i2cbus .read_i2c_block_data (address , reg , length )
4444 except IOError :
45- ret_val = - 1 ;
45+ ret_val = - 1
4646
47- if ( ret_val == 0 ) :
47+ if ret_val == 0 :
4848 for index in range (length ):
4949 data_p [index ] = result [index ]
5050
5151 return ret_val
5252
53+
5354# i2c bus write callback
5455def i2c_write (address , reg , data_p , length ):
55- ret_val = 0 ;
56+ ret_val = 0
5657 data = []
5758
5859 for index in range (length ):
5960 data .append (data_p [index ])
6061 try :
6162 i2cbus .write_i2c_block_data (address , reg , data )
6263 except IOError :
63- ret_val = - 1 ;
64+ ret_val = - 1
6465
6566 return ret_val
6667
67- # Load VL53L0X shared lib
68+
69+ # Load VL53L0X shared lib
6870_possible_lib_locations = site .getsitepackages () + ['../bin' ]
6971for lib_location in _possible_lib_locations :
7072 try :
@@ -86,22 +88,24 @@ def i2c_write(address, reg, data_p, length):
8688# pass i2c read and write function pointers to VL53L0X library
8789tof_lib .VL53L0X_set_i2c (read_func , write_func )
8890
91+
8992class VL53L0X (object ):
9093 """VL53L0X ToF."""
9194
9295 object_number = 0
9396
94- def __init__ (self , address = 0x29 , TCA9548A_Num = 255 , TCA9548A_Addr = 0 , ** kwargs ):
97+ def __init__ (self , address = 0x29 , TCA9548A_Num = 255 , TCA9548A_Addr = 0 ):
9598 """Initialize the VL53L0X ToF Sensor from ST"""
9699 self .device_address = address
97100 self .TCA9548A_Device = TCA9548A_Num
98101 self .TCA9548A_Address = TCA9548A_Addr
99102 self .my_object_number = VL53L0X .object_number
100103 VL53L0X .object_number += 1
101104
102- def start_ranging (self , mode = VL53L0X_GOOD_ACCURACY_MODE ):
105+ def start_ranging (self , mode = VL53L0X_GOOD_ACCURACY_MODE ):
103106 """Start VL53L0X ToF Sensor Ranging"""
104- tof_lib .startRanging (self .my_object_number , mode , self .device_address , self .TCA9548A_Device , self .TCA9548A_Address )
107+ tof_lib .startRanging (self .my_object_number , mode , self .device_address ,
108+ self .TCA9548A_Device , self .TCA9548A_Address )
105109
106110 def stop_ranging (self ):
107111 """Stop VL53L0X ToF Sensor Ranging"""
@@ -114,12 +118,11 @@ def get_distance(self):
114118 # This function included to show how to access the ST library directly
115119 # from python instead of through the simplified interface
116120 def get_timing (self ):
117- Dev = POINTER (c_void_p )
118- Dev = tof_lib .getDev (self .my_object_number )
121+ dev = tof_lib .getDev (self .my_object_number )
119122 budget = c_uint (0 )
120123 budget_p = pointer (budget )
121- Status = tof_lib .VL53L0X_GetMeasurementTimingBudgetMicroSeconds (Dev , budget_p )
122- if ( Status == 0 ) :
123- return ( budget .value + 1000 )
124+ status = tof_lib .VL53L0X_GetMeasurementTimingBudgetMicroSeconds (dev , budget_p )
125+ if status == 0 :
126+ return budget .value + 1000
124127 else :
125128 return 0
0 commit comments