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