1use crate::utility::*;
2use crate::*;
3use azure_kinect_sys::k4a::*;
4use std::ptr;
5
6pub struct Device<'a> {
7 pub(crate) api: &'a azure_kinect_sys::api::Api,
8 pub(crate) handle: k4a_device_t,
9}
10
11#[derive(Copy, Clone)]
12pub struct ColorControlCapabilities {
13 pub supports_auto: bool,
14 pub min_value: i32,
15 pub max_value: i32,
16 pub step_value: i32,
17 pub default_value: i32,
18 pub default_mode: ColorControlMode,
19}
20
21impl Device<'_> {
22 pub(crate) fn from_handle(api: &azure_kinect_sys::api::Api, handle: k4a_device_t) -> Device {
23 Device { api, handle }
24 }
25
26 pub fn start_cameras(&self, configuration: &DeviceConfiguration) -> Result<Camera, Error> {
28 Camera::new(&self, configuration)
29 }
30
31 pub fn get_serialnum(&self) -> Result<String, Error> {
33 get_k4a_string(&|serialnum, buffer| unsafe {
34 (self.api.funcs.k4a_device_get_serialnum)(self.handle, serialnum, buffer)
35 })
36 }
37
38 pub fn get_color_control(
40 &self,
41 command: ColorControlCommand,
42 ) -> Result<(ColorControlMode, i32), Error> {
43 let mut mode: k4a_color_control_mode_t =
44 k4a_color_control_mode_t_K4A_COLOR_CONTROL_MODE_AUTO;
45 let mut value: i32 = 0;
46 Error::from_k4a_result_t(unsafe {
47 (self.api.funcs.k4a_device_get_color_control)(
48 self.handle,
49 command.into(),
50 &mut mode,
51 &mut value,
52 )
53 })
54 .to_result((ColorControlMode::from_primitive(mode), value))
55 }
56
57 pub fn set_color_control(
59 &mut self,
60 command: ColorControlCommand,
61 mode: ColorControlMode,
62 value: i32,
63 ) -> Result<(), Error> {
64 Error::from_k4a_result_t(unsafe {
65 (self.api.funcs.k4a_device_set_color_control)(
66 self.handle,
67 command.into(),
68 mode.into(),
69 value,
70 )
71 })
72 .to_result(())
73 }
74
75 pub fn get_color_control_capabilities(
76 &self,
77 command: ColorControlCommand,
78 ) -> Result<ColorControlCapabilities, Error> {
79 let mut capabilties = unsafe { std::mem::zeroed::<ColorControlCapabilities>() };
80 let mut mode: k4a_color_control_mode_t = k4a_color_control_mode_t::default();
81 Error::from_k4a_result_t(unsafe {
82 (self.api.funcs.k4a_device_get_color_control_capabilities)(
83 self.handle,
84 command.into(),
85 &mut capabilties.supports_auto,
86 &mut capabilties.min_value,
87 &mut capabilties.max_value,
88 &mut capabilties.step_value,
89 &mut capabilties.default_value,
90 &mut mode,
91 )
92 })
93 .to_result({
94 capabilties.default_mode = ColorControlMode::from_primitive(mode);
95 capabilties
96 })
97 }
98
99 pub fn get_raw_calibration(&self) -> Result<Vec<u8>, Error> {
101 get_k4a_binary_data(&|calibration, buffer| unsafe {
102 (self.api.funcs.k4a_device_get_raw_calibration)(self.handle, calibration, buffer)
103 })
104 }
105
106 pub fn get_calibration(
108 &self,
109 depth_mode: DepthMode,
110 color_resolution: ColorResolution,
111 ) -> Result<Calibration, Error> {
112 let mut calibaraion = k4a_calibration_t::default();
113 Error::from_k4a_result_t(unsafe {
114 (self.api.funcs.k4a_device_get_calibration)(
115 self.handle,
116 depth_mode.into(),
117 color_resolution.into(),
118 &mut calibaraion,
119 )
120 })
121 .to_result_fn(|| Calibration::from_handle(self.api, calibaraion))
122 }
123
124 pub fn is_sync_connected(&self) -> Result<(bool, bool), Error> {
126 let mut sync_in_jack_connected = false;
127 let mut sync_out_jack_connected = false;
128 Error::from_k4a_result_t(unsafe {
129 (self.api.funcs.k4a_device_get_sync_jack)(
130 self.handle,
131 &mut sync_in_jack_connected,
132 &mut sync_out_jack_connected,
133 )
134 })
135 .to_result((sync_in_jack_connected, sync_out_jack_connected))
136 }
137
138 pub fn is_sync_in_connected(&self) -> Result<bool, Error> {
140 Ok(self.is_sync_connected()?.0)
141 }
142
143 pub fn is_sync_out_connected(&self) -> Result<bool, Error> {
145 Ok(self.is_sync_connected()?.1)
146 }
147
148 pub fn get_version(&self) -> Result<HardwareVersion, Error> {
150 let mut version = k4a_hardware_version_t::default();
151 Error::from_k4a_result_t(unsafe {
152 (self.api.funcs.k4a_device_get_version)(self.handle, &mut version)
153 })
154 .to_result(HardwareVersion { value: version })
155 }
156}
157
158impl Drop for Device<'_> {
159 fn drop(&mut self) {
160 unsafe {
161 (self.api.funcs.k4a_device_close)(self.handle);
162 }
163 self.handle = ptr::null_mut();
164 }
165}
166
167#[derive(Copy, Clone, Default)]
168pub struct DeviceConfiguration {
169 pub(crate) value: k4a_device_configuration_t,
170}
171
172impl DeviceConfiguration {
173 pub(crate) fn for_k4arecord(&self) -> &azure_kinect_sys::k4arecord::k4a_device_configuration_t {
174 unsafe { std::mem::transmute(&self.value) }
175 }
176
177 pub fn builder() -> DeviceConfigurationBuilder {
178 DeviceConfigurationBuilder::default()
179 }
180
181 #[doc = " Image format to capture with the color camera."]
182 pub fn color_format(&self) -> ImageFormat {
183 ImageFormat::from_primitive(self.value.color_format)
184 }
185
186 #[doc = " Image resolution to capture with the color camera."]
187 pub fn color_resolution(&self) -> ColorResolution {
188 ColorResolution::from_primitive(self.value.color_resolution)
189 }
190
191 #[doc = " Capture mode for the depth camera."]
192 pub fn depth_mode(&self) -> DepthMode {
193 DepthMode::from_primitive(self.value.depth_mode)
194 }
195
196 #[doc = " Desired frame rate for the color and depth camera."]
197 pub fn camera_fps(&self) -> Fps {
198 Fps::from_primitive(self.value.camera_fps.into())
199 }
200
201 #[doc = " Only produce k4a_capture_t objects if they contain synchronized color and depth images."]
202 pub fn synchronized_images_only(&self) -> bool {
203 self.value.synchronized_images_only
204 }
205
206 #[doc = " Desired delay between the capture of the color image and the capture of the depth image."]
207 pub fn depth_delay_off_color_usec(&self) -> i32 {
208 self.value.depth_delay_off_color_usec
209 }
210
211 #[doc = " The external synchronization mode."]
212 pub fn wired_sync_mode(&self) -> WiredSyncMode {
213 WiredSyncMode::from_primitive(self.value.wired_sync_mode.into())
214 }
215
216 #[doc = " The external synchronization timing."]
217 pub fn subordinate_delay_off_master_usec(&self) -> u32 {
218 self.value.subordinate_delay_off_master_usec
219 }
220
221 #[doc = " Streaming indicator automatically turns on when the color or depth camera's are in use."]
222 pub fn disable_streaming_indicator(&self) -> bool {
223 self.value.disable_streaming_indicator
224 }
225}
226
227#[derive(Default)]
228pub struct DeviceConfigurationBuilder {
229 value: k4a_device_configuration_t,
230}
231
232impl DeviceConfigurationBuilder {
233 #[doc = " Image format to capture with the color camera."]
234 pub fn color_format(mut self, value: ImageFormat) -> DeviceConfigurationBuilder {
235 self.value.color_format = value.into();
236 self
237 }
238
239 #[doc = " Image resolution to capture with the color camera."]
240 pub fn color_resolution(mut self, value: ColorResolution) -> DeviceConfigurationBuilder {
241 self.value.color_resolution = value.into();
242 self
243 }
244
245 #[doc = " Capture mode for the depth camera."]
246 pub fn depth_mode(mut self, value: DepthMode) -> DeviceConfigurationBuilder {
247 self.value.depth_mode = value.into();
248 self
249 }
250
251 #[doc = " Desired frame rate for the color and depth camera."]
252 pub fn camera_fps(mut self, value: Fps) -> DeviceConfigurationBuilder {
253 self.value.camera_fps = value.into();
254 self
255 }
256
257 #[doc = " Only produce k4a_capture_t objects if they contain synchronized color and depth images."]
258 pub fn synchronized_images_only(mut self, value: bool) -> DeviceConfigurationBuilder {
259 self.value.synchronized_images_only = value;
260 self
261 }
262
263 #[doc = " Desired delay between the capture of the color image and the capture of the depth image."]
264 pub fn depth_delay_off_color_usec(mut self, value: i32) -> DeviceConfigurationBuilder {
265 self.value.depth_delay_off_color_usec = value;
266 self
267 }
268
269 #[doc = " The external synchronization mode."]
270 pub fn wired_sync_mode(mut self, value: WiredSyncMode) -> DeviceConfigurationBuilder {
271 self.value.wired_sync_mode = value.into();
272 self
273 }
274
275 #[doc = " The external synchronization timing."]
276 pub fn subordinate_delay_off_master_usec(mut self, value: u32) -> DeviceConfigurationBuilder {
277 self.value.subordinate_delay_off_master_usec = value;
278 self
279 }
280
281 #[doc = " Streaming indicator automatically turns on when the color or depth camera's are in use."]
282 pub fn disable_streaming_indicator(mut self, value: bool) -> DeviceConfigurationBuilder {
283 self.value.disable_streaming_indicator = value;
284 self
285 }
286
287 pub fn build(&self) -> DeviceConfiguration {
288 DeviceConfiguration { value: self.value }
289 }
290}