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