@@ -9,67 +9,116 @@ This source code is licensed under the BSD-3-Clause license found in the
9
9
LICENSE file in the root directory of this source tree.
10
10
"""
11
11
12
- from libc.stdint cimport uint8_t, uint16_t, int32_t
12
+ import atexit
13
+ from libc.stdint cimport int32_t, uint8_t, uint16_t
13
14
import numpy as np
14
15
cimport numpy as np
15
16
16
17
cdef extern from " ../VL53L5CX_Linux_driver_1.1.2/user/uld-driver/inc/vl53l5cx_api.h" :
18
+ enum : VL53L5CX_DEFAULT_I2C_ADDRESS
19
+ enum : VL53L5CX_RESOLUTION_8X8
20
+ enum : VL53L5CX_NB_TARGET_PER_ZONE
17
21
ctypedef struct VL53L5CX_Configuration:
18
22
pass
19
- cdef enum :
20
- VL53L5CX_DEFAULT_I2C_ADDRESS
23
+ # ctypedef struct motion_indicator_t:
24
+ # uint32_t global_indicator_1
25
+ # uint32_t global_indicator_2
26
+ # uint8_t status
27
+ # uint8_t nb_of_detected_aggregates
28
+ # uint8_t nb_of_aggregates
29
+ # uint8_t spare
30
+ # uint32_t motion[32]
31
+ ctypedef struct VL53L5CX_ResultsData:
32
+ # ifndef VL53L5CX_DISABLE_AMBIENT_PER_SPAD
33
+ np.uint32_t ambient_per_spad[VL53L5CX_RESOLUTION_8X8]
34
+ # endif
21
35
22
- cdef extern from " ../include/vl53l5cx_py.h" :
23
- ctypedef struct XYZ_ZoneCoordinates_t:
24
- double Xpos[64 ]
25
- double Ypos[64 ]
26
- double Zpos[64 ]
36
+ # ifndef VL53L5CX_DISABLE_NB_TARGET_DETECTED
37
+ np.uint8_t nb_target_detected[VL53L5CX_RESOLUTION_8X8]
38
+ # endif
39
+
40
+ # ifndef VL53L5CX_DISABLE_NB_SPADS_ENABLED
41
+ np.uint32_t nb_spads_enabled[VL53L5CX_RESOLUTION_8X8]
42
+ # endif
43
+
44
+ # ifndef VL53L5CX_DISABLE_SIGNAL_PER_SPAD
45
+ np.uint32_t signal_per_spad[(VL53L5CX_RESOLUTION_8X8
46
+ * VL53L5CX_NB_TARGET_PER_ZONE)]
47
+ # endif
48
+
49
+ # ifndef VL53L5CX_DISABLE_RANGE_SIGMA_MM
50
+ np.uint16_t range_sigma_mm[(VL53L5CX_RESOLUTION_8X8
51
+ * VL53L5CX_NB_TARGET_PER_ZONE)]
52
+ # endif
53
+
54
+ # ifndef VL53L5CX_DISABLE_DISTANCE_MM
55
+ np.int16_t distance_mm[(VL53L5CX_RESOLUTION_8X8
56
+ * VL53L5CX_NB_TARGET_PER_ZONE)]
57
+ # endif
27
58
59
+ # ifndef VL53L5CX_DISABLE_REFLECTANCE_PERCENT
60
+ np.uint8_t reflectance[(VL53L5CX_RESOLUTION_8X8
61
+ * VL53L5CX_NB_TARGET_PER_ZONE)]
62
+ # endif
63
+
64
+ # ifndef VL53L5CX_DISABLE_TARGET_STATUS
65
+ np.uint8_t target_status[(VL53L5CX_RESOLUTION_8X8
66
+ * VL53L5CX_NB_TARGET_PER_ZONE)]
67
+ # endif
68
+
69
+ # ifndef VL53L5CX_DISABLE_MOTION_INDICATOR
70
+ # motion_indicator_t motion_indicator
71
+ # endif
72
+
73
+ cdef extern from " ../include/vl53l5cx_py.h" :
28
74
int32_t vl53l5cx_py_init(VL53L5CX_Configuration* dev_conf,
29
75
const char * dev_path, uint16_t target_addr,
30
76
uint8_t freq)
31
77
int32_t vl53l5cx_py_close(VL53L5CX_Configuration* dev_conf)
32
78
int32_t vl53l5cx_py_start_ranging(VL53L5CX_Configuration* dev_conf)
33
79
int32_t vl53l5cx_py_stop_ranging(VL53L5CX_Configuration* dev_conf)
34
80
int32_t vl53l5cx_py_get_range(VL53L5CX_Configuration* dev_conf,
35
- uint8_t transform,
36
- XYZ_ZoneCoordinates_t XYZ_Coordinates)
81
+ VL53L5CX_ResultsData* results)
37
82
38
- cpdef VL53L5CX_PY_DEFAULT_ADDR = VL53L5CX_DEFAULT_I2C_ADDRESS
39
- cpdef VL53L5CX_PY_DEFAULT_FREQ = 15
40
- cpdef VL53L5CX_PY_DEFAULT_TRANSFORM = 0
83
+ cdef VL53L5CX_PY_DEFAULT_ADDR = VL53L5CX_DEFAULT_I2C_ADDRESS
84
+ cdef VL53L5CX_PY_DEFAULT_FREQ = 15
41
85
42
86
cdef class VL53L5CX:
43
87
cdef VL53L5CX_Configuration dev_conf
44
- cdef uint8_t transform
88
+ cdef bint ranging
45
89
46
90
def __cinit__ (self , dev_path: str ,
47
91
target_addr: np.uint16_t = VL53L5CX_PY_DEFAULT_ADDR,
48
- freq: np.uint8_t = VL53L5CX_PY_DEFAULT_FREQ,
49
- transform: np.uint8_t = VL53L5CX_PY_DEFAULT_TRANSFORM) -> None:
50
- self.transform = transform
92
+ freq: np.uint8_t = VL53L5CX_PY_DEFAULT_FREQ) -> None:
51
93
cdef int32_t status = vl53l5cx_py_init(& self .dev_conf, dev_path.encode(), target_addr, freq)
52
94
if status != 0:
53
95
raise RuntimeError
96
+ self.ranging = False
97
+ atexit.register(self.__del__ )
54
98
55
- def __dealloc__(self ) -> None:
99
+ def __del__(self ) -> None:
100
+ if self.ranging:
101
+ self.stop_ranging()
56
102
cdef int32_t status = vl53l5cx_py_close(& self .dev_conf)
57
103
if status != 0:
58
104
raise RuntimeError
105
+ atexit.unregister(self.__del__ )
59
106
60
107
cpdef void start_ranging(self ):
61
108
cdef int32_t status = vl53l5cx_py_start_ranging(& self .dev_conf)
62
109
if status != 0 :
63
110
raise RuntimeError
111
+ self .ranging = True
64
112
65
113
cpdef void stop_ranging(self ):
66
114
cdef int32_t status = vl53l5cx_py_stop_ranging(& self .dev_conf)
67
115
if status != 0 :
68
116
raise RuntimeError
117
+ self .ranging = False
69
118
70
- cpdef tuple get_range(self ):
71
- cdef XYZ_ZoneCoordinates_t XYZ_Coordinates
72
- cdef int32_t status = vl53l5cx_py_get_range(& self .dev_conf, self .transform, XYZ_Coordinates )
119
+ cpdef VL53L5CX_ResultsData get_range(self ):
120
+ cdef VL53L5CX_ResultsData results
121
+ cdef int32_t status = vl53l5cx_py_get_range(& self .dev_conf, & results )
73
122
if status != 0 :
74
123
raise RuntimeError
75
- return (XYZ_Coordinates.Xpos, XYZ_Coordinates.Ypos, XYZ_Coordinates.Zpos)
124
+ return results
0 commit comments