tinkerforge_async/bindings/
distance_us_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 between 30cm and 500cm with ultrasound.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/DistanceUSV2_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 DistanceUsV2BrickletFunction {
24    GetDistance,
25    SetDistanceCallbackConfiguration,
26    GetDistanceCallbackConfiguration,
27    SetUpdateRate,
28    GetUpdateRate,
29    SetDistanceLedConfig,
30    GetDistanceLedConfig,
31    GetSpitfpErrorCount,
32    SetBootloaderMode,
33    GetBootloaderMode,
34    SetWriteFirmwarePointer,
35    WriteFirmware,
36    SetStatusLedConfig,
37    GetStatusLedConfig,
38    GetChipTemperature,
39    Reset,
40    WriteUid,
41    ReadUid,
42    GetIdentity,
43    CallbackDistance,
44}
45impl From<DistanceUsV2BrickletFunction> for u8 {
46    fn from(fun: DistanceUsV2BrickletFunction) -> Self {
47        match fun {
48            DistanceUsV2BrickletFunction::GetDistance => 1,
49            DistanceUsV2BrickletFunction::SetDistanceCallbackConfiguration => 2,
50            DistanceUsV2BrickletFunction::GetDistanceCallbackConfiguration => 3,
51            DistanceUsV2BrickletFunction::SetUpdateRate => 5,
52            DistanceUsV2BrickletFunction::GetUpdateRate => 6,
53            DistanceUsV2BrickletFunction::SetDistanceLedConfig => 7,
54            DistanceUsV2BrickletFunction::GetDistanceLedConfig => 8,
55            DistanceUsV2BrickletFunction::GetSpitfpErrorCount => 234,
56            DistanceUsV2BrickletFunction::SetBootloaderMode => 235,
57            DistanceUsV2BrickletFunction::GetBootloaderMode => 236,
58            DistanceUsV2BrickletFunction::SetWriteFirmwarePointer => 237,
59            DistanceUsV2BrickletFunction::WriteFirmware => 238,
60            DistanceUsV2BrickletFunction::SetStatusLedConfig => 239,
61            DistanceUsV2BrickletFunction::GetStatusLedConfig => 240,
62            DistanceUsV2BrickletFunction::GetChipTemperature => 242,
63            DistanceUsV2BrickletFunction::Reset => 243,
64            DistanceUsV2BrickletFunction::WriteUid => 248,
65            DistanceUsV2BrickletFunction::ReadUid => 249,
66            DistanceUsV2BrickletFunction::GetIdentity => 255,
67            DistanceUsV2BrickletFunction::CallbackDistance => 4,
68        }
69    }
70}
71pub const DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
72pub const DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
73pub const DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
74pub const DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
75pub const DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
76pub const DISTANCE_US_V2_BRICKLET_UPDATE_RATE_2_HZ: u8 = 0;
77pub const DISTANCE_US_V2_BRICKLET_UPDATE_RATE_10_HZ: u8 = 1;
78pub const DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_OFF: u8 = 0;
79pub const DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_ON: u8 = 1;
80pub const DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
81pub const DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_DISTANCE: u8 = 3;
82pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
83pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
84pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
85pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
86pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
87pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
88pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
89pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
90pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
91pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
92pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
93pub const DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
94pub const DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
95pub const DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
96pub const DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
97
98#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
99pub struct DistanceCallbackConfiguration {
100    pub period: u32,
101    pub value_has_to_change: bool,
102    pub option: char,
103    pub min: u16,
104    pub max: u16,
105}
106impl FromByteSlice for DistanceCallbackConfiguration {
107    fn bytes_expected() -> usize {
108        10
109    }
110    fn from_le_byte_slice(bytes: &[u8]) -> DistanceCallbackConfiguration {
111        DistanceCallbackConfiguration {
112            period: <u32>::from_le_byte_slice(&bytes[0..4]),
113            value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
114            option: <char>::from_le_byte_slice(&bytes[5..6]),
115            min: <u16>::from_le_byte_slice(&bytes[6..8]),
116            max: <u16>::from_le_byte_slice(&bytes[8..10]),
117        }
118    }
119}
120
121#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
122pub struct SpitfpErrorCount {
123    pub error_count_ack_checksum: u32,
124    pub error_count_message_checksum: u32,
125    pub error_count_frame: u32,
126    pub error_count_overflow: u32,
127}
128impl FromByteSlice for SpitfpErrorCount {
129    fn bytes_expected() -> usize {
130        16
131    }
132    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
133        SpitfpErrorCount {
134            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
135            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
136            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
137            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
138        }
139    }
140}
141
142#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
143pub struct Identity {
144    pub uid: String,
145    pub connected_uid: String,
146    pub position: char,
147    pub hardware_version: [u8; 3],
148    pub firmware_version: [u8; 3],
149    pub device_identifier: u16,
150}
151impl FromByteSlice for Identity {
152    fn bytes_expected() -> usize {
153        25
154    }
155    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
156        Identity {
157            uid: <String>::from_le_byte_slice(&bytes[0..8]),
158            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
159            position: <char>::from_le_byte_slice(&bytes[16..17]),
160            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
161            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
162            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
163        }
164    }
165}
166
167/// Measures distance between 30cm and 500cm with ultrasound
168#[derive(Clone)]
169pub struct DistanceUsV2Bricklet {
170    device: Device,
171}
172impl DistanceUsV2Bricklet {
173    pub const DEVICE_IDENTIFIER: u16 = 299;
174    pub const DEVICE_DISPLAY_NAME: &'static str = "Distance US Bricklet 2.0";
175    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
176    pub fn new(uid: Uid, connection: AsyncIpConnection) -> DistanceUsV2Bricklet {
177        let mut result = DistanceUsV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
178        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetDistance) as usize] = ResponseExpectedFlag::AlwaysTrue;
179        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::SetDistanceCallbackConfiguration) as usize] =
180            ResponseExpectedFlag::True;
181        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetDistanceCallbackConfiguration) as usize] =
182            ResponseExpectedFlag::AlwaysTrue;
183        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::SetUpdateRate) as usize] = ResponseExpectedFlag::False;
184        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetUpdateRate) as usize] = ResponseExpectedFlag::AlwaysTrue;
185        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::SetDistanceLedConfig) as usize] =
186            ResponseExpectedFlag::False;
187        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetDistanceLedConfig) as usize] =
188            ResponseExpectedFlag::AlwaysTrue;
189        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetSpitfpErrorCount) as usize] =
190            ResponseExpectedFlag::AlwaysTrue;
191        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::SetBootloaderMode) as usize] =
192            ResponseExpectedFlag::AlwaysTrue;
193        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetBootloaderMode) as usize] =
194            ResponseExpectedFlag::AlwaysTrue;
195        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
196            ResponseExpectedFlag::False;
197        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
198        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
199        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetStatusLedConfig) as usize] =
200            ResponseExpectedFlag::AlwaysTrue;
201        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetChipTemperature) as usize] =
202            ResponseExpectedFlag::AlwaysTrue;
203        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
204        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
205        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
206        result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
207        result
208    }
209
210    /// Returns the response expected flag for the function specified by the function ID parameter.
211    /// It is true if the function is expected to send a response, false otherwise.
212    ///
213    /// For getter functions this is enabled by default and cannot be disabled, because those
214    /// functions will always send a response. For callback configuration functions it is enabled
215    /// by default too, but can be disabled by [`set_response_expected`](crate::distance_us_v2_bricklet::DistanceUsV2Bricklet::set_response_expected).
216    /// For setter functions it is disabled by default and can be enabled.
217    ///
218    /// Enabling the response expected flag for a setter function allows to detect timeouts
219    /// and other error conditions calls of this setter as well. The device will then send a response
220    /// for this purpose. If this flag is disabled for a setter function then no response is sent
221    /// and errors are silently ignored, because they cannot be detected.
222    ///
223    /// See [`set_response_expected`](crate::distance_us_v2_bricklet::DistanceUsV2Bricklet::set_response_expected) for the list of function ID constants available for this function.
224    pub fn get_response_expected(&mut self, fun: DistanceUsV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
225        self.device.get_response_expected(u8::from(fun))
226    }
227
228    /// Changes the response expected flag of the function specified by the function ID parameter.
229    /// This flag can only be changed for setter (default value: false) and callback configuration
230    /// functions (default value: true). For getter functions it is always enabled.
231    ///
232    /// Enabling the response expected flag for a setter function allows to detect timeouts and
233    /// other error conditions calls of this setter as well. The device will then send a response
234    /// for this purpose. If this flag is disabled for a setter function then no response is sent
235    /// and errors are silently ignored, because they cannot be detected.
236    pub fn set_response_expected(
237        &mut self,
238        fun: DistanceUsV2BrickletFunction,
239        response_expected: bool,
240    ) -> Result<(), SetResponseExpectedError> {
241        self.device.set_response_expected(u8::from(fun), response_expected)
242    }
243
244    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
245    pub fn set_response_expected_all(&mut self, response_expected: bool) {
246        self.device.set_response_expected_all(response_expected)
247    }
248
249    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
250    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
251    pub fn get_api_version(&self) -> [u8; 3] {
252        self.device.api_version
253    }
254
255    /// This receiver is triggered periodically according to the configuration set by
256    /// [`set_distance_callback_configuration`].
257    ///
258    /// The parameter is the same as [`get_distance`].
259    ///
260    /// [`get_distance`]: #method.get_distance
261    /// [`set_distance_callback_configuration`]: #method.set_distance_callback_configuration
262    pub async fn get_distance_callback_receiver(&mut self) -> impl Stream<Item = u16> {
263        self.device
264            .get_callback_receiver(u8::from(DistanceUsV2BrickletFunction::CallbackDistance))
265            .await
266            .map(|p| u16::from_le_byte_slice(p.body()))
267    }
268
269    /// Returns the distance.
270    ///
271    ///
272    /// If you want to get the value periodically, it is recommended to use the
273    /// [`get_distance_callback_receiver`] receiver. You can set the receiver configuration
274    /// with [`set_distance_callback_configuration`].
275    pub async fn get_distance(&mut self) -> Result<u16, TinkerforgeError> {
276        let payload = [0; 0];
277
278        #[allow(unused_variables)]
279        let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetDistance), &payload).await?;
280        Ok(u16::from_le_byte_slice(result.body()))
281    }
282
283    /// The period is the period with which the [`get_distance_callback_receiver`] receiver is triggered
284    /// periodically. A value of 0 turns the receiver off.
285    ///
286    /// If the `value has to change`-parameter is set to true, the receiver is only
287    /// triggered after the value has changed. If the value didn't change
288    /// within the period, the receiver is triggered immediately on change.
289    ///
290    /// If it is set to false, the receiver is continuously triggered with the period,
291    /// independent of the value.
292    ///
293    /// It is furthermore possible to constrain the receiver with thresholds.
294    ///
295    /// The `option`-parameter together with min/max sets a threshold for the [`get_distance_callback_receiver`] receiver.
296    ///
297    /// The following options are possible:
298    ///
299    ///  Option| Description
300    ///  --- | ---
301    ///  'x'|    Threshold is turned off
302    ///  'o'|    Threshold is triggered when the value is *outside* the min and max values
303    ///  'i'|    Threshold is triggered when the value is *inside* or equal to the min and max values
304    ///  '<'|    Threshold is triggered when the value is smaller than the min value (max is ignored)
305    ///  '>'|    Threshold is triggered when the value is greater than the min value (max is ignored)
306    ///
307    /// If the option is set to 'x' (threshold turned off) the receiver is triggered with the fixed period.
308    ///
309    /// Associated constants:
310    /// * DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_OFF
311    ///	* DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
312    ///	* DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
313    ///	* DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
314    ///	* DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_GREATER
315    pub async fn set_distance_callback_configuration(
316        &mut self,
317        period: u32,
318        value_has_to_change: bool,
319        option: char,
320        min: u16,
321        max: u16,
322    ) -> Result<(), TinkerforgeError> {
323        let mut payload = [0; 10];
324        period.write_to_slice(&mut payload[0..4]);
325        value_has_to_change.write_to_slice(&mut payload[4..5]);
326        option.write_to_slice(&mut payload[5..6]);
327        min.write_to_slice(&mut payload[6..8]);
328        max.write_to_slice(&mut payload[8..10]);
329
330        #[allow(unused_variables)]
331        let result = self.device.set(u8::from(DistanceUsV2BrickletFunction::SetDistanceCallbackConfiguration), &payload).await?;
332        Ok(())
333    }
334
335    /// Returns the receiver configuration as set by [`set_distance_callback_configuration`].
336    ///
337    /// Associated constants:
338    /// * DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_OFF
339    ///	* DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
340    ///	* DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
341    ///	* DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
342    ///	* DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_GREATER
343    pub async fn get_distance_callback_configuration(&mut self) -> Result<DistanceCallbackConfiguration, TinkerforgeError> {
344        let payload = [0; 0];
345
346        #[allow(unused_variables)]
347        let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetDistanceCallbackConfiguration), &payload).await?;
348        Ok(DistanceCallbackConfiguration::from_le_byte_slice(result.body()))
349    }
350
351    /// Sets the update rate to 2 Hz or 10 Hz.
352    ///
353    /// With 2 Hz update rate the noise is about ±1mm, while with 10 Hz update rate the noise
354    /// increases to about ±5mm.
355    ///
356    /// Associated constants:
357    /// * DISTANCE_US_V2_BRICKLET_UPDATE_RATE_2_HZ
358    ///	* DISTANCE_US_V2_BRICKLET_UPDATE_RATE_10_HZ
359    pub async fn set_update_rate(&mut self, update_rate: u8) -> Result<(), TinkerforgeError> {
360        let mut payload = [0; 1];
361        update_rate.write_to_slice(&mut payload[0..1]);
362
363        #[allow(unused_variables)]
364        let result = self.device.set(u8::from(DistanceUsV2BrickletFunction::SetUpdateRate), &payload).await?;
365        Ok(())
366    }
367
368    /// Returns the update rate as set by [`set_update_rate`].
369    ///
370    /// Associated constants:
371    /// * DISTANCE_US_V2_BRICKLET_UPDATE_RATE_2_HZ
372    ///	* DISTANCE_US_V2_BRICKLET_UPDATE_RATE_10_HZ
373    pub async fn get_update_rate(&mut self) -> Result<u8, TinkerforgeError> {
374        let payload = [0; 0];
375
376        #[allow(unused_variables)]
377        let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetUpdateRate), &payload).await?;
378        Ok(u8::from_le_byte_slice(result.body()))
379    }
380
381    /// Configures the distance LED to be either turned off, turned on, blink in
382    /// heartbeat mode or show the distance (brighter = object is nearer).
383    ///
384    /// Associated constants:
385    /// * DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_OFF
386    ///	* DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_ON
387    ///	* DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_HEARTBEAT
388    ///	* DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_DISTANCE
389    pub async fn set_distance_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
390        let mut payload = [0; 1];
391        config.write_to_slice(&mut payload[0..1]);
392
393        #[allow(unused_variables)]
394        let result = self.device.set(u8::from(DistanceUsV2BrickletFunction::SetDistanceLedConfig), &payload).await?;
395        Ok(())
396    }
397
398    /// Returns the LED configuration as set by [`set_distance_led_config`]
399    ///
400    /// Associated constants:
401    /// * DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_OFF
402    ///	* DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_ON
403    ///	* DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_HEARTBEAT
404    ///	* DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_DISTANCE
405    pub async fn get_distance_led_config(&mut self) -> Result<u8, TinkerforgeError> {
406        let payload = [0; 0];
407
408        #[allow(unused_variables)]
409        let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetDistanceLedConfig), &payload).await?;
410        Ok(u8::from_le_byte_slice(result.body()))
411    }
412
413    /// Returns the error count for the communication between Brick and Bricklet.
414    ///
415    /// The errors are divided into
416    ///
417    /// * ACK checksum errors,
418    /// * message checksum errors,
419    /// * framing errors and
420    /// * overflow errors.
421    ///
422    /// The errors counts are for errors that occur on the Bricklet side. All
423    /// Bricks have a similar function that returns the errors on the Brick side.
424    pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
425        let payload = [0; 0];
426
427        #[allow(unused_variables)]
428        let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
429        Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
430    }
431
432    /// Sets the bootloader mode and returns the status after the requested
433    /// mode change was instigated.
434    ///
435    /// You can change from bootloader mode to firmware mode and vice versa. A change
436    /// from bootloader mode to firmware mode will only take place if the entry function,
437    /// device identifier and CRC are present and correct.
438    ///
439    /// This function is used by Brick Viewer during flashing. It should not be
440    /// necessary to call it in a normal user program.
441    ///
442    /// Associated constants:
443    /// * DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
444    ///	* DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
445    ///	* DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
446    ///	* DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
447    ///	* DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
448    ///	* DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_OK
449    ///	* DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
450    ///	* DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
451    ///	* DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
452    ///	* DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
453    ///	* DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
454    pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
455        let mut payload = [0; 1];
456        mode.write_to_slice(&mut payload[0..1]);
457
458        #[allow(unused_variables)]
459        let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::SetBootloaderMode), &payload).await?;
460        Ok(u8::from_le_byte_slice(result.body()))
461    }
462
463    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
464    ///
465    /// Associated constants:
466    /// * DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
467    ///	* DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
468    ///	* DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
469    ///	* DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
470    ///	* DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
471    pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
472        let payload = [0; 0];
473
474        #[allow(unused_variables)]
475        let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetBootloaderMode), &payload).await?;
476        Ok(u8::from_le_byte_slice(result.body()))
477    }
478
479    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
480    /// to be increased by chunks of size 64. The data is written to flash
481    /// every 4 chunks (which equals to one page of size 256).
482    ///
483    /// This function is used by Brick Viewer during flashing. It should not be
484    /// necessary to call it in a normal user program.
485    pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
486        let mut payload = [0; 4];
487        pointer.write_to_slice(&mut payload[0..4]);
488
489        #[allow(unused_variables)]
490        let result = self.device.set(u8::from(DistanceUsV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
491        Ok(())
492    }
493
494    /// Writes 64 Bytes of firmware at the position as written by
495    /// [`set_write_firmware_pointer`] before. The firmware is written
496    /// to flash every 4 chunks.
497    ///
498    /// You can only write firmware in bootloader mode.
499    ///
500    /// This function is used by Brick Viewer during flashing. It should not be
501    /// necessary to call it in a normal user program.
502    pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
503        let mut payload = [0; 64];
504        data.write_to_slice(&mut payload[0..64]);
505
506        #[allow(unused_variables)]
507        let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::WriteFirmware), &payload).await?;
508        Ok(u8::from_le_byte_slice(result.body()))
509    }
510
511    /// Sets the status LED configuration. By default the LED shows
512    /// communication traffic between Brick and Bricklet, it flickers once
513    /// for every 10 received data packets.
514    ///
515    /// You can also turn the LED permanently on/off or show a heartbeat.
516    ///
517    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
518    ///
519    /// Associated constants:
520    /// * DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_OFF
521    ///	* DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_ON
522    ///	* DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
523    ///	* DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
524    pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
525        let mut payload = [0; 1];
526        config.write_to_slice(&mut payload[0..1]);
527
528        #[allow(unused_variables)]
529        let result = self.device.set(u8::from(DistanceUsV2BrickletFunction::SetStatusLedConfig), &payload).await?;
530        Ok(())
531    }
532
533    /// Returns the configuration as set by [`set_status_led_config`]
534    ///
535    /// Associated constants:
536    /// * DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_OFF
537    ///	* DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_ON
538    ///	* DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
539    ///	* DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
540    pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
541        let payload = [0; 0];
542
543        #[allow(unused_variables)]
544        let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetStatusLedConfig), &payload).await?;
545        Ok(u8::from_le_byte_slice(result.body()))
546    }
547
548    /// Returns the temperature as measured inside the microcontroller. The
549    /// value returned is not the ambient temperature!
550    ///
551    /// The temperature is only proportional to the real temperature and it has bad
552    /// accuracy. Practically it is only useful as an indicator for
553    /// temperature changes.
554    pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
555        let payload = [0; 0];
556
557        #[allow(unused_variables)]
558        let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetChipTemperature), &payload).await?;
559        Ok(i16::from_le_byte_slice(result.body()))
560    }
561
562    /// Calling this function will reset the Bricklet. All configurations
563    /// will be lost.
564    ///
565    /// After a reset you have to create new device objects,
566    /// calling functions on the existing ones will result in
567    /// undefined behavior!
568    pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
569        let payload = [0; 0];
570
571        #[allow(unused_variables)]
572        let result = self.device.set(u8::from(DistanceUsV2BrickletFunction::Reset), &payload).await?;
573        Ok(())
574    }
575
576    /// Writes a new UID into flash. If you want to set a new UID
577    /// you have to decode the Base58 encoded UID string into an
578    /// integer first.
579    ///
580    /// We recommend that you use Brick Viewer to change the UID.
581    pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
582        let mut payload = [0; 4];
583        uid.write_to_slice(&mut payload[0..4]);
584
585        #[allow(unused_variables)]
586        let result = self.device.set(u8::from(DistanceUsV2BrickletFunction::WriteUid), &payload).await?;
587        Ok(())
588    }
589
590    /// Returns the current UID as an integer. Encode as
591    /// Base58 to get the usual string version.
592    pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
593        let payload = [0; 0];
594
595        #[allow(unused_variables)]
596        let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::ReadUid), &payload).await?;
597        Ok(u32::from_le_byte_slice(result.body()))
598    }
599
600    /// Returns the UID, the UID where the Bricklet is connected to,
601    /// the position, the hardware and firmware version as well as the
602    /// device identifier.
603    ///
604    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
605    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
606    /// position 'z'.
607    ///
608    /// The device identifier numbers can be found [here](device_identifier).
609    /// |device_identifier_constant|
610    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
611        let payload = [0; 0];
612
613        #[allow(unused_variables)]
614        let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetIdentity), &payload).await?;
615        Ok(Identity::from_le_byte_slice(result.body()))
616    }
617}