Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
Empty file added __init__.py
Empty file.
65 changes: 37 additions & 28 deletions python/VL53L0X.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,59 @@
# 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
import os
import smbus2
from ctypes import *
import smbus

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 = smbus2.SMBus(1)

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):
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

# 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)
Expand All @@ -78,6 +86,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."""

Expand All @@ -91,10 +100,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)
Expand All @@ -106,12 +116,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
Empty file added python/__init__.py
Empty file.