tinkerforge_async/bindings/
laser_range_finder_bricklet.rs

1/* ***********************************************************
2 * This file was automatically generated on 2024-02-16.      *
3 *                                                           *
4 * Rust Bindings Version 2.0.20                              *
5 *                                                           *
6 * If you have a bugfix for this file and want to commit it, *
7 * please fix the bug in the generator. You can find a link  *
8 * to the generators git repository on tinkerforge.com       *
9 *************************************************************/
10
11//! Measures distance up to 40m with laser light.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/LaserRangeFinder_Bricklet_Rust.html).
14#[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 LaserRangeFinderBrickletFunction {
24    GetDistance,
25    GetVelocity,
26    SetDistanceCallbackPeriod,
27    GetDistanceCallbackPeriod,
28    SetVelocityCallbackPeriod,
29    GetVelocityCallbackPeriod,
30    SetDistanceCallbackThreshold,
31    GetDistanceCallbackThreshold,
32    SetVelocityCallbackThreshold,
33    GetVelocityCallbackThreshold,
34    SetDebouncePeriod,
35    GetDebouncePeriod,
36    SetMovingAverage,
37    GetMovingAverage,
38    SetMode,
39    GetMode,
40    EnableLaser,
41    DisableLaser,
42    IsLaserEnabled,
43    GetSensorHardwareVersion,
44    SetConfiguration,
45    GetConfiguration,
46    GetIdentity,
47    CallbackDistance,
48    CallbackVelocity,
49    CallbackDistanceReached,
50    CallbackVelocityReached,
51}
52impl From<LaserRangeFinderBrickletFunction> for u8 {
53    fn from(fun: LaserRangeFinderBrickletFunction) -> Self {
54        match fun {
55            LaserRangeFinderBrickletFunction::GetDistance => 1,
56            LaserRangeFinderBrickletFunction::GetVelocity => 2,
57            LaserRangeFinderBrickletFunction::SetDistanceCallbackPeriod => 3,
58            LaserRangeFinderBrickletFunction::GetDistanceCallbackPeriod => 4,
59            LaserRangeFinderBrickletFunction::SetVelocityCallbackPeriod => 5,
60            LaserRangeFinderBrickletFunction::GetVelocityCallbackPeriod => 6,
61            LaserRangeFinderBrickletFunction::SetDistanceCallbackThreshold => 7,
62            LaserRangeFinderBrickletFunction::GetDistanceCallbackThreshold => 8,
63            LaserRangeFinderBrickletFunction::SetVelocityCallbackThreshold => 9,
64            LaserRangeFinderBrickletFunction::GetVelocityCallbackThreshold => 10,
65            LaserRangeFinderBrickletFunction::SetDebouncePeriod => 11,
66            LaserRangeFinderBrickletFunction::GetDebouncePeriod => 12,
67            LaserRangeFinderBrickletFunction::SetMovingAverage => 13,
68            LaserRangeFinderBrickletFunction::GetMovingAverage => 14,
69            LaserRangeFinderBrickletFunction::SetMode => 15,
70            LaserRangeFinderBrickletFunction::GetMode => 16,
71            LaserRangeFinderBrickletFunction::EnableLaser => 17,
72            LaserRangeFinderBrickletFunction::DisableLaser => 18,
73            LaserRangeFinderBrickletFunction::IsLaserEnabled => 19,
74            LaserRangeFinderBrickletFunction::GetSensorHardwareVersion => 24,
75            LaserRangeFinderBrickletFunction::SetConfiguration => 25,
76            LaserRangeFinderBrickletFunction::GetConfiguration => 26,
77            LaserRangeFinderBrickletFunction::GetIdentity => 255,
78            LaserRangeFinderBrickletFunction::CallbackDistance => 20,
79            LaserRangeFinderBrickletFunction::CallbackVelocity => 21,
80            LaserRangeFinderBrickletFunction::CallbackDistanceReached => 22,
81            LaserRangeFinderBrickletFunction::CallbackVelocityReached => 23,
82        }
83    }
84}
85pub const LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
86pub const LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
87pub const LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
88pub const LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
89pub const LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
90pub const LASER_RANGE_FINDER_BRICKLET_MODE_DISTANCE: u8 = 0;
91pub const LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_13MS: u8 = 1;
92pub const LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_32MS: u8 = 2;
93pub const LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_64MS: u8 = 3;
94pub const LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_127MS: u8 = 4;
95pub const LASER_RANGE_FINDER_BRICKLET_VERSION_1: u8 = 1;
96pub const LASER_RANGE_FINDER_BRICKLET_VERSION_3: u8 = 3;
97
98#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
99pub struct DistanceCallbackThreshold {
100    pub option: char,
101    pub min: u16,
102    pub max: u16,
103}
104impl FromByteSlice for DistanceCallbackThreshold {
105    fn bytes_expected() -> usize {
106        5
107    }
108    fn from_le_byte_slice(bytes: &[u8]) -> DistanceCallbackThreshold {
109        DistanceCallbackThreshold {
110            option: <char>::from_le_byte_slice(&bytes[0..1]),
111            min: <u16>::from_le_byte_slice(&bytes[1..3]),
112            max: <u16>::from_le_byte_slice(&bytes[3..5]),
113        }
114    }
115}
116
117#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
118pub struct VelocityCallbackThreshold {
119    pub option: char,
120    pub min: i16,
121    pub max: i16,
122}
123impl FromByteSlice for VelocityCallbackThreshold {
124    fn bytes_expected() -> usize {
125        5
126    }
127    fn from_le_byte_slice(bytes: &[u8]) -> VelocityCallbackThreshold {
128        VelocityCallbackThreshold {
129            option: <char>::from_le_byte_slice(&bytes[0..1]),
130            min: <i16>::from_le_byte_slice(&bytes[1..3]),
131            max: <i16>::from_le_byte_slice(&bytes[3..5]),
132        }
133    }
134}
135
136#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
137pub struct MovingAverage {
138    pub distance_average_length: u8,
139    pub velocity_average_length: u8,
140}
141impl FromByteSlice for MovingAverage {
142    fn bytes_expected() -> usize {
143        2
144    }
145    fn from_le_byte_slice(bytes: &[u8]) -> MovingAverage {
146        MovingAverage {
147            distance_average_length: <u8>::from_le_byte_slice(&bytes[0..1]),
148            velocity_average_length: <u8>::from_le_byte_slice(&bytes[1..2]),
149        }
150    }
151}
152
153#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
154pub struct Configuration {
155    pub acquisition_count: u8,
156    pub enable_quick_termination: bool,
157    pub threshold_value: u8,
158    pub measurement_frequency: u16,
159}
160impl FromByteSlice for Configuration {
161    fn bytes_expected() -> usize {
162        5
163    }
164    fn from_le_byte_slice(bytes: &[u8]) -> Configuration {
165        Configuration {
166            acquisition_count: <u8>::from_le_byte_slice(&bytes[0..1]),
167            enable_quick_termination: <bool>::from_le_byte_slice(&bytes[1..2]),
168            threshold_value: <u8>::from_le_byte_slice(&bytes[2..3]),
169            measurement_frequency: <u16>::from_le_byte_slice(&bytes[3..5]),
170        }
171    }
172}
173
174#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
175pub struct Identity {
176    pub uid: String,
177    pub connected_uid: String,
178    pub position: char,
179    pub hardware_version: [u8; 3],
180    pub firmware_version: [u8; 3],
181    pub device_identifier: u16,
182}
183impl FromByteSlice for Identity {
184    fn bytes_expected() -> usize {
185        25
186    }
187    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
188        Identity {
189            uid: <String>::from_le_byte_slice(&bytes[0..8]),
190            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
191            position: <char>::from_le_byte_slice(&bytes[16..17]),
192            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
193            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
194            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
195        }
196    }
197}
198
199/// Measures distance up to 40m with laser light
200#[derive(Clone)]
201pub struct LaserRangeFinderBricklet {
202    device: Device,
203}
204impl LaserRangeFinderBricklet {
205    pub const DEVICE_IDENTIFIER: u16 = 255;
206    pub const DEVICE_DISPLAY_NAME: &'static str = "Laser Range Finder Bricklet";
207    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
208    pub fn new(uid: Uid, connection: AsyncIpConnection) -> LaserRangeFinderBricklet {
209        let mut result = LaserRangeFinderBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
210        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetDistance) as usize] =
211            ResponseExpectedFlag::AlwaysTrue;
212        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetVelocity) as usize] =
213            ResponseExpectedFlag::AlwaysTrue;
214        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetDistanceCallbackPeriod) as usize] =
215            ResponseExpectedFlag::True;
216        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetDistanceCallbackPeriod) as usize] =
217            ResponseExpectedFlag::AlwaysTrue;
218        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetVelocityCallbackPeriod) as usize] =
219            ResponseExpectedFlag::True;
220        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetVelocityCallbackPeriod) as usize] =
221            ResponseExpectedFlag::AlwaysTrue;
222        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetDistanceCallbackThreshold) as usize] =
223            ResponseExpectedFlag::True;
224        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetDistanceCallbackThreshold) as usize] =
225            ResponseExpectedFlag::AlwaysTrue;
226        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetVelocityCallbackThreshold) as usize] =
227            ResponseExpectedFlag::True;
228        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetVelocityCallbackThreshold) as usize] =
229            ResponseExpectedFlag::AlwaysTrue;
230        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetDebouncePeriod) as usize] =
231            ResponseExpectedFlag::True;
232        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetDebouncePeriod) as usize] =
233            ResponseExpectedFlag::AlwaysTrue;
234        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetMovingAverage) as usize] =
235            ResponseExpectedFlag::False;
236        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetMovingAverage) as usize] =
237            ResponseExpectedFlag::AlwaysTrue;
238        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetMode) as usize] = ResponseExpectedFlag::False;
239        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
240        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::EnableLaser) as usize] = ResponseExpectedFlag::False;
241        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::DisableLaser) as usize] = ResponseExpectedFlag::False;
242        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::IsLaserEnabled) as usize] =
243            ResponseExpectedFlag::AlwaysTrue;
244        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetSensorHardwareVersion) as usize] =
245            ResponseExpectedFlag::AlwaysTrue;
246        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetConfiguration) as usize] =
247            ResponseExpectedFlag::False;
248        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetConfiguration) as usize] =
249            ResponseExpectedFlag::AlwaysTrue;
250        result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetIdentity) as usize] =
251            ResponseExpectedFlag::AlwaysTrue;
252        result
253    }
254
255    /// Returns the response expected flag for the function specified by the function ID parameter.
256    /// It is true if the function is expected to send a response, false otherwise.
257    ///
258    /// For getter functions this is enabled by default and cannot be disabled, because those
259    /// functions will always send a response. For callback configuration functions it is enabled
260    /// by default too, but can be disabled by [`set_response_expected`](crate::laser_range_finder_bricklet::LaserRangeFinderBricklet::set_response_expected).
261    /// For setter functions it is disabled by default and can be enabled.
262    ///
263    /// Enabling the response expected flag for a setter function allows to detect timeouts
264    /// and other error conditions calls of this setter as well. The device will then send a response
265    /// for this purpose. If this flag is disabled for a setter function then no response is sent
266    /// and errors are silently ignored, because they cannot be detected.
267    ///
268    /// See [`set_response_expected`](crate::laser_range_finder_bricklet::LaserRangeFinderBricklet::set_response_expected) for the list of function ID constants available for this function.
269    pub fn get_response_expected(&mut self, fun: LaserRangeFinderBrickletFunction) -> Result<bool, GetResponseExpectedError> {
270        self.device.get_response_expected(u8::from(fun))
271    }
272
273    /// Changes the response expected flag of the function specified by the function ID parameter.
274    /// This flag can only be changed for setter (default value: false) and callback configuration
275    /// functions (default value: true). For getter functions it is always enabled.
276    ///
277    /// Enabling the response expected flag for a setter function allows to detect timeouts and
278    /// other error conditions calls of this setter as well. The device will then send a response
279    /// for this purpose. If this flag is disabled for a setter function then no response is sent
280    /// and errors are silently ignored, because they cannot be detected.
281    pub fn set_response_expected(
282        &mut self,
283        fun: LaserRangeFinderBrickletFunction,
284        response_expected: bool,
285    ) -> Result<(), SetResponseExpectedError> {
286        self.device.set_response_expected(u8::from(fun), response_expected)
287    }
288
289    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
290    pub fn set_response_expected_all(&mut self, response_expected: bool) {
291        self.device.set_response_expected_all(response_expected)
292    }
293
294    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
295    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
296    pub fn get_api_version(&self) -> [u8; 3] {
297        self.device.api_version
298    }
299
300    /// This receiver is triggered periodically with the period that is set by
301    /// [`set_distance_callback_period`]. The parameter is the distance
302    /// value of the sensor.
303    ///
304    /// The [`get_distance_callback_receiver`] receiver is only triggered if the distance value has changed
305    /// since the last triggering.
306    ///
307    /// [`set_distance_callback_period`]: #method.set_distance_callback_period
308    /// [`get_distance_callback_receiver`]: #method.get_distance_callback_receiver
309    pub async fn get_distance_callback_receiver(&mut self) -> impl Stream<Item = u16> {
310        self.device
311            .get_callback_receiver(u8::from(LaserRangeFinderBrickletFunction::CallbackDistance))
312            .await
313            .map(|p| u16::from_le_byte_slice(p.body()))
314    }
315
316    /// This receiver is triggered periodically with the period that is set by
317    /// [`set_velocity_callback_period`]. The parameter is the velocity
318    /// value of the sensor.
319    ///
320    /// The [`get_velocity_callback_receiver`] receiver is only triggered if the velocity has changed since
321    /// the last triggering.
322    pub async fn get_velocity_callback_receiver(&mut self) -> impl Stream<Item = i16> {
323        self.device
324            .get_callback_receiver(u8::from(LaserRangeFinderBrickletFunction::CallbackVelocity))
325            .await
326            .map(|p| i16::from_le_byte_slice(p.body()))
327    }
328
329    /// This receiver is triggered when the threshold as set by
330    /// [`set_distance_callback_threshold`] is reached.
331    /// The parameter is the distance value of the sensor.
332    ///
333    /// If the threshold keeps being reached, the receiver is triggered periodically
334    /// with the period as set by [`set_debounce_period`].
335    pub async fn get_distance_reached_callback_receiver(&mut self) -> impl Stream<Item = u16> {
336        self.device
337            .get_callback_receiver(u8::from(LaserRangeFinderBrickletFunction::CallbackDistanceReached))
338            .await
339            .map(|p| u16::from_le_byte_slice(p.body()))
340    }
341
342    /// This receiver is triggered when the threshold as set by
343    /// [`set_velocity_callback_threshold`] is reached.
344    /// The parameter is the velocity value of the sensor.
345    ///
346    /// If the threshold keeps being reached, the receiver is triggered periodically
347    /// with the period as set by [`set_debounce_period`].
348    pub async fn get_velocity_reached_callback_receiver(&mut self) -> impl Stream<Item = i16> {
349        self.device
350            .get_callback_receiver(u8::from(LaserRangeFinderBrickletFunction::CallbackVelocityReached))
351            .await
352            .map(|p| i16::from_le_byte_slice(p.body()))
353    }
354
355    /// Returns the measured distance.
356    ///
357    /// Sensor hardware version 1 (see [`get_sensor_hardware_version`]) cannot
358    /// measure distance and velocity at the same time. Therefore, the distance mode
359    /// has to be enabled using [`set_mode`].
360    /// Sensor hardware version 3 can measure distance and velocity at the same
361    /// time. Also the laser has to be enabled, see [`enable_laser`].
362    ///
363    /// If you want to get the distance periodically, it is recommended to
364    /// use the [`get_distance_callback_receiver`] receiver and set the period with
365    /// [`set_distance_callback_period`].
366    pub async fn get_distance(&mut self) -> Result<u16, TinkerforgeError> {
367        let payload = [0; 0];
368
369        #[allow(unused_variables)]
370        let result = self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetDistance), &payload).await?;
371        Ok(u16::from_le_byte_slice(result.body()))
372    }
373
374    /// Returns the measured velocity.
375    ///
376    /// Sensor hardware version 1 (see [`get_sensor_hardware_version`]) cannot
377    /// measure distance and velocity at the same time. Therefore, the velocity mode
378    /// has to be enabled using [`set_mode`].
379    /// Sensor hardware version 3 can measure distance and velocity at the same
380    /// time, but the velocity measurement only produces stables results if a fixed
381    /// measurement rate (see [`set_configuration`]) is configured. Also the laser
382    /// has to be enabled, see [`enable_laser`].
383    ///
384    /// If you want to get the velocity periodically, it is recommended to
385    /// use the [`get_velocity_callback_receiver`] receiver and set the period with
386    /// [`set_velocity_callback_period`].
387    pub async fn get_velocity(&mut self) -> Result<i16, TinkerforgeError> {
388        let payload = [0; 0];
389
390        #[allow(unused_variables)]
391        let result = self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetVelocity), &payload).await?;
392        Ok(i16::from_le_byte_slice(result.body()))
393    }
394
395    /// Sets the period with which the [`get_distance_callback_receiver`] receiver is triggered
396    /// periodically. A value of 0 turns the receiver off.
397    ///
398    /// The [`get_distance_callback_receiver`] receiver is only triggered if the distance value has
399    /// changed since the last triggering.
400    pub async fn set_distance_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
401        let mut payload = [0; 4];
402        period.write_to_slice(&mut payload[0..4]);
403
404        #[allow(unused_variables)]
405        let result = self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetDistanceCallbackPeriod), &payload).await?;
406        Ok(())
407    }
408
409    /// Returns the period as set by [`set_distance_callback_period`].
410    pub async fn get_distance_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
411        let payload = [0; 0];
412
413        #[allow(unused_variables)]
414        let result = self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetDistanceCallbackPeriod), &payload).await?;
415        Ok(u32::from_le_byte_slice(result.body()))
416    }
417
418    /// Sets the period with which the [`get_velocity_callback_receiver`] receiver is triggered
419    /// periodically. A value of 0 turns the receiver off.
420    ///
421    /// The [`get_velocity_callback_receiver`] receiver is only triggered if the velocity value has
422    /// changed since the last triggering.
423    pub async fn set_velocity_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
424        let mut payload = [0; 4];
425        period.write_to_slice(&mut payload[0..4]);
426
427        #[allow(unused_variables)]
428        let result = self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetVelocityCallbackPeriod), &payload).await?;
429        Ok(())
430    }
431
432    /// Returns the period as set by [`set_velocity_callback_period`].
433    pub async fn get_velocity_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
434        let payload = [0; 0];
435
436        #[allow(unused_variables)]
437        let result = self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetVelocityCallbackPeriod), &payload).await?;
438        Ok(u32::from_le_byte_slice(result.body()))
439    }
440
441    /// Sets the thresholds for the [`get_distance_reached_callback_receiver`] receiver.
442    ///
443    /// The following options are possible:
444    ///
445    ///  Option| Description
446    ///  --- | ---
447    ///  'x'|    Receiver is turned off
448    ///  'o'|    Receiver is triggered when the distance value is *outside* the min and max values
449    ///  'i'|    Receiver is triggered when the distance value is *inside* the min and max values
450    ///  '<'|    Receiver is triggered when the distance value is smaller than the min value (max is ignored)
451    ///  '>'|    Receiver is triggered when the distance value is greater than the min value (max is ignored)
452    ///
453    /// Associated constants:
454    /// * LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_OFF
455    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_OUTSIDE
456    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_INSIDE
457    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_SMALLER
458    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_GREATER
459    pub async fn set_distance_callback_threshold(&mut self, option: char, min: u16, max: u16) -> Result<(), TinkerforgeError> {
460        let mut payload = [0; 5];
461        option.write_to_slice(&mut payload[0..1]);
462        min.write_to_slice(&mut payload[1..3]);
463        max.write_to_slice(&mut payload[3..5]);
464
465        #[allow(unused_variables)]
466        let result = self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetDistanceCallbackThreshold), &payload).await?;
467        Ok(())
468    }
469
470    /// Returns the threshold as set by [`set_distance_callback_threshold`].
471    ///
472    /// Associated constants:
473    /// * LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_OFF
474    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_OUTSIDE
475    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_INSIDE
476    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_SMALLER
477    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_GREATER
478    pub async fn get_distance_callback_threshold(&mut self) -> Result<DistanceCallbackThreshold, TinkerforgeError> {
479        let payload = [0; 0];
480
481        #[allow(unused_variables)]
482        let result = self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetDistanceCallbackThreshold), &payload).await?;
483        Ok(DistanceCallbackThreshold::from_le_byte_slice(result.body()))
484    }
485
486    /// Sets the thresholds for the [`get_velocity_reached_callback_receiver`] receiver.
487    ///
488    /// The following options are possible:
489    ///
490    ///  Option| Description
491    ///  --- | ---
492    ///  'x'|    Receiver is turned off
493    ///  'o'|    Receiver is triggered when the velocity is *outside* the min and max values
494    ///  'i'|    Receiver is triggered when the velocity is *inside* the min and max values
495    ///  '<'|    Receiver is triggered when the velocity is smaller than the min value (max is ignored)
496    ///  '>'|    Receiver is triggered when the velocity is greater than the min value (max is ignored)
497    ///
498    /// Associated constants:
499    /// * LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_OFF
500    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_OUTSIDE
501    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_INSIDE
502    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_SMALLER
503    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_GREATER
504    pub async fn set_velocity_callback_threshold(&mut self, option: char, min: i16, max: i16) -> Result<(), TinkerforgeError> {
505        let mut payload = [0; 5];
506        option.write_to_slice(&mut payload[0..1]);
507        min.write_to_slice(&mut payload[1..3]);
508        max.write_to_slice(&mut payload[3..5]);
509
510        #[allow(unused_variables)]
511        let result = self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetVelocityCallbackThreshold), &payload).await?;
512        Ok(())
513    }
514
515    /// Returns the threshold as set by [`set_velocity_callback_threshold`].
516    ///
517    /// Associated constants:
518    /// * LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_OFF
519    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_OUTSIDE
520    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_INSIDE
521    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_SMALLER
522    ///	* LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_GREATER
523    pub async fn get_velocity_callback_threshold(&mut self) -> Result<VelocityCallbackThreshold, TinkerforgeError> {
524        let payload = [0; 0];
525
526        #[allow(unused_variables)]
527        let result = self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetVelocityCallbackThreshold), &payload).await?;
528        Ok(VelocityCallbackThreshold::from_le_byte_slice(result.body()))
529    }
530
531    /// Sets the period with which the threshold receivers
532    ///
533    /// * [`get_distance_reached_callback_receiver`],
534    /// * [`get_velocity_reached_callback_receiver`],
535    ///
536    /// are triggered, if the thresholds
537    ///
538    /// * [`set_distance_callback_threshold`],
539    /// * [`set_velocity_callback_threshold`],
540    ///
541    /// keep being reached.
542    pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
543        let mut payload = [0; 4];
544        debounce.write_to_slice(&mut payload[0..4]);
545
546        #[allow(unused_variables)]
547        let result = self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetDebouncePeriod), &payload).await?;
548        Ok(())
549    }
550
551    /// Returns the debounce period as set by [`set_debounce_period`].
552    pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
553        let payload = [0; 0];
554
555        #[allow(unused_variables)]
556        let result = self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetDebouncePeriod), &payload).await?;
557        Ok(u32::from_le_byte_slice(result.body()))
558    }
559
560    /// Sets the length of a [moving averaging](https://en.wikipedia.org/wiki/Moving_average)__
561    /// for the distance and velocity.
562    ///
563    /// Setting the length to 0 will turn the averaging completely off. With less
564    /// averaging, there is more noise on the data.
565    pub async fn set_moving_average(&mut self, distance_average_length: u8, velocity_average_length: u8) -> Result<(), TinkerforgeError> {
566        let mut payload = [0; 2];
567        distance_average_length.write_to_slice(&mut payload[0..1]);
568        velocity_average_length.write_to_slice(&mut payload[1..2]);
569
570        #[allow(unused_variables)]
571        let result = self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetMovingAverage), &payload).await?;
572        Ok(())
573    }
574
575    /// Returns the length moving average as set by [`set_moving_average`].
576    pub async fn get_moving_average(&mut self) -> Result<MovingAverage, TinkerforgeError> {
577        let payload = [0; 0];
578
579        #[allow(unused_variables)]
580        let result = self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetMovingAverage), &payload).await?;
581        Ok(MovingAverage::from_le_byte_slice(result.body()))
582    }
583
584    /// # Note
585    ///  This function is only available if you have a LIDAR-Lite sensor with hardware
586    ///  version 1. Use [`set_configuration`] for hardware version 3. You can check
587    ///  the sensor hardware version using [`get_sensor_hardware_version`].
588    ///
589    /// The LIDAR-Lite sensor (hardware version 1) has five different modes. One mode is
590    /// for distance measurements and four modes are for velocity measurements with
591    /// different ranges.
592    ///
593    /// The following modes are available:
594    ///
595    /// * 0: Distance is measured with resolution 1.0 cm and range 0-4000 cm
596    /// * 1: Velocity is measured with resolution 0.1 m/s and range is 0-12.7 m/s
597    /// * 2: Velocity is measured with resolution 0.25 m/s and range is 0-31.75 m/s
598    /// * 3: Velocity is measured with resolution 0.5 m/s and range is 0-63.5 m/s
599    /// * 4: Velocity is measured with resolution 1.0 m/s and range is 0-127 m/s
600    ///
601    /// Associated constants:
602    /// * LASER_RANGE_FINDER_BRICKLET_MODE_DISTANCE
603    ///	* LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_13MS
604    ///	* LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_32MS
605    ///	* LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_64MS
606    ///	* LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_127MS
607    pub async fn set_mode(&mut self, mode: u8) -> Result<(), TinkerforgeError> {
608        let mut payload = [0; 1];
609        mode.write_to_slice(&mut payload[0..1]);
610
611        #[allow(unused_variables)]
612        let result = self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetMode), &payload).await?;
613        Ok(())
614    }
615
616    /// Returns the mode as set by [`set_mode`].
617    ///
618    /// Associated constants:
619    /// * LASER_RANGE_FINDER_BRICKLET_MODE_DISTANCE
620    ///	* LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_13MS
621    ///	* LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_32MS
622    ///	* LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_64MS
623    ///	* LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_127MS
624    pub async fn get_mode(&mut self) -> Result<u8, TinkerforgeError> {
625        let payload = [0; 0];
626
627        #[allow(unused_variables)]
628        let result = self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetMode), &payload).await?;
629        Ok(u8::from_le_byte_slice(result.body()))
630    }
631
632    /// Activates the laser of the LIDAR.
633    ///
634    /// We recommend that you wait 250ms after enabling the laser before
635    /// the first call of [`get_distance`] to ensure stable measurements.
636    pub async fn enable_laser(&mut self) -> Result<(), TinkerforgeError> {
637        let payload = [0; 0];
638
639        #[allow(unused_variables)]
640        let result = self.device.set(u8::from(LaserRangeFinderBrickletFunction::EnableLaser), &payload).await?;
641        Ok(())
642    }
643
644    /// Deactivates the laser of the LIDAR.
645    pub async fn disable_laser(&mut self) -> Result<(), TinkerforgeError> {
646        let payload = [0; 0];
647
648        #[allow(unused_variables)]
649        let result = self.device.set(u8::from(LaserRangeFinderBrickletFunction::DisableLaser), &payload).await?;
650        Ok(())
651    }
652
653    /// Returns *true* if the laser is enabled, *false* otherwise.
654    pub async fn is_laser_enabled(&mut self) -> Result<bool, TinkerforgeError> {
655        let payload = [0; 0];
656
657        #[allow(unused_variables)]
658        let result = self.device.get(u8::from(LaserRangeFinderBrickletFunction::IsLaserEnabled), &payload).await?;
659        Ok(bool::from_le_byte_slice(result.body()))
660    }
661
662    /// Returns the LIDAR-Lite hardware version.
663    ///
664    ///
665    /// .. versionadded:: 2.0.3$nbsp;(Plugin)
666    ///
667    /// Associated constants:
668    /// * LASER_RANGE_FINDER_BRICKLET_VERSION_1
669    ///	* LASER_RANGE_FINDER_BRICKLET_VERSION_3
670    pub async fn get_sensor_hardware_version(&mut self) -> Result<u8, TinkerforgeError> {
671        let payload = [0; 0];
672
673        #[allow(unused_variables)]
674        let result = self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetSensorHardwareVersion), &payload).await?;
675        Ok(u8::from_le_byte_slice(result.body()))
676    }
677
678    /// # Note
679    ///  This function is only available if you have a LIDAR-Lite sensor with hardware
680    ///  version 3. Use [`set_mode`] for hardware version 1. You can check
681    ///  the sensor hardware version using [`get_sensor_hardware_version`].
682    ///
683    /// The **Acquisition Count** defines the number of times the Laser Range Finder Bricklet
684    /// will integrate acquisitions to find a correlation record peak. With a higher count,
685    /// the Bricklet can measure longer distances. With a lower count, the rate increases. The
686    /// allowed values are 1-255.
687    ///
688    /// If you set **Enable Quick Termination** to true, the distance measurement will be terminated
689    /// early if a high peak was already detected. This means that a higher measurement rate can be achieved
690    /// and long distances can be measured at the same time. However, the chance of false-positive
691    /// distance measurements increases.
692    ///
693    /// Normally the distance is calculated with a detection algorithm that uses peak value,
694    /// signal strength and noise. You can however also define a fixed **Threshold Value**.
695    /// Set this to a low value if you want to measure the distance to something that has
696    /// very little reflection (e.g. glass) and set it to a high value if you want to measure
697    /// the distance to something with a very high reflection (e.g. mirror). Set this to 0 to
698    /// use the default algorithm. The other allowed values are 1-255.
699    ///
700    /// Set the **Measurement Frequency** to force a fixed measurement rate. If set to 0,
701    /// the Laser Range Finder Bricklet will use the optimal frequency according to the other
702    /// configurations and the actual measured distance. Since the rate is not fixed in this case,
703    /// the velocity measurement is not stable. For a stable velocity measurement you should
704    /// set a fixed measurement frequency. The lower the frequency, the higher is the resolution
705    /// of the calculated velocity. The allowed values are 10Hz-500Hz (and 0 to turn the fixed
706    /// frequency off).
707    ///
708    ///
709    /// .. versionadded:: 2.0.3$nbsp;(Plugin)
710    pub async fn set_configuration(
711        &mut self,
712        acquisition_count: u8,
713        enable_quick_termination: bool,
714        threshold_value: u8,
715        measurement_frequency: u16,
716    ) -> Result<(), TinkerforgeError> {
717        let mut payload = [0; 5];
718        acquisition_count.write_to_slice(&mut payload[0..1]);
719        enable_quick_termination.write_to_slice(&mut payload[1..2]);
720        threshold_value.write_to_slice(&mut payload[2..3]);
721        measurement_frequency.write_to_slice(&mut payload[3..5]);
722
723        #[allow(unused_variables)]
724        let result = self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetConfiguration), &payload).await?;
725        Ok(())
726    }
727
728    /// Returns the configuration as set by [`set_configuration`].
729    ///
730    ///
731    /// .. versionadded:: 2.0.3$nbsp;(Plugin)
732    pub async fn get_configuration(&mut self) -> Result<Configuration, TinkerforgeError> {
733        let payload = [0; 0];
734
735        #[allow(unused_variables)]
736        let result = self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetConfiguration), &payload).await?;
737        Ok(Configuration::from_le_byte_slice(result.body()))
738    }
739
740    /// Returns the UID, the UID where the Bricklet is connected to,
741    /// the position, the hardware and firmware version as well as the
742    /// device identifier.
743    ///
744    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
745    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
746    /// position 'z'.
747    ///
748    /// The device identifier numbers can be found [here](device_identifier).
749    /// |device_identifier_constant|
750    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
751        let payload = [0; 0];
752
753        #[allow(unused_variables)]
754        let result = self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetIdentity), &payload).await?;
755        Ok(Identity::from_le_byte_slice(result.body()))
756    }
757}