tinkerforge/bindings/
humidity_v2_bricklet.rs

1/* ***********************************************************
2 * This file was automatically generated on 2024-02-27.      *
3 *                                                           *
4 * Rust Bindings Version 2.0.21                              *
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 relative humidity.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/HumidityV2_Bricklet_Rust.html).
14use crate::{
15    byte_converter::*, converting_callback_receiver::ConvertingCallbackReceiver, converting_receiver::ConvertingReceiver, device::*,
16    ip_connection::GetRequestSender,
17};
18pub enum HumidityV2BrickletFunction {
19    GetHumidity,
20    SetHumidityCallbackConfiguration,
21    GetHumidityCallbackConfiguration,
22    GetTemperature,
23    SetTemperatureCallbackConfiguration,
24    GetTemperatureCallbackConfiguration,
25    SetHeaterConfiguration,
26    GetHeaterConfiguration,
27    SetMovingAverageConfiguration,
28    GetMovingAverageConfiguration,
29    SetSamplesPerSecond,
30    GetSamplesPerSecond,
31    GetSpitfpErrorCount,
32    SetBootloaderMode,
33    GetBootloaderMode,
34    SetWriteFirmwarePointer,
35    WriteFirmware,
36    SetStatusLedConfig,
37    GetStatusLedConfig,
38    GetChipTemperature,
39    Reset,
40    WriteUid,
41    ReadUid,
42    GetIdentity,
43    CallbackHumidity,
44    CallbackTemperature,
45}
46impl From<HumidityV2BrickletFunction> for u8 {
47    fn from(fun: HumidityV2BrickletFunction) -> Self {
48        match fun {
49            HumidityV2BrickletFunction::GetHumidity => 1,
50            HumidityV2BrickletFunction::SetHumidityCallbackConfiguration => 2,
51            HumidityV2BrickletFunction::GetHumidityCallbackConfiguration => 3,
52            HumidityV2BrickletFunction::GetTemperature => 5,
53            HumidityV2BrickletFunction::SetTemperatureCallbackConfiguration => 6,
54            HumidityV2BrickletFunction::GetTemperatureCallbackConfiguration => 7,
55            HumidityV2BrickletFunction::SetHeaterConfiguration => 9,
56            HumidityV2BrickletFunction::GetHeaterConfiguration => 10,
57            HumidityV2BrickletFunction::SetMovingAverageConfiguration => 11,
58            HumidityV2BrickletFunction::GetMovingAverageConfiguration => 12,
59            HumidityV2BrickletFunction::SetSamplesPerSecond => 13,
60            HumidityV2BrickletFunction::GetSamplesPerSecond => 14,
61            HumidityV2BrickletFunction::GetSpitfpErrorCount => 234,
62            HumidityV2BrickletFunction::SetBootloaderMode => 235,
63            HumidityV2BrickletFunction::GetBootloaderMode => 236,
64            HumidityV2BrickletFunction::SetWriteFirmwarePointer => 237,
65            HumidityV2BrickletFunction::WriteFirmware => 238,
66            HumidityV2BrickletFunction::SetStatusLedConfig => 239,
67            HumidityV2BrickletFunction::GetStatusLedConfig => 240,
68            HumidityV2BrickletFunction::GetChipTemperature => 242,
69            HumidityV2BrickletFunction::Reset => 243,
70            HumidityV2BrickletFunction::WriteUid => 248,
71            HumidityV2BrickletFunction::ReadUid => 249,
72            HumidityV2BrickletFunction::GetIdentity => 255,
73            HumidityV2BrickletFunction::CallbackHumidity => 4,
74            HumidityV2BrickletFunction::CallbackTemperature => 8,
75        }
76    }
77}
78pub const HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
79pub const HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
80pub const HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
81pub const HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
82pub const HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
83pub const HUMIDITY_V2_BRICKLET_HEATER_CONFIG_DISABLED: u8 = 0;
84pub const HUMIDITY_V2_BRICKLET_HEATER_CONFIG_ENABLED: u8 = 1;
85pub const HUMIDITY_V2_BRICKLET_SPS_20: u8 = 0;
86pub const HUMIDITY_V2_BRICKLET_SPS_10: u8 = 1;
87pub const HUMIDITY_V2_BRICKLET_SPS_5: u8 = 2;
88pub const HUMIDITY_V2_BRICKLET_SPS_1: u8 = 3;
89pub const HUMIDITY_V2_BRICKLET_SPS_02: u8 = 4;
90pub const HUMIDITY_V2_BRICKLET_SPS_01: u8 = 5;
91pub const HUMIDITY_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
92pub const HUMIDITY_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
93pub const HUMIDITY_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
94pub const HUMIDITY_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
95pub const HUMIDITY_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
96pub const HUMIDITY_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
97pub const HUMIDITY_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
98pub const HUMIDITY_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
99pub const HUMIDITY_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
100pub const HUMIDITY_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
101pub const HUMIDITY_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
102pub const HUMIDITY_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
103pub const HUMIDITY_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
104pub const HUMIDITY_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
105pub const HUMIDITY_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
106
107#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
108pub struct HumidityCallbackConfiguration {
109    pub period: u32,
110    pub value_has_to_change: bool,
111    pub option: char,
112    pub min: u16,
113    pub max: u16,
114}
115impl FromByteSlice for HumidityCallbackConfiguration {
116    fn bytes_expected() -> usize { 10 }
117    fn from_le_byte_slice(bytes: &[u8]) -> HumidityCallbackConfiguration {
118        HumidityCallbackConfiguration {
119            period: <u32>::from_le_byte_slice(&bytes[0..4]),
120            value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
121            option: <char>::from_le_byte_slice(&bytes[5..6]),
122            min: <u16>::from_le_byte_slice(&bytes[6..8]),
123            max: <u16>::from_le_byte_slice(&bytes[8..10]),
124        }
125    }
126}
127
128#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
129pub struct TemperatureCallbackConfiguration {
130    pub period: u32,
131    pub value_has_to_change: bool,
132    pub option: char,
133    pub min: i16,
134    pub max: i16,
135}
136impl FromByteSlice for TemperatureCallbackConfiguration {
137    fn bytes_expected() -> usize { 10 }
138    fn from_le_byte_slice(bytes: &[u8]) -> TemperatureCallbackConfiguration {
139        TemperatureCallbackConfiguration {
140            period: <u32>::from_le_byte_slice(&bytes[0..4]),
141            value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
142            option: <char>::from_le_byte_slice(&bytes[5..6]),
143            min: <i16>::from_le_byte_slice(&bytes[6..8]),
144            max: <i16>::from_le_byte_slice(&bytes[8..10]),
145        }
146    }
147}
148
149#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
150pub struct MovingAverageConfiguration {
151    pub moving_average_length_humidity: u16,
152    pub moving_average_length_temperature: u16,
153}
154impl FromByteSlice for MovingAverageConfiguration {
155    fn bytes_expected() -> usize { 4 }
156    fn from_le_byte_slice(bytes: &[u8]) -> MovingAverageConfiguration {
157        MovingAverageConfiguration {
158            moving_average_length_humidity: <u16>::from_le_byte_slice(&bytes[0..2]),
159            moving_average_length_temperature: <u16>::from_le_byte_slice(&bytes[2..4]),
160        }
161    }
162}
163
164#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
165pub struct SpitfpErrorCount {
166    pub error_count_ack_checksum: u32,
167    pub error_count_message_checksum: u32,
168    pub error_count_frame: u32,
169    pub error_count_overflow: u32,
170}
171impl FromByteSlice for SpitfpErrorCount {
172    fn bytes_expected() -> usize { 16 }
173    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
174        SpitfpErrorCount {
175            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
176            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
177            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
178            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
179        }
180    }
181}
182
183#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
184pub struct Identity {
185    pub uid: String,
186    pub connected_uid: String,
187    pub position: char,
188    pub hardware_version: [u8; 3],
189    pub firmware_version: [u8; 3],
190    pub device_identifier: u16,
191}
192impl FromByteSlice for Identity {
193    fn bytes_expected() -> usize { 25 }
194    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
195        Identity {
196            uid: <String>::from_le_byte_slice(&bytes[0..8]),
197            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
198            position: <char>::from_le_byte_slice(&bytes[16..17]),
199            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
200            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
201            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
202        }
203    }
204}
205
206/// Measures relative humidity
207#[derive(Clone)]
208pub struct HumidityV2Bricklet {
209    device: Device,
210}
211impl HumidityV2Bricklet {
212    pub const DEVICE_IDENTIFIER: u16 = 283;
213    pub const DEVICE_DISPLAY_NAME: &'static str = "Humidity Bricklet 2.0";
214    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
215    pub fn new<T: GetRequestSender>(uid: &str, req_sender: T) -> HumidityV2Bricklet {
216        let mut result = HumidityV2Bricklet { device: Device::new([2, 0, 2], uid, req_sender, 0) };
217        result.device.response_expected[u8::from(HumidityV2BrickletFunction::GetHumidity) as usize] = ResponseExpectedFlag::AlwaysTrue;
218        result.device.response_expected[u8::from(HumidityV2BrickletFunction::SetHumidityCallbackConfiguration) as usize] =
219            ResponseExpectedFlag::True;
220        result.device.response_expected[u8::from(HumidityV2BrickletFunction::GetHumidityCallbackConfiguration) as usize] =
221            ResponseExpectedFlag::AlwaysTrue;
222        result.device.response_expected[u8::from(HumidityV2BrickletFunction::GetTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
223        result.device.response_expected[u8::from(HumidityV2BrickletFunction::SetTemperatureCallbackConfiguration) as usize] =
224            ResponseExpectedFlag::True;
225        result.device.response_expected[u8::from(HumidityV2BrickletFunction::GetTemperatureCallbackConfiguration) as usize] =
226            ResponseExpectedFlag::AlwaysTrue;
227        result.device.response_expected[u8::from(HumidityV2BrickletFunction::SetHeaterConfiguration) as usize] =
228            ResponseExpectedFlag::False;
229        result.device.response_expected[u8::from(HumidityV2BrickletFunction::GetHeaterConfiguration) as usize] =
230            ResponseExpectedFlag::AlwaysTrue;
231        result.device.response_expected[u8::from(HumidityV2BrickletFunction::SetMovingAverageConfiguration) as usize] =
232            ResponseExpectedFlag::False;
233        result.device.response_expected[u8::from(HumidityV2BrickletFunction::GetMovingAverageConfiguration) as usize] =
234            ResponseExpectedFlag::AlwaysTrue;
235        result.device.response_expected[u8::from(HumidityV2BrickletFunction::SetSamplesPerSecond) as usize] = ResponseExpectedFlag::False;
236        result.device.response_expected[u8::from(HumidityV2BrickletFunction::GetSamplesPerSecond) as usize] =
237            ResponseExpectedFlag::AlwaysTrue;
238        result.device.response_expected[u8::from(HumidityV2BrickletFunction::GetSpitfpErrorCount) as usize] =
239            ResponseExpectedFlag::AlwaysTrue;
240        result.device.response_expected[u8::from(HumidityV2BrickletFunction::SetBootloaderMode) as usize] =
241            ResponseExpectedFlag::AlwaysTrue;
242        result.device.response_expected[u8::from(HumidityV2BrickletFunction::GetBootloaderMode) as usize] =
243            ResponseExpectedFlag::AlwaysTrue;
244        result.device.response_expected[u8::from(HumidityV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
245            ResponseExpectedFlag::False;
246        result.device.response_expected[u8::from(HumidityV2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
247        result.device.response_expected[u8::from(HumidityV2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
248        result.device.response_expected[u8::from(HumidityV2BrickletFunction::GetStatusLedConfig) as usize] =
249            ResponseExpectedFlag::AlwaysTrue;
250        result.device.response_expected[u8::from(HumidityV2BrickletFunction::GetChipTemperature) as usize] =
251            ResponseExpectedFlag::AlwaysTrue;
252        result.device.response_expected[u8::from(HumidityV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
253        result.device.response_expected[u8::from(HumidityV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
254        result.device.response_expected[u8::from(HumidityV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
255        result.device.response_expected[u8::from(HumidityV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
256        result
257    }
258
259    /// Returns the response expected flag for the function specified by the function ID parameter.
260    /// It is true if the function is expected to send a response, false otherwise.
261    ///
262    /// For getter functions this is enabled by default and cannot be disabled, because those
263    /// functions will always send a response. For callback configuration functions it is enabled
264    /// by default too, but can be disabled by [`set_response_expected`](crate::humidity_v2_bricklet::HumidityV2Bricklet::set_response_expected).
265    /// For setter functions it is disabled by default and can be enabled.
266    ///
267    /// Enabling the response expected flag for a setter function allows to detect timeouts
268    /// and other error conditions calls of this setter as well. The device will then send a response
269    /// for this purpose. If this flag is disabled for a setter function then no response is sent
270    /// and errors are silently ignored, because they cannot be detected.
271    ///
272    /// See [`set_response_expected`](crate::humidity_v2_bricklet::HumidityV2Bricklet::set_response_expected) for the list of function ID constants available for this function.
273    pub fn get_response_expected(&mut self, fun: HumidityV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
274        self.device.get_response_expected(u8::from(fun))
275    }
276
277    /// Changes the response expected flag of the function specified by the function ID parameter.
278    /// This flag can only be changed for setter (default value: false) and callback configuration
279    /// functions (default value: true). For getter functions it is always enabled.
280    ///
281    /// Enabling the response expected flag for a setter function allows to detect timeouts and
282    /// other error conditions calls of this setter as well. The device will then send a response
283    /// for this purpose. If this flag is disabled for a setter function then no response is sent
284    /// and errors are silently ignored, because they cannot be detected.
285    pub fn set_response_expected(
286        &mut self,
287        fun: HumidityV2BrickletFunction,
288        response_expected: bool,
289    ) -> Result<(), SetResponseExpectedError> {
290        self.device.set_response_expected(u8::from(fun), response_expected)
291    }
292
293    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
294    pub fn set_response_expected_all(&mut self, response_expected: bool) { self.device.set_response_expected_all(response_expected) }
295
296    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
297    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
298    pub fn get_api_version(&self) -> [u8; 3] { self.device.api_version }
299
300    /// This receiver is triggered periodically according to the configuration set by
301    /// [`set_humidity_callback_configuration`].
302    ///
303    /// The parameter is the same as [`get_humidity`].
304    ///
305    /// [`get_humidity`]: #method.get_humidity
306    /// [`set_humidity_callback_configuration`]: #method.set_humidity_callback_configuration
307    pub fn get_humidity_callback_receiver(&self) -> ConvertingCallbackReceiver<u16> {
308        self.device.get_callback_receiver(u8::from(HumidityV2BrickletFunction::CallbackHumidity))
309    }
310
311    /// This receiver is triggered periodically according to the configuration set by
312    /// [`set_temperature_callback_configuration`].
313    ///
314    /// The parameter is the same as [`get_temperature`].
315    pub fn get_temperature_callback_receiver(&self) -> ConvertingCallbackReceiver<i16> {
316        self.device.get_callback_receiver(u8::from(HumidityV2BrickletFunction::CallbackTemperature))
317    }
318
319    /// Returns the humidity measured by the sensor.
320    ///
321    ///
322    /// If you want to get the value periodically, it is recommended to use the
323    /// [`get_humidity_callback_receiver`] receiver. You can set the receiver configuration
324    /// with [`set_humidity_callback_configuration`].
325    pub fn get_humidity(&self) -> ConvertingReceiver<u16> {
326        let payload = vec![0; 0];
327
328        self.device.get(u8::from(HumidityV2BrickletFunction::GetHumidity), payload)
329    }
330
331    /// The period is the period with which the [`get_humidity_callback_receiver`] receiver is triggered
332    /// periodically. A value of 0 turns the receiver off.
333    ///
334    /// If the `value has to change`-parameter is set to true, the receiver is only
335    /// triggered after the value has changed. If the value didn't change
336    /// within the period, the receiver is triggered immediately on change.
337    ///
338    /// If it is set to false, the receiver is continuously triggered with the period,
339    /// independent of the value.
340    ///
341    /// It is furthermore possible to constrain the receiver with thresholds.
342    ///
343    /// The `option`-parameter together with min/max sets a threshold for the [`get_humidity_callback_receiver`] receiver.
344    ///
345    /// The following options are possible:
346    ///
347    ///  Option| Description
348    ///  --- | ---
349    ///  'x'|    Threshold is turned off
350    ///  'o'|    Threshold is triggered when the value is *outside* the min and max values
351    ///  'i'|    Threshold is triggered when the value is *inside* or equal to the min and max values
352    ///  '<'|    Threshold is triggered when the value is smaller than the min value (max is ignored)
353    ///  '>'|    Threshold is triggered when the value is greater than the min value (max is ignored)
354    ///
355    /// If the option is set to 'x' (threshold turned off) the receiver is triggered with the fixed period.
356    ///
357    /// Associated constants:
358    /// * HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_OFF
359    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
360    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
361    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
362    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_GREATER
363    pub fn set_humidity_callback_configuration(
364        &self,
365        period: u32,
366        value_has_to_change: bool,
367        option: char,
368        min: u16,
369        max: u16,
370    ) -> ConvertingReceiver<()> {
371        let mut payload = vec![0; 10];
372        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
373        payload[4..5].copy_from_slice(&<bool>::to_le_byte_vec(value_has_to_change));
374        payload[5..6].copy_from_slice(&<char>::to_le_byte_vec(option));
375        payload[6..8].copy_from_slice(&<u16>::to_le_byte_vec(min));
376        payload[8..10].copy_from_slice(&<u16>::to_le_byte_vec(max));
377
378        self.device.set(u8::from(HumidityV2BrickletFunction::SetHumidityCallbackConfiguration), payload)
379    }
380
381    /// Returns the receiver configuration as set by [`set_humidity_callback_configuration`].
382    ///
383    /// Associated constants:
384    /// * HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_OFF
385    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
386    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
387    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
388    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_GREATER
389    pub fn get_humidity_callback_configuration(&self) -> ConvertingReceiver<HumidityCallbackConfiguration> {
390        let payload = vec![0; 0];
391
392        self.device.get(u8::from(HumidityV2BrickletFunction::GetHumidityCallbackConfiguration), payload)
393    }
394
395    /// Returns the temperature measured by the sensor.
396    ///
397    ///
398    /// If you want to get the value periodically, it is recommended to use the
399    /// [`get_temperature_callback_receiver`] receiver. You can set the receiver configuration
400    /// with [`set_temperature_callback_configuration`].
401    pub fn get_temperature(&self) -> ConvertingReceiver<i16> {
402        let payload = vec![0; 0];
403
404        self.device.get(u8::from(HumidityV2BrickletFunction::GetTemperature), payload)
405    }
406
407    /// The period is the period with which the [`get_temperature_callback_receiver`] receiver is triggered
408    /// periodically. A value of 0 turns the receiver off.
409    ///
410    /// If the `value has to change`-parameter is set to true, the receiver is only
411    /// triggered after the value has changed. If the value didn't change
412    /// within the period, the receiver is triggered immediately on change.
413    ///
414    /// If it is set to false, the receiver is continuously triggered with the period,
415    /// independent of the value.
416    ///
417    /// It is furthermore possible to constrain the receiver with thresholds.
418    ///
419    /// The `option`-parameter together with min/max sets a threshold for the [`get_temperature_callback_receiver`] receiver.
420    ///
421    /// The following options are possible:
422    ///
423    ///  Option| Description
424    ///  --- | ---
425    ///  'x'|    Threshold is turned off
426    ///  'o'|    Threshold is triggered when the value is *outside* the min and max values
427    ///  'i'|    Threshold is triggered when the value is *inside* or equal to the min and max values
428    ///  '<'|    Threshold is triggered when the value is smaller than the min value (max is ignored)
429    ///  '>'|    Threshold is triggered when the value is greater than the min value (max is ignored)
430    ///
431    /// If the option is set to 'x' (threshold turned off) the receiver is triggered with the fixed period.
432    ///
433    /// Associated constants:
434    /// * HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_OFF
435    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
436    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
437    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
438    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_GREATER
439    pub fn set_temperature_callback_configuration(
440        &self,
441        period: u32,
442        value_has_to_change: bool,
443        option: char,
444        min: i16,
445        max: i16,
446    ) -> ConvertingReceiver<()> {
447        let mut payload = vec![0; 10];
448        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
449        payload[4..5].copy_from_slice(&<bool>::to_le_byte_vec(value_has_to_change));
450        payload[5..6].copy_from_slice(&<char>::to_le_byte_vec(option));
451        payload[6..8].copy_from_slice(&<i16>::to_le_byte_vec(min));
452        payload[8..10].copy_from_slice(&<i16>::to_le_byte_vec(max));
453
454        self.device.set(u8::from(HumidityV2BrickletFunction::SetTemperatureCallbackConfiguration), payload)
455    }
456
457    /// Returns the receiver configuration as set by [`set_temperature_callback_configuration`].
458    ///
459    /// Associated constants:
460    /// * HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_OFF
461    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
462    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
463    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
464    ///	* HUMIDITY_V2_BRICKLET_THRESHOLD_OPTION_GREATER
465    pub fn get_temperature_callback_configuration(&self) -> ConvertingReceiver<TemperatureCallbackConfiguration> {
466        let payload = vec![0; 0];
467
468        self.device.get(u8::from(HumidityV2BrickletFunction::GetTemperatureCallbackConfiguration), payload)
469    }
470
471    /// Enables/disables the heater. The heater can be used to dry the sensor in
472    /// extremely wet conditions.
473    ///
474    /// Associated constants:
475    /// * HUMIDITY_V2_BRICKLET_HEATER_CONFIG_DISABLED
476    ///	* HUMIDITY_V2_BRICKLET_HEATER_CONFIG_ENABLED
477    pub fn set_heater_configuration(&self, heater_config: u8) -> ConvertingReceiver<()> {
478        let mut payload = vec![0; 1];
479        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(heater_config));
480
481        self.device.set(u8::from(HumidityV2BrickletFunction::SetHeaterConfiguration), payload)
482    }
483
484    /// Returns the heater configuration as set by [`set_heater_configuration`].
485    ///
486    /// Associated constants:
487    /// * HUMIDITY_V2_BRICKLET_HEATER_CONFIG_DISABLED
488    ///	* HUMIDITY_V2_BRICKLET_HEATER_CONFIG_ENABLED
489    pub fn get_heater_configuration(&self) -> ConvertingReceiver<u8> {
490        let payload = vec![0; 0];
491
492        self.device.get(u8::from(HumidityV2BrickletFunction::GetHeaterConfiguration), payload)
493    }
494
495    /// Sets the length of a [moving averaging](https://en.wikipedia.org/wiki/Moving_average)__
496    /// for the humidity and temperature.
497    ///
498    /// Setting the length to 1 will turn the averaging off. With less
499    /// averaging, there is more noise on the data.
500    ///
501    /// New data is gathered every 50ms*. With a moving average of length 1000 the resulting
502    /// averaging window has a length of 50s. If you want to do long term measurements the longest
503    /// moving average will give the cleanest results.
504    ///
505    /// \* In firmware version 2.0.3 we added the [`set_samples_per_second`] function. It
506    /// configures the measurement frequency. Since high frequencies can result in self-heating
507    /// of th IC, changed the default value from 20 samples per second to 1. With 1 sample per second
508    /// a moving average length of 1000 would result in an averaging window of 1000 seconds!
509    pub fn set_moving_average_configuration(
510        &self,
511        moving_average_length_humidity: u16,
512        moving_average_length_temperature: u16,
513    ) -> ConvertingReceiver<()> {
514        let mut payload = vec![0; 4];
515        payload[0..2].copy_from_slice(&<u16>::to_le_byte_vec(moving_average_length_humidity));
516        payload[2..4].copy_from_slice(&<u16>::to_le_byte_vec(moving_average_length_temperature));
517
518        self.device.set(u8::from(HumidityV2BrickletFunction::SetMovingAverageConfiguration), payload)
519    }
520
521    /// Returns the moving average configuration as set by [`set_moving_average_configuration`].
522    pub fn get_moving_average_configuration(&self) -> ConvertingReceiver<MovingAverageConfiguration> {
523        let payload = vec![0; 0];
524
525        self.device.get(u8::from(HumidityV2BrickletFunction::GetMovingAverageConfiguration), payload)
526    }
527
528    /// Sets the samples per second that are gathered by the humidity/temperature sensor HDC1080.
529    ///
530    /// We added this function since we found out that a high measurement frequency can lead to
531    /// self-heating of the sensor. Which can distort the temperature measurement.
532    ///
533    /// If you don't need a lot of measurements, you can use the lowest available measurement
534    /// frequency of 0.1 samples per second for the least amount of self-heating.
535    ///
536    /// Before version 2.0.3 the default was 20 samples per second. The new default is 1 sample per second.
537    ///
538    ///
539    /// .. versionadded:: 2.0.3$nbsp;(Plugin)
540    ///
541    /// Associated constants:
542    /// * HUMIDITY_V2_BRICKLET_SPS_20
543    ///	* HUMIDITY_V2_BRICKLET_SPS_10
544    ///	* HUMIDITY_V2_BRICKLET_SPS_5
545    ///	* HUMIDITY_V2_BRICKLET_SPS_1
546    ///	* HUMIDITY_V2_BRICKLET_SPS_02
547    ///	* HUMIDITY_V2_BRICKLET_SPS_01
548    pub fn set_samples_per_second(&self, sps: u8) -> ConvertingReceiver<()> {
549        let mut payload = vec![0; 1];
550        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(sps));
551
552        self.device.set(u8::from(HumidityV2BrickletFunction::SetSamplesPerSecond), payload)
553    }
554
555    /// Returnes the samples per second, as set by [`set_samples_per_second`].
556    ///
557    ///
558    /// .. versionadded:: 2.0.3$nbsp;(Plugin)
559    ///
560    /// Associated constants:
561    /// * HUMIDITY_V2_BRICKLET_SPS_20
562    ///	* HUMIDITY_V2_BRICKLET_SPS_10
563    ///	* HUMIDITY_V2_BRICKLET_SPS_5
564    ///	* HUMIDITY_V2_BRICKLET_SPS_1
565    ///	* HUMIDITY_V2_BRICKLET_SPS_02
566    ///	* HUMIDITY_V2_BRICKLET_SPS_01
567    pub fn get_samples_per_second(&self) -> ConvertingReceiver<u8> {
568        let payload = vec![0; 0];
569
570        self.device.get(u8::from(HumidityV2BrickletFunction::GetSamplesPerSecond), payload)
571    }
572
573    /// Returns the error count for the communication between Brick and Bricklet.
574    ///
575    /// The errors are divided into
576    ///
577    /// * ACK checksum errors,
578    /// * message checksum errors,
579    /// * framing errors and
580    /// * overflow errors.
581    ///
582    /// The errors counts are for errors that occur on the Bricklet side. All
583    /// Bricks have a similar function that returns the errors on the Brick side.
584    pub fn get_spitfp_error_count(&self) -> ConvertingReceiver<SpitfpErrorCount> {
585        let payload = vec![0; 0];
586
587        self.device.get(u8::from(HumidityV2BrickletFunction::GetSpitfpErrorCount), payload)
588    }
589
590    /// Sets the bootloader mode and returns the status after the requested
591    /// mode change was instigated.
592    ///
593    /// You can change from bootloader mode to firmware mode and vice versa. A change
594    /// from bootloader mode to firmware mode will only take place if the entry function,
595    /// device identifier and CRC are present and correct.
596    ///
597    /// This function is used by Brick Viewer during flashing. It should not be
598    /// necessary to call it in a normal user program.
599    ///
600    /// Associated constants:
601    /// * HUMIDITY_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
602    ///	* HUMIDITY_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
603    ///	* HUMIDITY_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
604    ///	* HUMIDITY_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
605    ///	* HUMIDITY_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
606    ///	* HUMIDITY_V2_BRICKLET_BOOTLOADER_STATUS_OK
607    ///	* HUMIDITY_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
608    ///	* HUMIDITY_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
609    ///	* HUMIDITY_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
610    ///	* HUMIDITY_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
611    ///	* HUMIDITY_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
612    pub fn set_bootloader_mode(&self, mode: u8) -> ConvertingReceiver<u8> {
613        let mut payload = vec![0; 1];
614        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(mode));
615
616        self.device.get(u8::from(HumidityV2BrickletFunction::SetBootloaderMode), payload)
617    }
618
619    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
620    ///
621    /// Associated constants:
622    /// * HUMIDITY_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
623    ///	* HUMIDITY_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
624    ///	* HUMIDITY_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
625    ///	* HUMIDITY_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
626    ///	* HUMIDITY_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
627    pub fn get_bootloader_mode(&self) -> ConvertingReceiver<u8> {
628        let payload = vec![0; 0];
629
630        self.device.get(u8::from(HumidityV2BrickletFunction::GetBootloaderMode), payload)
631    }
632
633    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
634    /// to be increased by chunks of size 64. The data is written to flash
635    /// every 4 chunks (which equals to one page of size 256).
636    ///
637    /// This function is used by Brick Viewer during flashing. It should not be
638    /// necessary to call it in a normal user program.
639    pub fn set_write_firmware_pointer(&self, pointer: u32) -> ConvertingReceiver<()> {
640        let mut payload = vec![0; 4];
641        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(pointer));
642
643        self.device.set(u8::from(HumidityV2BrickletFunction::SetWriteFirmwarePointer), payload)
644    }
645
646    /// Writes 64 Bytes of firmware at the position as written by
647    /// [`set_write_firmware_pointer`] before. The firmware is written
648    /// to flash every 4 chunks.
649    ///
650    /// You can only write firmware in bootloader mode.
651    ///
652    /// This function is used by Brick Viewer during flashing. It should not be
653    /// necessary to call it in a normal user program.
654    pub fn write_firmware(&self, data: [u8; 64]) -> ConvertingReceiver<u8> {
655        let mut payload = vec![0; 64];
656        payload[0..64].copy_from_slice(&<[u8; 64]>::to_le_byte_vec(data));
657
658        self.device.get(u8::from(HumidityV2BrickletFunction::WriteFirmware), payload)
659    }
660
661    /// Sets the status LED configuration. By default the LED shows
662    /// communication traffic between Brick and Bricklet, it flickers once
663    /// for every 10 received data packets.
664    ///
665    /// You can also turn the LED permanently on/off or show a heartbeat.
666    ///
667    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
668    ///
669    /// Associated constants:
670    /// * HUMIDITY_V2_BRICKLET_STATUS_LED_CONFIG_OFF
671    ///	* HUMIDITY_V2_BRICKLET_STATUS_LED_CONFIG_ON
672    ///	* HUMIDITY_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
673    ///	* HUMIDITY_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
674    pub fn set_status_led_config(&self, config: u8) -> ConvertingReceiver<()> {
675        let mut payload = vec![0; 1];
676        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(config));
677
678        self.device.set(u8::from(HumidityV2BrickletFunction::SetStatusLedConfig), payload)
679    }
680
681    /// Returns the configuration as set by [`set_status_led_config`]
682    ///
683    /// Associated constants:
684    /// * HUMIDITY_V2_BRICKLET_STATUS_LED_CONFIG_OFF
685    ///	* HUMIDITY_V2_BRICKLET_STATUS_LED_CONFIG_ON
686    ///	* HUMIDITY_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
687    ///	* HUMIDITY_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
688    pub fn get_status_led_config(&self) -> ConvertingReceiver<u8> {
689        let payload = vec![0; 0];
690
691        self.device.get(u8::from(HumidityV2BrickletFunction::GetStatusLedConfig), payload)
692    }
693
694    /// Returns the temperature as measured inside the microcontroller. The
695    /// value returned is not the ambient temperature!
696    ///
697    /// The temperature is only proportional to the real temperature and it has bad
698    /// accuracy. Practically it is only useful as an indicator for
699    /// temperature changes.
700    pub fn get_chip_temperature(&self) -> ConvertingReceiver<i16> {
701        let payload = vec![0; 0];
702
703        self.device.get(u8::from(HumidityV2BrickletFunction::GetChipTemperature), payload)
704    }
705
706    /// Calling this function will reset the Bricklet. All configurations
707    /// will be lost.
708    ///
709    /// After a reset you have to create new device objects,
710    /// calling functions on the existing ones will result in
711    /// undefined behavior!
712    pub fn reset(&self) -> ConvertingReceiver<()> {
713        let payload = vec![0; 0];
714
715        self.device.set(u8::from(HumidityV2BrickletFunction::Reset), payload)
716    }
717
718    /// Writes a new UID into flash. If you want to set a new UID
719    /// you have to decode the Base58 encoded UID string into an
720    /// integer first.
721    ///
722    /// We recommend that you use Brick Viewer to change the UID.
723    pub fn write_uid(&self, uid: u32) -> ConvertingReceiver<()> {
724        let mut payload = vec![0; 4];
725        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(uid));
726
727        self.device.set(u8::from(HumidityV2BrickletFunction::WriteUid), payload)
728    }
729
730    /// Returns the current UID as an integer. Encode as
731    /// Base58 to get the usual string version.
732    pub fn read_uid(&self) -> ConvertingReceiver<u32> {
733        let payload = vec![0; 0];
734
735        self.device.get(u8::from(HumidityV2BrickletFunction::ReadUid), payload)
736    }
737
738    /// Returns the UID, the UID where the Bricklet is connected to,
739    /// the position, the hardware and firmware version as well as the
740    /// device identifier.
741    ///
742    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
743    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
744    /// position 'z'.
745    ///
746    /// The device identifier numbers can be found [here](device_identifier).
747    /// |device_identifier_constant|
748    pub fn get_identity(&self) -> ConvertingReceiver<Identity> {
749        let payload = vec![0; 0];
750
751        self.device.get(u8::from(HumidityV2BrickletFunction::GetIdentity), payload)
752    }
753}