tinkerforge_async/bindings/
industrial_dual_0_20ma_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 two DC currents between 0mA and 20mA (IEC 60381-1).
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/IndustrialDual020mA_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 IndustrialDual020maBrickletFunction {
24    GetCurrent,
25    SetCurrentCallbackPeriod,
26    GetCurrentCallbackPeriod,
27    SetCurrentCallbackThreshold,
28    GetCurrentCallbackThreshold,
29    SetDebouncePeriod,
30    GetDebouncePeriod,
31    SetSampleRate,
32    GetSampleRate,
33    GetIdentity,
34    CallbackCurrent,
35    CallbackCurrentReached,
36}
37impl From<IndustrialDual020maBrickletFunction> for u8 {
38    fn from(fun: IndustrialDual020maBrickletFunction) -> Self {
39        match fun {
40            IndustrialDual020maBrickletFunction::GetCurrent => 1,
41            IndustrialDual020maBrickletFunction::SetCurrentCallbackPeriod => 2,
42            IndustrialDual020maBrickletFunction::GetCurrentCallbackPeriod => 3,
43            IndustrialDual020maBrickletFunction::SetCurrentCallbackThreshold => 4,
44            IndustrialDual020maBrickletFunction::GetCurrentCallbackThreshold => 5,
45            IndustrialDual020maBrickletFunction::SetDebouncePeriod => 6,
46            IndustrialDual020maBrickletFunction::GetDebouncePeriod => 7,
47            IndustrialDual020maBrickletFunction::SetSampleRate => 8,
48            IndustrialDual020maBrickletFunction::GetSampleRate => 9,
49            IndustrialDual020maBrickletFunction::GetIdentity => 255,
50            IndustrialDual020maBrickletFunction::CallbackCurrent => 10,
51            IndustrialDual020maBrickletFunction::CallbackCurrentReached => 11,
52        }
53    }
54}
55pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
56pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
57pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
58pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
59pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
60pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_240_SPS: u8 = 0;
61pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_60_SPS: u8 = 1;
62pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_15_SPS: u8 = 2;
63pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_4_SPS: u8 = 3;
64
65#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
66pub struct CurrentCallbackThreshold {
67    pub option: char,
68    pub min: i32,
69    pub max: i32,
70}
71impl FromByteSlice for CurrentCallbackThreshold {
72    fn bytes_expected() -> usize {
73        9
74    }
75    fn from_le_byte_slice(bytes: &[u8]) -> CurrentCallbackThreshold {
76        CurrentCallbackThreshold {
77            option: <char>::from_le_byte_slice(&bytes[0..1]),
78            min: <i32>::from_le_byte_slice(&bytes[1..5]),
79            max: <i32>::from_le_byte_slice(&bytes[5..9]),
80        }
81    }
82}
83
84#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
85pub struct CurrentEvent {
86    pub sensor: u8,
87    pub current: i32,
88}
89impl FromByteSlice for CurrentEvent {
90    fn bytes_expected() -> usize {
91        5
92    }
93    fn from_le_byte_slice(bytes: &[u8]) -> CurrentEvent {
94        CurrentEvent { sensor: <u8>::from_le_byte_slice(&bytes[0..1]), current: <i32>::from_le_byte_slice(&bytes[1..5]) }
95    }
96}
97
98#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
99pub struct CurrentReachedEvent {
100    pub sensor: u8,
101    pub current: i32,
102}
103impl FromByteSlice for CurrentReachedEvent {
104    fn bytes_expected() -> usize {
105        5
106    }
107    fn from_le_byte_slice(bytes: &[u8]) -> CurrentReachedEvent {
108        CurrentReachedEvent { sensor: <u8>::from_le_byte_slice(&bytes[0..1]), current: <i32>::from_le_byte_slice(&bytes[1..5]) }
109    }
110}
111
112#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
113pub struct Identity {
114    pub uid: String,
115    pub connected_uid: String,
116    pub position: char,
117    pub hardware_version: [u8; 3],
118    pub firmware_version: [u8; 3],
119    pub device_identifier: u16,
120}
121impl FromByteSlice for Identity {
122    fn bytes_expected() -> usize {
123        25
124    }
125    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
126        Identity {
127            uid: <String>::from_le_byte_slice(&bytes[0..8]),
128            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
129            position: <char>::from_le_byte_slice(&bytes[16..17]),
130            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
131            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
132            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
133        }
134    }
135}
136
137/// Measures two DC currents between 0mA and 20mA (IEC 60381-1)
138#[derive(Clone)]
139pub struct IndustrialDual020maBricklet {
140    device: Device,
141}
142impl IndustrialDual020maBricklet {
143    pub const DEVICE_IDENTIFIER: u16 = 228;
144    pub const DEVICE_DISPLAY_NAME: &'static str = "Industrial Dual 0-20mA Bricklet";
145    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
146    pub fn new(uid: Uid, connection: AsyncIpConnection) -> IndustrialDual020maBricklet {
147        let mut result = IndustrialDual020maBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
148        result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::GetCurrent) as usize] =
149            ResponseExpectedFlag::AlwaysTrue;
150        result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::SetCurrentCallbackPeriod) as usize] =
151            ResponseExpectedFlag::True;
152        result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::GetCurrentCallbackPeriod) as usize] =
153            ResponseExpectedFlag::AlwaysTrue;
154        result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::SetCurrentCallbackThreshold) as usize] =
155            ResponseExpectedFlag::True;
156        result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::GetCurrentCallbackThreshold) as usize] =
157            ResponseExpectedFlag::AlwaysTrue;
158        result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::SetDebouncePeriod) as usize] =
159            ResponseExpectedFlag::True;
160        result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::GetDebouncePeriod) as usize] =
161            ResponseExpectedFlag::AlwaysTrue;
162        result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::SetSampleRate) as usize] =
163            ResponseExpectedFlag::False;
164        result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::GetSampleRate) as usize] =
165            ResponseExpectedFlag::AlwaysTrue;
166        result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::GetIdentity) as usize] =
167            ResponseExpectedFlag::AlwaysTrue;
168        result
169    }
170
171    /// Returns the response expected flag for the function specified by the function ID parameter.
172    /// It is true if the function is expected to send a response, false otherwise.
173    ///
174    /// For getter functions this is enabled by default and cannot be disabled, because those
175    /// functions will always send a response. For callback configuration functions it is enabled
176    /// by default too, but can be disabled by [`set_response_expected`](crate::industrial_dual_0_20ma_bricklet::IndustrialDual020maBricklet::set_response_expected).
177    /// For setter functions it is disabled by default and can be enabled.
178    ///
179    /// Enabling the response expected flag for a setter function allows to detect timeouts
180    /// and other error conditions calls of this setter as well. The device will then send a response
181    /// for this purpose. If this flag is disabled for a setter function then no response is sent
182    /// and errors are silently ignored, because they cannot be detected.
183    ///
184    /// See [`set_response_expected`](crate::industrial_dual_0_20ma_bricklet::IndustrialDual020maBricklet::set_response_expected) for the list of function ID constants available for this function.
185    pub fn get_response_expected(&mut self, fun: IndustrialDual020maBrickletFunction) -> Result<bool, GetResponseExpectedError> {
186        self.device.get_response_expected(u8::from(fun))
187    }
188
189    /// Changes the response expected flag of the function specified by the function ID parameter.
190    /// This flag can only be changed for setter (default value: false) and callback configuration
191    /// functions (default value: true). For getter functions it is always enabled.
192    ///
193    /// Enabling the response expected flag for a setter function allows to detect timeouts and
194    /// other error conditions calls of this setter as well. The device will then send a response
195    /// for this purpose. If this flag is disabled for a setter function then no response is sent
196    /// and errors are silently ignored, because they cannot be detected.
197    pub fn set_response_expected(
198        &mut self,
199        fun: IndustrialDual020maBrickletFunction,
200        response_expected: bool,
201    ) -> Result<(), SetResponseExpectedError> {
202        self.device.set_response_expected(u8::from(fun), response_expected)
203    }
204
205    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
206    pub fn set_response_expected_all(&mut self, response_expected: bool) {
207        self.device.set_response_expected_all(response_expected)
208    }
209
210    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
211    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
212    pub fn get_api_version(&self) -> [u8; 3] {
213        self.device.api_version
214    }
215
216    /// This receiver is triggered periodically with the period that is set by
217    /// [`set_current_callback_period`]. The parameter is the current of the
218    /// sensor.
219    ///
220    /// The [`get_current_callback_receiver`] receiver is only triggered if the current has changed since the
221    /// last triggering.
222    ///
223    /// [`set_current_callback_period`]: #method.set_current_callback_period
224    /// [`get_current_callback_receiver`]: #method.get_current_callback_receiver
225    pub async fn get_current_callback_receiver(&mut self) -> impl Stream<Item = CurrentEvent> {
226        self.device
227            .get_callback_receiver(u8::from(IndustrialDual020maBrickletFunction::CallbackCurrent))
228            .await
229            .map(|p| CurrentEvent::from_le_byte_slice(p.body()))
230    }
231
232    /// This receiver is triggered when the threshold as set by
233    /// [`set_current_callback_threshold`] is reached.
234    /// The parameter is the current of the sensor.
235    ///
236    /// If the threshold keeps being reached, the receiver is triggered periodically
237    /// with the period as set by [`set_debounce_period`].
238    pub async fn get_current_reached_callback_receiver(&mut self) -> impl Stream<Item = CurrentReachedEvent> {
239        self.device
240            .get_callback_receiver(u8::from(IndustrialDual020maBrickletFunction::CallbackCurrentReached))
241            .await
242            .map(|p| CurrentReachedEvent::from_le_byte_slice(p.body()))
243    }
244
245    /// Returns the current of the specified sensor.
246    ///
247    /// It is possible to detect if an IEC 60381-1 compatible sensor is connected
248    /// and if it works properly.
249    ///
250    /// If the returned current is below 4mA, there is likely no sensor connected
251    /// or the sensor may be defect. If the returned current is over 20mA, there might
252    /// be a short circuit or the sensor may be defect.
253    ///
254    /// If you want to get the current periodically, it is recommended to use the
255    /// [`get_current_callback_receiver`] receiver and set the period with
256    /// [`set_current_callback_period`].
257    pub async fn get_current(&mut self, sensor: u8) -> Result<i32, TinkerforgeError> {
258        let mut payload = [0; 1];
259        sensor.write_to_slice(&mut payload[0..1]);
260
261        #[allow(unused_variables)]
262        let result = self.device.get(u8::from(IndustrialDual020maBrickletFunction::GetCurrent), &payload).await?;
263        Ok(i32::from_le_byte_slice(result.body()))
264    }
265
266    /// Sets the period with which the [`get_current_callback_receiver`] receiver is triggered
267    /// periodically for the given sensor. A value of 0 turns the receiver off.
268    ///
269    /// The [`get_current_callback_receiver`] receiver is only triggered if the current has changed since the
270    /// last triggering.
271    pub async fn set_current_callback_period(&mut self, sensor: u8, period: u32) -> Result<(), TinkerforgeError> {
272        let mut payload = [0; 5];
273        sensor.write_to_slice(&mut payload[0..1]);
274        period.write_to_slice(&mut payload[1..5]);
275
276        #[allow(unused_variables)]
277        let result = self.device.set(u8::from(IndustrialDual020maBrickletFunction::SetCurrentCallbackPeriod), &payload).await?;
278        Ok(())
279    }
280
281    /// Returns the period as set by [`set_current_callback_period`].
282    pub async fn get_current_callback_period(&mut self, sensor: u8) -> Result<u32, TinkerforgeError> {
283        let mut payload = [0; 1];
284        sensor.write_to_slice(&mut payload[0..1]);
285
286        #[allow(unused_variables)]
287        let result = self.device.get(u8::from(IndustrialDual020maBrickletFunction::GetCurrentCallbackPeriod), &payload).await?;
288        Ok(u32::from_le_byte_slice(result.body()))
289    }
290
291    /// Sets the thresholds for the [`get_current_reached_callback_receiver`] receiver for the given
292    /// sensor.
293    ///
294    /// The following options are possible:
295    ///
296    ///  Option| Description
297    ///  --- | ---
298    ///  'x'|    Receiver is turned off
299    ///  'o'|    Receiver is triggered when the current is *outside* the min and max values
300    ///  'i'|    Receiver is triggered when the current is *inside* the min and max values
301    ///  '<'|    Receiver is triggered when the current is smaller than the min value (max is ignored)
302    ///  '>'|    Receiver is triggered when the current is greater than the min value (max is ignored)
303    ///
304    /// Associated constants:
305    /// * INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_OFF
306    ///	* INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_OUTSIDE
307    ///	* INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_INSIDE
308    ///	* INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_SMALLER
309    ///	* INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_GREATER
310    pub async fn set_current_callback_threshold(&mut self, sensor: u8, option: char, min: i32, max: i32) -> Result<(), TinkerforgeError> {
311        let mut payload = [0; 10];
312        sensor.write_to_slice(&mut payload[0..1]);
313        option.write_to_slice(&mut payload[1..2]);
314        min.write_to_slice(&mut payload[2..6]);
315        max.write_to_slice(&mut payload[6..10]);
316
317        #[allow(unused_variables)]
318        let result = self.device.set(u8::from(IndustrialDual020maBrickletFunction::SetCurrentCallbackThreshold), &payload).await?;
319        Ok(())
320    }
321
322    /// Returns the threshold as set by [`set_current_callback_threshold`].
323    ///
324    /// Associated constants:
325    /// * INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_OFF
326    ///	* INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_OUTSIDE
327    ///	* INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_INSIDE
328    ///	* INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_SMALLER
329    ///	* INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_GREATER
330    pub async fn get_current_callback_threshold(&mut self, sensor: u8) -> Result<CurrentCallbackThreshold, TinkerforgeError> {
331        let mut payload = [0; 1];
332        sensor.write_to_slice(&mut payload[0..1]);
333
334        #[allow(unused_variables)]
335        let result = self.device.get(u8::from(IndustrialDual020maBrickletFunction::GetCurrentCallbackThreshold), &payload).await?;
336        Ok(CurrentCallbackThreshold::from_le_byte_slice(result.body()))
337    }
338
339    /// Sets the period with which the threshold receiver
340    ///
341    /// * [`get_current_reached_callback_receiver`]
342    ///
343    /// is triggered, if the threshold
344    ///
345    /// * [`set_current_callback_threshold`]
346    ///
347    /// keeps being reached.
348    pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
349        let mut payload = [0; 4];
350        debounce.write_to_slice(&mut payload[0..4]);
351
352        #[allow(unused_variables)]
353        let result = self.device.set(u8::from(IndustrialDual020maBrickletFunction::SetDebouncePeriod), &payload).await?;
354        Ok(())
355    }
356
357    /// Returns the debounce period as set by [`set_debounce_period`].
358    pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
359        let payload = [0; 0];
360
361        #[allow(unused_variables)]
362        let result = self.device.get(u8::from(IndustrialDual020maBrickletFunction::GetDebouncePeriod), &payload).await?;
363        Ok(u32::from_le_byte_slice(result.body()))
364    }
365
366    /// Sets the sample rate to either 240, 60, 15 or 4 samples per second.
367    /// The resolution for the rates is 12, 14, 16 and 18 bit respectively.
368    ///
369    ///  Value| Description
370    ///  --- | ---
371    ///  0|    240 samples per second| 12 bit resolution
372    ///  1|    60 samples per second| 14 bit resolution
373    ///  2|    15 samples per second| 16 bit resolution
374    ///  3|    4 samples per second| 18 bit resolution
375    ///
376    /// Associated constants:
377    /// * INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_240_SPS
378    ///	* INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_60_SPS
379    ///	* INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_15_SPS
380    ///	* INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_4_SPS
381    pub async fn set_sample_rate(&mut self, rate: u8) -> Result<(), TinkerforgeError> {
382        let mut payload = [0; 1];
383        rate.write_to_slice(&mut payload[0..1]);
384
385        #[allow(unused_variables)]
386        let result = self.device.set(u8::from(IndustrialDual020maBrickletFunction::SetSampleRate), &payload).await?;
387        Ok(())
388    }
389
390    /// Returns the sample rate as set by [`set_sample_rate`].
391    ///
392    /// Associated constants:
393    /// * INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_240_SPS
394    ///	* INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_60_SPS
395    ///	* INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_15_SPS
396    ///	* INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_4_SPS
397    pub async fn get_sample_rate(&mut self) -> Result<u8, TinkerforgeError> {
398        let payload = [0; 0];
399
400        #[allow(unused_variables)]
401        let result = self.device.get(u8::from(IndustrialDual020maBrickletFunction::GetSampleRate), &payload).await?;
402        Ok(u8::from_le_byte_slice(result.body()))
403    }
404
405    /// Returns the UID, the UID where the Bricklet is connected to,
406    /// the position, the hardware and firmware version as well as the
407    /// device identifier.
408    ///
409    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
410    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
411    /// position 'z'.
412    ///
413    /// The device identifier numbers can be found [here](device_identifier).
414    /// |device_identifier_constant|
415    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
416        let payload = [0; 0];
417
418        #[allow(unused_variables)]
419        let result = self.device.get(u8::from(IndustrialDual020maBrickletFunction::GetIdentity), &payload).await?;
420        Ok(Identity::from_le_byte_slice(result.body()))
421    }
422}