Skip to content

Commit bc58642

Browse files
author
Ramsay, Grant (NZ)
committed
Add setup.py
1 parent 5239233 commit bc58642

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

python/VL53L0X.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,15 @@ def i2c_write(address, reg, data_p, length):
6565
return ret_val
6666

6767
# Load VL53L0X shared lib
68-
tof_lib = CDLL("../bin/vl53l0x_python.so")
68+
_possible_lib_locations = site.getsitepackages() + ['../bin']
69+
for lib_location in _possible_lib_locations:
70+
try:
71+
tof_lib = CDLL(lib_location + "/vl53l0x_python.so")
72+
break
73+
except OSError:
74+
pass
75+
else:
76+
raise OSError('Could not find vl53l0x_python.so')
6977

7078
# Create read function pointer
7179
READFUNC = CFUNCTYPE(c_int, c_ubyte, c_ubyte, POINTER(c_ubyte), c_ubyte)

setup.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
cmdclass={'build_ext': build_ext})

0 commit comments

Comments
 (0)