Skip to content

Commit

Permalink
Tidied up
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Jul 3, 2018
1 parent 2f32581 commit c701c66
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 131 deletions.
137 changes: 17 additions & 120 deletions python/VL53L1X.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,56 +22,16 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from ctypes import CDLL, CFUNCTYPE, POINTER, c_int, c_uint, pointer, c_ubyte, c_uint8, c_uint32
import pkg_resources
SMBUS='smbus'
for dist in pkg_resources.working_set:
#print(dist.project_name, dist.version)
if dist.project_name == 'smbus':
break
if dist.project_name == 'smbus2':
SMBUS='smbus2'
break
if SMBUS == 'smbus':
import smbus
elif SMBUS == 'smbus2':
import smbus2 as smbus
from smbus2 import SMBus, i2c_msg
import site


class Vl53l0xError(RuntimeError):
class VL53L1xError(RuntimeError):
pass


class Vl53l0xAccuracyMode:
GOOD = 0 # 33 ms timing budget 1.2m range
BETTER = 1 # 66 ms timing budget 1.2m range
BEST = 2 # 200 ms 1.2m range
LONG_RANGE = 3 # 33 ms timing budget 2m range
HIGH_SPEED = 4 # 20 ms timing budget 1.2m range


class Vl53l0xDeviceMode:
SINGLE_RANGING = 0
CONTINUOUS_RANGING = 1
SINGLE_HISTOGRAM = 2
CONTINUOUS_TIMED_RANGING = 3
SINGLE_ALS = 10
GPIO_DRIVE = 20
GPIO_OSC = 21


class Vl53l0xGpioAlarmType:
OFF = 0
THRESHOLD_CROSSED_LOW = 1
THRESHOLD_CROSSED_HIGH = 2
THRESHOLD_CROSSED_OUT = 3
NEW_MEASUREMENT_READY = 4


class Vl53l0xInterruptPolarity:
LOW = 0
HIGH = 1

class VL53L1xDistanceMode:
SHORT = 1
MEDIUM = 2
LONG = 3

# Read/write function pointer types.
_I2C_READ_FUNC = CFUNCTYPE(c_int, c_ubyte, c_ubyte, POINTER(c_ubyte), c_ubyte)
Expand All @@ -97,7 +57,7 @@ def __init__(self, i2c_bus=1, i2c_address=0x29, tca9548a_num=255, tca9548a_addr=
self.i2c_address = i2c_address
self._tca9548a_num = tca9548a_num
self._tca9548a_addr = tca9548a_addr
self._i2c = smbus.SMBus()
self._i2c = SMBus(1)
self._dev = None
# Resgiter Address
self.ADDR_UNIT_ID_HIGH = 0x16 # Serial number high byte
Expand All @@ -119,22 +79,15 @@ def _configure_i2c_library_functions(self):
# I2C bus read callback for low level library.
def _i2c_read(address, reg, data_p, length):
ret_val = 0
result = []

print("Read {} bytes from {}.".format(length, reg))

try:
for i in range(0, length, 32):
read_length = min(length - i, 32)
result += self._i2c.read_i2c_block_data(address, reg + i, read_length)
except IOError:
ret_val = -1
msg_w = i2c_msg.write(address, [reg >> 8, reg & 0xff])
msg_r = i2c_msg.read(address, length)

print(result)
self._i2c.i2c_rdwr(msg_w, msg_r)

if ret_val == 0:
for index in range(length):
data_p[index] = result[index]
data_p[index] = ord(msg_r.buf[index])

return ret_val

Expand All @@ -143,16 +96,12 @@ def _i2c_write(address, reg, data_p, length):
ret_val = 0
data = []

print("Write {} bytes to {}.".format(length, reg))

for index in range(length):
data.append(data_p[index])
print(data)
try:
for i in range(0, len(data), 32):
self._i2c.write_i2c_block_data(address, reg + i, data[i:i+32])
except IOError:
ret_val = -1

msg_w = i2c_msg.write(address, [reg >> 8, reg & 0xff] + data)

self._i2c.i2c_rdwr(msg_w)

return ret_val

Expand All @@ -161,7 +110,7 @@ def _i2c_write(address, reg, data_p, length):
self._i2c_write_func = _I2C_WRITE_FUNC(_i2c_write)
_TOF_LIBRARY.VL53L1_set_i2c(self._i2c_read_func, self._i2c_write_func)

def start_ranging(self, mode=Vl53l0xAccuracyMode.GOOD):
def start_ranging(self, mode=VL53L1xDistanceMode.LONG):
"""Start VL53L1X ToF Sensor Ranging"""
_TOF_LIBRARY.startRanging(self._dev, mode)

Expand All @@ -184,57 +133,5 @@ def get_timing(self):
else:
return 0

def configure_gpio_interrupt(
self, proximity_alarm_type=Vl53l0xGpioAlarmType.THRESHOLD_CROSSED_LOW,
interrupt_polarity=Vl53l0xInterruptPolarity.HIGH, threshold_low_mm=250, threshold_high_mm=500):
"""
Configures a GPIO interrupt from device, be sure to call "clear_interrupt" after interrupt is processed.
"""
pin = c_uint8(0) # 0 is only GPIO pin.
device_mode = c_uint8(Vl53l0xDeviceMode.CONTINUOUS_RANGING)
functionality = c_uint8(proximity_alarm_type)
polarity = c_uint8(interrupt_polarity)
status = _TOF_LIBRARY.VL53L1X_SetGpioConfig(self._dev, pin, device_mode, functionality, polarity)
if status != 0:
raise Vl53l0xError('Error setting VL53L1X GPIO config')

threshold_low = c_uint32(threshold_low_mm << 16)
threshold_high = c_uint32(threshold_high_mm << 16)
status = _TOF_LIBRARY.VL53L1X_SetInterruptThresholds(self._dev, device_mode, threshold_low, threshold_high)
if status != 0:
raise Vl53l0xError('Error setting VL53L1X thresholds')

# Ensure any pending interrupts are cleared.
self.clear_interrupt()

def clear_interrupt(self):
mask = c_uint32(0)
status = _TOF_LIBRARY.VL53L1X_ClearInterruptMask(self._dev, mask)
if status != 0:
raise Vl53l0xError('Error clearing VL53L1X interrupt')

def change_address(self, new_address):
if self._dev is not None:
raise Vl53l0xError('Error changing VL53L1X address')

self._i2c.open(bus=self._i2c_bus)

if new_address == None:
return
elif new_address == self.i2c_address:
return
else:
# read value from 0x16,0x17
high = self._i2c.read_byte_data(self.i2c_address, self.ADDR_UNIT_ID_HIGH)
low = self._i2c.read_byte_data(self.i2c_address, self.ADDR_UNIT_ID_LOW)

# write value to 0x18,0x19
self._i2c.write_byte_data(self.i2c_address, self.ADDR_I2C_ID_HIGH, high)
self._i2c.write_byte_data(self.i2c_address, self.ADDR_I2C_ID_LOW, low)

# write new_address to 0x1a
self._i2c.write_byte_data(self.i2c_address, self.ADDR_I2C_SEC_ADDR, new_address)

self.i2c_address = new_address

self._i2c.close()
_TOF_LIBRARY.setDeviceAddress(self._dev, new_address)
18 changes: 10 additions & 8 deletions python_lib/vl53l1x_python.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,11 @@ VL53L1_DEV *initialise(uint8_t i2c_address)
memset(dev, 0, sizeof(VL53L1_Dev_t));

dev->I2cDevAddr = i2c_address;
printf("software_reset\n");
Status = VL53L1_software_reset(dev);
printf("WaitDeviceBooted\n");
Status = VL53L1_WaitDeviceBooted(dev);
printf("DataInit\n");
Status = VL53L1_DataInit(dev);
Status = VL53L1_StaticInit(dev);
printf("Status: %d\n", Status);
//if(Status == VL53L1_ERROR_NONE){
printf("GetDeviceInfo\n");
Status = VL53L1_GetDeviceInfo(dev, &DeviceInfo);
if(Status == VL53L1_ERROR_NONE){
printf("VL53L0X_GetDeviceInfo:\n");
Expand All @@ -95,6 +90,14 @@ VL53L1_DEV *initialise(uint8_t i2c_address)
return dev;
}

VL53L1_Error setDeviceAddress(VL53L1_Dev_t *dev, int i2c_address)
{
printf("Set addr: %x", i2c_address);
VL53L1_Error Status = VL53L1_SetDeviceAddress(dev, i2c_address << 1);
dev->I2cDevAddr = i2c_address;
return Status;
}

/******************************************************************************
* @brief Start Ranging
* @param mode - ranging mode
Expand All @@ -119,9 +122,9 @@ VL53L1_DEV *initialise(uint8_t i2c_address)
VL53L1_Error startRanging(VL53L1_Dev_t *dev, int mode)
{
VL53L1_Error Status = VL53L1_ERROR_NONE;
Status = VL53L1_SetDistanceMode(dev, VL53L1_DISTANCEMODE_LONG);
Status = VL53L1_SetDistanceMode(dev, mode);
Status = VL53L1_SetMeasurementTimingBudgetMicroSeconds(dev, 66000);
Status = VL53L1_SetInterMeasurementPeriodMilliSeconds(dev, 50);
Status = VL53L1_SetInterMeasurementPeriodMilliSeconds(dev, 10);
Status = VL53L1_StartMeasurement(dev);
return Status;
}
Expand All @@ -136,7 +139,6 @@ int32_t getDistance(VL53L1_Dev_t *dev)
int32_t current_distance = -1;
Status = VL53L1_WaitMeasurementDataReady(dev);
Status = VL53L1_GetRangingMeasurementData(dev, pRangingMeasurementData);
printf("Range Status: %d, Command Status: %d\n", pRangingMeasurementData->RangeStatus, Status);
current_distance = pRangingMeasurementData->RangeMilliMeter;
VL53L1_ClearInterruptAndStartMeasurement(dev);
return current_distance;
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
'python_lib/vl53l1x_python.c'])

setup(name='vl53l1x',
version='1.0.4',
description='vl53l1x sensor for raspberry PI/JetsonTX2',
version='0.0.1',
description='vl53l1x distance sensor driver for Raspberry Pi',
# author='?',
# author_email='?',
url='https://github.com/pimoroni/vl53l1x-python',
Expand All @@ -32,4 +32,4 @@
ext_modules=[extension],
package_dir={'': 'python'},
py_modules=['VL53L1X'],
requires=['smbus' or 'smbus2'])
requires=['smbus2'])

0 comments on commit c701c66

Please sign in to comment.