Skip to content

Commit c701c66

Browse files
committed
Tidied up
1 parent 2f32581 commit c701c66

3 files changed

Lines changed: 30 additions & 131 deletions

File tree

python/VL53L1X.py

Lines changed: 17 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -22,56 +22,16 @@
2222
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
# SOFTWARE.
2424
from 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
3826
import 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)

python_lib/vl53l1x_python.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,11 @@ VL53L1_DEV *initialise(uint8_t i2c_address)
7171
memset(dev, 0, sizeof(VL53L1_Dev_t));
7272

7373
dev->I2cDevAddr = i2c_address;
74-
printf("software_reset\n");
7574
Status = VL53L1_software_reset(dev);
76-
printf("WaitDeviceBooted\n");
7775
Status = VL53L1_WaitDeviceBooted(dev);
78-
printf("DataInit\n");
7976
Status = VL53L1_DataInit(dev);
8077
Status = VL53L1_StaticInit(dev);
81-
printf("Status: %d\n", Status);
8278
//if(Status == VL53L1_ERROR_NONE){
83-
printf("GetDeviceInfo\n");
8479
Status = VL53L1_GetDeviceInfo(dev, &DeviceInfo);
8580
if(Status == VL53L1_ERROR_NONE){
8681
printf("VL53L0X_GetDeviceInfo:\n");
@@ -95,6 +90,14 @@ VL53L1_DEV *initialise(uint8_t i2c_address)
9590
return dev;
9691
}
9792

93+
VL53L1_Error setDeviceAddress(VL53L1_Dev_t *dev, int i2c_address)
94+
{
95+
printf("Set addr: %x", i2c_address);
96+
VL53L1_Error Status = VL53L1_SetDeviceAddress(dev, i2c_address << 1);
97+
dev->I2cDevAddr = i2c_address;
98+
return Status;
99+
}
100+
98101
/******************************************************************************
99102
* @brief Start Ranging
100103
* @param mode - ranging mode
@@ -119,9 +122,9 @@ VL53L1_DEV *initialise(uint8_t i2c_address)
119122
VL53L1_Error startRanging(VL53L1_Dev_t *dev, int mode)
120123
{
121124
VL53L1_Error Status = VL53L1_ERROR_NONE;
122-
Status = VL53L1_SetDistanceMode(dev, VL53L1_DISTANCEMODE_LONG);
125+
Status = VL53L1_SetDistanceMode(dev, mode);
123126
Status = VL53L1_SetMeasurementTimingBudgetMicroSeconds(dev, 66000);
124-
Status = VL53L1_SetInterMeasurementPeriodMilliSeconds(dev, 50);
127+
Status = VL53L1_SetInterMeasurementPeriodMilliSeconds(dev, 10);
125128
Status = VL53L1_StartMeasurement(dev);
126129
return Status;
127130
}
@@ -136,7 +139,6 @@ int32_t getDistance(VL53L1_Dev_t *dev)
136139
int32_t current_distance = -1;
137140
Status = VL53L1_WaitMeasurementDataReady(dev);
138141
Status = VL53L1_GetRangingMeasurementData(dev, pRangingMeasurementData);
139-
printf("Range Status: %d, Command Status: %d\n", pRangingMeasurementData->RangeStatus, Status);
140142
current_distance = pRangingMeasurementData->RangeMilliMeter;
141143
VL53L1_ClearInterruptAndStartMeasurement(dev);
142144
return current_distance;

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
'python_lib/vl53l1x_python.c'])
2222

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

0 commit comments

Comments
 (0)