tinkerforge_async/bindings/
current25_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 Current25BrickletFunction {
24 GetCurrent,
25 Calibrate,
26 IsOverCurrent,
27 GetAnalogValue,
28 SetCurrentCallbackPeriod,
29 GetCurrentCallbackPeriod,
30 SetAnalogValueCallbackPeriod,
31 GetAnalogValueCallbackPeriod,
32 SetCurrentCallbackThreshold,
33 GetCurrentCallbackThreshold,
34 SetAnalogValueCallbackThreshold,
35 GetAnalogValueCallbackThreshold,
36 SetDebouncePeriod,
37 GetDebouncePeriod,
38 GetIdentity,
39 CallbackCurrent,
40 CallbackAnalogValue,
41 CallbackCurrentReached,
42 CallbackAnalogValueReached,
43 CallbackOverCurrent,
44}
45impl From<Current25BrickletFunction> for u8 {
46 fn from(fun: Current25BrickletFunction) -> Self {
47 match fun {
48 Current25BrickletFunction::GetCurrent => 1,
49 Current25BrickletFunction::Calibrate => 2,
50 Current25BrickletFunction::IsOverCurrent => 3,
51 Current25BrickletFunction::GetAnalogValue => 4,
52 Current25BrickletFunction::SetCurrentCallbackPeriod => 5,
53 Current25BrickletFunction::GetCurrentCallbackPeriod => 6,
54 Current25BrickletFunction::SetAnalogValueCallbackPeriod => 7,
55 Current25BrickletFunction::GetAnalogValueCallbackPeriod => 8,
56 Current25BrickletFunction::SetCurrentCallbackThreshold => 9,
57 Current25BrickletFunction::GetCurrentCallbackThreshold => 10,
58 Current25BrickletFunction::SetAnalogValueCallbackThreshold => 11,
59 Current25BrickletFunction::GetAnalogValueCallbackThreshold => 12,
60 Current25BrickletFunction::SetDebouncePeriod => 13,
61 Current25BrickletFunction::GetDebouncePeriod => 14,
62 Current25BrickletFunction::GetIdentity => 255,
63 Current25BrickletFunction::CallbackCurrent => 15,
64 Current25BrickletFunction::CallbackAnalogValue => 16,
65 Current25BrickletFunction::CallbackCurrentReached => 17,
66 Current25BrickletFunction::CallbackAnalogValueReached => 18,
67 Current25BrickletFunction::CallbackOverCurrent => 19,
68 }
69 }
70}
71pub const CURRENT25_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
72pub const CURRENT25_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
73pub const CURRENT25_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
74pub const CURRENT25_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
75pub const CURRENT25_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
76
77#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
78pub struct CurrentCallbackThreshold {
79 pub option: char,
80 pub min: i16,
81 pub max: i16,
82}
83impl FromByteSlice for CurrentCallbackThreshold {
84 fn bytes_expected() -> usize {
85 5
86 }
87 fn from_le_byte_slice(bytes: &[u8]) -> CurrentCallbackThreshold {
88 CurrentCallbackThreshold {
89 option: <char>::from_le_byte_slice(&bytes[0..1]),
90 min: <i16>::from_le_byte_slice(&bytes[1..3]),
91 max: <i16>::from_le_byte_slice(&bytes[3..5]),
92 }
93 }
94}
95
96#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
97pub struct AnalogValueCallbackThreshold {
98 pub option: char,
99 pub min: u16,
100 pub max: u16,
101}
102impl FromByteSlice for AnalogValueCallbackThreshold {
103 fn bytes_expected() -> usize {
104 5
105 }
106 fn from_le_byte_slice(bytes: &[u8]) -> AnalogValueCallbackThreshold {
107 AnalogValueCallbackThreshold {
108 option: <char>::from_le_byte_slice(&bytes[0..1]),
109 min: <u16>::from_le_byte_slice(&bytes[1..3]),
110 max: <u16>::from_le_byte_slice(&bytes[3..5]),
111 }
112 }
113}
114
115#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
116pub struct Identity {
117 pub uid: String,
118 pub connected_uid: String,
119 pub position: char,
120 pub hardware_version: [u8; 3],
121 pub firmware_version: [u8; 3],
122 pub device_identifier: u16,
123}
124impl FromByteSlice for Identity {
125 fn bytes_expected() -> usize {
126 25
127 }
128 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
129 Identity {
130 uid: <String>::from_le_byte_slice(&bytes[0..8]),
131 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
132 position: <char>::from_le_byte_slice(&bytes[16..17]),
133 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
134 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
135 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
136 }
137 }
138}
139
140#[derive(Clone)]
142pub struct Current25Bricklet {
143 device: Device,
144}
145impl Current25Bricklet {
146 pub const DEVICE_IDENTIFIER: u16 = 24;
147 pub const DEVICE_DISPLAY_NAME: &'static str = "Current25 Bricklet";
148 pub fn new(uid: Uid, connection: AsyncIpConnection) -> Current25Bricklet {
150 let mut result = Current25Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
151 result.device.response_expected[u8::from(Current25BrickletFunction::GetCurrent) as usize] = ResponseExpectedFlag::AlwaysTrue;
152 result.device.response_expected[u8::from(Current25BrickletFunction::Calibrate) as usize] = ResponseExpectedFlag::False;
153 result.device.response_expected[u8::from(Current25BrickletFunction::IsOverCurrent) as usize] = ResponseExpectedFlag::AlwaysTrue;
154 result.device.response_expected[u8::from(Current25BrickletFunction::GetAnalogValue) as usize] = ResponseExpectedFlag::AlwaysTrue;
155 result.device.response_expected[u8::from(Current25BrickletFunction::SetCurrentCallbackPeriod) as usize] =
156 ResponseExpectedFlag::True;
157 result.device.response_expected[u8::from(Current25BrickletFunction::GetCurrentCallbackPeriod) as usize] =
158 ResponseExpectedFlag::AlwaysTrue;
159 result.device.response_expected[u8::from(Current25BrickletFunction::SetAnalogValueCallbackPeriod) as usize] =
160 ResponseExpectedFlag::True;
161 result.device.response_expected[u8::from(Current25BrickletFunction::GetAnalogValueCallbackPeriod) as usize] =
162 ResponseExpectedFlag::AlwaysTrue;
163 result.device.response_expected[u8::from(Current25BrickletFunction::SetCurrentCallbackThreshold) as usize] =
164 ResponseExpectedFlag::True;
165 result.device.response_expected[u8::from(Current25BrickletFunction::GetCurrentCallbackThreshold) as usize] =
166 ResponseExpectedFlag::AlwaysTrue;
167 result.device.response_expected[u8::from(Current25BrickletFunction::SetAnalogValueCallbackThreshold) as usize] =
168 ResponseExpectedFlag::True;
169 result.device.response_expected[u8::from(Current25BrickletFunction::GetAnalogValueCallbackThreshold) as usize] =
170 ResponseExpectedFlag::AlwaysTrue;
171 result.device.response_expected[u8::from(Current25BrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
172 result.device.response_expected[u8::from(Current25BrickletFunction::GetDebouncePeriod) as usize] = ResponseExpectedFlag::AlwaysTrue;
173 result.device.response_expected[u8::from(Current25BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
174 result
175 }
176
177 pub fn get_response_expected(&mut self, fun: Current25BrickletFunction) -> Result<bool, GetResponseExpectedError> {
192 self.device.get_response_expected(u8::from(fun))
193 }
194
195 pub fn set_response_expected(
204 &mut self,
205 fun: Current25BrickletFunction,
206 response_expected: bool,
207 ) -> Result<(), SetResponseExpectedError> {
208 self.device.set_response_expected(u8::from(fun), response_expected)
209 }
210
211 pub fn set_response_expected_all(&mut self, response_expected: bool) {
213 self.device.set_response_expected_all(response_expected)
214 }
215
216 pub fn get_api_version(&self) -> [u8; 3] {
219 self.device.api_version
220 }
221
222 pub async fn get_current_callback_receiver(&mut self) -> impl Stream<Item = i16> {
232 self.device
233 .get_callback_receiver(u8::from(Current25BrickletFunction::CallbackCurrent))
234 .await
235 .map(|p| i16::from_le_byte_slice(p.body()))
236 }
237
238 pub async fn get_analog_value_callback_receiver(&mut self) -> impl Stream<Item = u16> {
245 self.device
246 .get_callback_receiver(u8::from(Current25BrickletFunction::CallbackAnalogValue))
247 .await
248 .map(|p| u16::from_le_byte_slice(p.body()))
249 }
250
251 pub async fn get_current_reached_callback_receiver(&mut self) -> impl Stream<Item = i16> {
258 self.device
259 .get_callback_receiver(u8::from(Current25BrickletFunction::CallbackCurrentReached))
260 .await
261 .map(|p| i16::from_le_byte_slice(p.body()))
262 }
263
264 pub async fn get_analog_value_reached_callback_receiver(&mut self) -> impl Stream<Item = u16> {
271 self.device
272 .get_callback_receiver(u8::from(Current25BrickletFunction::CallbackAnalogValueReached))
273 .await
274 .map(|p| u16::from_le_byte_slice(p.body()))
275 }
276
277 pub async fn get_over_current_callback_receiver(&mut self) -> impl Stream<Item = ()> {
280 self.device.get_callback_receiver(u8::from(Current25BrickletFunction::CallbackOverCurrent)).await.map(|_p| ())
281 }
282
283 pub async fn get_current(&mut self) -> Result<i16, TinkerforgeError> {
289 let payload = [0; 0];
290
291 #[allow(unused_variables)]
292 let result = self.device.get(u8::from(Current25BrickletFunction::GetCurrent), &payload).await?;
293 Ok(i16::from_le_byte_slice(result.body()))
294 }
295
296 pub async fn calibrate(&mut self) -> Result<(), TinkerforgeError> {
308 let payload = [0; 0];
309
310 #[allow(unused_variables)]
311 let result = self.device.set(u8::from(Current25BrickletFunction::Calibrate), &payload).await?;
312 Ok(())
313 }
314
315 pub async fn is_over_current(&mut self) -> Result<bool, TinkerforgeError> {
320 let payload = [0; 0];
321
322 #[allow(unused_variables)]
323 let result = self.device.get(u8::from(Current25BrickletFunction::IsOverCurrent), &payload).await?;
324 Ok(bool::from_le_byte_slice(result.body()))
325 }
326
327 pub async fn get_analog_value(&mut self) -> Result<u16, TinkerforgeError> {
339 let payload = [0; 0];
340
341 #[allow(unused_variables)]
342 let result = self.device.get(u8::from(Current25BrickletFunction::GetAnalogValue), &payload).await?;
343 Ok(u16::from_le_byte_slice(result.body()))
344 }
345
346 pub async fn set_current_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
352 let mut payload = [0; 4];
353 period.write_to_slice(&mut payload[0..4]);
354
355 #[allow(unused_variables)]
356 let result = self.device.set(u8::from(Current25BrickletFunction::SetCurrentCallbackPeriod), &payload).await?;
357 Ok(())
358 }
359
360 pub async fn get_current_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
362 let payload = [0; 0];
363
364 #[allow(unused_variables)]
365 let result = self.device.get(u8::from(Current25BrickletFunction::GetCurrentCallbackPeriod), &payload).await?;
366 Ok(u32::from_le_byte_slice(result.body()))
367 }
368
369 pub async fn set_analog_value_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
375 let mut payload = [0; 4];
376 period.write_to_slice(&mut payload[0..4]);
377
378 #[allow(unused_variables)]
379 let result = self.device.set(u8::from(Current25BrickletFunction::SetAnalogValueCallbackPeriod), &payload).await?;
380 Ok(())
381 }
382
383 pub async fn get_analog_value_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
385 let payload = [0; 0];
386
387 #[allow(unused_variables)]
388 let result = self.device.get(u8::from(Current25BrickletFunction::GetAnalogValueCallbackPeriod), &payload).await?;
389 Ok(u32::from_le_byte_slice(result.body()))
390 }
391
392 pub async fn set_current_callback_threshold(&mut self, option: char, min: i16, max: i16) -> Result<(), TinkerforgeError> {
411 let mut payload = [0; 5];
412 option.write_to_slice(&mut payload[0..1]);
413 min.write_to_slice(&mut payload[1..3]);
414 max.write_to_slice(&mut payload[3..5]);
415
416 #[allow(unused_variables)]
417 let result = self.device.set(u8::from(Current25BrickletFunction::SetCurrentCallbackThreshold), &payload).await?;
418 Ok(())
419 }
420
421 pub async fn get_current_callback_threshold(&mut self) -> Result<CurrentCallbackThreshold, TinkerforgeError> {
430 let payload = [0; 0];
431
432 #[allow(unused_variables)]
433 let result = self.device.get(u8::from(Current25BrickletFunction::GetCurrentCallbackThreshold), &payload).await?;
434 Ok(CurrentCallbackThreshold::from_le_byte_slice(result.body()))
435 }
436
437 pub async fn set_analog_value_callback_threshold(&mut self, option: char, min: u16, max: u16) -> Result<(), TinkerforgeError> {
456 let mut payload = [0; 5];
457 option.write_to_slice(&mut payload[0..1]);
458 min.write_to_slice(&mut payload[1..3]);
459 max.write_to_slice(&mut payload[3..5]);
460
461 #[allow(unused_variables)]
462 let result = self.device.set(u8::from(Current25BrickletFunction::SetAnalogValueCallbackThreshold), &payload).await?;
463 Ok(())
464 }
465
466 pub async fn get_analog_value_callback_threshold(&mut self) -> Result<AnalogValueCallbackThreshold, TinkerforgeError> {
475 let payload = [0; 0];
476
477 #[allow(unused_variables)]
478 let result = self.device.get(u8::from(Current25BrickletFunction::GetAnalogValueCallbackThreshold), &payload).await?;
479 Ok(AnalogValueCallbackThreshold::from_le_byte_slice(result.body()))
480 }
481
482 pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
494 let mut payload = [0; 4];
495 debounce.write_to_slice(&mut payload[0..4]);
496
497 #[allow(unused_variables)]
498 let result = self.device.set(u8::from(Current25BrickletFunction::SetDebouncePeriod), &payload).await?;
499 Ok(())
500 }
501
502 pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
504 let payload = [0; 0];
505
506 #[allow(unused_variables)]
507 let result = self.device.get(u8::from(Current25BrickletFunction::GetDebouncePeriod), &payload).await?;
508 Ok(u32::from_le_byte_slice(result.body()))
509 }
510
511 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
522 let payload = [0; 0];
523
524 #[allow(unused_variables)]
525 let result = self.device.get(u8::from(Current25BrickletFunction::GetIdentity), &payload).await?;
526 Ok(Identity::from_le_byte_slice(result.body()))
527 }
528}