tinkerforge_async/bindings/
dust_detector_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 dust density.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/DustDetector_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 DustDetectorBrickletFunction {
24    GetDustDensity,
25    SetDustDensityCallbackPeriod,
26    GetDustDensityCallbackPeriod,
27    SetDustDensityCallbackThreshold,
28    GetDustDensityCallbackThreshold,
29    SetDebouncePeriod,
30    GetDebouncePeriod,
31    SetMovingAverage,
32    GetMovingAverage,
33    GetIdentity,
34    CallbackDustDensity,
35    CallbackDustDensityReached,
36}
37impl From<DustDetectorBrickletFunction> for u8 {
38    fn from(fun: DustDetectorBrickletFunction) -> Self {
39        match fun {
40            DustDetectorBrickletFunction::GetDustDensity => 1,
41            DustDetectorBrickletFunction::SetDustDensityCallbackPeriod => 2,
42            DustDetectorBrickletFunction::GetDustDensityCallbackPeriod => 3,
43            DustDetectorBrickletFunction::SetDustDensityCallbackThreshold => 4,
44            DustDetectorBrickletFunction::GetDustDensityCallbackThreshold => 5,
45            DustDetectorBrickletFunction::SetDebouncePeriod => 6,
46            DustDetectorBrickletFunction::GetDebouncePeriod => 7,
47            DustDetectorBrickletFunction::SetMovingAverage => 10,
48            DustDetectorBrickletFunction::GetMovingAverage => 11,
49            DustDetectorBrickletFunction::GetIdentity => 255,
50            DustDetectorBrickletFunction::CallbackDustDensity => 8,
51            DustDetectorBrickletFunction::CallbackDustDensityReached => 9,
52        }
53    }
54}
55pub const DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
56pub const DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
57pub const DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
58pub const DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
59pub const DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
60
61#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
62pub struct DustDensityCallbackThreshold {
63    pub option: char,
64    pub min: u16,
65    pub max: u16,
66}
67impl FromByteSlice for DustDensityCallbackThreshold {
68    fn bytes_expected() -> usize {
69        5
70    }
71    fn from_le_byte_slice(bytes: &[u8]) -> DustDensityCallbackThreshold {
72        DustDensityCallbackThreshold {
73            option: <char>::from_le_byte_slice(&bytes[0..1]),
74            min: <u16>::from_le_byte_slice(&bytes[1..3]),
75            max: <u16>::from_le_byte_slice(&bytes[3..5]),
76        }
77    }
78}
79
80#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
81pub struct Identity {
82    pub uid: String,
83    pub connected_uid: String,
84    pub position: char,
85    pub hardware_version: [u8; 3],
86    pub firmware_version: [u8; 3],
87    pub device_identifier: u16,
88}
89impl FromByteSlice for Identity {
90    fn bytes_expected() -> usize {
91        25
92    }
93    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
94        Identity {
95            uid: <String>::from_le_byte_slice(&bytes[0..8]),
96            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
97            position: <char>::from_le_byte_slice(&bytes[16..17]),
98            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
99            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
100            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
101        }
102    }
103}
104
105/// Measures dust density
106#[derive(Clone)]
107pub struct DustDetectorBricklet {
108    device: Device,
109}
110impl DustDetectorBricklet {
111    pub const DEVICE_IDENTIFIER: u16 = 260;
112    pub const DEVICE_DISPLAY_NAME: &'static str = "Dust Detector Bricklet";
113    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
114    pub fn new(uid: Uid, connection: AsyncIpConnection) -> DustDetectorBricklet {
115        let mut result = DustDetectorBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
116        result.device.response_expected[u8::from(DustDetectorBrickletFunction::GetDustDensity) as usize] = ResponseExpectedFlag::AlwaysTrue;
117        result.device.response_expected[u8::from(DustDetectorBrickletFunction::SetDustDensityCallbackPeriod) as usize] =
118            ResponseExpectedFlag::True;
119        result.device.response_expected[u8::from(DustDetectorBrickletFunction::GetDustDensityCallbackPeriod) as usize] =
120            ResponseExpectedFlag::AlwaysTrue;
121        result.device.response_expected[u8::from(DustDetectorBrickletFunction::SetDustDensityCallbackThreshold) as usize] =
122            ResponseExpectedFlag::True;
123        result.device.response_expected[u8::from(DustDetectorBrickletFunction::GetDustDensityCallbackThreshold) as usize] =
124            ResponseExpectedFlag::AlwaysTrue;
125        result.device.response_expected[u8::from(DustDetectorBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
126        result.device.response_expected[u8::from(DustDetectorBrickletFunction::GetDebouncePeriod) as usize] =
127            ResponseExpectedFlag::AlwaysTrue;
128        result.device.response_expected[u8::from(DustDetectorBrickletFunction::SetMovingAverage) as usize] = ResponseExpectedFlag::False;
129        result.device.response_expected[u8::from(DustDetectorBrickletFunction::GetMovingAverage) as usize] =
130            ResponseExpectedFlag::AlwaysTrue;
131        result.device.response_expected[u8::from(DustDetectorBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
132        result
133    }
134
135    /// Returns the response expected flag for the function specified by the function ID parameter.
136    /// It is true if the function is expected to send a response, false otherwise.
137    ///
138    /// For getter functions this is enabled by default and cannot be disabled, because those
139    /// functions will always send a response. For callback configuration functions it is enabled
140    /// by default too, but can be disabled by [`set_response_expected`](crate::dust_detector_bricklet::DustDetectorBricklet::set_response_expected).
141    /// For setter functions it is disabled by default and can be enabled.
142    ///
143    /// Enabling the response expected flag for a setter function allows to detect timeouts
144    /// and other error conditions calls of this setter as well. The device will then send a response
145    /// for this purpose. If this flag is disabled for a setter function then no response is sent
146    /// and errors are silently ignored, because they cannot be detected.
147    ///
148    /// See [`set_response_expected`](crate::dust_detector_bricklet::DustDetectorBricklet::set_response_expected) for the list of function ID constants available for this function.
149    pub fn get_response_expected(&mut self, fun: DustDetectorBrickletFunction) -> Result<bool, GetResponseExpectedError> {
150        self.device.get_response_expected(u8::from(fun))
151    }
152
153    /// Changes the response expected flag of the function specified by the function ID parameter.
154    /// This flag can only be changed for setter (default value: false) and callback configuration
155    /// functions (default value: true). For getter functions it is always enabled.
156    ///
157    /// Enabling the response expected flag for a setter function allows to detect timeouts and
158    /// other error conditions calls of this setter as well. The device will then send a response
159    /// for this purpose. If this flag is disabled for a setter function then no response is sent
160    /// and errors are silently ignored, because they cannot be detected.
161    pub fn set_response_expected(
162        &mut self,
163        fun: DustDetectorBrickletFunction,
164        response_expected: bool,
165    ) -> Result<(), SetResponseExpectedError> {
166        self.device.set_response_expected(u8::from(fun), response_expected)
167    }
168
169    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
170    pub fn set_response_expected_all(&mut self, response_expected: bool) {
171        self.device.set_response_expected_all(response_expected)
172    }
173
174    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
175    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
176    pub fn get_api_version(&self) -> [u8; 3] {
177        self.device.api_version
178    }
179
180    /// This receiver is triggered periodically with the period that is set by
181    /// [`set_dust_density_callback_period`]. The parameter is the
182    /// dust density of the sensor.
183    ///
184    /// Der [`get_dust_density_callback_receiver`] receiver is only triggered if the dust density value has
185    /// changed since the last triggering.
186    ///
187    /// [`set_dust_density_callback_period`]: #method.set_dust_density_callback_period
188    /// [`get_dust_density_callback_receiver`]: #method.get_dust_density_callback_receiver
189    pub async fn get_dust_density_callback_receiver(&mut self) -> impl Stream<Item = u16> {
190        self.device
191            .get_callback_receiver(u8::from(DustDetectorBrickletFunction::CallbackDustDensity))
192            .await
193            .map(|p| u16::from_le_byte_slice(p.body()))
194    }
195
196    /// This receiver is triggered when the threshold as set by
197    /// [`set_dust_density_callback_threshold`] is reached.
198    /// The parameter is the dust density of the sensor.
199    ///
200    /// If the threshold keeps being reached, the receiver is triggered periodically
201    /// with the period as set by [`set_debounce_period`].
202    pub async fn get_dust_density_reached_callback_receiver(&mut self) -> impl Stream<Item = u16> {
203        self.device
204            .get_callback_receiver(u8::from(DustDetectorBrickletFunction::CallbackDustDensityReached))
205            .await
206            .map(|p| u16::from_le_byte_slice(p.body()))
207    }
208
209    /// Returns the dust density.
210    ///
211    /// If you want to get the dust density periodically, it is recommended
212    /// to use the [`get_dust_density_callback_receiver`] receiver and set the period with
213    /// [`set_dust_density_callback_period`].
214    pub async fn get_dust_density(&mut self) -> Result<u16, TinkerforgeError> {
215        let payload = [0; 0];
216
217        #[allow(unused_variables)]
218        let result = self.device.get(u8::from(DustDetectorBrickletFunction::GetDustDensity), &payload).await?;
219        Ok(u16::from_le_byte_slice(result.body()))
220    }
221
222    /// Sets the period with which the [`get_dust_density_callback_receiver`] receiver is triggered
223    /// periodically. A value of 0 turns the receiver off.
224    ///
225    /// The [`get_dust_density_callback_receiver`] receiver is only triggered if the dust density has
226    /// changed since the last triggering.
227    pub async fn set_dust_density_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
228        let mut payload = [0; 4];
229        period.write_to_slice(&mut payload[0..4]);
230
231        #[allow(unused_variables)]
232        let result = self.device.set(u8::from(DustDetectorBrickletFunction::SetDustDensityCallbackPeriod), &payload).await?;
233        Ok(())
234    }
235
236    /// Returns the period as set by [`set_dust_density_callback_period`].
237    pub async fn get_dust_density_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
238        let payload = [0; 0];
239
240        #[allow(unused_variables)]
241        let result = self.device.get(u8::from(DustDetectorBrickletFunction::GetDustDensityCallbackPeriod), &payload).await?;
242        Ok(u32::from_le_byte_slice(result.body()))
243    }
244
245    /// Sets the thresholds for the [`get_dust_density_reached_callback_receiver`] receiver.
246    ///
247    /// The following options are possible:
248    ///
249    ///  Option| Description
250    ///  --- | ---
251    ///  'x'|    Receiver is turned off
252    ///  'o'|    Receiver is triggered when the dust density value is *outside* the min and max values
253    ///  'i'|    Receiver is triggered when the dust density value is *inside* the min and max values
254    ///  '<'|    Receiver is triggered when the dust density value is smaller than the min value (max is ignored)
255    ///  '>'|    Receiver is triggered when the dust density value is greater than the min value (max is ignored)
256    ///
257    /// Associated constants:
258    /// * DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_OFF
259    ///	* DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_OUTSIDE
260    ///	* DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_INSIDE
261    ///	* DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_SMALLER
262    ///	* DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_GREATER
263    pub async fn set_dust_density_callback_threshold(&mut self, option: char, min: u16, max: u16) -> Result<(), TinkerforgeError> {
264        let mut payload = [0; 5];
265        option.write_to_slice(&mut payload[0..1]);
266        min.write_to_slice(&mut payload[1..3]);
267        max.write_to_slice(&mut payload[3..5]);
268
269        #[allow(unused_variables)]
270        let result = self.device.set(u8::from(DustDetectorBrickletFunction::SetDustDensityCallbackThreshold), &payload).await?;
271        Ok(())
272    }
273
274    /// Returns the threshold as set by [`set_dust_density_callback_threshold`].
275    ///
276    /// Associated constants:
277    /// * DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_OFF
278    ///	* DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_OUTSIDE
279    ///	* DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_INSIDE
280    ///	* DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_SMALLER
281    ///	* DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_GREATER
282    pub async fn get_dust_density_callback_threshold(&mut self) -> Result<DustDensityCallbackThreshold, TinkerforgeError> {
283        let payload = [0; 0];
284
285        #[allow(unused_variables)]
286        let result = self.device.get(u8::from(DustDetectorBrickletFunction::GetDustDensityCallbackThreshold), &payload).await?;
287        Ok(DustDensityCallbackThreshold::from_le_byte_slice(result.body()))
288    }
289
290    /// Sets the period with which the threshold receiver
291    ///
292    /// * [`get_dust_density_reached_callback_receiver`]
293    ///
294    /// is triggered, if the threshold
295    ///
296    /// * [`set_dust_density_callback_threshold`]
297    ///
298    /// keeps being reached.
299    pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
300        let mut payload = [0; 4];
301        debounce.write_to_slice(&mut payload[0..4]);
302
303        #[allow(unused_variables)]
304        let result = self.device.set(u8::from(DustDetectorBrickletFunction::SetDebouncePeriod), &payload).await?;
305        Ok(())
306    }
307
308    /// Returns the debounce period as set by [`set_debounce_period`].
309    pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
310        let payload = [0; 0];
311
312        #[allow(unused_variables)]
313        let result = self.device.get(u8::from(DustDetectorBrickletFunction::GetDebouncePeriod), &payload).await?;
314        Ok(u32::from_le_byte_slice(result.body()))
315    }
316
317    /// Sets the length of a [moving averaging](https://en.wikipedia.org/wiki/Moving_average)__
318    /// for the dust density.
319    ///
320    /// Setting the length to 0 will turn the averaging completely off. With less
321    /// averaging, there is more noise on the data.
322    pub async fn set_moving_average(&mut self, average: u8) -> Result<(), TinkerforgeError> {
323        let mut payload = [0; 1];
324        average.write_to_slice(&mut payload[0..1]);
325
326        #[allow(unused_variables)]
327        let result = self.device.set(u8::from(DustDetectorBrickletFunction::SetMovingAverage), &payload).await?;
328        Ok(())
329    }
330
331    /// Returns the length moving average as set by [`set_moving_average`].
332    pub async fn get_moving_average(&mut self) -> Result<u8, TinkerforgeError> {
333        let payload = [0; 0];
334
335        #[allow(unused_variables)]
336        let result = self.device.get(u8::from(DustDetectorBrickletFunction::GetMovingAverage), &payload).await?;
337        Ok(u8::from_le_byte_slice(result.body()))
338    }
339
340    /// Returns the UID, the UID where the Bricklet is connected to,
341    /// the position, the hardware and firmware version as well as the
342    /// device identifier.
343    ///
344    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
345    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
346    /// position 'z'.
347    ///
348    /// The device identifier numbers can be found [here](device_identifier).
349    /// |device_identifier_constant|
350    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
351        let payload = [0; 0];
352
353        #[allow(unused_variables)]
354        let result = self.device.get(u8::from(DustDetectorBrickletFunction::GetIdentity), &payload).await?;
355        Ok(Identity::from_le_byte_slice(result.body()))
356    }
357}