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