tinkerforge_async/bindings/
industrial_analog_out_bricklet.rs1#[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#[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 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 pub fn get_response_expected(&mut self, fun: IndustrialAnalogOutBrickletFunction) -> Result<bool, GetResponseExpectedError> {
141 self.device.get_response_expected(u8::from(fun))
142 }
143
144 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 pub fn set_response_expected_all(&mut self, response_expected: bool) {
162 self.device.set_response_expected_all(response_expected)
163 }
164
165 pub fn get_api_version(&self) -> [u8; 3] {
168 self.device.api_version
169 }
170
171 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 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 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 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 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 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 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 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 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 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}