tinkerforge/bindings/
laser_range_finder_v2_bricklet.rs

1/* ***********************************************************
2 * This file was automatically generated on 2024-02-27.      *
3 *                                                           *
4 * Rust Bindings Version 2.0.21                              *
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/LaserRangeFinderV2_Bricklet_Rust.html).
14use crate::{
15    byte_converter::*, converting_callback_receiver::ConvertingCallbackReceiver, converting_receiver::ConvertingReceiver, device::*,
16    ip_connection::GetRequestSender,
17};
18pub enum LaserRangeFinderV2BrickletFunction {
19    GetDistance,
20    SetDistanceCallbackConfiguration,
21    GetDistanceCallbackConfiguration,
22    GetVelocity,
23    SetVelocityCallbackConfiguration,
24    GetVelocityCallbackConfiguration,
25    SetEnable,
26    GetEnable,
27    SetConfiguration,
28    GetConfiguration,
29    SetMovingAverage,
30    GetMovingAverage,
31    SetOffsetCalibration,
32    GetOffsetCalibration,
33    SetDistanceLedConfig,
34    GetDistanceLedConfig,
35    GetSpitfpErrorCount,
36    SetBootloaderMode,
37    GetBootloaderMode,
38    SetWriteFirmwarePointer,
39    WriteFirmware,
40    SetStatusLedConfig,
41    GetStatusLedConfig,
42    GetChipTemperature,
43    Reset,
44    WriteUid,
45    ReadUid,
46    GetIdentity,
47    CallbackDistance,
48    CallbackVelocity,
49}
50impl From<LaserRangeFinderV2BrickletFunction> for u8 {
51    fn from(fun: LaserRangeFinderV2BrickletFunction) -> Self {
52        match fun {
53            LaserRangeFinderV2BrickletFunction::GetDistance => 1,
54            LaserRangeFinderV2BrickletFunction::SetDistanceCallbackConfiguration => 2,
55            LaserRangeFinderV2BrickletFunction::GetDistanceCallbackConfiguration => 3,
56            LaserRangeFinderV2BrickletFunction::GetVelocity => 5,
57            LaserRangeFinderV2BrickletFunction::SetVelocityCallbackConfiguration => 6,
58            LaserRangeFinderV2BrickletFunction::GetVelocityCallbackConfiguration => 7,
59            LaserRangeFinderV2BrickletFunction::SetEnable => 9,
60            LaserRangeFinderV2BrickletFunction::GetEnable => 10,
61            LaserRangeFinderV2BrickletFunction::SetConfiguration => 11,
62            LaserRangeFinderV2BrickletFunction::GetConfiguration => 12,
63            LaserRangeFinderV2BrickletFunction::SetMovingAverage => 13,
64            LaserRangeFinderV2BrickletFunction::GetMovingAverage => 14,
65            LaserRangeFinderV2BrickletFunction::SetOffsetCalibration => 15,
66            LaserRangeFinderV2BrickletFunction::GetOffsetCalibration => 16,
67            LaserRangeFinderV2BrickletFunction::SetDistanceLedConfig => 17,
68            LaserRangeFinderV2BrickletFunction::GetDistanceLedConfig => 18,
69            LaserRangeFinderV2BrickletFunction::GetSpitfpErrorCount => 234,
70            LaserRangeFinderV2BrickletFunction::SetBootloaderMode => 235,
71            LaserRangeFinderV2BrickletFunction::GetBootloaderMode => 236,
72            LaserRangeFinderV2BrickletFunction::SetWriteFirmwarePointer => 237,
73            LaserRangeFinderV2BrickletFunction::WriteFirmware => 238,
74            LaserRangeFinderV2BrickletFunction::SetStatusLedConfig => 239,
75            LaserRangeFinderV2BrickletFunction::GetStatusLedConfig => 240,
76            LaserRangeFinderV2BrickletFunction::GetChipTemperature => 242,
77            LaserRangeFinderV2BrickletFunction::Reset => 243,
78            LaserRangeFinderV2BrickletFunction::WriteUid => 248,
79            LaserRangeFinderV2BrickletFunction::ReadUid => 249,
80            LaserRangeFinderV2BrickletFunction::GetIdentity => 255,
81            LaserRangeFinderV2BrickletFunction::CallbackDistance => 4,
82            LaserRangeFinderV2BrickletFunction::CallbackVelocity => 8,
83        }
84    }
85}
86pub const LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
87pub const LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
88pub const LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
89pub const LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
90pub const LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
91pub const LASER_RANGE_FINDER_V2_BRICKLET_DISTANCE_LED_CONFIG_OFF: u8 = 0;
92pub const LASER_RANGE_FINDER_V2_BRICKLET_DISTANCE_LED_CONFIG_ON: u8 = 1;
93pub const LASER_RANGE_FINDER_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
94pub const LASER_RANGE_FINDER_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_DISTANCE: u8 = 3;
95pub const LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
96pub const LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
97pub const LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
98pub const LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
99pub const LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
100pub const LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
101pub const LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
102pub const LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
103pub const LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
104pub const LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
105pub const LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
106pub const LASER_RANGE_FINDER_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
107pub const LASER_RANGE_FINDER_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
108pub const LASER_RANGE_FINDER_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
109pub const LASER_RANGE_FINDER_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
110
111#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
112pub struct DistanceCallbackConfiguration {
113    pub period: u32,
114    pub value_has_to_change: bool,
115    pub option: char,
116    pub min: i16,
117    pub max: i16,
118}
119impl FromByteSlice for DistanceCallbackConfiguration {
120    fn bytes_expected() -> usize { 10 }
121    fn from_le_byte_slice(bytes: &[u8]) -> DistanceCallbackConfiguration {
122        DistanceCallbackConfiguration {
123            period: <u32>::from_le_byte_slice(&bytes[0..4]),
124            value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
125            option: <char>::from_le_byte_slice(&bytes[5..6]),
126            min: <i16>::from_le_byte_slice(&bytes[6..8]),
127            max: <i16>::from_le_byte_slice(&bytes[8..10]),
128        }
129    }
130}
131
132#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
133pub struct VelocityCallbackConfiguration {
134    pub period: u32,
135    pub value_has_to_change: bool,
136    pub option: char,
137    pub min: i16,
138    pub max: i16,
139}
140impl FromByteSlice for VelocityCallbackConfiguration {
141    fn bytes_expected() -> usize { 10 }
142    fn from_le_byte_slice(bytes: &[u8]) -> VelocityCallbackConfiguration {
143        VelocityCallbackConfiguration {
144            period: <u32>::from_le_byte_slice(&bytes[0..4]),
145            value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
146            option: <char>::from_le_byte_slice(&bytes[5..6]),
147            min: <i16>::from_le_byte_slice(&bytes[6..8]),
148            max: <i16>::from_le_byte_slice(&bytes[8..10]),
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 { 5 }
162    fn from_le_byte_slice(bytes: &[u8]) -> Configuration {
163        Configuration {
164            acquisition_count: <u8>::from_le_byte_slice(&bytes[0..1]),
165            enable_quick_termination: <bool>::from_le_byte_slice(&bytes[1..2]),
166            threshold_value: <u8>::from_le_byte_slice(&bytes[2..3]),
167            measurement_frequency: <u16>::from_le_byte_slice(&bytes[3..5]),
168        }
169    }
170}
171
172#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
173pub struct MovingAverage {
174    pub distance_average_length: u8,
175    pub velocity_average_length: u8,
176}
177impl FromByteSlice for MovingAverage {
178    fn bytes_expected() -> usize { 2 }
179    fn from_le_byte_slice(bytes: &[u8]) -> MovingAverage {
180        MovingAverage {
181            distance_average_length: <u8>::from_le_byte_slice(&bytes[0..1]),
182            velocity_average_length: <u8>::from_le_byte_slice(&bytes[1..2]),
183        }
184    }
185}
186
187#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
188pub struct SpitfpErrorCount {
189    pub error_count_ack_checksum: u32,
190    pub error_count_message_checksum: u32,
191    pub error_count_frame: u32,
192    pub error_count_overflow: u32,
193}
194impl FromByteSlice for SpitfpErrorCount {
195    fn bytes_expected() -> usize { 16 }
196    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
197        SpitfpErrorCount {
198            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
199            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
200            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
201            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
202        }
203    }
204}
205
206#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
207pub struct Identity {
208    pub uid: String,
209    pub connected_uid: String,
210    pub position: char,
211    pub hardware_version: [u8; 3],
212    pub firmware_version: [u8; 3],
213    pub device_identifier: u16,
214}
215impl FromByteSlice for Identity {
216    fn bytes_expected() -> usize { 25 }
217    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
218        Identity {
219            uid: <String>::from_le_byte_slice(&bytes[0..8]),
220            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
221            position: <char>::from_le_byte_slice(&bytes[16..17]),
222            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
223            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
224            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
225        }
226    }
227}
228
229/// Measures distance up to 40m with laser light
230#[derive(Clone)]
231pub struct LaserRangeFinderV2Bricklet {
232    device: Device,
233}
234impl LaserRangeFinderV2Bricklet {
235    pub const DEVICE_IDENTIFIER: u16 = 2144;
236    pub const DEVICE_DISPLAY_NAME: &'static str = "Laser Range Finder Bricklet 2.0";
237    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
238    pub fn new<T: GetRequestSender>(uid: &str, req_sender: T) -> LaserRangeFinderV2Bricklet {
239        let mut result = LaserRangeFinderV2Bricklet { device: Device::new([2, 0, 0], uid, req_sender, 0) };
240        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::GetDistance) as usize] =
241            ResponseExpectedFlag::AlwaysTrue;
242        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::SetDistanceCallbackConfiguration) as usize] =
243            ResponseExpectedFlag::True;
244        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::GetDistanceCallbackConfiguration) as usize] =
245            ResponseExpectedFlag::AlwaysTrue;
246        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::GetVelocity) as usize] =
247            ResponseExpectedFlag::AlwaysTrue;
248        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::SetVelocityCallbackConfiguration) as usize] =
249            ResponseExpectedFlag::True;
250        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::GetVelocityCallbackConfiguration) as usize] =
251            ResponseExpectedFlag::AlwaysTrue;
252        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::SetEnable) as usize] = ResponseExpectedFlag::False;
253        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::GetEnable) as usize] =
254            ResponseExpectedFlag::AlwaysTrue;
255        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::SetConfiguration) as usize] =
256            ResponseExpectedFlag::False;
257        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::GetConfiguration) as usize] =
258            ResponseExpectedFlag::AlwaysTrue;
259        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::SetMovingAverage) as usize] =
260            ResponseExpectedFlag::False;
261        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::GetMovingAverage) as usize] =
262            ResponseExpectedFlag::AlwaysTrue;
263        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::SetOffsetCalibration) as usize] =
264            ResponseExpectedFlag::False;
265        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::GetOffsetCalibration) as usize] =
266            ResponseExpectedFlag::AlwaysTrue;
267        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::SetDistanceLedConfig) as usize] =
268            ResponseExpectedFlag::False;
269        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::GetDistanceLedConfig) as usize] =
270            ResponseExpectedFlag::AlwaysTrue;
271        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::GetSpitfpErrorCount) as usize] =
272            ResponseExpectedFlag::AlwaysTrue;
273        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::SetBootloaderMode) as usize] =
274            ResponseExpectedFlag::AlwaysTrue;
275        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::GetBootloaderMode) as usize] =
276            ResponseExpectedFlag::AlwaysTrue;
277        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
278            ResponseExpectedFlag::False;
279        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::WriteFirmware) as usize] =
280            ResponseExpectedFlag::AlwaysTrue;
281        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::SetStatusLedConfig) as usize] =
282            ResponseExpectedFlag::False;
283        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::GetStatusLedConfig) as usize] =
284            ResponseExpectedFlag::AlwaysTrue;
285        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::GetChipTemperature) as usize] =
286            ResponseExpectedFlag::AlwaysTrue;
287        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
288        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
289        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
290        result.device.response_expected[u8::from(LaserRangeFinderV2BrickletFunction::GetIdentity) as usize] =
291            ResponseExpectedFlag::AlwaysTrue;
292        result
293    }
294
295    /// Returns the response expected flag for the function specified by the function ID parameter.
296    /// It is true if the function is expected to send a response, false otherwise.
297    ///
298    /// For getter functions this is enabled by default and cannot be disabled, because those
299    /// functions will always send a response. For callback configuration functions it is enabled
300    /// by default too, but can be disabled by [`set_response_expected`](crate::laser_range_finder_v2_bricklet::LaserRangeFinderV2Bricklet::set_response_expected).
301    /// For setter functions it is disabled by default and can be enabled.
302    ///
303    /// Enabling the response expected flag for a setter function allows to detect timeouts
304    /// and other error conditions calls of this setter as well. The device will then send a response
305    /// for this purpose. If this flag is disabled for a setter function then no response is sent
306    /// and errors are silently ignored, because they cannot be detected.
307    ///
308    /// See [`set_response_expected`](crate::laser_range_finder_v2_bricklet::LaserRangeFinderV2Bricklet::set_response_expected) for the list of function ID constants available for this function.
309    pub fn get_response_expected(&mut self, fun: LaserRangeFinderV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
310        self.device.get_response_expected(u8::from(fun))
311    }
312
313    /// Changes the response expected flag of the function specified by the function ID parameter.
314    /// This flag can only be changed for setter (default value: false) and callback configuration
315    /// functions (default value: true). For getter functions it is always enabled.
316    ///
317    /// Enabling the response expected flag for a setter function allows to detect timeouts and
318    /// other error conditions calls of this setter as well. The device will then send a response
319    /// for this purpose. If this flag is disabled for a setter function then no response is sent
320    /// and errors are silently ignored, because they cannot be detected.
321    pub fn set_response_expected(
322        &mut self,
323        fun: LaserRangeFinderV2BrickletFunction,
324        response_expected: bool,
325    ) -> Result<(), SetResponseExpectedError> {
326        self.device.set_response_expected(u8::from(fun), response_expected)
327    }
328
329    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
330    pub fn set_response_expected_all(&mut self, response_expected: bool) { self.device.set_response_expected_all(response_expected) }
331
332    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
333    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
334    pub fn get_api_version(&self) -> [u8; 3] { self.device.api_version }
335
336    /// This receiver is triggered periodically according to the configuration set by
337    /// [`set_distance_callback_configuration`].
338    ///
339    /// The parameter is the same as [`get_distance`].
340    ///
341    /// [`get_distance`]: #method.get_distance
342    /// [`set_distance_callback_configuration`]: #method.set_distance_callback_configuration
343    pub fn get_distance_callback_receiver(&self) -> ConvertingCallbackReceiver<i16> {
344        self.device.get_callback_receiver(u8::from(LaserRangeFinderV2BrickletFunction::CallbackDistance))
345    }
346
347    /// This receiver is triggered periodically according to the configuration set by
348    /// [`set_velocity_callback_configuration`].
349    ///
350    /// The parameter is the same as [`get_velocity`].
351    pub fn get_velocity_callback_receiver(&self) -> ConvertingCallbackReceiver<i16> {
352        self.device.get_callback_receiver(u8::from(LaserRangeFinderV2BrickletFunction::CallbackVelocity))
353    }
354
355    /// Returns the measured distance.
356    ///
357    /// The laser has to be enabled, see [`set_enable`].
358    ///
359    ///
360    /// If you want to get the value periodically, it is recommended to use the
361    /// [`get_distance_callback_receiver`] receiver. You can set the receiver configuration
362    /// with [`set_distance_callback_configuration`].
363    pub fn get_distance(&self) -> ConvertingReceiver<i16> {
364        let payload = vec![0; 0];
365
366        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::GetDistance), payload)
367    }
368
369    /// The period is the period with which the [`get_distance_callback_receiver`] receiver is triggered
370    /// periodically. A value of 0 turns the receiver off.
371    ///
372    /// If the `value has to change`-parameter is set to true, the receiver is only
373    /// triggered after the value has changed. If the value didn't change
374    /// within the period, the receiver is triggered immediately on change.
375    ///
376    /// If it is set to false, the receiver is continuously triggered with the period,
377    /// independent of the value.
378    ///
379    /// It is furthermore possible to constrain the receiver with thresholds.
380    ///
381    /// The `option`-parameter together with min/max sets a threshold for the [`get_distance_callback_receiver`] receiver.
382    ///
383    /// The following options are possible:
384    ///
385    ///  Option| Description
386    ///  --- | ---
387    ///  'x'|    Threshold is turned off
388    ///  'o'|    Threshold is triggered when the value is *outside* the min and max values
389    ///  'i'|    Threshold is triggered when the value is *inside* or equal to the min and max values
390    ///  '<'|    Threshold is triggered when the value is smaller than the min value (max is ignored)
391    ///  '>'|    Threshold is triggered when the value is greater than the min value (max is ignored)
392    ///
393    /// If the option is set to 'x' (threshold turned off) the receiver is triggered with the fixed period.
394    ///
395    /// Associated constants:
396    /// * LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_OFF
397    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
398    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
399    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
400    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_GREATER
401    pub fn set_distance_callback_configuration(
402        &self,
403        period: u32,
404        value_has_to_change: bool,
405        option: char,
406        min: i16,
407        max: i16,
408    ) -> ConvertingReceiver<()> {
409        let mut payload = vec![0; 10];
410        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
411        payload[4..5].copy_from_slice(&<bool>::to_le_byte_vec(value_has_to_change));
412        payload[5..6].copy_from_slice(&<char>::to_le_byte_vec(option));
413        payload[6..8].copy_from_slice(&<i16>::to_le_byte_vec(min));
414        payload[8..10].copy_from_slice(&<i16>::to_le_byte_vec(max));
415
416        self.device.set(u8::from(LaserRangeFinderV2BrickletFunction::SetDistanceCallbackConfiguration), payload)
417    }
418
419    /// Returns the receiver configuration as set by [`set_distance_callback_configuration`].
420    ///
421    /// Associated constants:
422    /// * LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_OFF
423    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
424    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
425    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
426    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_GREATER
427    pub fn get_distance_callback_configuration(&self) -> ConvertingReceiver<DistanceCallbackConfiguration> {
428        let payload = vec![0; 0];
429
430        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::GetDistanceCallbackConfiguration), payload)
431    }
432
433    /// Returns the measured velocity. The value has a range of -12800 to 12700
434    /// and is given in 1/100 m/s.
435    ///
436    /// The velocity measurement only produces stables results if a fixed
437    /// measurement rate (see [`set_configuration`]) is configured. Also the laser
438    /// has to be enabled, see [`set_enable`].
439    ///
440    ///
441    /// If you want to get the value periodically, it is recommended to use the
442    /// [`get_velocity_callback_receiver`] receiver. You can set the receiver configuration
443    /// with [`set_velocity_callback_configuration`].
444    pub fn get_velocity(&self) -> ConvertingReceiver<i16> {
445        let payload = vec![0; 0];
446
447        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::GetVelocity), payload)
448    }
449
450    /// The period is the period with which the [`get_velocity_callback_receiver`] receiver is triggered
451    /// periodically. A value of 0 turns the receiver off.
452    ///
453    /// If the `value has to change`-parameter is set to true, the receiver is only
454    /// triggered after the value has changed. If the value didn't change
455    /// within the period, the receiver is triggered immediately on change.
456    ///
457    /// If it is set to false, the receiver is continuously triggered with the period,
458    /// independent of the value.
459    ///
460    /// It is furthermore possible to constrain the receiver with thresholds.
461    ///
462    /// The `option`-parameter together with min/max sets a threshold for the [`get_velocity_callback_receiver`] receiver.
463    ///
464    /// The following options are possible:
465    ///
466    ///  Option| Description
467    ///  --- | ---
468    ///  'x'|    Threshold is turned off
469    ///  'o'|    Threshold is triggered when the value is *outside* the min and max values
470    ///  'i'|    Threshold is triggered when the value is *inside* or equal to the min and max values
471    ///  '<'|    Threshold is triggered when the value is smaller than the min value (max is ignored)
472    ///  '>'|    Threshold is triggered when the value is greater than the min value (max is ignored)
473    ///
474    /// If the option is set to 'x' (threshold turned off) the receiver is triggered with the fixed period.
475    ///
476    /// Associated constants:
477    /// * LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_OFF
478    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
479    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
480    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
481    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_GREATER
482    pub fn set_velocity_callback_configuration(
483        &self,
484        period: u32,
485        value_has_to_change: bool,
486        option: char,
487        min: i16,
488        max: i16,
489    ) -> ConvertingReceiver<()> {
490        let mut payload = vec![0; 10];
491        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
492        payload[4..5].copy_from_slice(&<bool>::to_le_byte_vec(value_has_to_change));
493        payload[5..6].copy_from_slice(&<char>::to_le_byte_vec(option));
494        payload[6..8].copy_from_slice(&<i16>::to_le_byte_vec(min));
495        payload[8..10].copy_from_slice(&<i16>::to_le_byte_vec(max));
496
497        self.device.set(u8::from(LaserRangeFinderV2BrickletFunction::SetVelocityCallbackConfiguration), payload)
498    }
499
500    /// Returns the receiver configuration as set by [`set_velocity_callback_configuration`].
501    ///
502    /// Associated constants:
503    /// * LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_OFF
504    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
505    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
506    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
507    ///	* LASER_RANGE_FINDER_V2_BRICKLET_THRESHOLD_OPTION_GREATER
508    pub fn get_velocity_callback_configuration(&self) -> ConvertingReceiver<VelocityCallbackConfiguration> {
509        let payload = vec![0; 0];
510
511        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::GetVelocityCallbackConfiguration), payload)
512    }
513
514    /// Enables the laser of the LIDAR if set to *true*.
515    ///
516    /// We recommend that you wait 250ms after enabling the laser before
517    /// the first call of [`get_distance`] to ensure stable measurements.
518    pub fn set_enable(&self, enable: bool) -> ConvertingReceiver<()> {
519        let mut payload = vec![0; 1];
520        payload[0..1].copy_from_slice(&<bool>::to_le_byte_vec(enable));
521
522        self.device.set(u8::from(LaserRangeFinderV2BrickletFunction::SetEnable), payload)
523    }
524
525    /// Returns the value as set by [`set_enable`].
526    pub fn get_enable(&self) -> ConvertingReceiver<bool> {
527        let payload = vec![0; 0];
528
529        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::GetEnable), payload)
530    }
531
532    /// The **Acquisition Count** defines the number of times the Laser Range Finder Bricklet
533    /// will integrate acquisitions to find a correlation record peak. With a higher count,
534    /// the Bricklet can measure longer distances. With a lower count, the rate increases. The
535    /// allowed values are 1-255.
536    ///
537    /// If you set **Enable Quick Termination** to true, the distance measurement will be terminated
538    /// early if a high peak was already detected. This means that a higher measurement rate can be achieved
539    /// and long distances can be measured at the same time. However, the chance of false-positive
540    /// distance measurements increases.
541    ///
542    /// Normally the distance is calculated with a detection algorithm that uses peak value,
543    /// signal strength and noise. You can however also define a fixed **Threshold Value**.
544    /// Set this to a low value if you want to measure the distance to something that has
545    /// very little reflection (e.g. glass) and set it to a high value if you want to measure
546    /// the distance to something with a very high reflection (e.g. mirror). Set this to 0 to
547    /// use the default algorithm. The other allowed values are 1-255.
548    ///
549    /// Set the **Measurement Frequency** to force a fixed measurement rate. If set to 0,
550    /// the Laser Range Finder Bricklet will use the optimal frequency according to the other
551    /// configurations and the actual measured distance. Since the rate is not fixed in this case,
552    /// the velocity measurement is not stable. For a stable velocity measurement you should
553    /// set a fixed measurement frequency. The lower the frequency, the higher is the resolution
554    /// of the calculated velocity. The allowed values are 10Hz-500Hz (and 0 to turn the fixed
555    /// frequency off).
556    ///
557    /// The default values for Acquisition Count, Enable Quick Termination, Threshold Value and
558    /// Measurement Frequency are 128, false, 0 and 0.
559    pub fn set_configuration(
560        &self,
561        acquisition_count: u8,
562        enable_quick_termination: bool,
563        threshold_value: u8,
564        measurement_frequency: u16,
565    ) -> ConvertingReceiver<()> {
566        let mut payload = vec![0; 5];
567        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(acquisition_count));
568        payload[1..2].copy_from_slice(&<bool>::to_le_byte_vec(enable_quick_termination));
569        payload[2..3].copy_from_slice(&<u8>::to_le_byte_vec(threshold_value));
570        payload[3..5].copy_from_slice(&<u16>::to_le_byte_vec(measurement_frequency));
571
572        self.device.set(u8::from(LaserRangeFinderV2BrickletFunction::SetConfiguration), payload)
573    }
574
575    /// Returns the configuration as set by [`set_configuration`].
576    pub fn get_configuration(&self) -> ConvertingReceiver<Configuration> {
577        let payload = vec![0; 0];
578
579        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::GetConfiguration), payload)
580    }
581
582    /// Sets the length of a [moving averaging](https://en.wikipedia.org/wiki/Moving_average)__
583    /// for the distance and velocity.
584    ///
585    /// Setting the length to 0 will turn the averaging completely off. With less
586    /// averaging, there is more noise on the data.
587    pub fn set_moving_average(&self, distance_average_length: u8, velocity_average_length: u8) -> ConvertingReceiver<()> {
588        let mut payload = vec![0; 2];
589        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(distance_average_length));
590        payload[1..2].copy_from_slice(&<u8>::to_le_byte_vec(velocity_average_length));
591
592        self.device.set(u8::from(LaserRangeFinderV2BrickletFunction::SetMovingAverage), payload)
593    }
594
595    /// Returns the length moving average as set by [`set_moving_average`].
596    pub fn get_moving_average(&self) -> ConvertingReceiver<MovingAverage> {
597        let payload = vec![0; 0];
598
599        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::GetMovingAverage), payload)
600    }
601
602    /// The offset is added to the measured distance.
603    /// It is saved in non-volatile memory, you only have to set it once.
604    ///
605    /// The Bricklet comes with a per-sensor factory-calibrated offset value,
606    /// you should not have to call this function.
607    ///
608    /// If you want to re-calibrate the offset you first have to set it to 0.
609    /// Calculate the offset by measuring the distance to a known distance
610    /// and set it again.
611    pub fn set_offset_calibration(&self, offset: i16) -> ConvertingReceiver<()> {
612        let mut payload = vec![0; 2];
613        payload[0..2].copy_from_slice(&<i16>::to_le_byte_vec(offset));
614
615        self.device.set(u8::from(LaserRangeFinderV2BrickletFunction::SetOffsetCalibration), payload)
616    }
617
618    /// Returns the offset value as set by [`set_offset_calibration`].
619    pub fn get_offset_calibration(&self) -> ConvertingReceiver<i16> {
620        let payload = vec![0; 0];
621
622        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::GetOffsetCalibration), payload)
623    }
624
625    /// Configures the distance LED to be either turned off, turned on, blink in
626    /// heartbeat mode or show the distance (brighter = object is nearer).
627    ///
628    /// Associated constants:
629    /// * LASER_RANGE_FINDER_V2_BRICKLET_DISTANCE_LED_CONFIG_OFF
630    ///	* LASER_RANGE_FINDER_V2_BRICKLET_DISTANCE_LED_CONFIG_ON
631    ///	* LASER_RANGE_FINDER_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_HEARTBEAT
632    ///	* LASER_RANGE_FINDER_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_DISTANCE
633    pub fn set_distance_led_config(&self, config: u8) -> ConvertingReceiver<()> {
634        let mut payload = vec![0; 1];
635        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(config));
636
637        self.device.set(u8::from(LaserRangeFinderV2BrickletFunction::SetDistanceLedConfig), payload)
638    }
639
640    /// Returns the LED configuration as set by [`set_distance_led_config`]
641    ///
642    /// Associated constants:
643    /// * LASER_RANGE_FINDER_V2_BRICKLET_DISTANCE_LED_CONFIG_OFF
644    ///	* LASER_RANGE_FINDER_V2_BRICKLET_DISTANCE_LED_CONFIG_ON
645    ///	* LASER_RANGE_FINDER_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_HEARTBEAT
646    ///	* LASER_RANGE_FINDER_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_DISTANCE
647    pub fn get_distance_led_config(&self) -> ConvertingReceiver<u8> {
648        let payload = vec![0; 0];
649
650        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::GetDistanceLedConfig), payload)
651    }
652
653    /// Returns the error count for the communication between Brick and Bricklet.
654    ///
655    /// The errors are divided into
656    ///
657    /// * ACK checksum errors,
658    /// * message checksum errors,
659    /// * framing errors and
660    /// * overflow errors.
661    ///
662    /// The errors counts are for errors that occur on the Bricklet side. All
663    /// Bricks have a similar function that returns the errors on the Brick side.
664    pub fn get_spitfp_error_count(&self) -> ConvertingReceiver<SpitfpErrorCount> {
665        let payload = vec![0; 0];
666
667        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::GetSpitfpErrorCount), payload)
668    }
669
670    /// Sets the bootloader mode and returns the status after the requested
671    /// mode change was instigated.
672    ///
673    /// You can change from bootloader mode to firmware mode and vice versa. A change
674    /// from bootloader mode to firmware mode will only take place if the entry function,
675    /// device identifier and CRC are present and correct.
676    ///
677    /// This function is used by Brick Viewer during flashing. It should not be
678    /// necessary to call it in a normal user program.
679    ///
680    /// Associated constants:
681    /// * LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
682    ///	* LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
683    ///	* LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
684    ///	* LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
685    ///	* LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
686    ///	* LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_STATUS_OK
687    ///	* LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
688    ///	* LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
689    ///	* LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
690    ///	* LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
691    ///	* LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
692    pub fn set_bootloader_mode(&self, mode: u8) -> ConvertingReceiver<u8> {
693        let mut payload = vec![0; 1];
694        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(mode));
695
696        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::SetBootloaderMode), payload)
697    }
698
699    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
700    ///
701    /// Associated constants:
702    /// * LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
703    ///	* LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
704    ///	* LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
705    ///	* LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
706    ///	* LASER_RANGE_FINDER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
707    pub fn get_bootloader_mode(&self) -> ConvertingReceiver<u8> {
708        let payload = vec![0; 0];
709
710        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::GetBootloaderMode), payload)
711    }
712
713    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
714    /// to be increased by chunks of size 64. The data is written to flash
715    /// every 4 chunks (which equals to one page of size 256).
716    ///
717    /// This function is used by Brick Viewer during flashing. It should not be
718    /// necessary to call it in a normal user program.
719    pub fn set_write_firmware_pointer(&self, pointer: u32) -> ConvertingReceiver<()> {
720        let mut payload = vec![0; 4];
721        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(pointer));
722
723        self.device.set(u8::from(LaserRangeFinderV2BrickletFunction::SetWriteFirmwarePointer), payload)
724    }
725
726    /// Writes 64 Bytes of firmware at the position as written by
727    /// [`set_write_firmware_pointer`] before. The firmware is written
728    /// to flash every 4 chunks.
729    ///
730    /// You can only write firmware in bootloader mode.
731    ///
732    /// This function is used by Brick Viewer during flashing. It should not be
733    /// necessary to call it in a normal user program.
734    pub fn write_firmware(&self, data: [u8; 64]) -> ConvertingReceiver<u8> {
735        let mut payload = vec![0; 64];
736        payload[0..64].copy_from_slice(&<[u8; 64]>::to_le_byte_vec(data));
737
738        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::WriteFirmware), payload)
739    }
740
741    /// Sets the status LED configuration. By default the LED shows
742    /// communication traffic between Brick and Bricklet, it flickers once
743    /// for every 10 received data packets.
744    ///
745    /// You can also turn the LED permanently on/off or show a heartbeat.
746    ///
747    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
748    ///
749    /// Associated constants:
750    /// * LASER_RANGE_FINDER_V2_BRICKLET_STATUS_LED_CONFIG_OFF
751    ///	* LASER_RANGE_FINDER_V2_BRICKLET_STATUS_LED_CONFIG_ON
752    ///	* LASER_RANGE_FINDER_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
753    ///	* LASER_RANGE_FINDER_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
754    pub fn set_status_led_config(&self, config: u8) -> ConvertingReceiver<()> {
755        let mut payload = vec![0; 1];
756        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(config));
757
758        self.device.set(u8::from(LaserRangeFinderV2BrickletFunction::SetStatusLedConfig), payload)
759    }
760
761    /// Returns the configuration as set by [`set_status_led_config`]
762    ///
763    /// Associated constants:
764    /// * LASER_RANGE_FINDER_V2_BRICKLET_STATUS_LED_CONFIG_OFF
765    ///	* LASER_RANGE_FINDER_V2_BRICKLET_STATUS_LED_CONFIG_ON
766    ///	* LASER_RANGE_FINDER_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
767    ///	* LASER_RANGE_FINDER_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
768    pub fn get_status_led_config(&self) -> ConvertingReceiver<u8> {
769        let payload = vec![0; 0];
770
771        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::GetStatusLedConfig), payload)
772    }
773
774    /// Returns the temperature as measured inside the microcontroller. The
775    /// value returned is not the ambient temperature!
776    ///
777    /// The temperature is only proportional to the real temperature and it has bad
778    /// accuracy. Practically it is only useful as an indicator for
779    /// temperature changes.
780    pub fn get_chip_temperature(&self) -> ConvertingReceiver<i16> {
781        let payload = vec![0; 0];
782
783        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::GetChipTemperature), payload)
784    }
785
786    /// Calling this function will reset the Bricklet. All configurations
787    /// will be lost.
788    ///
789    /// After a reset you have to create new device objects,
790    /// calling functions on the existing ones will result in
791    /// undefined behavior!
792    pub fn reset(&self) -> ConvertingReceiver<()> {
793        let payload = vec![0; 0];
794
795        self.device.set(u8::from(LaserRangeFinderV2BrickletFunction::Reset), payload)
796    }
797
798    /// Writes a new UID into flash. If you want to set a new UID
799    /// you have to decode the Base58 encoded UID string into an
800    /// integer first.
801    ///
802    /// We recommend that you use Brick Viewer to change the UID.
803    pub fn write_uid(&self, uid: u32) -> ConvertingReceiver<()> {
804        let mut payload = vec![0; 4];
805        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(uid));
806
807        self.device.set(u8::from(LaserRangeFinderV2BrickletFunction::WriteUid), payload)
808    }
809
810    /// Returns the current UID as an integer. Encode as
811    /// Base58 to get the usual string version.
812    pub fn read_uid(&self) -> ConvertingReceiver<u32> {
813        let payload = vec![0; 0];
814
815        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::ReadUid), payload)
816    }
817
818    /// Returns the UID, the UID where the Bricklet is connected to,
819    /// the position, the hardware and firmware version as well as the
820    /// device identifier.
821    ///
822    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
823    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
824    /// position 'z'.
825    ///
826    /// The device identifier numbers can be found [here](device_identifier).
827    /// |device_identifier_constant|
828    pub fn get_identity(&self) -> ConvertingReceiver<Identity> {
829        let payload = vec![0; 0];
830
831        self.device.get(u8::from(LaserRangeFinderV2BrickletFunction::GetIdentity), payload)
832    }
833}