tinkerforge_async/bindings/
distance_ir_v2_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 150cm with infrared light.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/DistanceIRV2_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 DistanceIrV2BrickletFunction {
24    GetDistance,
25    SetDistanceCallbackConfiguration,
26    GetDistanceCallbackConfiguration,
27    GetAnalogValue,
28    SetAnalogValueCallbackConfiguration,
29    GetAnalogValueCallbackConfiguration,
30    SetMovingAverageConfiguration,
31    GetMovingAverageConfiguration,
32    SetDistanceLedConfig,
33    GetDistanceLedConfig,
34    SetSensorType,
35    GetSensorType,
36    GetSpitfpErrorCount,
37    SetBootloaderMode,
38    GetBootloaderMode,
39    SetWriteFirmwarePointer,
40    WriteFirmware,
41    SetStatusLedConfig,
42    GetStatusLedConfig,
43    GetChipTemperature,
44    Reset,
45    WriteUid,
46    ReadUid,
47    GetIdentity,
48    CallbackDistance,
49    CallbackAnalogValue,
50}
51impl From<DistanceIrV2BrickletFunction> for u8 {
52    fn from(fun: DistanceIrV2BrickletFunction) -> Self {
53        match fun {
54            DistanceIrV2BrickletFunction::GetDistance => 1,
55            DistanceIrV2BrickletFunction::SetDistanceCallbackConfiguration => 2,
56            DistanceIrV2BrickletFunction::GetDistanceCallbackConfiguration => 3,
57            DistanceIrV2BrickletFunction::GetAnalogValue => 5,
58            DistanceIrV2BrickletFunction::SetAnalogValueCallbackConfiguration => 6,
59            DistanceIrV2BrickletFunction::GetAnalogValueCallbackConfiguration => 7,
60            DistanceIrV2BrickletFunction::SetMovingAverageConfiguration => 9,
61            DistanceIrV2BrickletFunction::GetMovingAverageConfiguration => 10,
62            DistanceIrV2BrickletFunction::SetDistanceLedConfig => 11,
63            DistanceIrV2BrickletFunction::GetDistanceLedConfig => 12,
64            DistanceIrV2BrickletFunction::SetSensorType => 13,
65            DistanceIrV2BrickletFunction::GetSensorType => 14,
66            DistanceIrV2BrickletFunction::GetSpitfpErrorCount => 234,
67            DistanceIrV2BrickletFunction::SetBootloaderMode => 235,
68            DistanceIrV2BrickletFunction::GetBootloaderMode => 236,
69            DistanceIrV2BrickletFunction::SetWriteFirmwarePointer => 237,
70            DistanceIrV2BrickletFunction::WriteFirmware => 238,
71            DistanceIrV2BrickletFunction::SetStatusLedConfig => 239,
72            DistanceIrV2BrickletFunction::GetStatusLedConfig => 240,
73            DistanceIrV2BrickletFunction::GetChipTemperature => 242,
74            DistanceIrV2BrickletFunction::Reset => 243,
75            DistanceIrV2BrickletFunction::WriteUid => 248,
76            DistanceIrV2BrickletFunction::ReadUid => 249,
77            DistanceIrV2BrickletFunction::GetIdentity => 255,
78            DistanceIrV2BrickletFunction::CallbackDistance => 4,
79            DistanceIrV2BrickletFunction::CallbackAnalogValue => 8,
80        }
81    }
82}
83pub const DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
84pub const DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
85pub const DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
86pub const DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
87pub const DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
88pub const DISTANCE_IR_V2_BRICKLET_DISTANCE_LED_CONFIG_OFF: u8 = 0;
89pub const DISTANCE_IR_V2_BRICKLET_DISTANCE_LED_CONFIG_ON: u8 = 1;
90pub const DISTANCE_IR_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
91pub const DISTANCE_IR_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_DISTANCE: u8 = 3;
92pub const DISTANCE_IR_V2_BRICKLET_SENSOR_TYPE_2Y0A41: u8 = 0;
93pub const DISTANCE_IR_V2_BRICKLET_SENSOR_TYPE_2Y0A21: u8 = 1;
94pub const DISTANCE_IR_V2_BRICKLET_SENSOR_TYPE_2Y0A02: u8 = 2;
95pub const DISTANCE_IR_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
96pub const DISTANCE_IR_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
97pub const DISTANCE_IR_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
98pub const DISTANCE_IR_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
99pub const DISTANCE_IR_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
100pub const DISTANCE_IR_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
101pub const DISTANCE_IR_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
102pub const DISTANCE_IR_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
103pub const DISTANCE_IR_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
104pub const DISTANCE_IR_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
105pub const DISTANCE_IR_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
106pub const DISTANCE_IR_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
107pub const DISTANCE_IR_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
108pub const DISTANCE_IR_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
109pub const DISTANCE_IR_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: u16,
117    pub max: u16,
118}
119impl FromByteSlice for DistanceCallbackConfiguration {
120    fn bytes_expected() -> usize {
121        10
122    }
123    fn from_le_byte_slice(bytes: &[u8]) -> DistanceCallbackConfiguration {
124        DistanceCallbackConfiguration {
125            period: <u32>::from_le_byte_slice(&bytes[0..4]),
126            value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
127            option: <char>::from_le_byte_slice(&bytes[5..6]),
128            min: <u16>::from_le_byte_slice(&bytes[6..8]),
129            max: <u16>::from_le_byte_slice(&bytes[8..10]),
130        }
131    }
132}
133
134#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
135pub struct AnalogValueCallbackConfiguration {
136    pub period: u32,
137    pub value_has_to_change: bool,
138    pub option: char,
139    pub min: u32,
140    pub max: u32,
141}
142impl FromByteSlice for AnalogValueCallbackConfiguration {
143    fn bytes_expected() -> usize {
144        14
145    }
146    fn from_le_byte_slice(bytes: &[u8]) -> AnalogValueCallbackConfiguration {
147        AnalogValueCallbackConfiguration {
148            period: <u32>::from_le_byte_slice(&bytes[0..4]),
149            value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
150            option: <char>::from_le_byte_slice(&bytes[5..6]),
151            min: <u32>::from_le_byte_slice(&bytes[6..10]),
152            max: <u32>::from_le_byte_slice(&bytes[10..14]),
153        }
154    }
155}
156
157#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
158pub struct SpitfpErrorCount {
159    pub error_count_ack_checksum: u32,
160    pub error_count_message_checksum: u32,
161    pub error_count_frame: u32,
162    pub error_count_overflow: u32,
163}
164impl FromByteSlice for SpitfpErrorCount {
165    fn bytes_expected() -> usize {
166        16
167    }
168    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
169        SpitfpErrorCount {
170            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
171            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
172            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
173            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
174        }
175    }
176}
177
178#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
179pub struct Identity {
180    pub uid: String,
181    pub connected_uid: String,
182    pub position: char,
183    pub hardware_version: [u8; 3],
184    pub firmware_version: [u8; 3],
185    pub device_identifier: u16,
186}
187impl FromByteSlice for Identity {
188    fn bytes_expected() -> usize {
189        25
190    }
191    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
192        Identity {
193            uid: <String>::from_le_byte_slice(&bytes[0..8]),
194            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
195            position: <char>::from_le_byte_slice(&bytes[16..17]),
196            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
197            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
198            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
199        }
200    }
201}
202
203/// Measures distance up to 150cm with infrared light
204#[derive(Clone)]
205pub struct DistanceIrV2Bricklet {
206    device: Device,
207}
208impl DistanceIrV2Bricklet {
209    pub const DEVICE_IDENTIFIER: u16 = 2125;
210    pub const DEVICE_DISPLAY_NAME: &'static str = "Distance IR Bricklet 2.0";
211    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
212    pub fn new(uid: Uid, connection: AsyncIpConnection) -> DistanceIrV2Bricklet {
213        let mut result = DistanceIrV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
214        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::GetDistance) as usize] = ResponseExpectedFlag::AlwaysTrue;
215        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::SetDistanceCallbackConfiguration) as usize] =
216            ResponseExpectedFlag::True;
217        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::GetDistanceCallbackConfiguration) as usize] =
218            ResponseExpectedFlag::AlwaysTrue;
219        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::GetAnalogValue) as usize] = ResponseExpectedFlag::AlwaysTrue;
220        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::SetAnalogValueCallbackConfiguration) as usize] =
221            ResponseExpectedFlag::True;
222        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::GetAnalogValueCallbackConfiguration) as usize] =
223            ResponseExpectedFlag::AlwaysTrue;
224        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::SetMovingAverageConfiguration) as usize] =
225            ResponseExpectedFlag::False;
226        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::GetMovingAverageConfiguration) as usize] =
227            ResponseExpectedFlag::AlwaysTrue;
228        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::SetDistanceLedConfig) as usize] =
229            ResponseExpectedFlag::False;
230        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::GetDistanceLedConfig) as usize] =
231            ResponseExpectedFlag::AlwaysTrue;
232        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::SetSensorType) as usize] = ResponseExpectedFlag::False;
233        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::GetSensorType) as usize] = ResponseExpectedFlag::AlwaysTrue;
234        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::GetSpitfpErrorCount) as usize] =
235            ResponseExpectedFlag::AlwaysTrue;
236        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::SetBootloaderMode) as usize] =
237            ResponseExpectedFlag::AlwaysTrue;
238        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::GetBootloaderMode) as usize] =
239            ResponseExpectedFlag::AlwaysTrue;
240        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
241            ResponseExpectedFlag::False;
242        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
243        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
244        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::GetStatusLedConfig) as usize] =
245            ResponseExpectedFlag::AlwaysTrue;
246        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::GetChipTemperature) as usize] =
247            ResponseExpectedFlag::AlwaysTrue;
248        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
249        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
250        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
251        result.device.response_expected[u8::from(DistanceIrV2BrickletFunction::GetIdentity) as usize] = 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::distance_ir_v2_bricklet::DistanceIrV2Bricklet::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::distance_ir_v2_bricklet::DistanceIrV2Bricklet::set_response_expected) for the list of function ID constants available for this function.
269    pub fn get_response_expected(&mut self, fun: DistanceIrV2BrickletFunction) -> 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: DistanceIrV2BrickletFunction,
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 according to the configuration set by
301    /// [`set_distance_callback_configuration`].
302    ///
303    /// The parameter is the same as [`get_distance`].
304    ///
305    /// [`get_distance`]: #method.get_distance
306    /// [`set_distance_callback_configuration`]: #method.set_distance_callback_configuration
307    pub async fn get_distance_callback_receiver(&mut self) -> impl Stream<Item = u16> {
308        self.device
309            .get_callback_receiver(u8::from(DistanceIrV2BrickletFunction::CallbackDistance))
310            .await
311            .map(|p| u16::from_le_byte_slice(p.body()))
312    }
313
314    /// This receiver is triggered periodically according to the configuration set by
315    /// [`set_analog_value_callback_configuration`].
316    ///
317    /// The parameter is the same as [`get_analog_value`].
318    pub async fn get_analog_value_callback_receiver(&mut self) -> impl Stream<Item = u32> {
319        self.device
320            .get_callback_receiver(u8::from(DistanceIrV2BrickletFunction::CallbackAnalogValue))
321            .await
322            .map(|p| u32::from_le_byte_slice(p.body()))
323    }
324
325    /// Returns the distance measured by the sensor. Possible
326    /// distance ranges are 40 to 300, 100 to 800 and 200 to 1500, depending on the
327    /// selected IR sensor.
328    ///
329    ///
330    /// If you want to get the value periodically, it is recommended to use the
331    /// [`get_distance_callback_receiver`] receiver. You can set the receiver configuration
332    /// with [`set_distance_callback_configuration`].
333    pub async fn get_distance(&mut self) -> Result<u16, TinkerforgeError> {
334        let payload = [0; 0];
335
336        #[allow(unused_variables)]
337        let result = self.device.get(u8::from(DistanceIrV2BrickletFunction::GetDistance), &payload).await?;
338        Ok(u16::from_le_byte_slice(result.body()))
339    }
340
341    /// The period is the period with which the [`get_distance_callback_receiver`] receiver is triggered
342    /// periodically. A value of 0 turns the receiver off.
343    ///
344    /// If the `value has to change`-parameter is set to true, the receiver is only
345    /// triggered after the value has changed. If the value didn't change
346    /// within the period, the receiver is triggered immediately on change.
347    ///
348    /// If it is set to false, the receiver is continuously triggered with the period,
349    /// independent of the value.
350    ///
351    /// It is furthermore possible to constrain the receiver with thresholds.
352    ///
353    /// The `option`-parameter together with min/max sets a threshold for the [`get_distance_callback_receiver`] receiver.
354    ///
355    /// The following options are possible:
356    ///
357    ///  Option| Description
358    ///  --- | ---
359    ///  'x'|    Threshold is turned off
360    ///  'o'|    Threshold is triggered when the value is *outside* the min and max values
361    ///  'i'|    Threshold is triggered when the value is *inside* or equal to the min and max values
362    ///  '<'|    Threshold is triggered when the value is smaller than the min value (max is ignored)
363    ///  '>'|    Threshold is triggered when the value is greater than the min value (max is ignored)
364    ///
365    /// If the option is set to 'x' (threshold turned off) the receiver is triggered with the fixed period.
366    ///
367    /// Associated constants:
368    /// * DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_OFF
369    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
370    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
371    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
372    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_GREATER
373    pub async fn set_distance_callback_configuration(
374        &mut self,
375        period: u32,
376        value_has_to_change: bool,
377        option: char,
378        min: u16,
379        max: u16,
380    ) -> Result<(), TinkerforgeError> {
381        let mut payload = [0; 10];
382        period.write_to_slice(&mut payload[0..4]);
383        value_has_to_change.write_to_slice(&mut payload[4..5]);
384        option.write_to_slice(&mut payload[5..6]);
385        min.write_to_slice(&mut payload[6..8]);
386        max.write_to_slice(&mut payload[8..10]);
387
388        #[allow(unused_variables)]
389        let result = self.device.set(u8::from(DistanceIrV2BrickletFunction::SetDistanceCallbackConfiguration), &payload).await?;
390        Ok(())
391    }
392
393    /// Returns the receiver configuration as set by [`set_distance_callback_configuration`].
394    ///
395    /// Associated constants:
396    /// * DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_OFF
397    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
398    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
399    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
400    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_GREATER
401    pub async fn get_distance_callback_configuration(&mut self) -> Result<DistanceCallbackConfiguration, TinkerforgeError> {
402        let payload = [0; 0];
403
404        #[allow(unused_variables)]
405        let result = self.device.get(u8::from(DistanceIrV2BrickletFunction::GetDistanceCallbackConfiguration), &payload).await?;
406        Ok(DistanceCallbackConfiguration::from_le_byte_slice(result.body()))
407    }
408
409    /// Returns the analog value as read by a analog-to-digital converter.
410    ///
411    /// This is unfiltered raw data. We made sure that the integration time
412    /// of the ADC is shorter then the measurement interval of the sensor
413    /// (10ms vs 16.5ms). So there is no information lost.
414    ///
415    /// If you want to do your own calibration or create your own lookup table
416    /// you can use this value.
417    ///
418    ///
419    /// If you want to get the value periodically, it is recommended to use the
420    /// [`get_analog_value_callback_receiver`] receiver. You can set the receiver configuration
421    /// with [`set_analog_value_callback_configuration`].
422    pub async fn get_analog_value(&mut self) -> Result<u32, TinkerforgeError> {
423        let payload = [0; 0];
424
425        #[allow(unused_variables)]
426        let result = self.device.get(u8::from(DistanceIrV2BrickletFunction::GetAnalogValue), &payload).await?;
427        Ok(u32::from_le_byte_slice(result.body()))
428    }
429
430    /// The period is the period with which the [`get_analog_value_callback_receiver`] receiver is triggered
431    /// periodically. A value of 0 turns the receiver off.
432    ///
433    /// If the `value has to change`-parameter is set to true, the receiver is only
434    /// triggered after the value has changed. If the value didn't change
435    /// within the period, the receiver is triggered immediately on change.
436    ///
437    /// If it is set to false, the receiver is continuously triggered with the period,
438    /// independent of the value.
439    ///
440    /// It is furthermore possible to constrain the receiver with thresholds.
441    ///
442    /// The `option`-parameter together with min/max sets a threshold for the [`get_analog_value_callback_receiver`] receiver.
443    ///
444    /// The following options are possible:
445    ///
446    ///  Option| Description
447    ///  --- | ---
448    ///  'x'|    Threshold is turned off
449    ///  'o'|    Threshold is triggered when the value is *outside* the min and max values
450    ///  'i'|    Threshold is triggered when the value is *inside* or equal to the min and max values
451    ///  '<'|    Threshold is triggered when the value is smaller than the min value (max is ignored)
452    ///  '>'|    Threshold is triggered when the value is greater than the min value (max is ignored)
453    ///
454    /// If the option is set to 'x' (threshold turned off) the receiver is triggered with the fixed period.
455    ///
456    /// Associated constants:
457    /// * DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_OFF
458    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
459    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
460    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
461    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_GREATER
462    pub async fn set_analog_value_callback_configuration(
463        &mut self,
464        period: u32,
465        value_has_to_change: bool,
466        option: char,
467        min: u32,
468        max: u32,
469    ) -> Result<(), TinkerforgeError> {
470        let mut payload = [0; 14];
471        period.write_to_slice(&mut payload[0..4]);
472        value_has_to_change.write_to_slice(&mut payload[4..5]);
473        option.write_to_slice(&mut payload[5..6]);
474        min.write_to_slice(&mut payload[6..10]);
475        max.write_to_slice(&mut payload[10..14]);
476
477        #[allow(unused_variables)]
478        let result = self.device.set(u8::from(DistanceIrV2BrickletFunction::SetAnalogValueCallbackConfiguration), &payload).await?;
479        Ok(())
480    }
481
482    /// Returns the receiver configuration as set by [`set_analog_value_callback_configuration`].
483    ///
484    /// Associated constants:
485    /// * DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_OFF
486    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
487    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
488    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
489    ///	* DISTANCE_IR_V2_BRICKLET_THRESHOLD_OPTION_GREATER
490    pub async fn get_analog_value_callback_configuration(&mut self) -> Result<AnalogValueCallbackConfiguration, TinkerforgeError> {
491        let payload = [0; 0];
492
493        #[allow(unused_variables)]
494        let result = self.device.get(u8::from(DistanceIrV2BrickletFunction::GetAnalogValueCallbackConfiguration), &payload).await?;
495        Ok(AnalogValueCallbackConfiguration::from_le_byte_slice(result.body()))
496    }
497
498    /// Sets the length of a [moving averaging](https://en.wikipedia.org/wiki/Moving_average)__
499    /// for the distance.
500    ///
501    /// Setting the length to 1 will turn the averaging off. With less averaging, there
502    /// is more noise on the data.
503    ///
504    /// New data is gathered every ~10ms. With a moving average of length 1000 the
505    /// resulting averaging window has a length of approximately 10s. If you want to do
506    /// long term measurements the longest moving average will give the cleanest results.
507    pub async fn set_moving_average_configuration(&mut self, moving_average_length: u16) -> Result<(), TinkerforgeError> {
508        let mut payload = [0; 2];
509        moving_average_length.write_to_slice(&mut payload[0..2]);
510
511        #[allow(unused_variables)]
512        let result = self.device.set(u8::from(DistanceIrV2BrickletFunction::SetMovingAverageConfiguration), &payload).await?;
513        Ok(())
514    }
515
516    /// Returns the moving average configuration as set by [`set_moving_average_configuration`].
517    pub async fn get_moving_average_configuration(&mut self) -> Result<u16, TinkerforgeError> {
518        let payload = [0; 0];
519
520        #[allow(unused_variables)]
521        let result = self.device.get(u8::from(DistanceIrV2BrickletFunction::GetMovingAverageConfiguration), &payload).await?;
522        Ok(u16::from_le_byte_slice(result.body()))
523    }
524
525    /// Configures the distance LED to be either turned off, turned on, blink in
526    /// heartbeat mode or show the distance (brighter = object is nearer).
527    ///
528    /// Associated constants:
529    /// * DISTANCE_IR_V2_BRICKLET_DISTANCE_LED_CONFIG_OFF
530    ///	* DISTANCE_IR_V2_BRICKLET_DISTANCE_LED_CONFIG_ON
531    ///	* DISTANCE_IR_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_HEARTBEAT
532    ///	* DISTANCE_IR_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_DISTANCE
533    pub async fn set_distance_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
534        let mut payload = [0; 1];
535        config.write_to_slice(&mut payload[0..1]);
536
537        #[allow(unused_variables)]
538        let result = self.device.set(u8::from(DistanceIrV2BrickletFunction::SetDistanceLedConfig), &payload).await?;
539        Ok(())
540    }
541
542    /// Returns the LED configuration as set by [`set_distance_led_config`]
543    ///
544    /// Associated constants:
545    /// * DISTANCE_IR_V2_BRICKLET_DISTANCE_LED_CONFIG_OFF
546    ///	* DISTANCE_IR_V2_BRICKLET_DISTANCE_LED_CONFIG_ON
547    ///	* DISTANCE_IR_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_HEARTBEAT
548    ///	* DISTANCE_IR_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_DISTANCE
549    pub async fn get_distance_led_config(&mut self) -> Result<u8, TinkerforgeError> {
550        let payload = [0; 0];
551
552        #[allow(unused_variables)]
553        let result = self.device.get(u8::from(DistanceIrV2BrickletFunction::GetDistanceLedConfig), &payload).await?;
554        Ok(u8::from_le_byte_slice(result.body()))
555    }
556
557    /// Sets the sensor type.
558    ///
559    /// The Bricklet comes configured with the correct sensor type
560    /// and the type is saved in flash (i.e. the Bricklet retains the information if
561    /// power is lost).
562    ///
563    /// If you want to change the sensor you can set the type in Brick Viewer,
564    /// you will likely never need to call this function from your program.
565    ///
566    /// Associated constants:
567    /// * DISTANCE_IR_V2_BRICKLET_SENSOR_TYPE_2Y0A41
568    ///	* DISTANCE_IR_V2_BRICKLET_SENSOR_TYPE_2Y0A21
569    ///	* DISTANCE_IR_V2_BRICKLET_SENSOR_TYPE_2Y0A02
570    pub async fn set_sensor_type(&mut self, sensor: u8) -> Result<(), TinkerforgeError> {
571        let mut payload = [0; 1];
572        sensor.write_to_slice(&mut payload[0..1]);
573
574        #[allow(unused_variables)]
575        let result = self.device.set(u8::from(DistanceIrV2BrickletFunction::SetSensorType), &payload).await?;
576        Ok(())
577    }
578
579    /// Returns the sensor type as set by [`set_sensor_type`].
580    ///
581    /// Associated constants:
582    /// * DISTANCE_IR_V2_BRICKLET_SENSOR_TYPE_2Y0A41
583    ///	* DISTANCE_IR_V2_BRICKLET_SENSOR_TYPE_2Y0A21
584    ///	* DISTANCE_IR_V2_BRICKLET_SENSOR_TYPE_2Y0A02
585    pub async fn get_sensor_type(&mut self) -> Result<u8, TinkerforgeError> {
586        let payload = [0; 0];
587
588        #[allow(unused_variables)]
589        let result = self.device.get(u8::from(DistanceIrV2BrickletFunction::GetSensorType), &payload).await?;
590        Ok(u8::from_le_byte_slice(result.body()))
591    }
592
593    /// Returns the error count for the communication between Brick and Bricklet.
594    ///
595    /// The errors are divided into
596    ///
597    /// * ACK checksum errors,
598    /// * message checksum errors,
599    /// * framing errors and
600    /// * overflow errors.
601    ///
602    /// The errors counts are for errors that occur on the Bricklet side. All
603    /// Bricks have a similar function that returns the errors on the Brick side.
604    pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
605        let payload = [0; 0];
606
607        #[allow(unused_variables)]
608        let result = self.device.get(u8::from(DistanceIrV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
609        Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
610    }
611
612    /// Sets the bootloader mode and returns the status after the requested
613    /// mode change was instigated.
614    ///
615    /// You can change from bootloader mode to firmware mode and vice versa. A change
616    /// from bootloader mode to firmware mode will only take place if the entry function,
617    /// device identifier and CRC are present and correct.
618    ///
619    /// This function is used by Brick Viewer during flashing. It should not be
620    /// necessary to call it in a normal user program.
621    ///
622    /// Associated constants:
623    /// * DISTANCE_IR_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
624    ///	* DISTANCE_IR_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
625    ///	* DISTANCE_IR_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
626    ///	* DISTANCE_IR_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
627    ///	* DISTANCE_IR_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
628    ///	* DISTANCE_IR_V2_BRICKLET_BOOTLOADER_STATUS_OK
629    ///	* DISTANCE_IR_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
630    ///	* DISTANCE_IR_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
631    ///	* DISTANCE_IR_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
632    ///	* DISTANCE_IR_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
633    ///	* DISTANCE_IR_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
634    pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
635        let mut payload = [0; 1];
636        mode.write_to_slice(&mut payload[0..1]);
637
638        #[allow(unused_variables)]
639        let result = self.device.get(u8::from(DistanceIrV2BrickletFunction::SetBootloaderMode), &payload).await?;
640        Ok(u8::from_le_byte_slice(result.body()))
641    }
642
643    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
644    ///
645    /// Associated constants:
646    /// * DISTANCE_IR_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
647    ///	* DISTANCE_IR_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
648    ///	* DISTANCE_IR_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
649    ///	* DISTANCE_IR_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
650    ///	* DISTANCE_IR_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
651    pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
652        let payload = [0; 0];
653
654        #[allow(unused_variables)]
655        let result = self.device.get(u8::from(DistanceIrV2BrickletFunction::GetBootloaderMode), &payload).await?;
656        Ok(u8::from_le_byte_slice(result.body()))
657    }
658
659    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
660    /// to be increased by chunks of size 64. The data is written to flash
661    /// every 4 chunks (which equals to one page of size 256).
662    ///
663    /// This function is used by Brick Viewer during flashing. It should not be
664    /// necessary to call it in a normal user program.
665    pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
666        let mut payload = [0; 4];
667        pointer.write_to_slice(&mut payload[0..4]);
668
669        #[allow(unused_variables)]
670        let result = self.device.set(u8::from(DistanceIrV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
671        Ok(())
672    }
673
674    /// Writes 64 Bytes of firmware at the position as written by
675    /// [`set_write_firmware_pointer`] before. The firmware is written
676    /// to flash every 4 chunks.
677    ///
678    /// You can only write firmware in bootloader mode.
679    ///
680    /// This function is used by Brick Viewer during flashing. It should not be
681    /// necessary to call it in a normal user program.
682    pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
683        let mut payload = [0; 64];
684        data.write_to_slice(&mut payload[0..64]);
685
686        #[allow(unused_variables)]
687        let result = self.device.get(u8::from(DistanceIrV2BrickletFunction::WriteFirmware), &payload).await?;
688        Ok(u8::from_le_byte_slice(result.body()))
689    }
690
691    /// Sets the status LED configuration. By default the LED shows
692    /// communication traffic between Brick and Bricklet, it flickers once
693    /// for every 10 received data packets.
694    ///
695    /// You can also turn the LED permanently on/off or show a heartbeat.
696    ///
697    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
698    ///
699    /// Associated constants:
700    /// * DISTANCE_IR_V2_BRICKLET_STATUS_LED_CONFIG_OFF
701    ///	* DISTANCE_IR_V2_BRICKLET_STATUS_LED_CONFIG_ON
702    ///	* DISTANCE_IR_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
703    ///	* DISTANCE_IR_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
704    pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
705        let mut payload = [0; 1];
706        config.write_to_slice(&mut payload[0..1]);
707
708        #[allow(unused_variables)]
709        let result = self.device.set(u8::from(DistanceIrV2BrickletFunction::SetStatusLedConfig), &payload).await?;
710        Ok(())
711    }
712
713    /// Returns the configuration as set by [`set_status_led_config`]
714    ///
715    /// Associated constants:
716    /// * DISTANCE_IR_V2_BRICKLET_STATUS_LED_CONFIG_OFF
717    ///	* DISTANCE_IR_V2_BRICKLET_STATUS_LED_CONFIG_ON
718    ///	* DISTANCE_IR_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
719    ///	* DISTANCE_IR_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
720    pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
721        let payload = [0; 0];
722
723        #[allow(unused_variables)]
724        let result = self.device.get(u8::from(DistanceIrV2BrickletFunction::GetStatusLedConfig), &payload).await?;
725        Ok(u8::from_le_byte_slice(result.body()))
726    }
727
728    /// Returns the temperature as measured inside the microcontroller. The
729    /// value returned is not the ambient temperature!
730    ///
731    /// The temperature is only proportional to the real temperature and it has bad
732    /// accuracy. Practically it is only useful as an indicator for
733    /// temperature changes.
734    pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
735        let payload = [0; 0];
736
737        #[allow(unused_variables)]
738        let result = self.device.get(u8::from(DistanceIrV2BrickletFunction::GetChipTemperature), &payload).await?;
739        Ok(i16::from_le_byte_slice(result.body()))
740    }
741
742    /// Calling this function will reset the Bricklet. All configurations
743    /// will be lost.
744    ///
745    /// After a reset you have to create new device objects,
746    /// calling functions on the existing ones will result in
747    /// undefined behavior!
748    pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
749        let payload = [0; 0];
750
751        #[allow(unused_variables)]
752        let result = self.device.set(u8::from(DistanceIrV2BrickletFunction::Reset), &payload).await?;
753        Ok(())
754    }
755
756    /// Writes a new UID into flash. If you want to set a new UID
757    /// you have to decode the Base58 encoded UID string into an
758    /// integer first.
759    ///
760    /// We recommend that you use Brick Viewer to change the UID.
761    pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
762        let mut payload = [0; 4];
763        uid.write_to_slice(&mut payload[0..4]);
764
765        #[allow(unused_variables)]
766        let result = self.device.set(u8::from(DistanceIrV2BrickletFunction::WriteUid), &payload).await?;
767        Ok(())
768    }
769
770    /// Returns the current UID as an integer. Encode as
771    /// Base58 to get the usual string version.
772    pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
773        let payload = [0; 0];
774
775        #[allow(unused_variables)]
776        let result = self.device.get(u8::from(DistanceIrV2BrickletFunction::ReadUid), &payload).await?;
777        Ok(u32::from_le_byte_slice(result.body()))
778    }
779
780    /// Returns the UID, the UID where the Bricklet is connected to,
781    /// the position, the hardware and firmware version as well as the
782    /// device identifier.
783    ///
784    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
785    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
786    /// position 'z'.
787    ///
788    /// The device identifier numbers can be found [here](device_identifier).
789    /// |device_identifier_constant|
790    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
791        let payload = [0; 0];
792
793        #[allow(unused_variables)]
794        let result = self.device.get(u8::from(DistanceIrV2BrickletFunction::GetIdentity), &payload).await?;
795        Ok(Identity::from_le_byte_slice(result.body()))
796    }
797}