tinkerforge_async/bindings/
industrial_dual_0_20ma_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 IndustrialDual020maBrickletFunction {
24 GetCurrent,
25 SetCurrentCallbackPeriod,
26 GetCurrentCallbackPeriod,
27 SetCurrentCallbackThreshold,
28 GetCurrentCallbackThreshold,
29 SetDebouncePeriod,
30 GetDebouncePeriod,
31 SetSampleRate,
32 GetSampleRate,
33 GetIdentity,
34 CallbackCurrent,
35 CallbackCurrentReached,
36}
37impl From<IndustrialDual020maBrickletFunction> for u8 {
38 fn from(fun: IndustrialDual020maBrickletFunction) -> Self {
39 match fun {
40 IndustrialDual020maBrickletFunction::GetCurrent => 1,
41 IndustrialDual020maBrickletFunction::SetCurrentCallbackPeriod => 2,
42 IndustrialDual020maBrickletFunction::GetCurrentCallbackPeriod => 3,
43 IndustrialDual020maBrickletFunction::SetCurrentCallbackThreshold => 4,
44 IndustrialDual020maBrickletFunction::GetCurrentCallbackThreshold => 5,
45 IndustrialDual020maBrickletFunction::SetDebouncePeriod => 6,
46 IndustrialDual020maBrickletFunction::GetDebouncePeriod => 7,
47 IndustrialDual020maBrickletFunction::SetSampleRate => 8,
48 IndustrialDual020maBrickletFunction::GetSampleRate => 9,
49 IndustrialDual020maBrickletFunction::GetIdentity => 255,
50 IndustrialDual020maBrickletFunction::CallbackCurrent => 10,
51 IndustrialDual020maBrickletFunction::CallbackCurrentReached => 11,
52 }
53 }
54}
55pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
56pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
57pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
58pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
59pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
60pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_240_SPS: u8 = 0;
61pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_60_SPS: u8 = 1;
62pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_15_SPS: u8 = 2;
63pub const INDUSTRIAL_DUAL_0_20MA_BRICKLET_SAMPLE_RATE_4_SPS: u8 = 3;
64
65#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
66pub struct CurrentCallbackThreshold {
67 pub option: char,
68 pub min: i32,
69 pub max: i32,
70}
71impl FromByteSlice for CurrentCallbackThreshold {
72 fn bytes_expected() -> usize {
73 9
74 }
75 fn from_le_byte_slice(bytes: &[u8]) -> CurrentCallbackThreshold {
76 CurrentCallbackThreshold {
77 option: <char>::from_le_byte_slice(&bytes[0..1]),
78 min: <i32>::from_le_byte_slice(&bytes[1..5]),
79 max: <i32>::from_le_byte_slice(&bytes[5..9]),
80 }
81 }
82}
83
84#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
85pub struct CurrentEvent {
86 pub sensor: u8,
87 pub current: i32,
88}
89impl FromByteSlice for CurrentEvent {
90 fn bytes_expected() -> usize {
91 5
92 }
93 fn from_le_byte_slice(bytes: &[u8]) -> CurrentEvent {
94 CurrentEvent { sensor: <u8>::from_le_byte_slice(&bytes[0..1]), current: <i32>::from_le_byte_slice(&bytes[1..5]) }
95 }
96}
97
98#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
99pub struct CurrentReachedEvent {
100 pub sensor: u8,
101 pub current: i32,
102}
103impl FromByteSlice for CurrentReachedEvent {
104 fn bytes_expected() -> usize {
105 5
106 }
107 fn from_le_byte_slice(bytes: &[u8]) -> CurrentReachedEvent {
108 CurrentReachedEvent { sensor: <u8>::from_le_byte_slice(&bytes[0..1]), current: <i32>::from_le_byte_slice(&bytes[1..5]) }
109 }
110}
111
112#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
113pub struct Identity {
114 pub uid: String,
115 pub connected_uid: String,
116 pub position: char,
117 pub hardware_version: [u8; 3],
118 pub firmware_version: [u8; 3],
119 pub device_identifier: u16,
120}
121impl FromByteSlice for Identity {
122 fn bytes_expected() -> usize {
123 25
124 }
125 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
126 Identity {
127 uid: <String>::from_le_byte_slice(&bytes[0..8]),
128 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
129 position: <char>::from_le_byte_slice(&bytes[16..17]),
130 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
131 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
132 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
133 }
134 }
135}
136
137#[derive(Clone)]
139pub struct IndustrialDual020maBricklet {
140 device: Device,
141}
142impl IndustrialDual020maBricklet {
143 pub const DEVICE_IDENTIFIER: u16 = 228;
144 pub const DEVICE_DISPLAY_NAME: &'static str = "Industrial Dual 0-20mA Bricklet";
145 pub fn new(uid: Uid, connection: AsyncIpConnection) -> IndustrialDual020maBricklet {
147 let mut result = IndustrialDual020maBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
148 result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::GetCurrent) as usize] =
149 ResponseExpectedFlag::AlwaysTrue;
150 result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::SetCurrentCallbackPeriod) as usize] =
151 ResponseExpectedFlag::True;
152 result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::GetCurrentCallbackPeriod) as usize] =
153 ResponseExpectedFlag::AlwaysTrue;
154 result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::SetCurrentCallbackThreshold) as usize] =
155 ResponseExpectedFlag::True;
156 result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::GetCurrentCallbackThreshold) as usize] =
157 ResponseExpectedFlag::AlwaysTrue;
158 result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::SetDebouncePeriod) as usize] =
159 ResponseExpectedFlag::True;
160 result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::GetDebouncePeriod) as usize] =
161 ResponseExpectedFlag::AlwaysTrue;
162 result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::SetSampleRate) as usize] =
163 ResponseExpectedFlag::False;
164 result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::GetSampleRate) as usize] =
165 ResponseExpectedFlag::AlwaysTrue;
166 result.device.response_expected[u8::from(IndustrialDual020maBrickletFunction::GetIdentity) as usize] =
167 ResponseExpectedFlag::AlwaysTrue;
168 result
169 }
170
171 pub fn get_response_expected(&mut self, fun: IndustrialDual020maBrickletFunction) -> Result<bool, GetResponseExpectedError> {
186 self.device.get_response_expected(u8::from(fun))
187 }
188
189 pub fn set_response_expected(
198 &mut self,
199 fun: IndustrialDual020maBrickletFunction,
200 response_expected: bool,
201 ) -> Result<(), SetResponseExpectedError> {
202 self.device.set_response_expected(u8::from(fun), response_expected)
203 }
204
205 pub fn set_response_expected_all(&mut self, response_expected: bool) {
207 self.device.set_response_expected_all(response_expected)
208 }
209
210 pub fn get_api_version(&self) -> [u8; 3] {
213 self.device.api_version
214 }
215
216 pub async fn get_current_callback_receiver(&mut self) -> impl Stream<Item = CurrentEvent> {
226 self.device
227 .get_callback_receiver(u8::from(IndustrialDual020maBrickletFunction::CallbackCurrent))
228 .await
229 .map(|p| CurrentEvent::from_le_byte_slice(p.body()))
230 }
231
232 pub async fn get_current_reached_callback_receiver(&mut self) -> impl Stream<Item = CurrentReachedEvent> {
239 self.device
240 .get_callback_receiver(u8::from(IndustrialDual020maBrickletFunction::CallbackCurrentReached))
241 .await
242 .map(|p| CurrentReachedEvent::from_le_byte_slice(p.body()))
243 }
244
245 pub async fn get_current(&mut self, sensor: u8) -> Result<i32, TinkerforgeError> {
258 let mut payload = [0; 1];
259 sensor.write_to_slice(&mut payload[0..1]);
260
261 #[allow(unused_variables)]
262 let result = self.device.get(u8::from(IndustrialDual020maBrickletFunction::GetCurrent), &payload).await?;
263 Ok(i32::from_le_byte_slice(result.body()))
264 }
265
266 pub async fn set_current_callback_period(&mut self, sensor: u8, period: u32) -> Result<(), TinkerforgeError> {
272 let mut payload = [0; 5];
273 sensor.write_to_slice(&mut payload[0..1]);
274 period.write_to_slice(&mut payload[1..5]);
275
276 #[allow(unused_variables)]
277 let result = self.device.set(u8::from(IndustrialDual020maBrickletFunction::SetCurrentCallbackPeriod), &payload).await?;
278 Ok(())
279 }
280
281 pub async fn get_current_callback_period(&mut self, sensor: u8) -> Result<u32, TinkerforgeError> {
283 let mut payload = [0; 1];
284 sensor.write_to_slice(&mut payload[0..1]);
285
286 #[allow(unused_variables)]
287 let result = self.device.get(u8::from(IndustrialDual020maBrickletFunction::GetCurrentCallbackPeriod), &payload).await?;
288 Ok(u32::from_le_byte_slice(result.body()))
289 }
290
291 pub async fn set_current_callback_threshold(&mut self, sensor: u8, option: char, min: i32, max: i32) -> Result<(), TinkerforgeError> {
311 let mut payload = [0; 10];
312 sensor.write_to_slice(&mut payload[0..1]);
313 option.write_to_slice(&mut payload[1..2]);
314 min.write_to_slice(&mut payload[2..6]);
315 max.write_to_slice(&mut payload[6..10]);
316
317 #[allow(unused_variables)]
318 let result = self.device.set(u8::from(IndustrialDual020maBrickletFunction::SetCurrentCallbackThreshold), &payload).await?;
319 Ok(())
320 }
321
322 pub async fn get_current_callback_threshold(&mut self, sensor: u8) -> Result<CurrentCallbackThreshold, TinkerforgeError> {
331 let mut payload = [0; 1];
332 sensor.write_to_slice(&mut payload[0..1]);
333
334 #[allow(unused_variables)]
335 let result = self.device.get(u8::from(IndustrialDual020maBrickletFunction::GetCurrentCallbackThreshold), &payload).await?;
336 Ok(CurrentCallbackThreshold::from_le_byte_slice(result.body()))
337 }
338
339 pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
349 let mut payload = [0; 4];
350 debounce.write_to_slice(&mut payload[0..4]);
351
352 #[allow(unused_variables)]
353 let result = self.device.set(u8::from(IndustrialDual020maBrickletFunction::SetDebouncePeriod), &payload).await?;
354 Ok(())
355 }
356
357 pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
359 let payload = [0; 0];
360
361 #[allow(unused_variables)]
362 let result = self.device.get(u8::from(IndustrialDual020maBrickletFunction::GetDebouncePeriod), &payload).await?;
363 Ok(u32::from_le_byte_slice(result.body()))
364 }
365
366 pub async fn set_sample_rate(&mut self, rate: u8) -> Result<(), TinkerforgeError> {
382 let mut payload = [0; 1];
383 rate.write_to_slice(&mut payload[0..1]);
384
385 #[allow(unused_variables)]
386 let result = self.device.set(u8::from(IndustrialDual020maBrickletFunction::SetSampleRate), &payload).await?;
387 Ok(())
388 }
389
390 pub async fn get_sample_rate(&mut self) -> Result<u8, TinkerforgeError> {
398 let payload = [0; 0];
399
400 #[allow(unused_variables)]
401 let result = self.device.get(u8::from(IndustrialDual020maBrickletFunction::GetSampleRate), &payload).await?;
402 Ok(u8::from_le_byte_slice(result.body()))
403 }
404
405 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
416 let payload = [0; 0];
417
418 #[allow(unused_variables)]
419 let result = self.device.get(u8::from(IndustrialDual020maBrickletFunction::GetIdentity), &payload).await?;
420 Ok(Identity::from_le_byte_slice(result.body()))
421 }
422}