tinkerforge_async/bindings/
industrial_dual_analog_in_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 IndustrialDualAnalogInBrickletFunction {
24 GetVoltage,
25 SetVoltageCallbackPeriod,
26 GetVoltageCallbackPeriod,
27 SetVoltageCallbackThreshold,
28 GetVoltageCallbackThreshold,
29 SetDebouncePeriod,
30 GetDebouncePeriod,
31 SetSampleRate,
32 GetSampleRate,
33 SetCalibration,
34 GetCalibration,
35 GetAdcValues,
36 GetIdentity,
37 CallbackVoltage,
38 CallbackVoltageReached,
39}
40impl From<IndustrialDualAnalogInBrickletFunction> for u8 {
41 fn from(fun: IndustrialDualAnalogInBrickletFunction) -> Self {
42 match fun {
43 IndustrialDualAnalogInBrickletFunction::GetVoltage => 1,
44 IndustrialDualAnalogInBrickletFunction::SetVoltageCallbackPeriod => 2,
45 IndustrialDualAnalogInBrickletFunction::GetVoltageCallbackPeriod => 3,
46 IndustrialDualAnalogInBrickletFunction::SetVoltageCallbackThreshold => 4,
47 IndustrialDualAnalogInBrickletFunction::GetVoltageCallbackThreshold => 5,
48 IndustrialDualAnalogInBrickletFunction::SetDebouncePeriod => 6,
49 IndustrialDualAnalogInBrickletFunction::GetDebouncePeriod => 7,
50 IndustrialDualAnalogInBrickletFunction::SetSampleRate => 8,
51 IndustrialDualAnalogInBrickletFunction::GetSampleRate => 9,
52 IndustrialDualAnalogInBrickletFunction::SetCalibration => 10,
53 IndustrialDualAnalogInBrickletFunction::GetCalibration => 11,
54 IndustrialDualAnalogInBrickletFunction::GetAdcValues => 12,
55 IndustrialDualAnalogInBrickletFunction::GetIdentity => 255,
56 IndustrialDualAnalogInBrickletFunction::CallbackVoltage => 13,
57 IndustrialDualAnalogInBrickletFunction::CallbackVoltageReached => 14,
58 }
59 }
60}
61pub const INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
62pub const INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
63pub const INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
64pub const INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
65pub const INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
66pub const INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_976_SPS: u8 = 0;
67pub const INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_488_SPS: u8 = 1;
68pub const INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_244_SPS: u8 = 2;
69pub const INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_122_SPS: u8 = 3;
70pub const INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_61_SPS: u8 = 4;
71pub const INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_4_SPS: u8 = 5;
72pub const INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_2_SPS: u8 = 6;
73pub const INDUSTRIAL_DUAL_ANALOG_IN_BRICKLET_SAMPLE_RATE_1_SPS: u8 = 7;
74
75#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
76pub struct VoltageCallbackThreshold {
77 pub option: char,
78 pub min: i32,
79 pub max: i32,
80}
81impl FromByteSlice for VoltageCallbackThreshold {
82 fn bytes_expected() -> usize {
83 9
84 }
85 fn from_le_byte_slice(bytes: &[u8]) -> VoltageCallbackThreshold {
86 VoltageCallbackThreshold {
87 option: <char>::from_le_byte_slice(&bytes[0..1]),
88 min: <i32>::from_le_byte_slice(&bytes[1..5]),
89 max: <i32>::from_le_byte_slice(&bytes[5..9]),
90 }
91 }
92}
93
94#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
95pub struct Calibration {
96 pub offset: [i32; 2],
97 pub gain: [i32; 2],
98}
99impl FromByteSlice for Calibration {
100 fn bytes_expected() -> usize {
101 16
102 }
103 fn from_le_byte_slice(bytes: &[u8]) -> Calibration {
104 Calibration { offset: <[i32; 2]>::from_le_byte_slice(&bytes[0..8]), gain: <[i32; 2]>::from_le_byte_slice(&bytes[8..16]) }
105 }
106}
107
108#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
109pub struct VoltageEvent {
110 pub channel: u8,
111 pub voltage: i32,
112}
113impl FromByteSlice for VoltageEvent {
114 fn bytes_expected() -> usize {
115 5
116 }
117 fn from_le_byte_slice(bytes: &[u8]) -> VoltageEvent {
118 VoltageEvent { channel: <u8>::from_le_byte_slice(&bytes[0..1]), voltage: <i32>::from_le_byte_slice(&bytes[1..5]) }
119 }
120}
121
122#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
123pub struct VoltageReachedEvent {
124 pub channel: u8,
125 pub voltage: i32,
126}
127impl FromByteSlice for VoltageReachedEvent {
128 fn bytes_expected() -> usize {
129 5
130 }
131 fn from_le_byte_slice(bytes: &[u8]) -> VoltageReachedEvent {
132 VoltageReachedEvent { channel: <u8>::from_le_byte_slice(&bytes[0..1]), voltage: <i32>::from_le_byte_slice(&bytes[1..5]) }
133 }
134}
135
136#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
137pub struct Identity {
138 pub uid: String,
139 pub connected_uid: String,
140 pub position: char,
141 pub hardware_version: [u8; 3],
142 pub firmware_version: [u8; 3],
143 pub device_identifier: u16,
144}
145impl FromByteSlice for Identity {
146 fn bytes_expected() -> usize {
147 25
148 }
149 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
150 Identity {
151 uid: <String>::from_le_byte_slice(&bytes[0..8]),
152 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
153 position: <char>::from_le_byte_slice(&bytes[16..17]),
154 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
155 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
156 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
157 }
158 }
159}
160
161#[derive(Clone)]
163pub struct IndustrialDualAnalogInBricklet {
164 device: Device,
165}
166impl IndustrialDualAnalogInBricklet {
167 pub const DEVICE_IDENTIFIER: u16 = 249;
168 pub const DEVICE_DISPLAY_NAME: &'static str = "Industrial Dual Analog In Bricklet";
169 pub fn new(uid: Uid, connection: AsyncIpConnection) -> IndustrialDualAnalogInBricklet {
171 let mut result = IndustrialDualAnalogInBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
172 result.device.response_expected[u8::from(IndustrialDualAnalogInBrickletFunction::GetVoltage) as usize] =
173 ResponseExpectedFlag::AlwaysTrue;
174 result.device.response_expected[u8::from(IndustrialDualAnalogInBrickletFunction::SetVoltageCallbackPeriod) as usize] =
175 ResponseExpectedFlag::True;
176 result.device.response_expected[u8::from(IndustrialDualAnalogInBrickletFunction::GetVoltageCallbackPeriod) as usize] =
177 ResponseExpectedFlag::AlwaysTrue;
178 result.device.response_expected[u8::from(IndustrialDualAnalogInBrickletFunction::SetVoltageCallbackThreshold) as usize] =
179 ResponseExpectedFlag::True;
180 result.device.response_expected[u8::from(IndustrialDualAnalogInBrickletFunction::GetVoltageCallbackThreshold) as usize] =
181 ResponseExpectedFlag::AlwaysTrue;
182 result.device.response_expected[u8::from(IndustrialDualAnalogInBrickletFunction::SetDebouncePeriod) as usize] =
183 ResponseExpectedFlag::True;
184 result.device.response_expected[u8::from(IndustrialDualAnalogInBrickletFunction::GetDebouncePeriod) as usize] =
185 ResponseExpectedFlag::AlwaysTrue;
186 result.device.response_expected[u8::from(IndustrialDualAnalogInBrickletFunction::SetSampleRate) as usize] =
187 ResponseExpectedFlag::False;
188 result.device.response_expected[u8::from(IndustrialDualAnalogInBrickletFunction::GetSampleRate) as usize] =
189 ResponseExpectedFlag::AlwaysTrue;
190 result.device.response_expected[u8::from(IndustrialDualAnalogInBrickletFunction::SetCalibration) as usize] =
191 ResponseExpectedFlag::False;
192 result.device.response_expected[u8::from(IndustrialDualAnalogInBrickletFunction::GetCalibration) as usize] =
193 ResponseExpectedFlag::AlwaysTrue;
194 result.device.response_expected[u8::from(IndustrialDualAnalogInBrickletFunction::GetAdcValues) as usize] =
195 ResponseExpectedFlag::AlwaysTrue;
196 result.device.response_expected[u8::from(IndustrialDualAnalogInBrickletFunction::GetIdentity) as usize] =
197 ResponseExpectedFlag::AlwaysTrue;
198 result
199 }
200
201 pub fn get_response_expected(&mut self, fun: IndustrialDualAnalogInBrickletFunction) -> Result<bool, GetResponseExpectedError> {
216 self.device.get_response_expected(u8::from(fun))
217 }
218
219 pub fn set_response_expected(
228 &mut self,
229 fun: IndustrialDualAnalogInBrickletFunction,
230 response_expected: bool,
231 ) -> Result<(), SetResponseExpectedError> {
232 self.device.set_response_expected(u8::from(fun), response_expected)
233 }
234
235 pub fn set_response_expected_all(&mut self, response_expected: bool) {
237 self.device.set_response_expected_all(response_expected)
238 }
239
240 pub fn get_api_version(&self) -> [u8; 3] {
243 self.device.api_version
244 }
245
246 pub async fn get_voltage_callback_receiver(&mut self) -> impl Stream<Item = VoltageEvent> {
256 self.device
257 .get_callback_receiver(u8::from(IndustrialDualAnalogInBrickletFunction::CallbackVoltage))
258 .await
259 .map(|p| VoltageEvent::from_le_byte_slice(p.body()))
260 }
261
262 pub async fn get_voltage_reached_callback_receiver(&mut self) -> impl Stream<Item = VoltageReachedEvent> {
269 self.device
270 .get_callback_receiver(u8::from(IndustrialDualAnalogInBrickletFunction::CallbackVoltageReached))
271 .await
272 .map(|p| VoltageReachedEvent::from_le_byte_slice(p.body()))
273 }
274
275 pub async fn get_voltage(&mut self, channel: u8) -> Result<i32, TinkerforgeError> {
281 let mut payload = [0; 1];
282 channel.write_to_slice(&mut payload[0..1]);
283
284 #[allow(unused_variables)]
285 let result = self.device.get(u8::from(IndustrialDualAnalogInBrickletFunction::GetVoltage), &payload).await?;
286 Ok(i32::from_le_byte_slice(result.body()))
287 }
288
289 pub async fn set_voltage_callback_period(&mut self, channel: u8, period: u32) -> Result<(), TinkerforgeError> {
295 let mut payload = [0; 5];
296 channel.write_to_slice(&mut payload[0..1]);
297 period.write_to_slice(&mut payload[1..5]);
298
299 #[allow(unused_variables)]
300 let result = self.device.set(u8::from(IndustrialDualAnalogInBrickletFunction::SetVoltageCallbackPeriod), &payload).await?;
301 Ok(())
302 }
303
304 pub async fn get_voltage_callback_period(&mut self, channel: u8) -> Result<u32, TinkerforgeError> {
306 let mut payload = [0; 1];
307 channel.write_to_slice(&mut payload[0..1]);
308
309 #[allow(unused_variables)]
310 let result = self.device.get(u8::from(IndustrialDualAnalogInBrickletFunction::GetVoltageCallbackPeriod), &payload).await?;
311 Ok(u32::from_le_byte_slice(result.body()))
312 }
313
314 pub async fn set_voltage_callback_threshold(&mut self, channel: u8, option: char, min: i32, max: i32) -> Result<(), TinkerforgeError> {
334 let mut payload = [0; 10];
335 channel.write_to_slice(&mut payload[0..1]);
336 option.write_to_slice(&mut payload[1..2]);
337 min.write_to_slice(&mut payload[2..6]);
338 max.write_to_slice(&mut payload[6..10]);
339
340 #[allow(unused_variables)]
341 let result = self.device.set(u8::from(IndustrialDualAnalogInBrickletFunction::SetVoltageCallbackThreshold), &payload).await?;
342 Ok(())
343 }
344
345 pub async fn get_voltage_callback_threshold(&mut self, channel: u8) -> Result<VoltageCallbackThreshold, TinkerforgeError> {
354 let mut payload = [0; 1];
355 channel.write_to_slice(&mut payload[0..1]);
356
357 #[allow(unused_variables)]
358 let result = self.device.get(u8::from(IndustrialDualAnalogInBrickletFunction::GetVoltageCallbackThreshold), &payload).await?;
359 Ok(VoltageCallbackThreshold::from_le_byte_slice(result.body()))
360 }
361
362 pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
372 let mut payload = [0; 4];
373 debounce.write_to_slice(&mut payload[0..4]);
374
375 #[allow(unused_variables)]
376 let result = self.device.set(u8::from(IndustrialDualAnalogInBrickletFunction::SetDebouncePeriod), &payload).await?;
377 Ok(())
378 }
379
380 pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
382 let payload = [0; 0];
383
384 #[allow(unused_variables)]
385 let result = self.device.get(u8::from(IndustrialDualAnalogInBrickletFunction::GetDebouncePeriod), &payload).await?;
386 Ok(u32::from_le_byte_slice(result.body()))
387 }
388
389 pub async fn set_sample_rate(&mut self, rate: u8) -> Result<(), TinkerforgeError> {
403 let mut payload = [0; 1];
404 rate.write_to_slice(&mut payload[0..1]);
405
406 #[allow(unused_variables)]
407 let result = self.device.set(u8::from(IndustrialDualAnalogInBrickletFunction::SetSampleRate), &payload).await?;
408 Ok(())
409 }
410
411 pub async fn get_sample_rate(&mut self) -> Result<u8, TinkerforgeError> {
423 let payload = [0; 0];
424
425 #[allow(unused_variables)]
426 let result = self.device.get(u8::from(IndustrialDualAnalogInBrickletFunction::GetSampleRate), &payload).await?;
427 Ok(u8::from_le_byte_slice(result.body()))
428 }
429
430 pub async fn set_calibration(&mut self, offset: &[i32; 2], gain: &[i32; 2]) -> Result<(), TinkerforgeError> {
436 let mut payload = [0; 16];
437 offset.write_to_slice(&mut payload[0..8]);
438 gain.write_to_slice(&mut payload[8..16]);
439
440 #[allow(unused_variables)]
441 let result = self.device.set(u8::from(IndustrialDualAnalogInBrickletFunction::SetCalibration), &payload).await?;
442 Ok(())
443 }
444
445 pub async fn get_calibration(&mut self) -> Result<Calibration, TinkerforgeError> {
447 let payload = [0; 0];
448
449 #[allow(unused_variables)]
450 let result = self.device.get(u8::from(IndustrialDualAnalogInBrickletFunction::GetCalibration), &payload).await?;
451 Ok(Calibration::from_le_byte_slice(result.body()))
452 }
453
454 pub async fn get_adc_values(&mut self) -> Result<Box<[i32; 2]>, TinkerforgeError> {
457 let payload = [0; 0];
458
459 #[allow(unused_variables)]
460 let result = self.device.get(u8::from(IndustrialDualAnalogInBrickletFunction::GetAdcValues), &payload).await?;
461 Ok(Box::<[i32; 2]>::from_le_byte_slice(result.body()))
462 }
463
464 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
475 let payload = [0; 0];
476
477 #[allow(unused_variables)]
478 let result = self.device.get(u8::from(IndustrialDualAnalogInBrickletFunction::GetIdentity), &payload).await?;
479 Ok(Identity::from_le_byte_slice(result.body()))
480 }
481}