Skip to content

Commit d076e99

Browse files
author
Ramsay, Grant (NZ)
committed
Merge branch 'feat/add-pip-setup'
2 parents 5239233 + ac7e350 commit d076e99

4 files changed

Lines changed: 100 additions & 25 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
.idea

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ Notes on Multiple sensor support:
3131
Notes on using TCA9548A I2C Multiplexer:
3232
- If limited on GPIO's that would be needed to set a new addresses for each sensor, using a TCA9548A I2C Multiplexer is a good option since it allows using up to 8 sensors without using GPIO's.
3333
- The TCA9548A is also a good option if using multiple boards on the same I2C bus and the total of all the combined I2C pullups would cause the bus not to function.
34-
- Theoretically you can connect mutltiple TCA9548A Multiplexers, each with up to 8 sensors as long each TCA9548A has a different address. This has not been tested but should work in theory.
34+
- Theoretically you can connect multiple TCA9548A Multiplexers, each with up to 8 sensors as long each TCA9548A has a different address. This has not been tested but should work in theory.
3535

3636
(Please note that while the author is an embedded software engineer, this is a first attempt at extending python and the author is by no means a python expert so any improvement suggestions are appreciated).
3737

3838

3939
### Installation
40-
40+
```bash
41+
# Python2
42+
pip2 install git+https://github.com/johnbryanmoore/VL53L0X_rasp_python.git
43+
# Python3
44+
pip3 install git+https://github.com/johnbryanmoore/VL53L0X_rasp_python.git
45+
```
4146

4247
### Compilation
4348

python/VL53L0X.py

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,61 @@
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
2624
from ctypes import *
27-
import smbus
25+
import 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

3534
i2cbus = smbus.SMBus(1)
3635

36+
3737
# i2c bus read callback
3838
def 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
5455
def 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-
tof_lib = CDLL("../bin/vl53l0x_python.so")
68+
69+
# Load VL53L0X shared lib
70+
_possible_lib_locations = site.getsitepackages() + ['../bin']
71+
for lib_location in _possible_lib_locations:
72+
try:
73+
tof_lib = CDLL(lib_location + "/vl53l0x_python.so")
74+
break
75+
except OSError:
76+
pass
77+
else:
78+
raise OSError('Could not find vl53l0x_python.so')
6979

7080
# Create read function pointer
7181
READFUNC = CFUNCTYPE(c_int, c_ubyte, c_ubyte, POINTER(c_ubyte), c_ubyte)
@@ -78,22 +88,24 @@ def i2c_write(address, reg, data_p, length):
7888
# pass i2c read and write function pointers to VL53L0X library
7989
tof_lib.VL53L0X_set_i2c(read_func, write_func)
8090

91+
8192
class VL53L0X(object):
8293
"""VL53L0X ToF."""
8394

8495
object_number = 0
8596

86-
def __init__(self, address=0x29, TCA9548A_Num=255, TCA9548A_Addr=0, **kwargs):
97+
def __init__(self, address=0x29, TCA9548A_Num=255, TCA9548A_Addr=0):
8798
"""Initialize the VL53L0X ToF Sensor from ST"""
8899
self.device_address = address
89100
self.TCA9548A_Device = TCA9548A_Num
90101
self.TCA9548A_Address = TCA9548A_Addr
91102
self.my_object_number = VL53L0X.object_number
92103
VL53L0X.object_number += 1
93104

94-
def start_ranging(self, mode = VL53L0X_GOOD_ACCURACY_MODE):
105+
def start_ranging(self, mode=VL53L0X_GOOD_ACCURACY_MODE):
95106
"""Start VL53L0X ToF Sensor Ranging"""
96-
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)
97109

98110
def stop_ranging(self):
99111
"""Stop VL53L0X ToF Sensor Ranging"""
@@ -106,12 +118,11 @@ def get_distance(self):
106118
# This function included to show how to access the ST library directly
107119
# from python instead of through the simplified interface
108120
def get_timing(self):
109-
Dev = POINTER(c_void_p)
110-
Dev = tof_lib.getDev(self.my_object_number)
121+
dev = tof_lib.getDev(self.my_object_number)
111122
budget = c_uint(0)
112123
budget_p = pointer(budget)
113-
Status = tof_lib.VL53L0X_GetMeasurementTimingBudgetMicroSeconds(Dev, budget_p)
114-
if (Status == 0):
115-
return (budget.value + 1000)
124+
status = tof_lib.VL53L0X_GetMeasurementTimingBudgetMicroSeconds(dev, budget_p)
125+
if status == 0:
126+
return budget.value + 1000
116127
else:
117128
return 0

setup.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from distutils.core import setup, Extension
2+
from distutils.command.build_ext import build_ext
3+
4+
5+
# Found this on stack overflow:
6+
# https://stackoverflow.com/questions/4529555/building-a-ctypes-based-c-library-with-distutils
7+
# noinspection PyPep8Naming
8+
class build_ext(build_ext):
9+
10+
def build_extension(self, ext):
11+
# noinspection PyAttributeOutsideInit
12+
self._ctypes = isinstance(ext, CTypesExtension)
13+
return super().build_extension(ext)
14+
15+
def get_export_symbols(self, ext):
16+
if self._ctypes:
17+
return ext.export_symbols
18+
return super().get_export_symbols(ext)
19+
20+
def get_ext_filename(self, ext_name):
21+
if self._ctypes:
22+
return ext_name + '.so'
23+
return super().get_ext_filename(ext_name)
24+
25+
26+
class CTypesExtension(Extension):
27+
pass
28+
29+
30+
extension = CTypesExtension(
31+
'vl53l0x_python',
32+
define_macros=[],
33+
include_dirs=['.', 'Api/core/inc', 'platform/inc'],
34+
libraries=[],
35+
library_dirs=[],
36+
sources=['Api/core/src/vl53l0x_api_calibration.c',
37+
'Api/core/src/vl53l0x_api_core.c',
38+
'Api/core/src/vl53l0x_api_ranging.c',
39+
'Api/core/src/vl53l0x_api_strings.c',
40+
'Api/core/src/vl53l0x_api.c',
41+
'platform/src/vl53l0x_platform.c',
42+
'python_lib/vl53l0x_python.c'])
43+
44+
setup(name='VL53L0X_rasp_python',
45+
version='1.0.2',
46+
description='VL53L0X sensor for raspberry PI',
47+
# author='?',
48+
# author_email='?',
49+
url='https://github.com/johnbryanmoore/VL53L0X_rasp_python',
50+
long_description='''
51+
VL53L0X sensor for raspberry PI.
52+
''',
53+
ext_modules=[extension],
54+
package_dir={'': 'python'},
55+
py_modules=['VL53L0X'],
56+
requires=['smbus2'],
57+
cmdclass={'build_ext': build_ext})

0 commit comments

Comments
 (0)