tinkerforge_async/bindings/
industrial_analog_out_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//! Generates configurable DC voltage and current, 0V to 10V and 4mA to 20mA.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/IndustrialAnalogOut_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 IndustrialAnalogOutBrickletFunction {
24    Enable,
25    Disable,
26    IsEnabled,
27    SetVoltage,
28    GetVoltage,
29    SetCurrent,
30    GetCurrent,
31    SetConfiguration,
32    GetConfiguration,
33    GetIdentity,
34}
35impl From<IndustrialAnalogOutBrickletFunction> for u8 {
36    fn from(fun: IndustrialAnalogOutBrickletFunction) -> Self {
37        match fun {
38            IndustrialAnalogOutBrickletFunction::Enable => 1,
39            IndustrialAnalogOutBrickletFunction::Disable => 2,
40            IndustrialAnalogOutBrickletFunction::IsEnabled => 3,
41            IndustrialAnalogOutBrickletFunction::SetVoltage => 4,
42            IndustrialAnalogOutBrickletFunction::GetVoltage => 5,
43            IndustrialAnalogOutBrickletFunction::SetCurrent => 6,
44            IndustrialAnalogOutBrickletFunction::GetCurrent => 7,
45            IndustrialAnalogOutBrickletFunction::SetConfiguration => 8,
46            IndustrialAnalogOutBrickletFunction::GetConfiguration => 9,
47            IndustrialAnalogOutBrickletFunction::GetIdentity => 255,
48        }
49    }
50}
51pub const INDUSTRIAL_ANALOG_OUT_BRICKLET_VOLTAGE_RANGE_0_TO_5V: u8 = 0;
52pub const INDUSTRIAL_ANALOG_OUT_BRICKLET_VOLTAGE_RANGE_0_TO_10V: u8 = 1;
53pub const INDUSTRIAL_ANALOG_OUT_BRICKLET_CURRENT_RANGE_4_TO_20MA: u8 = 0;
54pub const INDUSTRIAL_ANALOG_OUT_BRICKLET_CURRENT_RANGE_0_TO_20MA: u8 = 1;
55pub const INDUSTRIAL_ANALOG_OUT_BRICKLET_CURRENT_RANGE_0_TO_24MA: u8 = 2;
56
57#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
58pub struct Configuration {
59    pub voltage_range: u8,
60    pub current_range: u8,
61}
62impl FromByteSlice for Configuration {
63    fn bytes_expected() -> usize {
64        2
65    }
66    fn from_le_byte_slice(bytes: &[u8]) -> Configuration {
67        Configuration { voltage_range: <u8>::from_le_byte_slice(&bytes[0..1]), current_range: <u8>::from_le_byte_slice(&bytes[1..2]) }
68    }
69}
70
71#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
72pub struct Identity {
73    pub uid: String,
74    pub connected_uid: String,
75    pub position: char,
76    pub hardware_version: [u8; 3],
77    pub firmware_version: [u8; 3],
78    pub device_identifier: u16,
79}
80impl FromByteSlice for Identity {
81    fn bytes_expected() -> usize {
82        25
83    }
84    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
85        Identity {
86            uid: <String>::from_le_byte_slice(&bytes[0..8]),
87            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
88            position: <char>::from_le_byte_slice(&bytes[16..17]),
89            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
90            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
91            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
92        }
93    }
94}
95
96/// Generates configurable DC voltage and current, 0V to 10V and 4mA to 20mA
97#[derive(Clone)]
98pub struct IndustrialAnalogOutBricklet {
99    device: Device,
100}
101impl IndustrialAnalogOutBricklet {
102    pub const DEVICE_IDENTIFIER: u16 = 258;
103    pub const DEVICE_DISPLAY_NAME: &'static str = "Industrial Analog Out Bricklet";
104    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
105    pub fn new(uid: Uid, connection: AsyncIpConnection) -> IndustrialAnalogOutBricklet {
106        let mut result = IndustrialAnalogOutBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
107        result.device.response_expected[u8::from(IndustrialAnalogOutBrickletFunction::Enable) as usize] = ResponseExpectedFlag::False;
108        result.device.response_expected[u8::from(IndustrialAnalogOutBrickletFunction::Disable) as usize] = ResponseExpectedFlag::False;
109        result.device.response_expected[u8::from(IndustrialAnalogOutBrickletFunction::IsEnabled) as usize] =
110            ResponseExpectedFlag::AlwaysTrue;
111        result.device.response_expected[u8::from(IndustrialAnalogOutBrickletFunction::SetVoltage) as usize] = ResponseExpectedFlag::False;
112        result.device.response_expected[u8::from(IndustrialAnalogOutBrickletFunction::GetVoltage) as usize] =
113            ResponseExpectedFlag::AlwaysTrue;
114        result.device.response_expected[u8::from(IndustrialAnalogOutBrickletFunction::SetCurrent) as usize] = ResponseExpectedFlag::False;
115        result.device.response_expected[u8::from(IndustrialAnalogOutBrickletFunction::GetCurrent) as usize] =
116            ResponseExpectedFlag::AlwaysTrue;
117        result.device.response_expected[u8::from(IndustrialAnalogOutBrickletFunction::SetConfiguration) as usize] =
118            ResponseExpectedFlag::False;
119        result.device.response_expected[u8::from(IndustrialAnalogOutBrickletFunction::GetConfiguration) as usize] =
120            ResponseExpectedFlag::AlwaysTrue;
121        result.device.response_expected[u8::from(IndustrialAnalogOutBrickletFunction::GetIdentity) as usize] =
122            ResponseExpectedFlag::AlwaysTrue;
123        result
124    }
125
126    /// Returns the response expected flag for the function specified by the function ID parameter.
127    /// It is true if the function is expected to send a response, false otherwise.
128    ///
129    /// For getter functions this is enabled by default and cannot be disabled, because those
130    /// functions will always send a response. For callback configuration functions it is enabled
131    /// by default too, but can be disabled by [`set_response_expected`](crate::industrial_analog_out_bricklet::IndustrialAnalogOutBricklet::set_response_expected).
132    /// For setter functions it is disabled by default and can be enabled.
133    ///
134    /// Enabling the response expected flag for a setter function allows to detect timeouts
135    /// and other error conditions calls of this setter as well. The device will then send a response
136    /// for this purpose. If this flag is disabled for a setter function then no response is sent
137    /// and errors are silently ignored, because they cannot be detected.
138    ///
139    /// See [`set_response_expected`](crate::industrial_analog_out_bricklet::IndustrialAnalogOutBricklet::set_response_expected) for the list of function ID constants available for this function.
140    pub fn get_response_expected(&mut self, fun: IndustrialAnalogOutBrickletFunction) -> Result<bool, GetResponseExpectedError> {
141        self.device.get_response_expected(u8::from(fun))
142    }
143
144    /// Changes the response expected flag of the function specified by the function ID parameter.
145    /// This flag can only be changed for setter (default value: false) and callback configuration
146    /// functions (default value: true). For getter functions it is always enabled.
147    ///
148    /// Enabling the response expected flag for a setter function allows to detect timeouts and
149    /// other error conditions calls of this setter as well. The device will then send a response
150    /// for this purpose. If this flag is disabled for a setter function then no response is sent
151    /// and errors are silently ignored, because they cannot be detected.
152    pub fn set_response_expected(
153        &mut self,
154        fun: IndustrialAnalogOutBrickletFunction,
155        response_expected: bool,
156    ) -> Result<(), SetResponseExpectedError> {
157        self.device.set_response_expected(u8::from(fun), response_expected)
158    }
159
160    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
161    pub fn set_response_expected_all(&mut self, response_expected: bool) {
162        self.device.set_response_expected_all(response_expected)
163    }
164
165    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
166    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
167    pub fn get_api_version(&self) -> [u8; 3] {
168        self.device.api_version
169    }
170
171    /// Enables the output of voltage and current.
172    ///
173    /// The default is disabled.
174    pub async fn enable(&mut self) -> Result<(), TinkerforgeError> {
175        let payload = [0; 0];
176
177        #[allow(unused_variables)]
178        let result = self.device.set(u8::from(IndustrialAnalogOutBrickletFunction::Enable), &payload).await?;
179        Ok(())
180    }
181
182    /// Disables the output of voltage and current.
183    ///
184    /// The default is disabled.
185    pub async fn disable(&mut self) -> Result<(), TinkerforgeError> {
186        let payload = [0; 0];
187
188        #[allow(unused_variables)]
189        let result = self.device.set(u8::from(IndustrialAnalogOutBrickletFunction::Disable), &payload).await?;
190        Ok(())
191    }
192
193    /// Returns *true* if output of voltage and current is enabled, *false* otherwise.
194    pub async fn is_enabled(&mut self) -> Result<bool, TinkerforgeError> {
195        let payload = [0; 0];
196
197        #[allow(unused_variables)]
198        let result = self.device.get(u8::from(IndustrialAnalogOutBrickletFunction::IsEnabled), &payload).await?;
199        Ok(bool::from_le_byte_slice(result.body()))
200    }
201
202    /// Sets the output voltage.
203    ///
204    /// The output voltage and output current are linked. Changing the output voltage
205    /// also changes the output current.
206    pub async fn set_voltage(&mut self, voltage: u16) -> Result<(), TinkerforgeError> {
207        let mut payload = [0; 2];
208        voltage.write_to_slice(&mut payload[0..2]);
209
210        #[allow(unused_variables)]
211        let result = self.device.set(u8::from(IndustrialAnalogOutBrickletFunction::SetVoltage), &payload).await?;
212        Ok(())
213    }
214
215    /// Returns the voltage as set by [`set_voltage`].
216    pub async fn get_voltage(&mut self) -> Result<u16, TinkerforgeError> {
217        let payload = [0; 0];
218
219        #[allow(unused_variables)]
220        let result = self.device.get(u8::from(IndustrialAnalogOutBrickletFunction::GetVoltage), &payload).await?;
221        Ok(u16::from_le_byte_slice(result.body()))
222    }
223
224    /// Sets the output current.
225    ///
226    /// The output current and output voltage are linked. Changing the output current
227    /// also changes the output voltage.
228    pub async fn set_current(&mut self, current: u16) -> Result<(), TinkerforgeError> {
229        let mut payload = [0; 2];
230        current.write_to_slice(&mut payload[0..2]);
231
232        #[allow(unused_variables)]
233        let result = self.device.set(u8::from(IndustrialAnalogOutBrickletFunction::SetCurrent), &payload).await?;
234        Ok(())
235    }
236
237    /// Returns the current as set by [`set_current`].
238    pub async fn get_current(&mut self) -> Result<u16, TinkerforgeError> {
239        let payload = [0; 0];
240
241        #[allow(unused_variables)]
242        let result = self.device.get(u8::from(IndustrialAnalogOutBrickletFunction::GetCurrent), &payload).await?;
243        Ok(u16::from_le_byte_slice(result.body()))
244    }
245
246    /// Configures the voltage and current range.
247    ///
248    /// Possible voltage ranges are:
249    ///
250    /// * 0V to 5V
251    /// * 0V to 10V
252    ///
253    /// Possible current ranges are:
254    ///
255    /// * 4mA to 20mA
256    /// * 0mA to 20mA
257    /// * 0mA to 24mA
258    ///
259    /// The resolution will always be 12 bit. This means, that the
260    /// precision is higher with a smaller range.
261    ///
262    /// Associated constants:
263    /// * INDUSTRIAL_ANALOG_OUT_BRICKLET_VOLTAGE_RANGE_0_TO_5V
264    ///	* INDUSTRIAL_ANALOG_OUT_BRICKLET_VOLTAGE_RANGE_0_TO_10V
265    ///	* INDUSTRIAL_ANALOG_OUT_BRICKLET_CURRENT_RANGE_4_TO_20MA
266    ///	* INDUSTRIAL_ANALOG_OUT_BRICKLET_CURRENT_RANGE_0_TO_20MA
267    ///	* INDUSTRIAL_ANALOG_OUT_BRICKLET_CURRENT_RANGE_0_TO_24MA
268    pub async fn set_configuration(&mut self, voltage_range: u8, current_range: u8) -> Result<(), TinkerforgeError> {
269        let mut payload = [0; 2];
270        voltage_range.write_to_slice(&mut payload[0..1]);
271        current_range.write_to_slice(&mut payload[1..2]);
272
273        #[allow(unused_variables)]
274        let result = self.device.set(u8::from(IndustrialAnalogOutBrickletFunction::SetConfiguration), &payload).await?;
275        Ok(())
276    }
277
278    /// Returns the configuration as set by [`set_configuration`].
279    ///
280    /// Associated constants:
281    /// * INDUSTRIAL_ANALOG_OUT_BRICKLET_VOLTAGE_RANGE_0_TO_5V
282    ///	* INDUSTRIAL_ANALOG_OUT_BRICKLET_VOLTAGE_RANGE_0_TO_10V
283    ///	* INDUSTRIAL_ANALOG_OUT_BRICKLET_CURRENT_RANGE_4_TO_20MA
284    ///	* INDUSTRIAL_ANALOG_OUT_BRICKLET_CURRENT_RANGE_0_TO_20MA
285    ///	* INDUSTRIAL_ANALOG_OUT_BRICKLET_CURRENT_RANGE_0_TO_24MA
286    pub async fn get_configuration(&mut self) -> Result<Configuration, TinkerforgeError> {
287        let payload = [0; 0];
288
289        #[allow(unused_variables)]
290        let result = self.device.get(u8::from(IndustrialAnalogOutBrickletFunction::GetConfiguration), &payload).await?;
291        Ok(Configuration::from_le_byte_slice(result.body()))
292    }
293
294    /// Returns the UID, the UID where the Bricklet is connected to,
295    /// the position, the hardware and firmware version as well as the
296    /// device identifier.
297    ///
298    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
299    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
300    /// position 'z'.
301    ///
302    /// The device identifier numbers can be found [here](device_identifier).
303    /// |device_identifier_constant|
304    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
305        let payload = [0; 0];
306
307        #[allow(unused_variables)]
308        let result = self.device.get(u8::from(IndustrialAnalogOutBrickletFunction::GetIdentity), &payload).await?;
309        Ok(Identity::from_le_byte_slice(result.body()))
310    }
311}