tinkerforge_async/bindings/
ptc_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//! Reads temperatures from Pt100 und Pt1000 sensors.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/PTCV2_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 PtcV2BrickletFunction {
24    GetTemperature,
25    SetTemperatureCallbackConfiguration,
26    GetTemperatureCallbackConfiguration,
27    GetResistance,
28    SetResistanceCallbackConfiguration,
29    GetResistanceCallbackConfiguration,
30    SetNoiseRejectionFilter,
31    GetNoiseRejectionFilter,
32    IsSensorConnected,
33    SetWireMode,
34    GetWireMode,
35    SetMovingAverageConfiguration,
36    GetMovingAverageConfiguration,
37    SetSensorConnectedCallbackConfiguration,
38    GetSensorConnectedCallbackConfiguration,
39    GetSpitfpErrorCount,
40    SetBootloaderMode,
41    GetBootloaderMode,
42    SetWriteFirmwarePointer,
43    WriteFirmware,
44    SetStatusLedConfig,
45    GetStatusLedConfig,
46    GetChipTemperature,
47    Reset,
48    WriteUid,
49    ReadUid,
50    GetIdentity,
51    CallbackTemperature,
52    CallbackResistance,
53    CallbackSensorConnected,
54}
55impl From<PtcV2BrickletFunction> for u8 {
56    fn from(fun: PtcV2BrickletFunction) -> Self {
57        match fun {
58            PtcV2BrickletFunction::GetTemperature => 1,
59            PtcV2BrickletFunction::SetTemperatureCallbackConfiguration => 2,
60            PtcV2BrickletFunction::GetTemperatureCallbackConfiguration => 3,
61            PtcV2BrickletFunction::GetResistance => 5,
62            PtcV2BrickletFunction::SetResistanceCallbackConfiguration => 6,
63            PtcV2BrickletFunction::GetResistanceCallbackConfiguration => 7,
64            PtcV2BrickletFunction::SetNoiseRejectionFilter => 9,
65            PtcV2BrickletFunction::GetNoiseRejectionFilter => 10,
66            PtcV2BrickletFunction::IsSensorConnected => 11,
67            PtcV2BrickletFunction::SetWireMode => 12,
68            PtcV2BrickletFunction::GetWireMode => 13,
69            PtcV2BrickletFunction::SetMovingAverageConfiguration => 14,
70            PtcV2BrickletFunction::GetMovingAverageConfiguration => 15,
71            PtcV2BrickletFunction::SetSensorConnectedCallbackConfiguration => 16,
72            PtcV2BrickletFunction::GetSensorConnectedCallbackConfiguration => 17,
73            PtcV2BrickletFunction::GetSpitfpErrorCount => 234,
74            PtcV2BrickletFunction::SetBootloaderMode => 235,
75            PtcV2BrickletFunction::GetBootloaderMode => 236,
76            PtcV2BrickletFunction::SetWriteFirmwarePointer => 237,
77            PtcV2BrickletFunction::WriteFirmware => 238,
78            PtcV2BrickletFunction::SetStatusLedConfig => 239,
79            PtcV2BrickletFunction::GetStatusLedConfig => 240,
80            PtcV2BrickletFunction::GetChipTemperature => 242,
81            PtcV2BrickletFunction::Reset => 243,
82            PtcV2BrickletFunction::WriteUid => 248,
83            PtcV2BrickletFunction::ReadUid => 249,
84            PtcV2BrickletFunction::GetIdentity => 255,
85            PtcV2BrickletFunction::CallbackTemperature => 4,
86            PtcV2BrickletFunction::CallbackResistance => 8,
87            PtcV2BrickletFunction::CallbackSensorConnected => 18,
88        }
89    }
90}
91pub const PTC_V2_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
92pub const PTC_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
93pub const PTC_V2_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
94pub const PTC_V2_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
95pub const PTC_V2_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
96pub const PTC_V2_BRICKLET_FILTER_OPTION_50HZ: u8 = 0;
97pub const PTC_V2_BRICKLET_FILTER_OPTION_60HZ: u8 = 1;
98pub const PTC_V2_BRICKLET_WIRE_MODE_2: u8 = 2;
99pub const PTC_V2_BRICKLET_WIRE_MODE_3: u8 = 3;
100pub const PTC_V2_BRICKLET_WIRE_MODE_4: u8 = 4;
101pub const PTC_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
102pub const PTC_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
103pub const PTC_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
104pub const PTC_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
105pub const PTC_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
106pub const PTC_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
107pub const PTC_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
108pub const PTC_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
109pub const PTC_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
110pub const PTC_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
111pub const PTC_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
112pub const PTC_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
113pub const PTC_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
114pub const PTC_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
115pub const PTC_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
116
117#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
118pub struct TemperatureCallbackConfiguration {
119    pub period: u32,
120    pub value_has_to_change: bool,
121    pub option: char,
122    pub min: i32,
123    pub max: i32,
124}
125impl FromByteSlice for TemperatureCallbackConfiguration {
126    fn bytes_expected() -> usize {
127        14
128    }
129    fn from_le_byte_slice(bytes: &[u8]) -> TemperatureCallbackConfiguration {
130        TemperatureCallbackConfiguration {
131            period: <u32>::from_le_byte_slice(&bytes[0..4]),
132            value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
133            option: <char>::from_le_byte_slice(&bytes[5..6]),
134            min: <i32>::from_le_byte_slice(&bytes[6..10]),
135            max: <i32>::from_le_byte_slice(&bytes[10..14]),
136        }
137    }
138}
139
140#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
141pub struct ResistanceCallbackConfiguration {
142    pub period: u32,
143    pub value_has_to_change: bool,
144    pub option: char,
145    pub min: i32,
146    pub max: i32,
147}
148impl FromByteSlice for ResistanceCallbackConfiguration {
149    fn bytes_expected() -> usize {
150        14
151    }
152    fn from_le_byte_slice(bytes: &[u8]) -> ResistanceCallbackConfiguration {
153        ResistanceCallbackConfiguration {
154            period: <u32>::from_le_byte_slice(&bytes[0..4]),
155            value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
156            option: <char>::from_le_byte_slice(&bytes[5..6]),
157            min: <i32>::from_le_byte_slice(&bytes[6..10]),
158            max: <i32>::from_le_byte_slice(&bytes[10..14]),
159        }
160    }
161}
162
163#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
164pub struct MovingAverageConfiguration {
165    pub moving_average_length_resistance: u16,
166    pub moving_average_length_temperature: u16,
167}
168impl FromByteSlice for MovingAverageConfiguration {
169    fn bytes_expected() -> usize {
170        4
171    }
172    fn from_le_byte_slice(bytes: &[u8]) -> MovingAverageConfiguration {
173        MovingAverageConfiguration {
174            moving_average_length_resistance: <u16>::from_le_byte_slice(&bytes[0..2]),
175            moving_average_length_temperature: <u16>::from_le_byte_slice(&bytes[2..4]),
176        }
177    }
178}
179
180#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
181pub struct SpitfpErrorCount {
182    pub error_count_ack_checksum: u32,
183    pub error_count_message_checksum: u32,
184    pub error_count_frame: u32,
185    pub error_count_overflow: u32,
186}
187impl FromByteSlice for SpitfpErrorCount {
188    fn bytes_expected() -> usize {
189        16
190    }
191    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
192        SpitfpErrorCount {
193            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
194            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
195            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
196            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
197        }
198    }
199}
200
201#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
202pub struct Identity {
203    pub uid: String,
204    pub connected_uid: String,
205    pub position: char,
206    pub hardware_version: [u8; 3],
207    pub firmware_version: [u8; 3],
208    pub device_identifier: u16,
209}
210impl FromByteSlice for Identity {
211    fn bytes_expected() -> usize {
212        25
213    }
214    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
215        Identity {
216            uid: <String>::from_le_byte_slice(&bytes[0..8]),
217            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
218            position: <char>::from_le_byte_slice(&bytes[16..17]),
219            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
220            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
221            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
222        }
223    }
224}
225
226/// Reads temperatures from Pt100 und Pt1000 sensors
227#[derive(Clone)]
228pub struct PtcV2Bricklet {
229    device: Device,
230}
231impl PtcV2Bricklet {
232    pub const DEVICE_IDENTIFIER: u16 = 2101;
233    pub const DEVICE_DISPLAY_NAME: &'static str = "PTC Bricklet 2.0";
234    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
235    pub fn new(uid: Uid, connection: AsyncIpConnection) -> PtcV2Bricklet {
236        let mut result = PtcV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
237        result.device.response_expected[u8::from(PtcV2BrickletFunction::GetTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
238        result.device.response_expected[u8::from(PtcV2BrickletFunction::SetTemperatureCallbackConfiguration) as usize] =
239            ResponseExpectedFlag::True;
240        result.device.response_expected[u8::from(PtcV2BrickletFunction::GetTemperatureCallbackConfiguration) as usize] =
241            ResponseExpectedFlag::AlwaysTrue;
242        result.device.response_expected[u8::from(PtcV2BrickletFunction::GetResistance) as usize] = ResponseExpectedFlag::AlwaysTrue;
243        result.device.response_expected[u8::from(PtcV2BrickletFunction::SetResistanceCallbackConfiguration) as usize] =
244            ResponseExpectedFlag::True;
245        result.device.response_expected[u8::from(PtcV2BrickletFunction::GetResistanceCallbackConfiguration) as usize] =
246            ResponseExpectedFlag::AlwaysTrue;
247        result.device.response_expected[u8::from(PtcV2BrickletFunction::SetNoiseRejectionFilter) as usize] = ResponseExpectedFlag::False;
248        result.device.response_expected[u8::from(PtcV2BrickletFunction::GetNoiseRejectionFilter) as usize] =
249            ResponseExpectedFlag::AlwaysTrue;
250        result.device.response_expected[u8::from(PtcV2BrickletFunction::IsSensorConnected) as usize] = ResponseExpectedFlag::AlwaysTrue;
251        result.device.response_expected[u8::from(PtcV2BrickletFunction::SetWireMode) as usize] = ResponseExpectedFlag::False;
252        result.device.response_expected[u8::from(PtcV2BrickletFunction::GetWireMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
253        result.device.response_expected[u8::from(PtcV2BrickletFunction::SetMovingAverageConfiguration) as usize] =
254            ResponseExpectedFlag::False;
255        result.device.response_expected[u8::from(PtcV2BrickletFunction::GetMovingAverageConfiguration) as usize] =
256            ResponseExpectedFlag::AlwaysTrue;
257        result.device.response_expected[u8::from(PtcV2BrickletFunction::SetSensorConnectedCallbackConfiguration) as usize] =
258            ResponseExpectedFlag::True;
259        result.device.response_expected[u8::from(PtcV2BrickletFunction::GetSensorConnectedCallbackConfiguration) as usize] =
260            ResponseExpectedFlag::AlwaysTrue;
261        result.device.response_expected[u8::from(PtcV2BrickletFunction::GetSpitfpErrorCount) as usize] = ResponseExpectedFlag::AlwaysTrue;
262        result.device.response_expected[u8::from(PtcV2BrickletFunction::SetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
263        result.device.response_expected[u8::from(PtcV2BrickletFunction::GetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
264        result.device.response_expected[u8::from(PtcV2BrickletFunction::SetWriteFirmwarePointer) as usize] = ResponseExpectedFlag::False;
265        result.device.response_expected[u8::from(PtcV2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
266        result.device.response_expected[u8::from(PtcV2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
267        result.device.response_expected[u8::from(PtcV2BrickletFunction::GetStatusLedConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
268        result.device.response_expected[u8::from(PtcV2BrickletFunction::GetChipTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
269        result.device.response_expected[u8::from(PtcV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
270        result.device.response_expected[u8::from(PtcV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
271        result.device.response_expected[u8::from(PtcV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
272        result.device.response_expected[u8::from(PtcV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
273        result
274    }
275
276    /// Returns the response expected flag for the function specified by the function ID parameter.
277    /// It is true if the function is expected to send a response, false otherwise.
278    ///
279    /// For getter functions this is enabled by default and cannot be disabled, because those
280    /// functions will always send a response. For callback configuration functions it is enabled
281    /// by default too, but can be disabled by [`set_response_expected`](crate::ptc_v2_bricklet::PtcV2Bricklet::set_response_expected).
282    /// For setter functions it is disabled by default and can be enabled.
283    ///
284    /// Enabling the response expected flag for a setter function allows to detect timeouts
285    /// and other error conditions calls of this setter as well. The device will then send a response
286    /// for this purpose. If this flag is disabled for a setter function then no response is sent
287    /// and errors are silently ignored, because they cannot be detected.
288    ///
289    /// See [`set_response_expected`](crate::ptc_v2_bricklet::PtcV2Bricklet::set_response_expected) for the list of function ID constants available for this function.
290    pub fn get_response_expected(&mut self, fun: PtcV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
291        self.device.get_response_expected(u8::from(fun))
292    }
293
294    /// Changes the response expected flag of the function specified by the function ID parameter.
295    /// This flag can only be changed for setter (default value: false) and callback configuration
296    /// functions (default value: true). For getter functions it is always enabled.
297    ///
298    /// Enabling the response expected flag for a setter function allows to detect timeouts and
299    /// other error conditions calls of this setter as well. The device will then send a response
300    /// for this purpose. If this flag is disabled for a setter function then no response is sent
301    /// and errors are silently ignored, because they cannot be detected.
302    pub fn set_response_expected(&mut self, fun: PtcV2BrickletFunction, response_expected: bool) -> Result<(), SetResponseExpectedError> {
303        self.device.set_response_expected(u8::from(fun), response_expected)
304    }
305
306    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
307    pub fn set_response_expected_all(&mut self, response_expected: bool) {
308        self.device.set_response_expected_all(response_expected)
309    }
310
311    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
312    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
313    pub fn get_api_version(&self) -> [u8; 3] {
314        self.device.api_version
315    }
316
317    /// This receiver is triggered periodically according to the configuration set by
318    /// [`set_temperature_callback_configuration`].
319    ///
320    /// The parameter is the same as [`get_temperature`].
321    ///
322    /// [`get_temperature`]: #method.get_temperature
323    /// [`set_temperature_callback_configuration`]: #method.set_temperature_callback_configuration
324    pub async fn get_temperature_callback_receiver(&mut self) -> impl Stream<Item = i32> {
325        self.device
326            .get_callback_receiver(u8::from(PtcV2BrickletFunction::CallbackTemperature))
327            .await
328            .map(|p| i32::from_le_byte_slice(p.body()))
329    }
330
331    /// This receiver is triggered periodically according to the configuration set by
332    /// [`set_resistance_callback_configuration`].
333    ///
334    /// The parameter is the same as [`get_resistance`].
335    pub async fn get_resistance_callback_receiver(&mut self) -> impl Stream<Item = i32> {
336        self.device
337            .get_callback_receiver(u8::from(PtcV2BrickletFunction::CallbackResistance))
338            .await
339            .map(|p| i32::from_le_byte_slice(p.body()))
340    }
341
342    /// This receiver is triggered periodically according to the configuration set by
343    /// [`set_sensor_connected_callback_configuration`].
344    ///
345    /// The parameter is the same as [`is_sensor_connected`].
346    pub async fn get_sensor_connected_callback_receiver(&mut self) -> impl Stream<Item = bool> {
347        self.device
348            .get_callback_receiver(u8::from(PtcV2BrickletFunction::CallbackSensorConnected))
349            .await
350            .map(|p| bool::from_le_byte_slice(p.body()))
351    }
352
353    /// Returns the temperature of the connected sensor.
354    ///
355    ///
356    /// If you want to get the value periodically, it is recommended to use the
357    /// [`get_temperature_callback_receiver`] receiver. You can set the receiver configuration
358    /// with [`set_temperature_callback_configuration`].
359    pub async fn get_temperature(&mut self) -> Result<i32, TinkerforgeError> {
360        let payload = [0; 0];
361
362        #[allow(unused_variables)]
363        let result = self.device.get(u8::from(PtcV2BrickletFunction::GetTemperature), &payload).await?;
364        Ok(i32::from_le_byte_slice(result.body()))
365    }
366
367    /// The period is the period with which the [`get_temperature_callback_receiver`] receiver is triggered
368    /// periodically. A value of 0 turns the receiver off.
369    ///
370    /// If the `value has to change`-parameter is set to true, the receiver is only
371    /// triggered after the value has changed. If the value didn't change
372    /// within the period, the receiver is triggered immediately on change.
373    ///
374    /// If it is set to false, the receiver is continuously triggered with the period,
375    /// independent of the value.
376    ///
377    /// It is furthermore possible to constrain the receiver with thresholds.
378    ///
379    /// The `option`-parameter together with min/max sets a threshold for the [`get_temperature_callback_receiver`] receiver.
380    ///
381    /// The following options are possible:
382    ///
383    ///  Option| Description
384    ///  --- | ---
385    ///  'x'|    Threshold is turned off
386    ///  'o'|    Threshold is triggered when the value is *outside* the min and max values
387    ///  'i'|    Threshold is triggered when the value is *inside* or equal to the min and max values
388    ///  '<'|    Threshold is triggered when the value is smaller than the min value (max is ignored)
389    ///  '>'|    Threshold is triggered when the value is greater than the min value (max is ignored)
390    ///
391    /// If the option is set to 'x' (threshold turned off) the receiver is triggered with the fixed period.
392    ///
393    /// Associated constants:
394    /// * PTC_V2_BRICKLET_THRESHOLD_OPTION_OFF
395    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
396    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
397    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
398    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_GREATER
399    pub async fn set_temperature_callback_configuration(
400        &mut self,
401        period: u32,
402        value_has_to_change: bool,
403        option: char,
404        min: i32,
405        max: i32,
406    ) -> Result<(), TinkerforgeError> {
407        let mut payload = [0; 14];
408        period.write_to_slice(&mut payload[0..4]);
409        value_has_to_change.write_to_slice(&mut payload[4..5]);
410        option.write_to_slice(&mut payload[5..6]);
411        min.write_to_slice(&mut payload[6..10]);
412        max.write_to_slice(&mut payload[10..14]);
413
414        #[allow(unused_variables)]
415        let result = self.device.set(u8::from(PtcV2BrickletFunction::SetTemperatureCallbackConfiguration), &payload).await?;
416        Ok(())
417    }
418
419    /// Returns the receiver configuration as set by [`set_temperature_callback_configuration`].
420    ///
421    /// Associated constants:
422    /// * PTC_V2_BRICKLET_THRESHOLD_OPTION_OFF
423    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
424    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
425    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
426    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_GREATER
427    pub async fn get_temperature_callback_configuration(&mut self) -> Result<TemperatureCallbackConfiguration, TinkerforgeError> {
428        let payload = [0; 0];
429
430        #[allow(unused_variables)]
431        let result = self.device.get(u8::from(PtcV2BrickletFunction::GetTemperatureCallbackConfiguration), &payload).await?;
432        Ok(TemperatureCallbackConfiguration::from_le_byte_slice(result.body()))
433    }
434
435    /// Returns the value as measured by the MAX31865 precision delta-sigma ADC.
436    ///
437    /// The value can be converted with the following formulas:
438    ///
439    /// * Pt100:  resistance = (value * 390) / 32768
440    /// * Pt1000: resistance = (value * 3900) / 32768
441    ///
442    ///
443    /// If you want to get the value periodically, it is recommended to use the
444    /// [`get_resistance_callback_receiver`] receiver. You can set the receiver configuration
445    /// with [`set_resistance_callback_configuration`].
446    pub async fn get_resistance(&mut self) -> Result<i32, TinkerforgeError> {
447        let payload = [0; 0];
448
449        #[allow(unused_variables)]
450        let result = self.device.get(u8::from(PtcV2BrickletFunction::GetResistance), &payload).await?;
451        Ok(i32::from_le_byte_slice(result.body()))
452    }
453
454    /// The period is the period with which the [`get_resistance_callback_receiver`] receiver is triggered
455    /// periodically. A value of 0 turns the receiver off.
456    ///
457    /// If the `value has to change`-parameter is set to true, the receiver is only
458    /// triggered after the value has changed. If the value didn't change
459    /// within the period, the receiver is triggered immediately on change.
460    ///
461    /// If it is set to false, the receiver is continuously triggered with the period,
462    /// independent of the value.
463    ///
464    /// It is furthermore possible to constrain the receiver with thresholds.
465    ///
466    /// The `option`-parameter together with min/max sets a threshold for the [`get_resistance_callback_receiver`] receiver.
467    ///
468    /// The following options are possible:
469    ///
470    ///  Option| Description
471    ///  --- | ---
472    ///  'x'|    Threshold is turned off
473    ///  'o'|    Threshold is triggered when the value is *outside* the min and max values
474    ///  'i'|    Threshold is triggered when the value is *inside* or equal to the min and max values
475    ///  '<'|    Threshold is triggered when the value is smaller than the min value (max is ignored)
476    ///  '>'|    Threshold is triggered when the value is greater than the min value (max is ignored)
477    ///
478    /// If the option is set to 'x' (threshold turned off) the receiver is triggered with the fixed period.
479    ///
480    /// Associated constants:
481    /// * PTC_V2_BRICKLET_THRESHOLD_OPTION_OFF
482    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
483    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
484    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
485    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_GREATER
486    pub async fn set_resistance_callback_configuration(
487        &mut self,
488        period: u32,
489        value_has_to_change: bool,
490        option: char,
491        min: i32,
492        max: i32,
493    ) -> Result<(), TinkerforgeError> {
494        let mut payload = [0; 14];
495        period.write_to_slice(&mut payload[0..4]);
496        value_has_to_change.write_to_slice(&mut payload[4..5]);
497        option.write_to_slice(&mut payload[5..6]);
498        min.write_to_slice(&mut payload[6..10]);
499        max.write_to_slice(&mut payload[10..14]);
500
501        #[allow(unused_variables)]
502        let result = self.device.set(u8::from(PtcV2BrickletFunction::SetResistanceCallbackConfiguration), &payload).await?;
503        Ok(())
504    }
505
506    /// Returns the receiver configuration as set by [`set_resistance_callback_configuration`].
507    ///
508    /// Associated constants:
509    /// * PTC_V2_BRICKLET_THRESHOLD_OPTION_OFF
510    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
511    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
512    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
513    ///	* PTC_V2_BRICKLET_THRESHOLD_OPTION_GREATER
514    pub async fn get_resistance_callback_configuration(&mut self) -> Result<ResistanceCallbackConfiguration, TinkerforgeError> {
515        let payload = [0; 0];
516
517        #[allow(unused_variables)]
518        let result = self.device.get(u8::from(PtcV2BrickletFunction::GetResistanceCallbackConfiguration), &payload).await?;
519        Ok(ResistanceCallbackConfiguration::from_le_byte_slice(result.body()))
520    }
521
522    /// Sets the noise rejection filter to either 50Hz (0) or 60Hz (1).
523    /// Noise from 50Hz or 60Hz power sources (including
524    /// harmonics of the AC power's fundamental frequency) is
525    /// attenuated by 82dB.
526    ///
527    /// Associated constants:
528    /// * PTC_V2_BRICKLET_FILTER_OPTION_50HZ
529    ///	* PTC_V2_BRICKLET_FILTER_OPTION_60HZ
530    pub async fn set_noise_rejection_filter(&mut self, filter: u8) -> Result<(), TinkerforgeError> {
531        let mut payload = [0; 1];
532        filter.write_to_slice(&mut payload[0..1]);
533
534        #[allow(unused_variables)]
535        let result = self.device.set(u8::from(PtcV2BrickletFunction::SetNoiseRejectionFilter), &payload).await?;
536        Ok(())
537    }
538
539    /// Returns the noise rejection filter option as set by
540    /// [`set_noise_rejection_filter`]
541    ///
542    /// Associated constants:
543    /// * PTC_V2_BRICKLET_FILTER_OPTION_50HZ
544    ///	* PTC_V2_BRICKLET_FILTER_OPTION_60HZ
545    pub async fn get_noise_rejection_filter(&mut self) -> Result<u8, TinkerforgeError> {
546        let payload = [0; 0];
547
548        #[allow(unused_variables)]
549        let result = self.device.get(u8::from(PtcV2BrickletFunction::GetNoiseRejectionFilter), &payload).await?;
550        Ok(u8::from_le_byte_slice(result.body()))
551    }
552
553    /// Returns *true* if the sensor is connected correctly.
554    ///
555    /// If this function
556    /// returns *false*, there is either no Pt100 or Pt1000 sensor connected,
557    /// the sensor is connected incorrectly or the sensor itself is faulty.
558    ///
559    /// If you want to get the status automatically, it is recommended to use the
560    /// [`get_sensor_connected_callback_receiver`] receiver. You can set the receiver configuration
561    /// with [`set_sensor_connected_callback_configuration`].
562    pub async fn is_sensor_connected(&mut self) -> Result<bool, TinkerforgeError> {
563        let payload = [0; 0];
564
565        #[allow(unused_variables)]
566        let result = self.device.get(u8::from(PtcV2BrickletFunction::IsSensorConnected), &payload).await?;
567        Ok(bool::from_le_byte_slice(result.body()))
568    }
569
570    /// Sets the wire mode of the sensor. Possible values are 2, 3 and 4 which
571    /// correspond to 2-, 3- and 4-wire sensors. The value has to match the jumper
572    /// configuration on the Bricklet.
573    ///
574    /// Associated constants:
575    /// * PTC_V2_BRICKLET_WIRE_MODE_2
576    ///	* PTC_V2_BRICKLET_WIRE_MODE_3
577    ///	* PTC_V2_BRICKLET_WIRE_MODE_4
578    pub async fn set_wire_mode(&mut self, mode: u8) -> Result<(), TinkerforgeError> {
579        let mut payload = [0; 1];
580        mode.write_to_slice(&mut payload[0..1]);
581
582        #[allow(unused_variables)]
583        let result = self.device.set(u8::from(PtcV2BrickletFunction::SetWireMode), &payload).await?;
584        Ok(())
585    }
586
587    /// Returns the wire mode as set by [`set_wire_mode`]
588    ///
589    /// Associated constants:
590    /// * PTC_V2_BRICKLET_WIRE_MODE_2
591    ///	* PTC_V2_BRICKLET_WIRE_MODE_3
592    ///	* PTC_V2_BRICKLET_WIRE_MODE_4
593    pub async fn get_wire_mode(&mut self) -> Result<u8, TinkerforgeError> {
594        let payload = [0; 0];
595
596        #[allow(unused_variables)]
597        let result = self.device.get(u8::from(PtcV2BrickletFunction::GetWireMode), &payload).await?;
598        Ok(u8::from_le_byte_slice(result.body()))
599    }
600
601    /// Sets the length of a [moving averaging](https://en.wikipedia.org/wiki/Moving_average)__
602    /// for the resistance and temperature.
603    ///
604    /// Setting the length to 1 will turn the averaging off. With less
605    /// averaging, there is more noise on the data.
606    ///
607    /// New data is gathered every 20ms. With a moving average of length 1000 the resulting
608    /// averaging window has a length of 20s. If you want to do long term measurements the longest
609    /// moving average will give the cleanest results.
610    ///
611    /// The default values match the non-changeable averaging settings of the old PTC Bricklet 1.0
612    pub async fn set_moving_average_configuration(
613        &mut self,
614        moving_average_length_resistance: u16,
615        moving_average_length_temperature: u16,
616    ) -> Result<(), TinkerforgeError> {
617        let mut payload = [0; 4];
618        moving_average_length_resistance.write_to_slice(&mut payload[0..2]);
619        moving_average_length_temperature.write_to_slice(&mut payload[2..4]);
620
621        #[allow(unused_variables)]
622        let result = self.device.set(u8::from(PtcV2BrickletFunction::SetMovingAverageConfiguration), &payload).await?;
623        Ok(())
624    }
625
626    /// Returns the moving average configuration as set by [`set_moving_average_configuration`].
627    pub async fn get_moving_average_configuration(&mut self) -> Result<MovingAverageConfiguration, TinkerforgeError> {
628        let payload = [0; 0];
629
630        #[allow(unused_variables)]
631        let result = self.device.get(u8::from(PtcV2BrickletFunction::GetMovingAverageConfiguration), &payload).await?;
632        Ok(MovingAverageConfiguration::from_le_byte_slice(result.body()))
633    }
634
635    /// If you enable this receiver, the [`get_sensor_connected_callback_receiver`] receiver is triggered
636    /// every time a Pt sensor is connected/disconnected.
637    pub async fn set_sensor_connected_callback_configuration(&mut self, enabled: bool) -> Result<(), TinkerforgeError> {
638        let mut payload = [0; 1];
639        enabled.write_to_slice(&mut payload[0..1]);
640
641        #[allow(unused_variables)]
642        let result = self.device.set(u8::from(PtcV2BrickletFunction::SetSensorConnectedCallbackConfiguration), &payload).await?;
643        Ok(())
644    }
645
646    /// Returns the configuration as set by [`set_sensor_connected_callback_configuration`].
647    pub async fn get_sensor_connected_callback_configuration(&mut self) -> Result<bool, TinkerforgeError> {
648        let payload = [0; 0];
649
650        #[allow(unused_variables)]
651        let result = self.device.get(u8::from(PtcV2BrickletFunction::GetSensorConnectedCallbackConfiguration), &payload).await?;
652        Ok(bool::from_le_byte_slice(result.body()))
653    }
654
655    /// Returns the error count for the communication between Brick and Bricklet.
656    ///
657    /// The errors are divided into
658    ///
659    /// * ACK checksum errors,
660    /// * message checksum errors,
661    /// * framing errors and
662    /// * overflow errors.
663    ///
664    /// The errors counts are for errors that occur on the Bricklet side. All
665    /// Bricks have a similar function that returns the errors on the Brick side.
666    pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
667        let payload = [0; 0];
668
669        #[allow(unused_variables)]
670        let result = self.device.get(u8::from(PtcV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
671        Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
672    }
673
674    /// Sets the bootloader mode and returns the status after the requested
675    /// mode change was instigated.
676    ///
677    /// You can change from bootloader mode to firmware mode and vice versa. A change
678    /// from bootloader mode to firmware mode will only take place if the entry function,
679    /// device identifier and CRC are present and correct.
680    ///
681    /// This function is used by Brick Viewer during flashing. It should not be
682    /// necessary to call it in a normal user program.
683    ///
684    /// Associated constants:
685    /// * PTC_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
686    ///	* PTC_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
687    ///	* PTC_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
688    ///	* PTC_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
689    ///	* PTC_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
690    ///	* PTC_V2_BRICKLET_BOOTLOADER_STATUS_OK
691    ///	* PTC_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
692    ///	* PTC_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
693    ///	* PTC_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
694    ///	* PTC_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
695    ///	* PTC_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
696    pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
697        let mut payload = [0; 1];
698        mode.write_to_slice(&mut payload[0..1]);
699
700        #[allow(unused_variables)]
701        let result = self.device.get(u8::from(PtcV2BrickletFunction::SetBootloaderMode), &payload).await?;
702        Ok(u8::from_le_byte_slice(result.body()))
703    }
704
705    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
706    ///
707    /// Associated constants:
708    /// * PTC_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
709    ///	* PTC_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
710    ///	* PTC_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
711    ///	* PTC_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
712    ///	* PTC_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
713    pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
714        let payload = [0; 0];
715
716        #[allow(unused_variables)]
717        let result = self.device.get(u8::from(PtcV2BrickletFunction::GetBootloaderMode), &payload).await?;
718        Ok(u8::from_le_byte_slice(result.body()))
719    }
720
721    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
722    /// to be increased by chunks of size 64. The data is written to flash
723    /// every 4 chunks (which equals to one page of size 256).
724    ///
725    /// This function is used by Brick Viewer during flashing. It should not be
726    /// necessary to call it in a normal user program.
727    pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
728        let mut payload = [0; 4];
729        pointer.write_to_slice(&mut payload[0..4]);
730
731        #[allow(unused_variables)]
732        let result = self.device.set(u8::from(PtcV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
733        Ok(())
734    }
735
736    /// Writes 64 Bytes of firmware at the position as written by
737    /// [`set_write_firmware_pointer`] before. The firmware is written
738    /// to flash every 4 chunks.
739    ///
740    /// You can only write firmware in bootloader mode.
741    ///
742    /// This function is used by Brick Viewer during flashing. It should not be
743    /// necessary to call it in a normal user program.
744    pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
745        let mut payload = [0; 64];
746        data.write_to_slice(&mut payload[0..64]);
747
748        #[allow(unused_variables)]
749        let result = self.device.get(u8::from(PtcV2BrickletFunction::WriteFirmware), &payload).await?;
750        Ok(u8::from_le_byte_slice(result.body()))
751    }
752
753    /// Sets the status LED configuration. By default the LED shows
754    /// communication traffic between Brick and Bricklet, it flickers once
755    /// for every 10 received data packets.
756    ///
757    /// You can also turn the LED permanently on/off or show a heartbeat.
758    ///
759    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
760    ///
761    /// Associated constants:
762    /// * PTC_V2_BRICKLET_STATUS_LED_CONFIG_OFF
763    ///	* PTC_V2_BRICKLET_STATUS_LED_CONFIG_ON
764    ///	* PTC_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
765    ///	* PTC_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
766    pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
767        let mut payload = [0; 1];
768        config.write_to_slice(&mut payload[0..1]);
769
770        #[allow(unused_variables)]
771        let result = self.device.set(u8::from(PtcV2BrickletFunction::SetStatusLedConfig), &payload).await?;
772        Ok(())
773    }
774
775    /// Returns the configuration as set by [`set_status_led_config`]
776    ///
777    /// Associated constants:
778    /// * PTC_V2_BRICKLET_STATUS_LED_CONFIG_OFF
779    ///	* PTC_V2_BRICKLET_STATUS_LED_CONFIG_ON
780    ///	* PTC_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
781    ///	* PTC_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
782    pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
783        let payload = [0; 0];
784
785        #[allow(unused_variables)]
786        let result = self.device.get(u8::from(PtcV2BrickletFunction::GetStatusLedConfig), &payload).await?;
787        Ok(u8::from_le_byte_slice(result.body()))
788    }
789
790    /// Returns the temperature as measured inside the microcontroller. The
791    /// value returned is not the ambient temperature!
792    ///
793    /// The temperature is only proportional to the real temperature and it has bad
794    /// accuracy. Practically it is only useful as an indicator for
795    /// temperature changes.
796    pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
797        let payload = [0; 0];
798
799        #[allow(unused_variables)]
800        let result = self.device.get(u8::from(PtcV2BrickletFunction::GetChipTemperature), &payload).await?;
801        Ok(i16::from_le_byte_slice(result.body()))
802    }
803
804    /// Calling this function will reset the Bricklet. All configurations
805    /// will be lost.
806    ///
807    /// After a reset you have to create new device objects,
808    /// calling functions on the existing ones will result in
809    /// undefined behavior!
810    pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
811        let payload = [0; 0];
812
813        #[allow(unused_variables)]
814        let result = self.device.set(u8::from(PtcV2BrickletFunction::Reset), &payload).await?;
815        Ok(())
816    }
817
818    /// Writes a new UID into flash. If you want to set a new UID
819    /// you have to decode the Base58 encoded UID string into an
820    /// integer first.
821    ///
822    /// We recommend that you use Brick Viewer to change the UID.
823    pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
824        let mut payload = [0; 4];
825        uid.write_to_slice(&mut payload[0..4]);
826
827        #[allow(unused_variables)]
828        let result = self.device.set(u8::from(PtcV2BrickletFunction::WriteUid), &payload).await?;
829        Ok(())
830    }
831
832    /// Returns the current UID as an integer. Encode as
833    /// Base58 to get the usual string version.
834    pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
835        let payload = [0; 0];
836
837        #[allow(unused_variables)]
838        let result = self.device.get(u8::from(PtcV2BrickletFunction::ReadUid), &payload).await?;
839        Ok(u32::from_le_byte_slice(result.body()))
840    }
841
842    /// Returns the UID, the UID where the Bricklet is connected to,
843    /// the position, the hardware and firmware version as well as the
844    /// device identifier.
845    ///
846    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
847    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
848    /// position 'z'.
849    ///
850    /// The device identifier numbers can be found [here](device_identifier).
851    /// |device_identifier_constant|
852    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
853        let payload = [0; 0];
854
855        #[allow(unused_variables)]
856        let result = self.device.get(u8::from(PtcV2BrickletFunction::GetIdentity), &payload).await?;
857        Ok(Identity::from_le_byte_slice(result.body()))
858    }
859}