Time-of-Flight Library(ToF)  3.2.2
usb_interface.hpp
1 #ifndef _CHRONOPTICS_TOF_USB_INTERFACE_HPP_
2 #define _CHRONOPTICS_TOF_USB_INTERFACE_HPP_
3 
4 #include <chronoptics/tof/usb_interface.h>
5 
6 #include <chronoptics/tof/camera_config.hpp>
7 #include <chronoptics/tof/calibration.hpp>
8 #include <chronoptics/tof/data.hpp>
9 #include <chronoptics/tof/gige_interface.hpp>
10 
11 namespace chronoptics {
12 namespace tof {
13 
14 /** USB device
15 */
16 class UsbDevice : public detail::Base<tof_usb_device, tof_usb_device_delete> {
17  public:
18  /** Construct from pointer */
19  UsbDevice(tof_usb_device_t ptr = nullptr) {
20  this->ptr_ = ptr;
21  }
22 
23  /** Get serial corresponding with USB device
24  * @return Serial
25  */
26  const char* serial() {
27  return tof_usb_device_serial(this->ptr_, TOF_ERROR_HANDLER{});
28  }
29 
30 };
31 
32 /** Connect to a camera directly using the USB interface
33 */
34 class UsbInterface : public detail::Base<tof_usb_interface, tof_usb_interface_delete> {
35  public:
36  /** Construct from pointer */
37  UsbInterface(tof_usb_interface_t ptr = nullptr) {
38  this->ptr_ = ptr;
39  }
40 
41  /** Create USB camera from a usb device
42  * @param usb_device USB device
43  */
44  UsbInterface(UsbDevice &usb_device) {
45  this->ptr_ = tof_usb_interface_new(*reinterpret_cast<tof_usb_device_t*>(&usb_device), TOF_ERROR_HANDLER{});
46  }
47 
48  /** Check whether a connection is still maintained with the camera
49  * @return Is connected
50  */
51  bool is_connected() const {
52  return tof_usb_interface_is_connected(this->ptr_, TOF_ERROR_HANDLER{});
53  }
54 
55  /** Tests the connection speed of the USB port. This is useful to check
56  * whether a hub or USB port works well. The maximum speed that the camera is
57  * able to transmit is ~3.0 Gbps.
58  * @return Connection speed in Gpbs (bits)
59  */
60  double test_stream_speed() {
61  return tof_usb_interface_test_stream_speed(this->ptr_, TOF_ERROR_HANDLER{});
62  }
63 
64  /** Download camera configuration from the camera
65  * @return Camera configuration
66  */
68  CameraConfig new_camera_config(static_cast<tof_camera_config_t>(nullptr));
69  auto ptr = reinterpret_cast<tof_camera_config_t*>(&new_camera_config);
70  *ptr = tof_usb_interface_download_configuration(this->ptr_, TOF_ERROR_HANDLER{});
71  return new_camera_config;
72  }
73 
74  /** Download calibration from the camera
75  * @return Calibration
76  */
78  Calibration new_calibration(static_cast<tof_calibration_t>(nullptr));
79  auto ptr = reinterpret_cast<tof_calibration_t*>(&new_calibration);
80  *ptr = tof_usb_interface_download_calibration(this->ptr_, TOF_ERROR_HANDLER{});
81  return new_calibration;
82  }
83 
84  /** Upload camera configuration to the camera
85  * @param config Camera configuration
86  */
87  void upload_configuration(const CameraConfig &config) {
88  return tof_usb_interface_upload_configuration(this->ptr_, *reinterpret_cast<const tof_camera_config_t*>(&config), TOF_ERROR_HANDLER{});
89  }
90 
91  /** Check whether this camera is capable of streaming the specified type
92  * @param stream Depth stream type
93  * @return Is capable
94  */
95  bool depth_stream_capable(DepthStreamType stream) const {
96  return tof_usb_interface_depth_stream_capable(this->ptr_, static_cast<tof_depth_stream_type>(stream), TOF_ERROR_HANDLER{});
97  }
98 
99  /** Start the depth stream
100  * @param stream Depth stream type
101  */
102  void start_depth_stream(DepthStreamType stream) {
103  return tof_usb_interface_start_depth_stream(this->ptr_, static_cast<tof_depth_stream_type>(stream), TOF_ERROR_HANDLER{});
104  }
105 
106  /** Stop the depth stream
107  */
109  return tof_usb_interface_stop_depth_stream(this->ptr_, TOF_ERROR_HANDLER{});
110  }
111 
112  /** Returns true if a depth frame is available
113  * @return Depth frame available
114  */
115  bool has_depth_data() const {
116  return tof_usb_interface_has_depth_data(this->ptr_, TOF_ERROR_HANDLER{});
117  }
118 
119  /** Get a depth frame
120  * @return Depth frame
121  */
123  Data new_data(static_cast<tof_data_t>(nullptr));
124  auto ptr = reinterpret_cast<tof_data_t*>(&new_data);
125  *ptr = tof_usb_interface_get_depth_data(this->ptr_, TOF_ERROR_HANDLER{});
126  return new_data;
127  }
128 
129  /** Check whether camera supports an image stream
130  * @return Camera supports an image stream
131  */
132  bool image_stream_capable() const {
133  return tof_usb_interface_image_stream_capable(this->ptr_, TOF_ERROR_HANDLER{});
134  }
135 
136  /** Start image stream
137  */
139  return tof_usb_interface_start_image_stream(this->ptr_, TOF_ERROR_HANDLER{});
140  }
141 
142  /** Stop image stream
143  */
145  return tof_usb_interface_stop_image_stream(this->ptr_, TOF_ERROR_HANDLER{});
146  }
147 
148  /** Check whether image frame is available
149  * @return Image frame available
150  */
151  bool has_image_data() const {
152  return tof_usb_interface_has_image_data(this->ptr_, TOF_ERROR_HANDLER{});
153  }
154 
155  /** Get image frame
156  * @return Image data
157  */
159  Data new_data(static_cast<tof_data_t>(nullptr));
160  auto ptr = reinterpret_cast<tof_data_t*>(&new_data);
161  *ptr = tof_usb_interface_get_image_data(this->ptr_, TOF_ERROR_HANDLER{});
162  return new_data;
163  }
164 
165  /** Get the image frame into the supplied pointer
166  * @param pointer The pointer to the data to write into
167  * @param capacity The amount of data the pointer can hold
168  * @param callback Callback that will be called when pointer is no longer in
169  * use
170  * @return Image data
171  */
172  Data get_image_data_into_pointer(uint8_t* pointer, size_t capacity, user_pointer_destructed_fn &callback) {
173  Data new_data(static_cast<tof_data_t>(nullptr));
174  auto ptr = reinterpret_cast<tof_data_t*>(&new_data);
175  *ptr = tof_usb_interface_get_image_data_into_pointer(this->ptr_, pointer, capacity, user_pointer_destructed_cb_handler, &callback, TOF_ERROR_HANDLER{});
176  return new_data;
177  }
178 
179  /** Get the image frame into the supplied pointer
180  * @param pointer The pointer to the data to write into
181  * @param capacity The amount of data the pointer can hold
182  * @param callback Callback that will be called when pointer is no longer in
183  * use
184  * @param callback_user_data User data that will be passed back when the function pointer is called
185  * @return Image data
186  */
187  Data get_image_data_into_pointer(uint8_t* pointer, size_t capacity, tof_user_pointer_destructed_t callback, void* callback_user_data) {
188  Data new_data(static_cast<tof_data_t>(nullptr));
189  auto ptr = reinterpret_cast<tof_data_t*>(&new_data);
190  *ptr = tof_usb_interface_get_image_data_into_pointer(this->ptr_, pointer, capacity, callback, callback_user_data, TOF_ERROR_HANDLER{});
191  return new_data;
192  }
193 
194  /** Get the capacity for amount of user pointers that can be registered
195  * @return User pointer capacity
196  */
197  size_t get_user_pointer_capacity() const {
198  return tof_usb_interface_get_user_pointer_capacity(this->ptr_, TOF_ERROR_HANDLER{});
199  }
200 
201  /** Set the capacity for amount of user pointers that can be registered
202  * @param capacity User pointer capacity
203  */
204  void set_user_pointer_capacity(size_t capacity) {
205  return tof_usb_interface_set_user_pointer_capacity(this->ptr_, capacity, TOF_ERROR_HANDLER{});
206  }
207 
208  /** Add a pointer that will be filled with depth data. This method allows you
209  * to fill data straight into your own data structure without any additional
210  * copying.
211  * @param pointer The pointer to the data to write into
212  * @param capacity The amount of data the pointer can hold
213  * @param callback Callback that will be called when pointer is no longer in
214  * use
215  */
216  void add_depth_user_pointer(uint8_t* pointer, size_t capacity, user_pointer_destructed_fn &callback) {
217  return tof_usb_interface_add_depth_user_pointer(this->ptr_, pointer, capacity, user_pointer_destructed_cb_handler, &callback, TOF_ERROR_HANDLER{});
218  }
219 
220  /** Add a pointer that will be filled with depth data. This method allows you
221  * to fill data straight into your own data structure without any additional
222  * copying.
223  * @param pointer The pointer to the data to write into
224  * @param capacity The amount of data the pointer can hold
225  * @param callback Callback that will be called when pointer is no longer in
226  * use
227  * @param callback_user_data User data that will be passed back when the function pointer is called
228  */
229  void add_depth_user_pointer(uint8_t* pointer, size_t capacity, tof_user_pointer_destructed_t callback, void* callback_user_data) {
230  return tof_usb_interface_add_depth_user_pointer(this->ptr_, pointer, capacity, callback, callback_user_data, TOF_ERROR_HANDLER{});
231  }
232 
233  /** Get the tof library version running on the camera
234  * @return Version
235  */
236  const char* version() {
237  return tof_usb_interface_version(this->ptr_, TOF_ERROR_HANDLER{});
238  }
239 
240 };
241 
242 /** Discover all connected chronoptics usb cameras
243  * @return The usb devices
244  */
245 inline std::vector<UsbDevice> usb_device_discover() {
246  size_t size = tof_usb_device_discover(nullptr, 0, TOF_ERROR_HANDLER{});
247  std::vector<UsbDevice> vec;
248  vec.reserve(size);
249  for (size_t i = 0; i < size; i++)
250  vec.emplace_back(static_cast<tof_usb_device_t>(nullptr));
251  auto data = reinterpret_cast<tof_usb_device_t*>(vec.data());
252  size = tof_usb_device_discover(data, vec.size(), TOF_ERROR_HANDLER{});
253  return vec;
254 }
255 
256 /** Discover one chronoptics usb camera
257  * @return A USB device
258  */
259 inline UsbDevice usb_device_discover_one() {
260  UsbDevice new_usb_device(static_cast<tof_usb_device_t>(nullptr));
261  auto ptr = reinterpret_cast<tof_usb_device_t*>(&new_usb_device);
262  *ptr = tof_usb_device_discover_one(TOF_ERROR_HANDLER{});
263  return new_usb_device;
264 }
265 
266 /** Discover usb camera with the given serial
267  * @param serial Serial of the camera to find
268  * @return A USB device
269  */
270 inline UsbDevice usb_device_find(StringView serial) {
271  UsbDevice new_usb_device(static_cast<tof_usb_device_t>(nullptr));
272  auto ptr = reinterpret_cast<tof_usb_device_t*>(&new_usb_device);
273  *ptr = tof_usb_device_find(serial, TOF_ERROR_HANDLER{});
274  return new_usb_device;
275 }
276 
277 } // tof
278 } // chronoptics
279 
280 #endif
This class contains all the calibration information.
Definition: calibration.hpp:13
This class allows you to view/edit the camera settings.
This is the class that contains depth or image data.
Definition: data.hpp:31
UsbDevice(tof_usb_device_t ptr=nullptr)
Construct from pointer.
const char * serial()
Get serial corresponding with USB device.
Connect to a camera directly using the USB interface.
bool depth_stream_capable(DepthStreamType stream) const
Check whether this camera is capable of streaming the specified type.
bool is_connected() const
Check whether a connection is still maintained with the camera.
bool image_stream_capable() const
Check whether camera supports an image stream.
void stop_image_stream()
Stop image stream.
UsbInterface(UsbDevice &usb_device)
Create USB camera from a usb device.
const char * version()
Get the tof library version running on the camera.
void set_user_pointer_capacity(size_t capacity)
Set the capacity for amount of user pointers that can be registered.
double test_stream_speed()
Tests the connection speed of the USB port.
void start_depth_stream(DepthStreamType stream)
Start the depth stream.
Data get_image_data_into_pointer(uint8_t *pointer, size_t capacity, tof_user_pointer_destructed_t callback, void *callback_user_data)
Get the image frame into the supplied pointer.
bool has_depth_data() const
Returns true if a depth frame is available.
void stop_depth_stream()
Stop the depth stream.
Data get_image_data()
Get image frame.
void add_depth_user_pointer(uint8_t *pointer, size_t capacity, user_pointer_destructed_fn &callback)
Add a pointer that will be filled with depth data.
void start_image_stream()
Start image stream.
void add_depth_user_pointer(uint8_t *pointer, size_t capacity, tof_user_pointer_destructed_t callback, void *callback_user_data)
Add a pointer that will be filled with depth data.
Data get_image_data_into_pointer(uint8_t *pointer, size_t capacity, user_pointer_destructed_fn &callback)
Get the image frame into the supplied pointer.
size_t get_user_pointer_capacity() const
Get the capacity for amount of user pointers that can be registered.
UsbInterface(tof_usb_interface_t ptr=nullptr)
Construct from pointer.
Calibration download_calibration()
Download calibration from the camera.
Data get_depth_data()
Get a depth frame.
bool has_image_data() const
Check whether image frame is available.
CameraConfig download_configuration()
Download camera configuration from the camera.
void upload_configuration(const CameraConfig &config)
Upload camera configuration to the camera.