tinkerforge_async/bindings/
analog_out_v2_bricklet.rs

1/* ***********************************************************
2 * This file was automatically generated on 2024-02-16.      *
3 *                                                           *
4 * Rust Bindings Version 2.0.20                              *
5 *                                                           *
6 * If you have a bugfix for this file and want to commit it, *
7 * please fix the bug in the generator. You can find a link  *
8 * to the generators git repository on tinkerforge.com       *
9 *************************************************************/
10
11//! Generates configurable DC voltage between 0V and 12V.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/AnalogOutV2_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 AnalogOutV2BrickletFunction {
24    SetOutputVoltage,
25    GetOutputVoltage,
26    GetInputVoltage,
27    GetIdentity,
28}
29impl From<AnalogOutV2BrickletFunction> for u8 {
30    fn from(fun: AnalogOutV2BrickletFunction) -> Self {
31        match fun {
32            AnalogOutV2BrickletFunction::SetOutputVoltage => 1,
33            AnalogOutV2BrickletFunction::GetOutputVoltage => 2,
34            AnalogOutV2BrickletFunction::GetInputVoltage => 3,
35            AnalogOutV2BrickletFunction::GetIdentity => 255,
36        }
37    }
38}
39
40#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
41pub struct Identity {
42    pub uid: String,
43    pub connected_uid: String,
44    pub position: char,
45    pub hardware_version: [u8; 3],
46    pub firmware_version: [u8; 3],
47    pub device_identifier: u16,
48}
49impl FromByteSlice for Identity {
50    fn bytes_expected() -> usize {
51        25
52    }
53    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
54        Identity {
55            uid: <String>::from_le_byte_slice(&bytes[0..8]),
56            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
57            position: <char>::from_le_byte_slice(&bytes[16..17]),
58            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
59            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
60            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
61        }
62    }
63}
64
65/// Generates configurable DC voltage between 0V and 12V
66#[derive(Clone)]
67pub struct AnalogOutV2Bricklet {
68    device: Device,
69}
70impl AnalogOutV2Bricklet {
71    pub const DEVICE_IDENTIFIER: u16 = 256;
72    pub const DEVICE_DISPLAY_NAME: &'static str = "Analog Out Bricklet 2.0";
73    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
74    pub fn new(uid: Uid, connection: AsyncIpConnection) -> AnalogOutV2Bricklet {
75        let mut result = AnalogOutV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
76        result.device.response_expected[u8::from(AnalogOutV2BrickletFunction::SetOutputVoltage) as usize] = ResponseExpectedFlag::False;
77        result.device.response_expected[u8::from(AnalogOutV2BrickletFunction::GetOutputVoltage) as usize] =
78            ResponseExpectedFlag::AlwaysTrue;
79        result.device.response_expected[u8::from(AnalogOutV2BrickletFunction::GetInputVoltage) as usize] = ResponseExpectedFlag::AlwaysTrue;
80        result.device.response_expected[u8::from(AnalogOutV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
81        result
82    }
83
84    /// Returns the response expected flag for the function specified by the function ID parameter.
85    /// It is true if the function is expected to send a response, false otherwise.
86    ///
87    /// For getter functions this is enabled by default and cannot be disabled, because those
88    /// functions will always send a response. For callback configuration functions it is enabled
89    /// by default too, but can be disabled by [`set_response_expected`](crate::analog_out_v2_bricklet::AnalogOutV2Bricklet::set_response_expected).
90    /// For setter functions it is disabled by default and can be enabled.
91    ///
92    /// Enabling the response expected flag for a setter function allows to detect timeouts
93    /// and other error conditions calls of this setter as well. The device will then send a response
94    /// for this purpose. If this flag is disabled for a setter function then no response is sent
95    /// and errors are silently ignored, because they cannot be detected.
96    ///
97    /// See [`set_response_expected`](crate::analog_out_v2_bricklet::AnalogOutV2Bricklet::set_response_expected) for the list of function ID constants available for this function.
98    pub fn get_response_expected(&mut self, fun: AnalogOutV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
99        self.device.get_response_expected(u8::from(fun))
100    }
101
102    /// Changes the response expected flag of the function specified by the function ID parameter.
103    /// This flag can only be changed for setter (default value: false) and callback configuration
104    /// functions (default value: true). For getter functions it is always enabled.
105    ///
106    /// Enabling the response expected flag for a setter function allows to detect timeouts and
107    /// other error conditions calls of this setter as well. The device will then send a response
108    /// for this purpose. If this flag is disabled for a setter function then no response is sent
109    /// and errors are silently ignored, because they cannot be detected.
110    pub fn set_response_expected(
111        &mut self,
112        fun: AnalogOutV2BrickletFunction,
113        response_expected: bool,
114    ) -> Result<(), SetResponseExpectedError> {
115        self.device.set_response_expected(u8::from(fun), response_expected)
116    }
117
118    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
119    pub fn set_response_expected_all(&mut self, response_expected: bool) {
120        self.device.set_response_expected_all(response_expected)
121    }
122
123    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
124    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
125    pub fn get_api_version(&self) -> [u8; 3] {
126        self.device.api_version
127    }
128
129    /// Sets the voltage.
130    pub async fn set_output_voltage(&mut self, voltage: u16) -> Result<(), TinkerforgeError> {
131        let mut payload = [0; 2];
132        voltage.write_to_slice(&mut payload[0..2]);
133
134        #[allow(unused_variables)]
135        let result = self.device.set(u8::from(AnalogOutV2BrickletFunction::SetOutputVoltage), &payload).await?;
136        Ok(())
137    }
138
139    /// Returns the voltage as set by [`set_output_voltage`].
140    pub async fn get_output_voltage(&mut self) -> Result<u16, TinkerforgeError> {
141        let payload = [0; 0];
142
143        #[allow(unused_variables)]
144        let result = self.device.get(u8::from(AnalogOutV2BrickletFunction::GetOutputVoltage), &payload).await?;
145        Ok(u16::from_le_byte_slice(result.body()))
146    }
147
148    /// Returns the input voltage.
149    pub async fn get_input_voltage(&mut self) -> Result<u16, TinkerforgeError> {
150        let payload = [0; 0];
151
152        #[allow(unused_variables)]
153        let result = self.device.get(u8::from(AnalogOutV2BrickletFunction::GetInputVoltage), &payload).await?;
154        Ok(u16::from_le_byte_slice(result.body()))
155    }
156
157    /// Returns the UID, the UID where the Bricklet is connected to,
158    /// the position, the hardware and firmware version as well as the
159    /// device identifier.
160    ///
161    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
162    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
163    /// position 'z'.
164    ///
165    /// The device identifier numbers can be found [here](device_identifier).
166    /// |device_identifier_constant|
167    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
168        let payload = [0; 0];
169
170        #[allow(unused_variables)]
171        let result = self.device.get(u8::from(AnalogOutV2BrickletFunction::GetIdentity), &payload).await?;
172        Ok(Identity::from_le_byte_slice(result.body()))
173    }
174}