From 77d9a768692ddcf268928c1bd2767babbe20503c Mon Sep 17 00:00:00 2001 From: lucian Date: Wed, 15 Aug 2018 12:54:22 +0300 Subject: [PATCH 1/3] Add __init__ files and use dynamic absolute path to shared lib --- __init__.py | 0 python/VL53L0X.py | 53 +++++++++++++++++++++++++--------------------- python/__init__.py | 0 3 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 __init__.py create mode 100644 python/__init__.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python/VL53L0X.py b/python/VL53L0X.py index 07c35ab..14112a7 100755 --- a/python/VL53L0X.py +++ b/python/VL53L0X.py @@ -21,38 +21,41 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. - -import time -from ctypes import * +import os import smbus +from ctypes import * -VL53L0X_GOOD_ACCURACY_MODE = 0 # Good Accuracy mode -VL53L0X_BETTER_ACCURACY_MODE = 1 # Better Accuracy mode -VL53L0X_BEST_ACCURACY_MODE = 2 # Best Accuracy mode -VL53L0X_LONG_RANGE_MODE = 3 # Longe Range mode -VL53L0X_HIGH_SPEED_MODE = 4 # High Speed mode +ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + +VL53L0X_GOOD_ACCURACY_MODE = 0 # Good Accuracy mode +VL53L0X_BETTER_ACCURACY_MODE = 1 # Better Accuracy mode +VL53L0X_BEST_ACCURACY_MODE = 2 # Best Accuracy mode +VL53L0X_LONG_RANGE_MODE = 3 # Longe Range mode +VL53L0X_HIGH_SPEED_MODE = 4 # High Speed mode i2cbus = smbus.SMBus(1) + # i2c bus read callback def i2c_read(address, reg, data_p, length): - ret_val = 0; + ret_val = 0 result = [] - + try: result = i2cbus.read_i2c_block_data(address, reg, length) except IOError: - ret_val = -1; + ret_val = -1 - if (ret_val == 0): + if ret_val == 0: for index in range(length): data_p[index] = result[index] return ret_val + # i2c bus write callback def i2c_write(address, reg, data_p, length): - ret_val = 0; + ret_val = 0 data = [] for index in range(length): @@ -60,12 +63,13 @@ def i2c_write(address, reg, data_p, length): try: i2cbus.write_i2c_block_data(address, reg, data) except IOError: - ret_val = -1; + ret_val = -1 return ret_val -# Load VL53L0X shared lib -tof_lib = CDLL("../bin/vl53l0x_python.so") + +# Load VL53L0X shared lib +tof_lib = CDLL(os.path.join(ROOT_DIR, "bin/vl53l0x_python.so")) # Create read function pointer READFUNC = CFUNCTYPE(c_int, c_ubyte, c_ubyte, POINTER(c_ubyte), c_ubyte) @@ -78,6 +82,7 @@ def i2c_write(address, reg, data_p, length): # pass i2c read and write function pointers to VL53L0X library tof_lib.VL53L0X_set_i2c(read_func, write_func) + class VL53L0X(object): """VL53L0X ToF.""" @@ -91,10 +96,11 @@ def __init__(self, address=0x29, TCA9548A_Num=255, TCA9548A_Addr=0, **kwargs): self.my_object_number = VL53L0X.object_number VL53L0X.object_number += 1 - def start_ranging(self, mode = VL53L0X_GOOD_ACCURACY_MODE): + def start_ranging(self, mode=VL53L0X_GOOD_ACCURACY_MODE): """Start VL53L0X ToF Sensor Ranging""" - tof_lib.startRanging(self.my_object_number, mode, self.device_address, self.TCA9548A_Device, self.TCA9548A_Address) - + tof_lib.startRanging(self.my_object_number, mode, self.device_address, self.TCA9548A_Device, + self.TCA9548A_Address) + def stop_ranging(self): """Stop VL53L0X ToF Sensor Ranging""" tof_lib.stopRanging(self.my_object_number) @@ -106,12 +112,11 @@ def get_distance(self): # This function included to show how to access the ST library directly # from python instead of through the simplified interface def get_timing(self): - Dev = POINTER(c_void_p) - Dev = tof_lib.getDev(self.my_object_number) + dev = tof_lib.getDev(self.my_object_number) budget = c_uint(0) budget_p = pointer(budget) - Status = tof_lib.VL53L0X_GetMeasurementTimingBudgetMicroSeconds(Dev, budget_p) - if (Status == 0): - return (budget.value + 1000) + status = tof_lib.VL53L0X_GetMeasurementTimingBudgetMicroSeconds(dev, budget_p) + if status == 0: + return budget.value + 1000 else: return 0 diff --git a/python/__init__.py b/python/__init__.py new file mode 100644 index 0000000..e69de29 From 8a5147fb30f59cefa0bb82bfac6b9ef4e2682a5c Mon Sep 17 00:00:00 2001 From: lucian Date: Wed, 15 Aug 2018 13:08:39 +0300 Subject: [PATCH 2/3] Add support for python3 --- Makefile | 2 +- python/VL53L0X.py | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 93c1099..ab026c3 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ INCLUDES = \ -I$(ROOT_DIR)/platform/inc PYTHON_INCLUDES = \ - -I/usr/include/python2.7 + -I/usr/include/python3.5 VPATH = \ $(API_DIR)/core/src \ diff --git a/python/VL53L0X.py b/python/VL53L0X.py index 14112a7..5c1f9c7 100755 --- a/python/VL53L0X.py +++ b/python/VL53L0X.py @@ -60,10 +60,14 @@ def i2c_write(address, reg, data_p, length): for index in range(length): data.append(data_p[index]) - try: - i2cbus.write_i2c_block_data(address, reg, data) - except IOError: - ret_val = -1 + + if data: + try: + i2cbus.write_i2c_block_data(address, reg, data) + except IOError: + ret_val = -1 + except OverflowError: + ret_val = -1 return ret_val From 45f098f4723a32d208110fdb32d6f495147285c5 Mon Sep 17 00:00:00 2001 From: lucian Date: Thu, 30 Aug 2018 22:14:29 +0300 Subject: [PATCH 3/3] Switch to smbus2 --- python/VL53L0X.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/VL53L0X.py b/python/VL53L0X.py index 5c1f9c7..2d8a288 100755 --- a/python/VL53L0X.py +++ b/python/VL53L0X.py @@ -22,7 +22,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import os -import smbus +import smbus2 from ctypes import * ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -33,7 +33,7 @@ VL53L0X_LONG_RANGE_MODE = 3 # Longe Range mode VL53L0X_HIGH_SPEED_MODE = 4 # High Speed mode -i2cbus = smbus.SMBus(1) +i2cbus = smbus2.SMBus(1) # i2c bus read callback