tinkerforge_async/bindings/
linear_poti_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 LinearPotiBrickletFunction {
24 GetPosition,
25 GetAnalogValue,
26 SetPositionCallbackPeriod,
27 GetPositionCallbackPeriod,
28 SetAnalogValueCallbackPeriod,
29 GetAnalogValueCallbackPeriod,
30 SetPositionCallbackThreshold,
31 GetPositionCallbackThreshold,
32 SetAnalogValueCallbackThreshold,
33 GetAnalogValueCallbackThreshold,
34 SetDebouncePeriod,
35 GetDebouncePeriod,
36 GetIdentity,
37 CallbackPosition,
38 CallbackAnalogValue,
39 CallbackPositionReached,
40 CallbackAnalogValueReached,
41}
42impl From<LinearPotiBrickletFunction> for u8 {
43 fn from(fun: LinearPotiBrickletFunction) -> Self {
44 match fun {
45 LinearPotiBrickletFunction::GetPosition => 1,
46 LinearPotiBrickletFunction::GetAnalogValue => 2,
47 LinearPotiBrickletFunction::SetPositionCallbackPeriod => 3,
48 LinearPotiBrickletFunction::GetPositionCallbackPeriod => 4,
49 LinearPotiBrickletFunction::SetAnalogValueCallbackPeriod => 5,
50 LinearPotiBrickletFunction::GetAnalogValueCallbackPeriod => 6,
51 LinearPotiBrickletFunction::SetPositionCallbackThreshold => 7,
52 LinearPotiBrickletFunction::GetPositionCallbackThreshold => 8,
53 LinearPotiBrickletFunction::SetAnalogValueCallbackThreshold => 9,
54 LinearPotiBrickletFunction::GetAnalogValueCallbackThreshold => 10,
55 LinearPotiBrickletFunction::SetDebouncePeriod => 11,
56 LinearPotiBrickletFunction::GetDebouncePeriod => 12,
57 LinearPotiBrickletFunction::GetIdentity => 255,
58 LinearPotiBrickletFunction::CallbackPosition => 13,
59 LinearPotiBrickletFunction::CallbackAnalogValue => 14,
60 LinearPotiBrickletFunction::CallbackPositionReached => 15,
61 LinearPotiBrickletFunction::CallbackAnalogValueReached => 16,
62 }
63 }
64}
65pub const LINEAR_POTI_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
66pub const LINEAR_POTI_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
67pub const LINEAR_POTI_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
68pub const LINEAR_POTI_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
69pub const LINEAR_POTI_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
70
71#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
72pub struct PositionCallbackThreshold {
73 pub option: char,
74 pub min: u16,
75 pub max: u16,
76}
77impl FromByteSlice for PositionCallbackThreshold {
78 fn bytes_expected() -> usize {
79 5
80 }
81 fn from_le_byte_slice(bytes: &[u8]) -> PositionCallbackThreshold {
82 PositionCallbackThreshold {
83 option: <char>::from_le_byte_slice(&bytes[0..1]),
84 min: <u16>::from_le_byte_slice(&bytes[1..3]),
85 max: <u16>::from_le_byte_slice(&bytes[3..5]),
86 }
87 }
88}
89
90#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
91pub struct AnalogValueCallbackThreshold {
92 pub option: char,
93 pub min: u16,
94 pub max: u16,
95}
96impl FromByteSlice for AnalogValueCallbackThreshold {
97 fn bytes_expected() -> usize {
98 5
99 }
100 fn from_le_byte_slice(bytes: &[u8]) -> AnalogValueCallbackThreshold {
101 AnalogValueCallbackThreshold {
102 option: <char>::from_le_byte_slice(&bytes[0..1]),
103 min: <u16>::from_le_byte_slice(&bytes[1..3]),
104 max: <u16>::from_le_byte_slice(&bytes[3..5]),
105 }
106 }
107}
108
109#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
110pub struct Identity {
111 pub uid: String,
112 pub connected_uid: String,
113 pub position: char,
114 pub hardware_version: [u8; 3],
115 pub firmware_version: [u8; 3],
116 pub device_identifier: u16,
117}
118impl FromByteSlice for Identity {
119 fn bytes_expected() -> usize {
120 25
121 }
122 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
123 Identity {
124 uid: <String>::from_le_byte_slice(&bytes[0..8]),
125 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
126 position: <char>::from_le_byte_slice(&bytes[16..17]),
127 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
128 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
129 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
130 }
131 }
132}
133
134#[derive(Clone)]
136pub struct LinearPotiBricklet {
137 device: Device,
138}
139impl LinearPotiBricklet {
140 pub const DEVICE_IDENTIFIER: u16 = 213;
141 pub const DEVICE_DISPLAY_NAME: &'static str = "Linear Poti Bricklet";
142 pub fn new(uid: Uid, connection: AsyncIpConnection) -> LinearPotiBricklet {
144 let mut result = LinearPotiBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
145 result.device.response_expected[u8::from(LinearPotiBrickletFunction::GetPosition) as usize] = ResponseExpectedFlag::AlwaysTrue;
146 result.device.response_expected[u8::from(LinearPotiBrickletFunction::GetAnalogValue) as usize] = ResponseExpectedFlag::AlwaysTrue;
147 result.device.response_expected[u8::from(LinearPotiBrickletFunction::SetPositionCallbackPeriod) as usize] =
148 ResponseExpectedFlag::True;
149 result.device.response_expected[u8::from(LinearPotiBrickletFunction::GetPositionCallbackPeriod) as usize] =
150 ResponseExpectedFlag::AlwaysTrue;
151 result.device.response_expected[u8::from(LinearPotiBrickletFunction::SetAnalogValueCallbackPeriod) as usize] =
152 ResponseExpectedFlag::True;
153 result.device.response_expected[u8::from(LinearPotiBrickletFunction::GetAnalogValueCallbackPeriod) as usize] =
154 ResponseExpectedFlag::AlwaysTrue;
155 result.device.response_expected[u8::from(LinearPotiBrickletFunction::SetPositionCallbackThreshold) as usize] =
156 ResponseExpectedFlag::True;
157 result.device.response_expected[u8::from(LinearPotiBrickletFunction::GetPositionCallbackThreshold) as usize] =
158 ResponseExpectedFlag::AlwaysTrue;
159 result.device.response_expected[u8::from(LinearPotiBrickletFunction::SetAnalogValueCallbackThreshold) as usize] =
160 ResponseExpectedFlag::True;
161 result.device.response_expected[u8::from(LinearPotiBrickletFunction::GetAnalogValueCallbackThreshold) as usize] =
162 ResponseExpectedFlag::AlwaysTrue;
163 result.device.response_expected[u8::from(LinearPotiBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
164 result.device.response_expected[u8::from(LinearPotiBrickletFunction::GetDebouncePeriod) as usize] =
165 ResponseExpectedFlag::AlwaysTrue;
166 result.device.response_expected[u8::from(LinearPotiBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
167 result
168 }
169
170 pub fn get_response_expected(&mut self, fun: LinearPotiBrickletFunction) -> Result<bool, GetResponseExpectedError> {
185 self.device.get_response_expected(u8::from(fun))
186 }
187
188 pub fn set_response_expected(
197 &mut self,
198 fun: LinearPotiBrickletFunction,
199 response_expected: bool,
200 ) -> Result<(), SetResponseExpectedError> {
201 self.device.set_response_expected(u8::from(fun), response_expected)
202 }
203
204 pub fn set_response_expected_all(&mut self, response_expected: bool) {
206 self.device.set_response_expected_all(response_expected)
207 }
208
209 pub fn get_api_version(&self) -> [u8; 3] {
212 self.device.api_version
213 }
214
215 pub async fn get_position_callback_receiver(&mut self) -> impl Stream<Item = u16> {
225 self.device
226 .get_callback_receiver(u8::from(LinearPotiBrickletFunction::CallbackPosition))
227 .await
228 .map(|p| u16::from_le_byte_slice(p.body()))
229 }
230
231 pub async fn get_analog_value_callback_receiver(&mut self) -> impl Stream<Item = u16> {
238 self.device
239 .get_callback_receiver(u8::from(LinearPotiBrickletFunction::CallbackAnalogValue))
240 .await
241 .map(|p| u16::from_le_byte_slice(p.body()))
242 }
243
244 pub async fn get_position_reached_callback_receiver(&mut self) -> impl Stream<Item = u16> {
251 self.device
252 .get_callback_receiver(u8::from(LinearPotiBrickletFunction::CallbackPositionReached))
253 .await
254 .map(|p| u16::from_le_byte_slice(p.body()))
255 }
256
257 pub async fn get_analog_value_reached_callback_receiver(&mut self) -> impl Stream<Item = u16> {
264 self.device
265 .get_callback_receiver(u8::from(LinearPotiBrickletFunction::CallbackAnalogValueReached))
266 .await
267 .map(|p| u16::from_le_byte_slice(p.body()))
268 }
269
270 pub async fn get_position(&mut self) -> Result<u16, TinkerforgeError> {
277 let payload = [0; 0];
278
279 #[allow(unused_variables)]
280 let result = self.device.get(u8::from(LinearPotiBrickletFunction::GetPosition), &payload).await?;
281 Ok(u16::from_le_byte_slice(result.body()))
282 }
283
284 pub async fn get_analog_value(&mut self) -> Result<u16, TinkerforgeError> {
296 let payload = [0; 0];
297
298 #[allow(unused_variables)]
299 let result = self.device.get(u8::from(LinearPotiBrickletFunction::GetAnalogValue), &payload).await?;
300 Ok(u16::from_le_byte_slice(result.body()))
301 }
302
303 pub async fn set_position_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
309 let mut payload = [0; 4];
310 period.write_to_slice(&mut payload[0..4]);
311
312 #[allow(unused_variables)]
313 let result = self.device.set(u8::from(LinearPotiBrickletFunction::SetPositionCallbackPeriod), &payload).await?;
314 Ok(())
315 }
316
317 pub async fn get_position_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
319 let payload = [0; 0];
320
321 #[allow(unused_variables)]
322 let result = self.device.get(u8::from(LinearPotiBrickletFunction::GetPositionCallbackPeriod), &payload).await?;
323 Ok(u32::from_le_byte_slice(result.body()))
324 }
325
326 pub async fn set_analog_value_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
332 let mut payload = [0; 4];
333 period.write_to_slice(&mut payload[0..4]);
334
335 #[allow(unused_variables)]
336 let result = self.device.set(u8::from(LinearPotiBrickletFunction::SetAnalogValueCallbackPeriod), &payload).await?;
337 Ok(())
338 }
339
340 pub async fn get_analog_value_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
342 let payload = [0; 0];
343
344 #[allow(unused_variables)]
345 let result = self.device.get(u8::from(LinearPotiBrickletFunction::GetAnalogValueCallbackPeriod), &payload).await?;
346 Ok(u32::from_le_byte_slice(result.body()))
347 }
348
349 pub async fn set_position_callback_threshold(&mut self, option: char, min: u16, max: u16) -> Result<(), TinkerforgeError> {
368 let mut payload = [0; 5];
369 option.write_to_slice(&mut payload[0..1]);
370 min.write_to_slice(&mut payload[1..3]);
371 max.write_to_slice(&mut payload[3..5]);
372
373 #[allow(unused_variables)]
374 let result = self.device.set(u8::from(LinearPotiBrickletFunction::SetPositionCallbackThreshold), &payload).await?;
375 Ok(())
376 }
377
378 pub async fn get_position_callback_threshold(&mut self) -> Result<PositionCallbackThreshold, TinkerforgeError> {
387 let payload = [0; 0];
388
389 #[allow(unused_variables)]
390 let result = self.device.get(u8::from(LinearPotiBrickletFunction::GetPositionCallbackThreshold), &payload).await?;
391 Ok(PositionCallbackThreshold::from_le_byte_slice(result.body()))
392 }
393
394 pub async fn set_analog_value_callback_threshold(&mut self, option: char, min: u16, max: u16) -> Result<(), TinkerforgeError> {
413 let mut payload = [0; 5];
414 option.write_to_slice(&mut payload[0..1]);
415 min.write_to_slice(&mut payload[1..3]);
416 max.write_to_slice(&mut payload[3..5]);
417
418 #[allow(unused_variables)]
419 let result = self.device.set(u8::from(LinearPotiBrickletFunction::SetAnalogValueCallbackThreshold), &payload).await?;
420 Ok(())
421 }
422
423 pub async fn get_analog_value_callback_threshold(&mut self) -> Result<AnalogValueCallbackThreshold, TinkerforgeError> {
432 let payload = [0; 0];
433
434 #[allow(unused_variables)]
435 let result = self.device.get(u8::from(LinearPotiBrickletFunction::GetAnalogValueCallbackThreshold), &payload).await?;
436 Ok(AnalogValueCallbackThreshold::from_le_byte_slice(result.body()))
437 }
438
439 pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
451 let mut payload = [0; 4];
452 debounce.write_to_slice(&mut payload[0..4]);
453
454 #[allow(unused_variables)]
455 let result = self.device.set(u8::from(LinearPotiBrickletFunction::SetDebouncePeriod), &payload).await?;
456 Ok(())
457 }
458
459 pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
461 let payload = [0; 0];
462
463 #[allow(unused_variables)]
464 let result = self.device.get(u8::from(LinearPotiBrickletFunction::GetDebouncePeriod), &payload).await?;
465 Ok(u32::from_le_byte_slice(result.body()))
466 }
467
468 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
479 let payload = [0; 0];
480
481 #[allow(unused_variables)]
482 let result = self.device.get(u8::from(LinearPotiBrickletFunction::GetIdentity), &payload).await?;
483 Ok(Identity::from_le_byte_slice(result.body()))
484 }
485}