1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use crate::{
common::*,
device::Device,
error::{ErrorChecker, Result},
kind::{CameraInfo, Rs2Option},
options::ToOptions,
processing_block_list::ProcessingBlockList,
sensor_kind,
stream_profile_list::StreamProfileList,
};
#[derive(Debug)]
pub enum ExtendedSensor {
Color(Sensor<sensor_kind::Color>),
Depth(Sensor<sensor_kind::Depth>),
DepthStereo(Sensor<sensor_kind::DepthStereo>),
L500Depth(Sensor<sensor_kind::L500Depth>),
Motion(Sensor<sensor_kind::Motion>),
FishEye(Sensor<sensor_kind::FishEye>),
Software(Sensor<sensor_kind::Software>),
Pose(Sensor<sensor_kind::Pose>),
Tm2(Sensor<sensor_kind::Tm2>),
Other(Sensor<sensor_kind::Any>),
}
#[derive(Debug)]
pub struct Sensor<Kind>
where
Kind: sensor_kind::SensorKind,
{
pub(crate) ptr: NonNull<sys::rs2_sensor>,
_phantom: PhantomData<Kind>,
}
pub type ColorSensor = Sensor<sensor_kind::Color>;
pub type DepthSensor = Sensor<sensor_kind::Depth>;
pub type DepthStereoSensor = Sensor<sensor_kind::DepthStereo>;
pub type L500DepthSensor = Sensor<sensor_kind::L500Depth>;
pub type MotionSensor = Sensor<sensor_kind::Motion>;
pub type FishEyeSensor = Sensor<sensor_kind::FishEye>;
pub type SoftwareSensor = Sensor<sensor_kind::Software>;
pub type PoseSensor = Sensor<sensor_kind::Pose>;
pub type Tm2Sensor = Sensor<sensor_kind::Tm2>;
pub type AnySensor = Sensor<sensor_kind::Any>;
impl<Kind> Sensor<Kind>
where
Kind: sensor_kind::SensorKind,
{
pub fn device(&self) -> Result<Device> {
let device = unsafe {
let mut checker = ErrorChecker::new();
let ptr =
sys::rs2_create_device_from_sensor(self.ptr.as_ptr(), checker.inner_mut_ptr());
checker.check()?;
Device::from_raw(ptr)
};
Ok(device)
}
pub fn get_option(&self, option: Rs2Option) -> Result<f32> {
unsafe {
let mut checker = ErrorChecker::new();
let val = sys::rs2_get_option(
self.ptr.as_ptr().cast::<sys::rs2_options>(),
option as sys::rs2_option,
checker.inner_mut_ptr(),
);
checker.check()?;
Ok(val)
}
}
pub fn stream_profiles(&self) -> Result<StreamProfileList> {
let list = unsafe {
let mut checker = ErrorChecker::new();
let ptr = sys::rs2_get_stream_profiles(self.ptr.as_ptr(), checker.inner_mut_ptr());
checker.check()?;
StreamProfileList::from_raw(ptr)
};
Ok(list)
}
pub fn recommended_processing_blocks(&self) -> Result<ProcessingBlockList> {
let list = unsafe {
let mut checker = ErrorChecker::new();
let ptr = sys::rs2_get_recommended_processing_blocks(
self.ptr.as_ptr(),
checker.inner_mut_ptr(),
);
checker.check()?;
ProcessingBlockList::from_raw(ptr)
};
Ok(list)
}
pub fn name(&self) -> Result<&str> {
self.info(CameraInfo::Name)
}
pub fn serial_number(&self) -> Result<&str> {
self.info(CameraInfo::SerialNumber)
}
pub fn recommended_firmware_version(&self) -> Result<&str> {
self.info(CameraInfo::RecommendedFirmwareVersion)
}
pub fn physical_port(&self) -> Result<&str> {
self.info(CameraInfo::PhysicalPort)
}
pub fn debug_op_code(&self) -> Result<&str> {
self.info(CameraInfo::DebugOpCode)
}
pub fn advanced_mode(&self) -> Result<&str> {
self.info(CameraInfo::AdvancedMode)
}
pub fn product_id(&self) -> Result<&str> {
self.info(CameraInfo::ProductId)
}
pub fn camera_locked(&self) -> Result<&str> {
self.info(CameraInfo::CameraLocked)
}
pub fn usb_type_descriptor(&self) -> Result<&str> {
self.info(CameraInfo::UsbTypeDescriptor)
}
pub fn product_line(&self) -> Result<&str> {
self.info(CameraInfo::ProductLine)
}
pub fn asic_serial_number(&self) -> Result<&str> {
self.info(CameraInfo::AsicSerialNumber)
}
pub fn firmware_update_id(&self) -> Result<&str> {
self.info(CameraInfo::FirmwareUpdateId)
}
pub fn count(&self) -> Result<&str> {
self.info(CameraInfo::Count)
}
pub fn info(&self, kind: CameraInfo) -> Result<&str> {
let ptr = unsafe {
let mut checker = ErrorChecker::new();
let ptr = sys::rs2_get_sensor_info(
self.ptr.as_ptr(),
kind as sys::rs2_camera_info,
checker.inner_mut_ptr(),
);
checker.check()?;
ptr
};
let string = unsafe { CStr::from_ptr(ptr).to_str().unwrap() };
Ok(string)
}
pub fn is_info_supported(&self, kind: CameraInfo) -> Result<bool> {
let val = unsafe {
let mut checker = ErrorChecker::new();
let val = sys::rs2_supports_sensor_info(
self.ptr.as_ptr(),
kind as sys::rs2_camera_info,
checker.inner_mut_ptr(),
);
checker.check()?;
val
};
Ok(val != 0)
}
pub fn into_raw(self) -> *mut sys::rs2_sensor {
let ptr = self.ptr;
mem::forget(self);
ptr.as_ptr()
}
pub unsafe fn from_raw(ptr: *mut sys::rs2_sensor) -> Self {
Self {
ptr: NonNull::new(ptr).unwrap(),
_phantom: PhantomData,
}
}
}
impl AnySensor {
pub fn is_extendable_to<Kind>(&self) -> Result<bool>
where
Kind: sensor_kind::NonAnySensorKind,
{
unsafe {
let mut checker = ErrorChecker::new();
let val = sys::rs2_is_sensor_extendable_to(
self.ptr.as_ptr(),
Kind::EXTENSION as sys::rs2_extension,
checker.inner_mut_ptr(),
);
checker.check()?;
Ok(val != 0)
}
}
pub fn try_extend_to<Kind>(self) -> Result<result::Result<Sensor<Kind>, Self>>
where
Kind: sensor_kind::NonAnySensorKind,
{
if self.is_extendable_to::<Kind>()? {
let ptr = self.into_raw();
let sensor = Sensor {
ptr: NonNull::new(ptr).unwrap(),
_phantom: PhantomData,
};
Ok(Ok(sensor))
} else {
Ok(Err(self))
}
}
pub fn try_extend(self) -> Result<ExtendedSensor> {
let sensor_any = self;
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::DepthStereo>()? {
Ok(sensor) => return Ok(ExtendedSensor::DepthStereo(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::Depth>()? {
Ok(sensor) => return Ok(ExtendedSensor::Depth(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::L500Depth>()? {
Ok(sensor) => return Ok(ExtendedSensor::L500Depth(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::Color>()? {
Ok(sensor) => return Ok(ExtendedSensor::Color(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::Motion>()? {
Ok(sensor) => return Ok(ExtendedSensor::Motion(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::FishEye>()? {
Ok(sensor) => return Ok(ExtendedSensor::FishEye(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::Software>()? {
Ok(sensor) => return Ok(ExtendedSensor::Software(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::Pose>()? {
Ok(sensor) => return Ok(ExtendedSensor::Pose(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::Tm2>()? {
Ok(sensor) => return Ok(ExtendedSensor::Tm2(sensor)),
Err(sensor) => sensor,
};
Ok(ExtendedSensor::Other(sensor_any))
}
}
impl DepthSensor {
pub fn depth_units(&self) -> Result<f32> {
self.get_option(Rs2Option::DepthUnits)
}
}
impl<Kind> ToOptions for Sensor<Kind>
where
Kind: sensor_kind::SensorKind,
{
fn get_options_ptr(&self) -> NonNull<sys::rs2_options> {
self.ptr.cast::<sys::rs2_options>()
}
}
unsafe impl<Kind> Send for Sensor<Kind> where Kind: sensor_kind::SensorKind {}
impl<Kind> Drop for Sensor<Kind>
where
Kind: sensor_kind::SensorKind,
{
fn drop(&mut self) {
unsafe {
sys::rs2_delete_sensor(self.ptr.as_ptr());
}
}
}