tinkerforge_async/bindings/
compass_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//! 3-axis compass with 10 nanotesla and 0.1° resolution.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/Compass_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 CompassBrickletFunction {
24    GetHeading,
25    SetHeadingCallbackConfiguration,
26    GetHeadingCallbackConfiguration,
27    GetMagneticFluxDensity,
28    SetMagneticFluxDensityCallbackConfiguration,
29    GetMagneticFluxDensityCallbackConfiguration,
30    SetConfiguration,
31    GetConfiguration,
32    SetCalibration,
33    GetCalibration,
34    GetSpitfpErrorCount,
35    SetBootloaderMode,
36    GetBootloaderMode,
37    SetWriteFirmwarePointer,
38    WriteFirmware,
39    SetStatusLedConfig,
40    GetStatusLedConfig,
41    GetChipTemperature,
42    Reset,
43    WriteUid,
44    ReadUid,
45    GetIdentity,
46    CallbackHeading,
47    CallbackMagneticFluxDensity,
48}
49impl From<CompassBrickletFunction> for u8 {
50    fn from(fun: CompassBrickletFunction) -> Self {
51        match fun {
52            CompassBrickletFunction::GetHeading => 1,
53            CompassBrickletFunction::SetHeadingCallbackConfiguration => 2,
54            CompassBrickletFunction::GetHeadingCallbackConfiguration => 3,
55            CompassBrickletFunction::GetMagneticFluxDensity => 5,
56            CompassBrickletFunction::SetMagneticFluxDensityCallbackConfiguration => 6,
57            CompassBrickletFunction::GetMagneticFluxDensityCallbackConfiguration => 7,
58            CompassBrickletFunction::SetConfiguration => 9,
59            CompassBrickletFunction::GetConfiguration => 10,
60            CompassBrickletFunction::SetCalibration => 11,
61            CompassBrickletFunction::GetCalibration => 12,
62            CompassBrickletFunction::GetSpitfpErrorCount => 234,
63            CompassBrickletFunction::SetBootloaderMode => 235,
64            CompassBrickletFunction::GetBootloaderMode => 236,
65            CompassBrickletFunction::SetWriteFirmwarePointer => 237,
66            CompassBrickletFunction::WriteFirmware => 238,
67            CompassBrickletFunction::SetStatusLedConfig => 239,
68            CompassBrickletFunction::GetStatusLedConfig => 240,
69            CompassBrickletFunction::GetChipTemperature => 242,
70            CompassBrickletFunction::Reset => 243,
71            CompassBrickletFunction::WriteUid => 248,
72            CompassBrickletFunction::ReadUid => 249,
73            CompassBrickletFunction::GetIdentity => 255,
74            CompassBrickletFunction::CallbackHeading => 4,
75            CompassBrickletFunction::CallbackMagneticFluxDensity => 8,
76        }
77    }
78}
79pub const COMPASS_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
80pub const COMPASS_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
81pub const COMPASS_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
82pub const COMPASS_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
83pub const COMPASS_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
84pub const COMPASS_BRICKLET_DATA_RATE_100HZ: u8 = 0;
85pub const COMPASS_BRICKLET_DATA_RATE_200HZ: u8 = 1;
86pub const COMPASS_BRICKLET_DATA_RATE_400HZ: u8 = 2;
87pub const COMPASS_BRICKLET_DATA_RATE_600HZ: u8 = 3;
88pub const COMPASS_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
89pub const COMPASS_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
90pub const COMPASS_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
91pub const COMPASS_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
92pub const COMPASS_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
93pub const COMPASS_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
94pub const COMPASS_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
95pub const COMPASS_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
96pub const COMPASS_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
97pub const COMPASS_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
98pub const COMPASS_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
99pub const COMPASS_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
100pub const COMPASS_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
101pub const COMPASS_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
102pub const COMPASS_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
103
104#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
105pub struct HeadingCallbackConfiguration {
106    pub period: u32,
107    pub value_has_to_change: bool,
108    pub option: char,
109    pub min: i16,
110    pub max: i16,
111}
112impl FromByteSlice for HeadingCallbackConfiguration {
113    fn bytes_expected() -> usize {
114        10
115    }
116    fn from_le_byte_slice(bytes: &[u8]) -> HeadingCallbackConfiguration {
117        HeadingCallbackConfiguration {
118            period: <u32>::from_le_byte_slice(&bytes[0..4]),
119            value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
120            option: <char>::from_le_byte_slice(&bytes[5..6]),
121            min: <i16>::from_le_byte_slice(&bytes[6..8]),
122            max: <i16>::from_le_byte_slice(&bytes[8..10]),
123        }
124    }
125}
126
127#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
128pub struct MagneticFluxDensity {
129    pub x: i32,
130    pub y: i32,
131    pub z: i32,
132}
133impl FromByteSlice for MagneticFluxDensity {
134    fn bytes_expected() -> usize {
135        12
136    }
137    fn from_le_byte_slice(bytes: &[u8]) -> MagneticFluxDensity {
138        MagneticFluxDensity {
139            x: <i32>::from_le_byte_slice(&bytes[0..4]),
140            y: <i32>::from_le_byte_slice(&bytes[4..8]),
141            z: <i32>::from_le_byte_slice(&bytes[8..12]),
142        }
143    }
144}
145
146#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
147pub struct MagneticFluxDensityCallbackConfiguration {
148    pub period: u32,
149    pub value_has_to_change: bool,
150}
151impl FromByteSlice for MagneticFluxDensityCallbackConfiguration {
152    fn bytes_expected() -> usize {
153        5
154    }
155    fn from_le_byte_slice(bytes: &[u8]) -> MagneticFluxDensityCallbackConfiguration {
156        MagneticFluxDensityCallbackConfiguration {
157            period: <u32>::from_le_byte_slice(&bytes[0..4]),
158            value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
159        }
160    }
161}
162
163#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
164pub struct MagneticFluxDensityEvent {
165    pub x: i32,
166    pub y: i32,
167    pub z: i32,
168}
169impl FromByteSlice for MagneticFluxDensityEvent {
170    fn bytes_expected() -> usize {
171        12
172    }
173    fn from_le_byte_slice(bytes: &[u8]) -> MagneticFluxDensityEvent {
174        MagneticFluxDensityEvent {
175            x: <i32>::from_le_byte_slice(&bytes[0..4]),
176            y: <i32>::from_le_byte_slice(&bytes[4..8]),
177            z: <i32>::from_le_byte_slice(&bytes[8..12]),
178        }
179    }
180}
181
182#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
183pub struct Configuration {
184    pub data_rate: u8,
185    pub background_calibration: bool,
186}
187impl FromByteSlice for Configuration {
188    fn bytes_expected() -> usize {
189        2
190    }
191    fn from_le_byte_slice(bytes: &[u8]) -> Configuration {
192        Configuration {
193            data_rate: <u8>::from_le_byte_slice(&bytes[0..1]),
194            background_calibration: <bool>::from_le_byte_slice(&bytes[1..2]),
195        }
196    }
197}
198
199#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
200pub struct Calibration {
201    pub offset: [i16; 3],
202    pub gain: [i16; 3],
203}
204impl FromByteSlice for Calibration {
205    fn bytes_expected() -> usize {
206        12
207    }
208    fn from_le_byte_slice(bytes: &[u8]) -> Calibration {
209        Calibration { offset: <[i16; 3]>::from_le_byte_slice(&bytes[0..6]), gain: <[i16; 3]>::from_le_byte_slice(&bytes[6..12]) }
210    }
211}
212
213#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
214pub struct SpitfpErrorCount {
215    pub error_count_ack_checksum: u32,
216    pub error_count_message_checksum: u32,
217    pub error_count_frame: u32,
218    pub error_count_overflow: u32,
219}
220impl FromByteSlice for SpitfpErrorCount {
221    fn bytes_expected() -> usize {
222        16
223    }
224    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
225        SpitfpErrorCount {
226            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
227            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
228            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
229            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
230        }
231    }
232}
233
234#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
235pub struct Identity {
236    pub uid: String,
237    pub connected_uid: String,
238    pub position: char,
239    pub hardware_version: [u8; 3],
240    pub firmware_version: [u8; 3],
241    pub device_identifier: u16,
242}
243impl FromByteSlice for Identity {
244    fn bytes_expected() -> usize {
245        25
246    }
247    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
248        Identity {
249            uid: <String>::from_le_byte_slice(&bytes[0..8]),
250            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
251            position: <char>::from_le_byte_slice(&bytes[16..17]),
252            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
253            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
254            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
255        }
256    }
257}
258
259/// 3-axis compass with 10 nanotesla and 0.1° resolution
260#[derive(Clone)]
261pub struct CompassBricklet {
262    device: Device,
263}
264impl CompassBricklet {
265    pub const DEVICE_IDENTIFIER: u16 = 2153;
266    pub const DEVICE_DISPLAY_NAME: &'static str = "Compass Bricklet";
267    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
268    pub fn new(uid: Uid, connection: AsyncIpConnection) -> CompassBricklet {
269        let mut result = CompassBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
270        result.device.response_expected[u8::from(CompassBrickletFunction::GetHeading) as usize] = ResponseExpectedFlag::AlwaysTrue;
271        result.device.response_expected[u8::from(CompassBrickletFunction::SetHeadingCallbackConfiguration) as usize] =
272            ResponseExpectedFlag::True;
273        result.device.response_expected[u8::from(CompassBrickletFunction::GetHeadingCallbackConfiguration) as usize] =
274            ResponseExpectedFlag::AlwaysTrue;
275        result.device.response_expected[u8::from(CompassBrickletFunction::GetMagneticFluxDensity) as usize] =
276            ResponseExpectedFlag::AlwaysTrue;
277        result.device.response_expected[u8::from(CompassBrickletFunction::SetMagneticFluxDensityCallbackConfiguration) as usize] =
278            ResponseExpectedFlag::True;
279        result.device.response_expected[u8::from(CompassBrickletFunction::GetMagneticFluxDensityCallbackConfiguration) as usize] =
280            ResponseExpectedFlag::AlwaysTrue;
281        result.device.response_expected[u8::from(CompassBrickletFunction::SetConfiguration) as usize] = ResponseExpectedFlag::False;
282        result.device.response_expected[u8::from(CompassBrickletFunction::GetConfiguration) as usize] = ResponseExpectedFlag::AlwaysTrue;
283        result.device.response_expected[u8::from(CompassBrickletFunction::SetCalibration) as usize] = ResponseExpectedFlag::False;
284        result.device.response_expected[u8::from(CompassBrickletFunction::GetCalibration) as usize] = ResponseExpectedFlag::AlwaysTrue;
285        result.device.response_expected[u8::from(CompassBrickletFunction::GetSpitfpErrorCount) as usize] = ResponseExpectedFlag::AlwaysTrue;
286        result.device.response_expected[u8::from(CompassBrickletFunction::SetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
287        result.device.response_expected[u8::from(CompassBrickletFunction::GetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
288        result.device.response_expected[u8::from(CompassBrickletFunction::SetWriteFirmwarePointer) as usize] = ResponseExpectedFlag::False;
289        result.device.response_expected[u8::from(CompassBrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
290        result.device.response_expected[u8::from(CompassBrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
291        result.device.response_expected[u8::from(CompassBrickletFunction::GetStatusLedConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
292        result.device.response_expected[u8::from(CompassBrickletFunction::GetChipTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
293        result.device.response_expected[u8::from(CompassBrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
294        result.device.response_expected[u8::from(CompassBrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
295        result.device.response_expected[u8::from(CompassBrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
296        result.device.response_expected[u8::from(CompassBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
297        result
298    }
299
300    /// Returns the response expected flag for the function specified by the function ID parameter.
301    /// It is true if the function is expected to send a response, false otherwise.
302    ///
303    /// For getter functions this is enabled by default and cannot be disabled, because those
304    /// functions will always send a response. For callback configuration functions it is enabled
305    /// by default too, but can be disabled by [`set_response_expected`](crate::compass_bricklet::CompassBricklet::set_response_expected).
306    /// For setter functions it is disabled by default and can be enabled.
307    ///
308    /// Enabling the response expected flag for a setter function allows to detect timeouts
309    /// and other error conditions calls of this setter as well. The device will then send a response
310    /// for this purpose. If this flag is disabled for a setter function then no response is sent
311    /// and errors are silently ignored, because they cannot be detected.
312    ///
313    /// See [`set_response_expected`](crate::compass_bricklet::CompassBricklet::set_response_expected) for the list of function ID constants available for this function.
314    pub fn get_response_expected(&mut self, fun: CompassBrickletFunction) -> Result<bool, GetResponseExpectedError> {
315        self.device.get_response_expected(u8::from(fun))
316    }
317
318    /// Changes the response expected flag of the function specified by the function ID parameter.
319    /// This flag can only be changed for setter (default value: false) and callback configuration
320    /// functions (default value: true). For getter functions it is always enabled.
321    ///
322    /// Enabling the response expected flag for a setter function allows to detect timeouts and
323    /// other error conditions calls of this setter as well. The device will then send a response
324    /// for this purpose. If this flag is disabled for a setter function then no response is sent
325    /// and errors are silently ignored, because they cannot be detected.
326    pub fn set_response_expected(&mut self, fun: CompassBrickletFunction, response_expected: bool) -> Result<(), SetResponseExpectedError> {
327        self.device.set_response_expected(u8::from(fun), response_expected)
328    }
329
330    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
331    pub fn set_response_expected_all(&mut self, response_expected: bool) {
332        self.device.set_response_expected_all(response_expected)
333    }
334
335    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
336    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
337    pub fn get_api_version(&self) -> [u8; 3] {
338        self.device.api_version
339    }
340
341    /// This receiver is triggered periodically according to the configuration set by
342    /// [`set_heading_callback_configuration`].
343    ///
344    /// The parameter is the same as [`get_heading`].
345    ///
346    /// [`get_heading`]: #method.get_heading
347    /// [`set_heading_callback_configuration`]: #method.set_heading_callback_configuration
348    pub async fn get_heading_callback_receiver(&mut self) -> impl Stream<Item = i16> {
349        self.device
350            .get_callback_receiver(u8::from(CompassBrickletFunction::CallbackHeading))
351            .await
352            .map(|p| i16::from_le_byte_slice(p.body()))
353    }
354
355    /// This receiver is triggered periodically according to the configuration set by
356    /// [`set_magnetic_flux_density_callback_configuration`].
357    ///
358    /// The parameters are the same as [`get_magnetic_flux_density`].
359    pub async fn get_magnetic_flux_density_callback_receiver(&mut self) -> impl Stream<Item = MagneticFluxDensityEvent> {
360        self.device
361            .get_callback_receiver(u8::from(CompassBrickletFunction::CallbackMagneticFluxDensity))
362            .await
363            .map(|p| MagneticFluxDensityEvent::from_le_byte_slice(p.body()))
364    }
365
366    /// Returns the heading (north = 0 degree, east = 90 degree).
367    ///
368    /// Alternatively you can use [`get_magnetic_flux_density`] and calculate the
369    /// heading with ``heading = atan2(y, x) * 180 / PI``.
370    ///
371    ///
372    /// If you want to get the value periodically, it is recommended to use the
373    /// [`get_heading_callback_receiver`] receiver. You can set the receiver configuration
374    /// with [`set_heading_callback_configuration`].
375    pub async fn get_heading(&mut self) -> Result<i16, TinkerforgeError> {
376        let payload = [0; 0];
377
378        #[allow(unused_variables)]
379        let result = self.device.get(u8::from(CompassBrickletFunction::GetHeading), &payload).await?;
380        Ok(i16::from_le_byte_slice(result.body()))
381    }
382
383    /// The period is the period with which the [`get_heading_callback_receiver`] receiver is triggered
384    /// periodically. A value of 0 turns the receiver off.
385    ///
386    /// If the `value has to change`-parameter is set to true, the receiver is only
387    /// triggered after the value has changed. If the value didn't change
388    /// within the period, the receiver is triggered immediately on change.
389    ///
390    /// If it is set to false, the receiver is continuously triggered with the period,
391    /// independent of the value.
392    ///
393    /// It is furthermore possible to constrain the receiver with thresholds.
394    ///
395    /// The `option`-parameter together with min/max sets a threshold for the [`get_heading_callback_receiver`] receiver.
396    ///
397    /// The following options are possible:
398    ///
399    ///  Option| Description
400    ///  --- | ---
401    ///  'x'|    Threshold is turned off
402    ///  'o'|    Threshold is triggered when the value is *outside* the min and max values
403    ///  'i'|    Threshold is triggered when the value is *inside* or equal to the min and max values
404    ///  '<'|    Threshold is triggered when the value is smaller than the min value (max is ignored)
405    ///  '>'|    Threshold is triggered when the value is greater than the min value (max is ignored)
406    ///
407    /// If the option is set to 'x' (threshold turned off) the receiver is triggered with the fixed period.
408    ///
409    /// Associated constants:
410    /// * COMPASS_BRICKLET_THRESHOLD_OPTION_OFF
411    ///	* COMPASS_BRICKLET_THRESHOLD_OPTION_OUTSIDE
412    ///	* COMPASS_BRICKLET_THRESHOLD_OPTION_INSIDE
413    ///	* COMPASS_BRICKLET_THRESHOLD_OPTION_SMALLER
414    ///	* COMPASS_BRICKLET_THRESHOLD_OPTION_GREATER
415    pub async fn set_heading_callback_configuration(
416        &mut self,
417        period: u32,
418        value_has_to_change: bool,
419        option: char,
420        min: i16,
421        max: i16,
422    ) -> Result<(), TinkerforgeError> {
423        let mut payload = [0; 10];
424        period.write_to_slice(&mut payload[0..4]);
425        value_has_to_change.write_to_slice(&mut payload[4..5]);
426        option.write_to_slice(&mut payload[5..6]);
427        min.write_to_slice(&mut payload[6..8]);
428        max.write_to_slice(&mut payload[8..10]);
429
430        #[allow(unused_variables)]
431        let result = self.device.set(u8::from(CompassBrickletFunction::SetHeadingCallbackConfiguration), &payload).await?;
432        Ok(())
433    }
434
435    /// Returns the receiver configuration as set by [`set_heading_callback_configuration`].
436    ///
437    /// Associated constants:
438    /// * COMPASS_BRICKLET_THRESHOLD_OPTION_OFF
439    ///	* COMPASS_BRICKLET_THRESHOLD_OPTION_OUTSIDE
440    ///	* COMPASS_BRICKLET_THRESHOLD_OPTION_INSIDE
441    ///	* COMPASS_BRICKLET_THRESHOLD_OPTION_SMALLER
442    ///	* COMPASS_BRICKLET_THRESHOLD_OPTION_GREATER
443    pub async fn get_heading_callback_configuration(&mut self) -> Result<HeadingCallbackConfiguration, TinkerforgeError> {
444        let payload = [0; 0];
445
446        #[allow(unused_variables)]
447        let result = self.device.get(u8::from(CompassBrickletFunction::GetHeadingCallbackConfiguration), &payload).await?;
448        Ok(HeadingCallbackConfiguration::from_le_byte_slice(result.body()))
449    }
450
451    /// Returns the [magnetic flux density (magnetic induction)](https://en.wikipedia.org/wiki/Magnetic_flux)__
452    /// for all three axis.
453    ///
454    /// If you want to get the value periodically, it is recommended to use the
455    /// [`get_magnetic_flux_density_callback_receiver`] receiver. You can set the receiver configuration
456    /// with [`set_magnetic_flux_density_callback_configuration`].
457    pub async fn get_magnetic_flux_density(&mut self) -> Result<MagneticFluxDensity, TinkerforgeError> {
458        let payload = [0; 0];
459
460        #[allow(unused_variables)]
461        let result = self.device.get(u8::from(CompassBrickletFunction::GetMagneticFluxDensity), &payload).await?;
462        Ok(MagneticFluxDensity::from_le_byte_slice(result.body()))
463    }
464
465    /// The period is the period with which the [`get_magnetic_flux_density_callback_receiver`] receiver
466    /// is triggered periodically. A value of 0 turns the receiver off.
467    ///
468    /// If the `value has to change`-parameter is set to true, the receiver is only
469    /// triggered after the value has changed. If the value didn't change within the
470    /// period, the receiver is triggered immediately on change.
471    ///
472    /// If it is set to false, the receiver is continuously triggered with the period,
473    /// independent of the value.
474    pub async fn set_magnetic_flux_density_callback_configuration(
475        &mut self,
476        period: u32,
477        value_has_to_change: bool,
478    ) -> Result<(), TinkerforgeError> {
479        let mut payload = [0; 5];
480        period.write_to_slice(&mut payload[0..4]);
481        value_has_to_change.write_to_slice(&mut payload[4..5]);
482
483        #[allow(unused_variables)]
484        let result = self.device.set(u8::from(CompassBrickletFunction::SetMagneticFluxDensityCallbackConfiguration), &payload).await?;
485        Ok(())
486    }
487
488    /// Returns the receiver configuration as set by
489    /// [`set_magnetic_flux_density_callback_configuration`].
490    pub async fn get_magnetic_flux_density_callback_configuration(
491        &mut self,
492    ) -> Result<MagneticFluxDensityCallbackConfiguration, TinkerforgeError> {
493        let payload = [0; 0];
494
495        #[allow(unused_variables)]
496        let result = self.device.get(u8::from(CompassBrickletFunction::GetMagneticFluxDensityCallbackConfiguration), &payload).await?;
497        Ok(MagneticFluxDensityCallbackConfiguration::from_le_byte_slice(result.body()))
498    }
499
500    /// Configures the data rate and background calibration.
501    ///
502    /// * Data Rate: Sets the data rate that is used by the magnetometer.
503    ///   The lower the data rate, the lower is the noise on the data.
504    /// * Background Calibration: Set to *true* to enable the background
505    ///   calibration and *false* to turn it off. If the background calibration
506    ///   is enabled the sensing polarity is flipped once per second to automatically
507    ///   calculate and remove offset that is caused by temperature changes.
508    ///   This polarity flipping takes about 20ms. This means that once a second
509    ///   you will not get new data for a period of 20ms. We highly recommend that
510    ///   you keep the background calibration enabled and only disable it if the 20ms
511    ///   off-time is a problem in your application.
512    ///
513    /// Associated constants:
514    /// * COMPASS_BRICKLET_DATA_RATE_100HZ
515    ///	* COMPASS_BRICKLET_DATA_RATE_200HZ
516    ///	* COMPASS_BRICKLET_DATA_RATE_400HZ
517    ///	* COMPASS_BRICKLET_DATA_RATE_600HZ
518    pub async fn set_configuration(&mut self, data_rate: u8, background_calibration: bool) -> Result<(), TinkerforgeError> {
519        let mut payload = [0; 2];
520        data_rate.write_to_slice(&mut payload[0..1]);
521        background_calibration.write_to_slice(&mut payload[1..2]);
522
523        #[allow(unused_variables)]
524        let result = self.device.set(u8::from(CompassBrickletFunction::SetConfiguration), &payload).await?;
525        Ok(())
526    }
527
528    /// Returns the configuration as set by [`set_configuration`].
529    ///
530    /// Associated constants:
531    /// * COMPASS_BRICKLET_DATA_RATE_100HZ
532    ///	* COMPASS_BRICKLET_DATA_RATE_200HZ
533    ///	* COMPASS_BRICKLET_DATA_RATE_400HZ
534    ///	* COMPASS_BRICKLET_DATA_RATE_600HZ
535    pub async fn get_configuration(&mut self) -> Result<Configuration, TinkerforgeError> {
536        let payload = [0; 0];
537
538        #[allow(unused_variables)]
539        let result = self.device.get(u8::from(CompassBrickletFunction::GetConfiguration), &payload).await?;
540        Ok(Configuration::from_le_byte_slice(result.body()))
541    }
542
543    /// Sets offset and gain for each of the three axes.
544    ///
545    /// The Bricklet is factory calibrated. If you want to re-calibrate the
546    /// Bricklet we recommend that you do the calibration through Brick Viewer.
547    ///
548    /// The calibration is saved in non-volatile memory and only has to be
549    /// done once.
550    pub async fn set_calibration(&mut self, offset: &[i16; 3], gain: &[i16; 3]) -> Result<(), TinkerforgeError> {
551        let mut payload = [0; 12];
552        offset.write_to_slice(&mut payload[0..6]);
553        gain.write_to_slice(&mut payload[6..12]);
554
555        #[allow(unused_variables)]
556        let result = self.device.set(u8::from(CompassBrickletFunction::SetCalibration), &payload).await?;
557        Ok(())
558    }
559
560    /// Returns the calibration parameters as set by [`set_calibration`].
561    pub async fn get_calibration(&mut self) -> Result<Calibration, TinkerforgeError> {
562        let payload = [0; 0];
563
564        #[allow(unused_variables)]
565        let result = self.device.get(u8::from(CompassBrickletFunction::GetCalibration), &payload).await?;
566        Ok(Calibration::from_le_byte_slice(result.body()))
567    }
568
569    /// Returns the error count for the communication between Brick and Bricklet.
570    ///
571    /// The errors are divided into
572    ///
573    /// * ACK checksum errors,
574    /// * message checksum errors,
575    /// * framing errors and
576    /// * overflow errors.
577    ///
578    /// The errors counts are for errors that occur on the Bricklet side. All
579    /// Bricks have a similar function that returns the errors on the Brick side.
580    pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
581        let payload = [0; 0];
582
583        #[allow(unused_variables)]
584        let result = self.device.get(u8::from(CompassBrickletFunction::GetSpitfpErrorCount), &payload).await?;
585        Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
586    }
587
588    /// Sets the bootloader mode and returns the status after the requested
589    /// mode change was instigated.
590    ///
591    /// You can change from bootloader mode to firmware mode and vice versa. A change
592    /// from bootloader mode to firmware mode will only take place if the entry function,
593    /// device identifier and CRC are present and correct.
594    ///
595    /// This function is used by Brick Viewer during flashing. It should not be
596    /// necessary to call it in a normal user program.
597    ///
598    /// Associated constants:
599    /// * COMPASS_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
600    ///	* COMPASS_BRICKLET_BOOTLOADER_MODE_FIRMWARE
601    ///	* COMPASS_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
602    ///	* COMPASS_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
603    ///	* COMPASS_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
604    ///	* COMPASS_BRICKLET_BOOTLOADER_STATUS_OK
605    ///	* COMPASS_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
606    ///	* COMPASS_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
607    ///	* COMPASS_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
608    ///	* COMPASS_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
609    ///	* COMPASS_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
610    pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
611        let mut payload = [0; 1];
612        mode.write_to_slice(&mut payload[0..1]);
613
614        #[allow(unused_variables)]
615        let result = self.device.get(u8::from(CompassBrickletFunction::SetBootloaderMode), &payload).await?;
616        Ok(u8::from_le_byte_slice(result.body()))
617    }
618
619    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
620    ///
621    /// Associated constants:
622    /// * COMPASS_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
623    ///	* COMPASS_BRICKLET_BOOTLOADER_MODE_FIRMWARE
624    ///	* COMPASS_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
625    ///	* COMPASS_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
626    ///	* COMPASS_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
627    pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
628        let payload = [0; 0];
629
630        #[allow(unused_variables)]
631        let result = self.device.get(u8::from(CompassBrickletFunction::GetBootloaderMode), &payload).await?;
632        Ok(u8::from_le_byte_slice(result.body()))
633    }
634
635    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
636    /// to be increased by chunks of size 64. The data is written to flash
637    /// every 4 chunks (which equals to one page of size 256).
638    ///
639    /// This function is used by Brick Viewer during flashing. It should not be
640    /// necessary to call it in a normal user program.
641    pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
642        let mut payload = [0; 4];
643        pointer.write_to_slice(&mut payload[0..4]);
644
645        #[allow(unused_variables)]
646        let result = self.device.set(u8::from(CompassBrickletFunction::SetWriteFirmwarePointer), &payload).await?;
647        Ok(())
648    }
649
650    /// Writes 64 Bytes of firmware at the position as written by
651    /// [`set_write_firmware_pointer`] before. The firmware is written
652    /// to flash every 4 chunks.
653    ///
654    /// You can only write firmware in bootloader mode.
655    ///
656    /// This function is used by Brick Viewer during flashing. It should not be
657    /// necessary to call it in a normal user program.
658    pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
659        let mut payload = [0; 64];
660        data.write_to_slice(&mut payload[0..64]);
661
662        #[allow(unused_variables)]
663        let result = self.device.get(u8::from(CompassBrickletFunction::WriteFirmware), &payload).await?;
664        Ok(u8::from_le_byte_slice(result.body()))
665    }
666
667    /// Sets the status LED configuration. By default the LED shows
668    /// communication traffic between Brick and Bricklet, it flickers once
669    /// for every 10 received data packets.
670    ///
671    /// You can also turn the LED permanently on/off or show a heartbeat.
672    ///
673    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
674    ///
675    /// Associated constants:
676    /// * COMPASS_BRICKLET_STATUS_LED_CONFIG_OFF
677    ///	* COMPASS_BRICKLET_STATUS_LED_CONFIG_ON
678    ///	* COMPASS_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
679    ///	* COMPASS_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
680    pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
681        let mut payload = [0; 1];
682        config.write_to_slice(&mut payload[0..1]);
683
684        #[allow(unused_variables)]
685        let result = self.device.set(u8::from(CompassBrickletFunction::SetStatusLedConfig), &payload).await?;
686        Ok(())
687    }
688
689    /// Returns the configuration as set by [`set_status_led_config`]
690    ///
691    /// Associated constants:
692    /// * COMPASS_BRICKLET_STATUS_LED_CONFIG_OFF
693    ///	* COMPASS_BRICKLET_STATUS_LED_CONFIG_ON
694    ///	* COMPASS_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
695    ///	* COMPASS_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
696    pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
697        let payload = [0; 0];
698
699        #[allow(unused_variables)]
700        let result = self.device.get(u8::from(CompassBrickletFunction::GetStatusLedConfig), &payload).await?;
701        Ok(u8::from_le_byte_slice(result.body()))
702    }
703
704    /// Returns the temperature as measured inside the microcontroller. The
705    /// value returned is not the ambient temperature!
706    ///
707    /// The temperature is only proportional to the real temperature and it has bad
708    /// accuracy. Practically it is only useful as an indicator for
709    /// temperature changes.
710    pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
711        let payload = [0; 0];
712
713        #[allow(unused_variables)]
714        let result = self.device.get(u8::from(CompassBrickletFunction::GetChipTemperature), &payload).await?;
715        Ok(i16::from_le_byte_slice(result.body()))
716    }
717
718    /// Calling this function will reset the Bricklet. All configurations
719    /// will be lost.
720    ///
721    /// After a reset you have to create new device objects,
722    /// calling functions on the existing ones will result in
723    /// undefined behavior!
724    pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
725        let payload = [0; 0];
726
727        #[allow(unused_variables)]
728        let result = self.device.set(u8::from(CompassBrickletFunction::Reset), &payload).await?;
729        Ok(())
730    }
731
732    /// Writes a new UID into flash. If you want to set a new UID
733    /// you have to decode the Base58 encoded UID string into an
734    /// integer first.
735    ///
736    /// We recommend that you use Brick Viewer to change the UID.
737    pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
738        let mut payload = [0; 4];
739        uid.write_to_slice(&mut payload[0..4]);
740
741        #[allow(unused_variables)]
742        let result = self.device.set(u8::from(CompassBrickletFunction::WriteUid), &payload).await?;
743        Ok(())
744    }
745
746    /// Returns the current UID as an integer. Encode as
747    /// Base58 to get the usual string version.
748    pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
749        let payload = [0; 0];
750
751        #[allow(unused_variables)]
752        let result = self.device.get(u8::from(CompassBrickletFunction::ReadUid), &payload).await?;
753        Ok(u32::from_le_byte_slice(result.body()))
754    }
755
756    /// Returns the UID, the UID where the Bricklet is connected to,
757    /// the position, the hardware and firmware version as well as the
758    /// device identifier.
759    ///
760    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
761    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
762    /// position 'z'.
763    ///
764    /// The device identifier numbers can be found [here](device_identifier).
765    /// |device_identifier_constant|
766    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
767        let payload = [0; 0];
768
769        #[allow(unused_variables)]
770        let result = self.device.get(u8::from(CompassBrickletFunction::GetIdentity), &payload).await?;
771        Ok(Identity::from_le_byte_slice(result.body()))
772    }
773}