tinkerforge_async/bindings/
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 between 0V and 5V.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/AnalogOut_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 AnalogOutBrickletFunction {
24    SetVoltage,
25    GetVoltage,
26    SetMode,
27    GetMode,
28    GetIdentity,
29}
30impl From<AnalogOutBrickletFunction> for u8 {
31    fn from(fun: AnalogOutBrickletFunction) -> Self {
32        match fun {
33            AnalogOutBrickletFunction::SetVoltage => 1,
34            AnalogOutBrickletFunction::GetVoltage => 2,
35            AnalogOutBrickletFunction::SetMode => 3,
36            AnalogOutBrickletFunction::GetMode => 4,
37            AnalogOutBrickletFunction::GetIdentity => 255,
38        }
39    }
40}
41pub const ANALOG_OUT_BRICKLET_MODE_ANALOG_VALUE: u8 = 0;
42pub const ANALOG_OUT_BRICKLET_MODE_1K_TO_GROUND: u8 = 1;
43pub const ANALOG_OUT_BRICKLET_MODE_100K_TO_GROUND: u8 = 2;
44pub const ANALOG_OUT_BRICKLET_MODE_500K_TO_GROUND: u8 = 3;
45
46#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
47pub struct Identity {
48    pub uid: String,
49    pub connected_uid: String,
50    pub position: char,
51    pub hardware_version: [u8; 3],
52    pub firmware_version: [u8; 3],
53    pub device_identifier: u16,
54}
55impl FromByteSlice for Identity {
56    fn bytes_expected() -> usize {
57        25
58    }
59    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
60        Identity {
61            uid: <String>::from_le_byte_slice(&bytes[0..8]),
62            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
63            position: <char>::from_le_byte_slice(&bytes[16..17]),
64            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
65            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
66            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
67        }
68    }
69}
70
71/// Generates configurable DC voltage between 0V and 5V
72#[derive(Clone)]
73pub struct AnalogOutBricklet {
74    device: Device,
75}
76impl AnalogOutBricklet {
77    pub const DEVICE_IDENTIFIER: u16 = 220;
78    pub const DEVICE_DISPLAY_NAME: &'static str = "Analog Out Bricklet";
79    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
80    pub fn new(uid: Uid, connection: AsyncIpConnection) -> AnalogOutBricklet {
81        let mut result = AnalogOutBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
82        result.device.response_expected[u8::from(AnalogOutBrickletFunction::SetVoltage) as usize] = ResponseExpectedFlag::False;
83        result.device.response_expected[u8::from(AnalogOutBrickletFunction::GetVoltage) as usize] = ResponseExpectedFlag::AlwaysTrue;
84        result.device.response_expected[u8::from(AnalogOutBrickletFunction::SetMode) as usize] = ResponseExpectedFlag::False;
85        result.device.response_expected[u8::from(AnalogOutBrickletFunction::GetMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
86        result.device.response_expected[u8::from(AnalogOutBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
87        result
88    }
89
90    /// Returns the response expected flag for the function specified by the function ID parameter.
91    /// It is true if the function is expected to send a response, false otherwise.
92    ///
93    /// For getter functions this is enabled by default and cannot be disabled, because those
94    /// functions will always send a response. For callback configuration functions it is enabled
95    /// by default too, but can be disabled by [`set_response_expected`](crate::analog_out_bricklet::AnalogOutBricklet::set_response_expected).
96    /// For setter functions it is disabled by default and can be enabled.
97    ///
98    /// Enabling the response expected flag for a setter function allows to detect timeouts
99    /// and other error conditions calls of this setter as well. The device will then send a response
100    /// for this purpose. If this flag is disabled for a setter function then no response is sent
101    /// and errors are silently ignored, because they cannot be detected.
102    ///
103    /// See [`set_response_expected`](crate::analog_out_bricklet::AnalogOutBricklet::set_response_expected) for the list of function ID constants available for this function.
104    pub fn get_response_expected(&mut self, fun: AnalogOutBrickletFunction) -> Result<bool, GetResponseExpectedError> {
105        self.device.get_response_expected(u8::from(fun))
106    }
107
108    /// Changes the response expected flag of the function specified by the function ID parameter.
109    /// This flag can only be changed for setter (default value: false) and callback configuration
110    /// functions (default value: true). For getter functions it is always enabled.
111    ///
112    /// Enabling the response expected flag for a setter function allows to detect timeouts and
113    /// other error conditions calls of this setter as well. The device will then send a response
114    /// for this purpose. If this flag is disabled for a setter function then no response is sent
115    /// and errors are silently ignored, because they cannot be detected.
116    pub fn set_response_expected(
117        &mut self,
118        fun: AnalogOutBrickletFunction,
119        response_expected: bool,
120    ) -> Result<(), SetResponseExpectedError> {
121        self.device.set_response_expected(u8::from(fun), response_expected)
122    }
123
124    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
125    pub fn set_response_expected_all(&mut self, response_expected: bool) {
126        self.device.set_response_expected_all(response_expected)
127    }
128
129    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
130    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
131    pub fn get_api_version(&self) -> [u8; 3] {
132        self.device.api_version
133    }
134
135    /// Sets the voltage. Calling this function will set
136    /// the mode to 0 (see [`set_mode`]).
137    ///
138    /// [`set_mode`]: #method.set_mode
139    pub async fn set_voltage(&mut self, voltage: u16) -> Result<(), TinkerforgeError> {
140        let mut payload = [0; 2];
141        voltage.write_to_slice(&mut payload[0..2]);
142
143        #[allow(unused_variables)]
144        let result = self.device.set(u8::from(AnalogOutBrickletFunction::SetVoltage), &payload).await?;
145        Ok(())
146    }
147
148    /// Returns the voltage as set by [`set_voltage`].
149    pub async fn get_voltage(&mut self) -> Result<u16, TinkerforgeError> {
150        let payload = [0; 0];
151
152        #[allow(unused_variables)]
153        let result = self.device.get(u8::from(AnalogOutBrickletFunction::GetVoltage), &payload).await?;
154        Ok(u16::from_le_byte_slice(result.body()))
155    }
156
157    /// Sets the mode of the analog value. Possible modes:
158    ///
159    /// * 0: Normal Mode (Analog value as set by [`set_voltage`] is applied)
160    /// * 1: 1k Ohm resistor to ground
161    /// * 2: 100k Ohm resistor to ground
162    /// * 3: 500k Ohm resistor to ground
163    ///
164    /// Setting the mode to 0 will result in an output voltage of 0 V. You can jump
165    /// to a higher output voltage directly by calling [`set_voltage`].
166    ///
167    /// Associated constants:
168    /// * ANALOG_OUT_BRICKLET_MODE_ANALOG_VALUE
169    ///	* ANALOG_OUT_BRICKLET_MODE_1K_TO_GROUND
170    ///	* ANALOG_OUT_BRICKLET_MODE_100K_TO_GROUND
171    ///	* ANALOG_OUT_BRICKLET_MODE_500K_TO_GROUND
172    pub async fn set_mode(&mut self, mode: u8) -> Result<(), TinkerforgeError> {
173        let mut payload = [0; 1];
174        mode.write_to_slice(&mut payload[0..1]);
175
176        #[allow(unused_variables)]
177        let result = self.device.set(u8::from(AnalogOutBrickletFunction::SetMode), &payload).await?;
178        Ok(())
179    }
180
181    /// Returns the mode as set by [`set_mode`].
182    ///
183    /// Associated constants:
184    /// * ANALOG_OUT_BRICKLET_MODE_ANALOG_VALUE
185    ///	* ANALOG_OUT_BRICKLET_MODE_1K_TO_GROUND
186    ///	* ANALOG_OUT_BRICKLET_MODE_100K_TO_GROUND
187    ///	* ANALOG_OUT_BRICKLET_MODE_500K_TO_GROUND
188    pub async fn get_mode(&mut self) -> Result<u8, TinkerforgeError> {
189        let payload = [0; 0];
190
191        #[allow(unused_variables)]
192        let result = self.device.get(u8::from(AnalogOutBrickletFunction::GetMode), &payload).await?;
193        Ok(u8::from_le_byte_slice(result.body()))
194    }
195
196    /// Returns the UID, the UID where the Bricklet is connected to,
197    /// the position, the hardware and firmware version as well as the
198    /// device identifier.
199    ///
200    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
201    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
202    /// position 'z'.
203    ///
204    /// The device identifier numbers can be found [here](device_identifier).
205    /// |device_identifier_constant|
206    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
207        let payload = [0; 0];
208
209        #[allow(unused_variables)]
210        let result = self.device.get(u8::from(AnalogOutBrickletFunction::GetIdentity), &payload).await?;
211        Ok(Identity::from_le_byte_slice(result.body()))
212    }
213}