tinkerforge_async/bindings/
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 AnalogInBrickletFunction {
24 GetVoltage,
25 GetAnalogValue,
26 SetVoltageCallbackPeriod,
27 GetVoltageCallbackPeriod,
28 SetAnalogValueCallbackPeriod,
29 GetAnalogValueCallbackPeriod,
30 SetVoltageCallbackThreshold,
31 GetVoltageCallbackThreshold,
32 SetAnalogValueCallbackThreshold,
33 GetAnalogValueCallbackThreshold,
34 SetDebouncePeriod,
35 GetDebouncePeriod,
36 SetRange,
37 GetRange,
38 SetAveraging,
39 GetAveraging,
40 GetIdentity,
41 CallbackVoltage,
42 CallbackAnalogValue,
43 CallbackVoltageReached,
44 CallbackAnalogValueReached,
45}
46impl From<AnalogInBrickletFunction> for u8 {
47 fn from(fun: AnalogInBrickletFunction) -> Self {
48 match fun {
49 AnalogInBrickletFunction::GetVoltage => 1,
50 AnalogInBrickletFunction::GetAnalogValue => 2,
51 AnalogInBrickletFunction::SetVoltageCallbackPeriod => 3,
52 AnalogInBrickletFunction::GetVoltageCallbackPeriod => 4,
53 AnalogInBrickletFunction::SetAnalogValueCallbackPeriod => 5,
54 AnalogInBrickletFunction::GetAnalogValueCallbackPeriod => 6,
55 AnalogInBrickletFunction::SetVoltageCallbackThreshold => 7,
56 AnalogInBrickletFunction::GetVoltageCallbackThreshold => 8,
57 AnalogInBrickletFunction::SetAnalogValueCallbackThreshold => 9,
58 AnalogInBrickletFunction::GetAnalogValueCallbackThreshold => 10,
59 AnalogInBrickletFunction::SetDebouncePeriod => 11,
60 AnalogInBrickletFunction::GetDebouncePeriod => 12,
61 AnalogInBrickletFunction::SetRange => 17,
62 AnalogInBrickletFunction::GetRange => 18,
63 AnalogInBrickletFunction::SetAveraging => 19,
64 AnalogInBrickletFunction::GetAveraging => 20,
65 AnalogInBrickletFunction::GetIdentity => 255,
66 AnalogInBrickletFunction::CallbackVoltage => 13,
67 AnalogInBrickletFunction::CallbackAnalogValue => 14,
68 AnalogInBrickletFunction::CallbackVoltageReached => 15,
69 AnalogInBrickletFunction::CallbackAnalogValueReached => 16,
70 }
71 }
72}
73pub const ANALOG_IN_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
74pub const ANALOG_IN_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
75pub const ANALOG_IN_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
76pub const ANALOG_IN_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
77pub const ANALOG_IN_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
78pub const ANALOG_IN_BRICKLET_RANGE_AUTOMATIC: u8 = 0;
79pub const ANALOG_IN_BRICKLET_RANGE_UP_TO_6V: u8 = 1;
80pub const ANALOG_IN_BRICKLET_RANGE_UP_TO_10V: u8 = 2;
81pub const ANALOG_IN_BRICKLET_RANGE_UP_TO_36V: u8 = 3;
82pub const ANALOG_IN_BRICKLET_RANGE_UP_TO_45V: u8 = 4;
83pub const ANALOG_IN_BRICKLET_RANGE_UP_TO_3V: u8 = 5;
84
85#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
86pub struct VoltageCallbackThreshold {
87 pub option: char,
88 pub min: u16,
89 pub max: u16,
90}
91impl FromByteSlice for VoltageCallbackThreshold {
92 fn bytes_expected() -> usize {
93 5
94 }
95 fn from_le_byte_slice(bytes: &[u8]) -> VoltageCallbackThreshold {
96 VoltageCallbackThreshold {
97 option: <char>::from_le_byte_slice(&bytes[0..1]),
98 min: <u16>::from_le_byte_slice(&bytes[1..3]),
99 max: <u16>::from_le_byte_slice(&bytes[3..5]),
100 }
101 }
102}
103
104#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
105pub struct AnalogValueCallbackThreshold {
106 pub option: char,
107 pub min: u16,
108 pub max: u16,
109}
110impl FromByteSlice for AnalogValueCallbackThreshold {
111 fn bytes_expected() -> usize {
112 5
113 }
114 fn from_le_byte_slice(bytes: &[u8]) -> AnalogValueCallbackThreshold {
115 AnalogValueCallbackThreshold {
116 option: <char>::from_le_byte_slice(&bytes[0..1]),
117 min: <u16>::from_le_byte_slice(&bytes[1..3]),
118 max: <u16>::from_le_byte_slice(&bytes[3..5]),
119 }
120 }
121}
122
123#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
124pub struct Identity {
125 pub uid: String,
126 pub connected_uid: String,
127 pub position: char,
128 pub hardware_version: [u8; 3],
129 pub firmware_version: [u8; 3],
130 pub device_identifier: u16,
131}
132impl FromByteSlice for Identity {
133 fn bytes_expected() -> usize {
134 25
135 }
136 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
137 Identity {
138 uid: <String>::from_le_byte_slice(&bytes[0..8]),
139 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
140 position: <char>::from_le_byte_slice(&bytes[16..17]),
141 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
142 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
143 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
144 }
145 }
146}
147
148#[derive(Clone)]
150pub struct AnalogInBricklet {
151 device: Device,
152}
153impl AnalogInBricklet {
154 pub const DEVICE_IDENTIFIER: u16 = 219;
155 pub const DEVICE_DISPLAY_NAME: &'static str = "Analog In Bricklet";
156 pub fn new(uid: Uid, connection: AsyncIpConnection) -> AnalogInBricklet {
158 let mut result = AnalogInBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
159 result.device.response_expected[u8::from(AnalogInBrickletFunction::GetVoltage) as usize] = ResponseExpectedFlag::AlwaysTrue;
160 result.device.response_expected[u8::from(AnalogInBrickletFunction::GetAnalogValue) as usize] = ResponseExpectedFlag::AlwaysTrue;
161 result.device.response_expected[u8::from(AnalogInBrickletFunction::SetVoltageCallbackPeriod) as usize] = ResponseExpectedFlag::True;
162 result.device.response_expected[u8::from(AnalogInBrickletFunction::GetVoltageCallbackPeriod) as usize] =
163 ResponseExpectedFlag::AlwaysTrue;
164 result.device.response_expected[u8::from(AnalogInBrickletFunction::SetAnalogValueCallbackPeriod) as usize] =
165 ResponseExpectedFlag::True;
166 result.device.response_expected[u8::from(AnalogInBrickletFunction::GetAnalogValueCallbackPeriod) as usize] =
167 ResponseExpectedFlag::AlwaysTrue;
168 result.device.response_expected[u8::from(AnalogInBrickletFunction::SetVoltageCallbackThreshold) as usize] =
169 ResponseExpectedFlag::True;
170 result.device.response_expected[u8::from(AnalogInBrickletFunction::GetVoltageCallbackThreshold) as usize] =
171 ResponseExpectedFlag::AlwaysTrue;
172 result.device.response_expected[u8::from(AnalogInBrickletFunction::SetAnalogValueCallbackThreshold) as usize] =
173 ResponseExpectedFlag::True;
174 result.device.response_expected[u8::from(AnalogInBrickletFunction::GetAnalogValueCallbackThreshold) as usize] =
175 ResponseExpectedFlag::AlwaysTrue;
176 result.device.response_expected[u8::from(AnalogInBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
177 result.device.response_expected[u8::from(AnalogInBrickletFunction::GetDebouncePeriod) as usize] = ResponseExpectedFlag::AlwaysTrue;
178 result.device.response_expected[u8::from(AnalogInBrickletFunction::SetRange) as usize] = ResponseExpectedFlag::False;
179 result.device.response_expected[u8::from(AnalogInBrickletFunction::GetRange) as usize] = ResponseExpectedFlag::AlwaysTrue;
180 result.device.response_expected[u8::from(AnalogInBrickletFunction::SetAveraging) as usize] = ResponseExpectedFlag::False;
181 result.device.response_expected[u8::from(AnalogInBrickletFunction::GetAveraging) as usize] = ResponseExpectedFlag::AlwaysTrue;
182 result.device.response_expected[u8::from(AnalogInBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
183 result
184 }
185
186 pub fn get_response_expected(&mut self, fun: AnalogInBrickletFunction) -> Result<bool, GetResponseExpectedError> {
201 self.device.get_response_expected(u8::from(fun))
202 }
203
204 pub fn set_response_expected(
213 &mut self,
214 fun: AnalogInBrickletFunction,
215 response_expected: bool,
216 ) -> Result<(), SetResponseExpectedError> {
217 self.device.set_response_expected(u8::from(fun), response_expected)
218 }
219
220 pub fn set_response_expected_all(&mut self, response_expected: bool) {
222 self.device.set_response_expected_all(response_expected)
223 }
224
225 pub fn get_api_version(&self) -> [u8; 3] {
228 self.device.api_version
229 }
230
231 pub async fn get_voltage_callback_receiver(&mut self) -> impl Stream<Item = u16> {
241 self.device
242 .get_callback_receiver(u8::from(AnalogInBrickletFunction::CallbackVoltage))
243 .await
244 .map(|p| u16::from_le_byte_slice(p.body()))
245 }
246
247 pub async fn get_analog_value_callback_receiver(&mut self) -> impl Stream<Item = u16> {
254 self.device
255 .get_callback_receiver(u8::from(AnalogInBrickletFunction::CallbackAnalogValue))
256 .await
257 .map(|p| u16::from_le_byte_slice(p.body()))
258 }
259
260 pub async fn get_voltage_reached_callback_receiver(&mut self) -> impl Stream<Item = u16> {
267 self.device
268 .get_callback_receiver(u8::from(AnalogInBrickletFunction::CallbackVoltageReached))
269 .await
270 .map(|p| u16::from_le_byte_slice(p.body()))
271 }
272
273 pub async fn get_analog_value_reached_callback_receiver(&mut self) -> impl Stream<Item = u16> {
280 self.device
281 .get_callback_receiver(u8::from(AnalogInBrickletFunction::CallbackAnalogValueReached))
282 .await
283 .map(|p| u16::from_le_byte_slice(p.body()))
284 }
285
286 pub async fn get_voltage(&mut self) -> Result<u16, TinkerforgeError> {
293 let payload = [0; 0];
294
295 #[allow(unused_variables)]
296 let result = self.device.get(u8::from(AnalogInBrickletFunction::GetVoltage), &payload).await?;
297 Ok(u16::from_le_byte_slice(result.body()))
298 }
299
300 pub async fn get_analog_value(&mut self) -> Result<u16, TinkerforgeError> {
312 let payload = [0; 0];
313
314 #[allow(unused_variables)]
315 let result = self.device.get(u8::from(AnalogInBrickletFunction::GetAnalogValue), &payload).await?;
316 Ok(u16::from_le_byte_slice(result.body()))
317 }
318
319 pub async fn set_voltage_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
325 let mut payload = [0; 4];
326 period.write_to_slice(&mut payload[0..4]);
327
328 #[allow(unused_variables)]
329 let result = self.device.set(u8::from(AnalogInBrickletFunction::SetVoltageCallbackPeriod), &payload).await?;
330 Ok(())
331 }
332
333 pub async fn get_voltage_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
335 let payload = [0; 0];
336
337 #[allow(unused_variables)]
338 let result = self.device.get(u8::from(AnalogInBrickletFunction::GetVoltageCallbackPeriod), &payload).await?;
339 Ok(u32::from_le_byte_slice(result.body()))
340 }
341
342 pub async fn set_analog_value_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
348 let mut payload = [0; 4];
349 period.write_to_slice(&mut payload[0..4]);
350
351 #[allow(unused_variables)]
352 let result = self.device.set(u8::from(AnalogInBrickletFunction::SetAnalogValueCallbackPeriod), &payload).await?;
353 Ok(())
354 }
355
356 pub async fn get_analog_value_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
358 let payload = [0; 0];
359
360 #[allow(unused_variables)]
361 let result = self.device.get(u8::from(AnalogInBrickletFunction::GetAnalogValueCallbackPeriod), &payload).await?;
362 Ok(u32::from_le_byte_slice(result.body()))
363 }
364
365 pub async fn set_voltage_callback_threshold(&mut self, option: char, min: u16, max: u16) -> Result<(), TinkerforgeError> {
384 let mut payload = [0; 5];
385 option.write_to_slice(&mut payload[0..1]);
386 min.write_to_slice(&mut payload[1..3]);
387 max.write_to_slice(&mut payload[3..5]);
388
389 #[allow(unused_variables)]
390 let result = self.device.set(u8::from(AnalogInBrickletFunction::SetVoltageCallbackThreshold), &payload).await?;
391 Ok(())
392 }
393
394 pub async fn get_voltage_callback_threshold(&mut self) -> Result<VoltageCallbackThreshold, TinkerforgeError> {
403 let payload = [0; 0];
404
405 #[allow(unused_variables)]
406 let result = self.device.get(u8::from(AnalogInBrickletFunction::GetVoltageCallbackThreshold), &payload).await?;
407 Ok(VoltageCallbackThreshold::from_le_byte_slice(result.body()))
408 }
409
410 pub async fn set_analog_value_callback_threshold(&mut self, option: char, min: u16, max: u16) -> Result<(), TinkerforgeError> {
429 let mut payload = [0; 5];
430 option.write_to_slice(&mut payload[0..1]);
431 min.write_to_slice(&mut payload[1..3]);
432 max.write_to_slice(&mut payload[3..5]);
433
434 #[allow(unused_variables)]
435 let result = self.device.set(u8::from(AnalogInBrickletFunction::SetAnalogValueCallbackThreshold), &payload).await?;
436 Ok(())
437 }
438
439 pub async fn get_analog_value_callback_threshold(&mut self) -> Result<AnalogValueCallbackThreshold, TinkerforgeError> {
448 let payload = [0; 0];
449
450 #[allow(unused_variables)]
451 let result = self.device.get(u8::from(AnalogInBrickletFunction::GetAnalogValueCallbackThreshold), &payload).await?;
452 Ok(AnalogValueCallbackThreshold::from_le_byte_slice(result.body()))
453 }
454
455 pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
467 let mut payload = [0; 4];
468 debounce.write_to_slice(&mut payload[0..4]);
469
470 #[allow(unused_variables)]
471 let result = self.device.set(u8::from(AnalogInBrickletFunction::SetDebouncePeriod), &payload).await?;
472 Ok(())
473 }
474
475 pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
477 let payload = [0; 0];
478
479 #[allow(unused_variables)]
480 let result = self.device.get(u8::from(AnalogInBrickletFunction::GetDebouncePeriod), &payload).await?;
481 Ok(u32::from_le_byte_slice(result.body()))
482 }
483
484 pub async fn set_range(&mut self, range: u8) -> Result<(), TinkerforgeError> {
504 let mut payload = [0; 1];
505 range.write_to_slice(&mut payload[0..1]);
506
507 #[allow(unused_variables)]
508 let result = self.device.set(u8::from(AnalogInBrickletFunction::SetRange), &payload).await?;
509 Ok(())
510 }
511
512 pub async fn get_range(&mut self) -> Result<u8, TinkerforgeError> {
525 let payload = [0; 0];
526
527 #[allow(unused_variables)]
528 let result = self.device.get(u8::from(AnalogInBrickletFunction::GetRange), &payload).await?;
529 Ok(u8::from_le_byte_slice(result.body()))
530 }
531
532 pub async fn set_averaging(&mut self, average: u8) -> Result<(), TinkerforgeError> {
541 let mut payload = [0; 1];
542 average.write_to_slice(&mut payload[0..1]);
543
544 #[allow(unused_variables)]
545 let result = self.device.set(u8::from(AnalogInBrickletFunction::SetAveraging), &payload).await?;
546 Ok(())
547 }
548
549 pub async fn get_averaging(&mut self) -> Result<u8, TinkerforgeError> {
554 let payload = [0; 0];
555
556 #[allow(unused_variables)]
557 let result = self.device.get(u8::from(AnalogInBrickletFunction::GetAveraging), &payload).await?;
558 Ok(u8::from_le_byte_slice(result.body()))
559 }
560
561 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
572 let payload = [0; 0];
573
574 #[allow(unused_variables)]
575 let result = self.device.get(u8::from(AnalogInBrickletFunction::GetIdentity), &payload).await?;
576 Ok(Identity::from_le_byte_slice(result.body()))
577 }
578}