tinkerforge_async/bindings/
distance_ir_bricklet.rs1#[allow(unused_imports)]
15use crate::{
16 base58::Uid, byte_converter::*, device::*, error::TinkerforgeError, ip_connection::async_io::AsyncIpConnection,
17 low_level_traits::LowLevelRead,
18};
19#[allow(unused_imports)]
20use futures_core::Stream;
21#[allow(unused_imports)]
22use tokio_stream::StreamExt;
23pub enum DistanceIrBrickletFunction {
24 GetDistance,
25 GetAnalogValue,
26 SetSamplingPoint,
27 GetSamplingPoint,
28 SetDistanceCallbackPeriod,
29 GetDistanceCallbackPeriod,
30 SetAnalogValueCallbackPeriod,
31 GetAnalogValueCallbackPeriod,
32 SetDistanceCallbackThreshold,
33 GetDistanceCallbackThreshold,
34 SetAnalogValueCallbackThreshold,
35 GetAnalogValueCallbackThreshold,
36 SetDebouncePeriod,
37 GetDebouncePeriod,
38 GetIdentity,
39 CallbackDistance,
40 CallbackAnalogValue,
41 CallbackDistanceReached,
42 CallbackAnalogValueReached,
43}
44impl From<DistanceIrBrickletFunction> for u8 {
45 fn from(fun: DistanceIrBrickletFunction) -> Self {
46 match fun {
47 DistanceIrBrickletFunction::GetDistance => 1,
48 DistanceIrBrickletFunction::GetAnalogValue => 2,
49 DistanceIrBrickletFunction::SetSamplingPoint => 3,
50 DistanceIrBrickletFunction::GetSamplingPoint => 4,
51 DistanceIrBrickletFunction::SetDistanceCallbackPeriod => 5,
52 DistanceIrBrickletFunction::GetDistanceCallbackPeriod => 6,
53 DistanceIrBrickletFunction::SetAnalogValueCallbackPeriod => 7,
54 DistanceIrBrickletFunction::GetAnalogValueCallbackPeriod => 8,
55 DistanceIrBrickletFunction::SetDistanceCallbackThreshold => 9,
56 DistanceIrBrickletFunction::GetDistanceCallbackThreshold => 10,
57 DistanceIrBrickletFunction::SetAnalogValueCallbackThreshold => 11,
58 DistanceIrBrickletFunction::GetAnalogValueCallbackThreshold => 12,
59 DistanceIrBrickletFunction::SetDebouncePeriod => 13,
60 DistanceIrBrickletFunction::GetDebouncePeriod => 14,
61 DistanceIrBrickletFunction::GetIdentity => 255,
62 DistanceIrBrickletFunction::CallbackDistance => 15,
63 DistanceIrBrickletFunction::CallbackAnalogValue => 16,
64 DistanceIrBrickletFunction::CallbackDistanceReached => 17,
65 DistanceIrBrickletFunction::CallbackAnalogValueReached => 18,
66 }
67 }
68}
69pub const DISTANCE_IR_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
70pub const DISTANCE_IR_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
71pub const DISTANCE_IR_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
72pub const DISTANCE_IR_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
73pub const DISTANCE_IR_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
74
75#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
76pub struct DistanceCallbackThreshold {
77 pub option: char,
78 pub min: u16,
79 pub max: u16,
80}
81impl FromByteSlice for DistanceCallbackThreshold {
82 fn bytes_expected() -> usize {
83 5
84 }
85 fn from_le_byte_slice(bytes: &[u8]) -> DistanceCallbackThreshold {
86 DistanceCallbackThreshold {
87 option: <char>::from_le_byte_slice(&bytes[0..1]),
88 min: <u16>::from_le_byte_slice(&bytes[1..3]),
89 max: <u16>::from_le_byte_slice(&bytes[3..5]),
90 }
91 }
92}
93
94#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
95pub struct AnalogValueCallbackThreshold {
96 pub option: char,
97 pub min: u16,
98 pub max: u16,
99}
100impl FromByteSlice for AnalogValueCallbackThreshold {
101 fn bytes_expected() -> usize {
102 5
103 }
104 fn from_le_byte_slice(bytes: &[u8]) -> AnalogValueCallbackThreshold {
105 AnalogValueCallbackThreshold {
106 option: <char>::from_le_byte_slice(&bytes[0..1]),
107 min: <u16>::from_le_byte_slice(&bytes[1..3]),
108 max: <u16>::from_le_byte_slice(&bytes[3..5]),
109 }
110 }
111}
112
113#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
114pub struct Identity {
115 pub uid: String,
116 pub connected_uid: String,
117 pub position: char,
118 pub hardware_version: [u8; 3],
119 pub firmware_version: [u8; 3],
120 pub device_identifier: u16,
121}
122impl FromByteSlice for Identity {
123 fn bytes_expected() -> usize {
124 25
125 }
126 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
127 Identity {
128 uid: <String>::from_le_byte_slice(&bytes[0..8]),
129 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
130 position: <char>::from_le_byte_slice(&bytes[16..17]),
131 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
132 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
133 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
134 }
135 }
136}
137
138#[derive(Clone)]
140pub struct DistanceIrBricklet {
141 device: Device,
142}
143impl DistanceIrBricklet {
144 pub const DEVICE_IDENTIFIER: u16 = 25;
145 pub const DEVICE_DISPLAY_NAME: &'static str = "Distance IR Bricklet";
146 pub fn new(uid: Uid, connection: AsyncIpConnection) -> DistanceIrBricklet {
148 let mut result = DistanceIrBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
149 result.device.response_expected[u8::from(DistanceIrBrickletFunction::GetDistance) as usize] = ResponseExpectedFlag::AlwaysTrue;
150 result.device.response_expected[u8::from(DistanceIrBrickletFunction::GetAnalogValue) as usize] = ResponseExpectedFlag::AlwaysTrue;
151 result.device.response_expected[u8::from(DistanceIrBrickletFunction::SetSamplingPoint) as usize] = ResponseExpectedFlag::False;
152 result.device.response_expected[u8::from(DistanceIrBrickletFunction::GetSamplingPoint) as usize] = ResponseExpectedFlag::AlwaysTrue;
153 result.device.response_expected[u8::from(DistanceIrBrickletFunction::SetDistanceCallbackPeriod) as usize] =
154 ResponseExpectedFlag::True;
155 result.device.response_expected[u8::from(DistanceIrBrickletFunction::GetDistanceCallbackPeriod) as usize] =
156 ResponseExpectedFlag::AlwaysTrue;
157 result.device.response_expected[u8::from(DistanceIrBrickletFunction::SetAnalogValueCallbackPeriod) as usize] =
158 ResponseExpectedFlag::True;
159 result.device.response_expected[u8::from(DistanceIrBrickletFunction::GetAnalogValueCallbackPeriod) as usize] =
160 ResponseExpectedFlag::AlwaysTrue;
161 result.device.response_expected[u8::from(DistanceIrBrickletFunction::SetDistanceCallbackThreshold) as usize] =
162 ResponseExpectedFlag::True;
163 result.device.response_expected[u8::from(DistanceIrBrickletFunction::GetDistanceCallbackThreshold) as usize] =
164 ResponseExpectedFlag::AlwaysTrue;
165 result.device.response_expected[u8::from(DistanceIrBrickletFunction::SetAnalogValueCallbackThreshold) as usize] =
166 ResponseExpectedFlag::True;
167 result.device.response_expected[u8::from(DistanceIrBrickletFunction::GetAnalogValueCallbackThreshold) as usize] =
168 ResponseExpectedFlag::AlwaysTrue;
169 result.device.response_expected[u8::from(DistanceIrBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
170 result.device.response_expected[u8::from(DistanceIrBrickletFunction::GetDebouncePeriod) as usize] =
171 ResponseExpectedFlag::AlwaysTrue;
172 result.device.response_expected[u8::from(DistanceIrBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
173 result
174 }
175
176 pub fn get_response_expected(&mut self, fun: DistanceIrBrickletFunction) -> Result<bool, GetResponseExpectedError> {
191 self.device.get_response_expected(u8::from(fun))
192 }
193
194 pub fn set_response_expected(
203 &mut self,
204 fun: DistanceIrBrickletFunction,
205 response_expected: bool,
206 ) -> Result<(), SetResponseExpectedError> {
207 self.device.set_response_expected(u8::from(fun), response_expected)
208 }
209
210 pub fn set_response_expected_all(&mut self, response_expected: bool) {
212 self.device.set_response_expected_all(response_expected)
213 }
214
215 pub fn get_api_version(&self) -> [u8; 3] {
218 self.device.api_version
219 }
220
221 pub async fn get_distance_callback_receiver(&mut self) -> impl Stream<Item = u16> {
231 self.device
232 .get_callback_receiver(u8::from(DistanceIrBrickletFunction::CallbackDistance))
233 .await
234 .map(|p| u16::from_le_byte_slice(p.body()))
235 }
236
237 pub async fn get_analog_value_callback_receiver(&mut self) -> impl Stream<Item = u16> {
244 self.device
245 .get_callback_receiver(u8::from(DistanceIrBrickletFunction::CallbackAnalogValue))
246 .await
247 .map(|p| u16::from_le_byte_slice(p.body()))
248 }
249
250 pub async fn get_distance_reached_callback_receiver(&mut self) -> impl Stream<Item = u16> {
257 self.device
258 .get_callback_receiver(u8::from(DistanceIrBrickletFunction::CallbackDistanceReached))
259 .await
260 .map(|p| u16::from_le_byte_slice(p.body()))
261 }
262
263 pub async fn get_analog_value_reached_callback_receiver(&mut self) -> impl Stream<Item = u16> {
270 self.device
271 .get_callback_receiver(u8::from(DistanceIrBrickletFunction::CallbackAnalogValueReached))
272 .await
273 .map(|p| u16::from_le_byte_slice(p.body()))
274 }
275
276 pub async fn get_distance(&mut self) -> Result<u16, TinkerforgeError> {
284 let payload = [0; 0];
285
286 #[allow(unused_variables)]
287 let result = self.device.get(u8::from(DistanceIrBrickletFunction::GetDistance), &payload).await?;
288 Ok(u16::from_le_byte_slice(result.body()))
289 }
290
291 pub async fn get_analog_value(&mut self) -> Result<u16, TinkerforgeError> {
303 let payload = [0; 0];
304
305 #[allow(unused_variables)]
306 let result = self.device.get(u8::from(DistanceIrBrickletFunction::GetAnalogValue), &payload).await?;
307 Ok(u16::from_le_byte_slice(result.body()))
308 }
309
310 pub async fn set_sampling_point(&mut self, position: u8, distance: u16) -> Result<(), TinkerforgeError> {
329 let mut payload = [0; 3];
330 position.write_to_slice(&mut payload[0..1]);
331 distance.write_to_slice(&mut payload[1..3]);
332
333 #[allow(unused_variables)]
334 let result = self.device.set(u8::from(DistanceIrBrickletFunction::SetSamplingPoint), &payload).await?;
335 Ok(())
336 }
337
338 pub async fn get_sampling_point(&mut self, position: u8) -> Result<u16, TinkerforgeError> {
341 let mut payload = [0; 1];
342 position.write_to_slice(&mut payload[0..1]);
343
344 #[allow(unused_variables)]
345 let result = self.device.get(u8::from(DistanceIrBrickletFunction::GetSamplingPoint), &payload).await?;
346 Ok(u16::from_le_byte_slice(result.body()))
347 }
348
349 pub async fn set_distance_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
355 let mut payload = [0; 4];
356 period.write_to_slice(&mut payload[0..4]);
357
358 #[allow(unused_variables)]
359 let result = self.device.set(u8::from(DistanceIrBrickletFunction::SetDistanceCallbackPeriod), &payload).await?;
360 Ok(())
361 }
362
363 pub async fn get_distance_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
365 let payload = [0; 0];
366
367 #[allow(unused_variables)]
368 let result = self.device.get(u8::from(DistanceIrBrickletFunction::GetDistanceCallbackPeriod), &payload).await?;
369 Ok(u32::from_le_byte_slice(result.body()))
370 }
371
372 pub async fn set_analog_value_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
378 let mut payload = [0; 4];
379 period.write_to_slice(&mut payload[0..4]);
380
381 #[allow(unused_variables)]
382 let result = self.device.set(u8::from(DistanceIrBrickletFunction::SetAnalogValueCallbackPeriod), &payload).await?;
383 Ok(())
384 }
385
386 pub async fn get_analog_value_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
388 let payload = [0; 0];
389
390 #[allow(unused_variables)]
391 let result = self.device.get(u8::from(DistanceIrBrickletFunction::GetAnalogValueCallbackPeriod), &payload).await?;
392 Ok(u32::from_le_byte_slice(result.body()))
393 }
394
395 pub async fn set_distance_callback_threshold(&mut self, option: char, min: u16, max: u16) -> Result<(), TinkerforgeError> {
414 let mut payload = [0; 5];
415 option.write_to_slice(&mut payload[0..1]);
416 min.write_to_slice(&mut payload[1..3]);
417 max.write_to_slice(&mut payload[3..5]);
418
419 #[allow(unused_variables)]
420 let result = self.device.set(u8::from(DistanceIrBrickletFunction::SetDistanceCallbackThreshold), &payload).await?;
421 Ok(())
422 }
423
424 pub async fn get_distance_callback_threshold(&mut self) -> Result<DistanceCallbackThreshold, TinkerforgeError> {
433 let payload = [0; 0];
434
435 #[allow(unused_variables)]
436 let result = self.device.get(u8::from(DistanceIrBrickletFunction::GetDistanceCallbackThreshold), &payload).await?;
437 Ok(DistanceCallbackThreshold::from_le_byte_slice(result.body()))
438 }
439
440 pub async fn set_analog_value_callback_threshold(&mut self, option: char, min: u16, max: u16) -> Result<(), TinkerforgeError> {
459 let mut payload = [0; 5];
460 option.write_to_slice(&mut payload[0..1]);
461 min.write_to_slice(&mut payload[1..3]);
462 max.write_to_slice(&mut payload[3..5]);
463
464 #[allow(unused_variables)]
465 let result = self.device.set(u8::from(DistanceIrBrickletFunction::SetAnalogValueCallbackThreshold), &payload).await?;
466 Ok(())
467 }
468
469 pub async fn get_analog_value_callback_threshold(&mut self) -> Result<AnalogValueCallbackThreshold, TinkerforgeError> {
478 let payload = [0; 0];
479
480 #[allow(unused_variables)]
481 let result = self.device.get(u8::from(DistanceIrBrickletFunction::GetAnalogValueCallbackThreshold), &payload).await?;
482 Ok(AnalogValueCallbackThreshold::from_le_byte_slice(result.body()))
483 }
484
485 pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
497 let mut payload = [0; 4];
498 debounce.write_to_slice(&mut payload[0..4]);
499
500 #[allow(unused_variables)]
501 let result = self.device.set(u8::from(DistanceIrBrickletFunction::SetDebouncePeriod), &payload).await?;
502 Ok(())
503 }
504
505 pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
507 let payload = [0; 0];
508
509 #[allow(unused_variables)]
510 let result = self.device.get(u8::from(DistanceIrBrickletFunction::GetDebouncePeriod), &payload).await?;
511 Ok(u32::from_le_byte_slice(result.body()))
512 }
513
514 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
525 let payload = [0; 0];
526
527 #[allow(unused_variables)]
528 let result = self.device.get(u8::from(DistanceIrBrickletFunction::GetIdentity), &payload).await?;
529 Ok(Identity::from_le_byte_slice(result.body()))
530 }
531}