tinkerforge_async/bindings/
linear_poti_v2_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 LinearPotiV2BrickletFunction {
24 GetPosition,
25 SetPositionCallbackConfiguration,
26 GetPositionCallbackConfiguration,
27 GetSpitfpErrorCount,
28 SetBootloaderMode,
29 GetBootloaderMode,
30 SetWriteFirmwarePointer,
31 WriteFirmware,
32 SetStatusLedConfig,
33 GetStatusLedConfig,
34 GetChipTemperature,
35 Reset,
36 WriteUid,
37 ReadUid,
38 GetIdentity,
39 CallbackPosition,
40}
41impl From<LinearPotiV2BrickletFunction> for u8 {
42 fn from(fun: LinearPotiV2BrickletFunction) -> Self {
43 match fun {
44 LinearPotiV2BrickletFunction::GetPosition => 1,
45 LinearPotiV2BrickletFunction::SetPositionCallbackConfiguration => 2,
46 LinearPotiV2BrickletFunction::GetPositionCallbackConfiguration => 3,
47 LinearPotiV2BrickletFunction::GetSpitfpErrorCount => 234,
48 LinearPotiV2BrickletFunction::SetBootloaderMode => 235,
49 LinearPotiV2BrickletFunction::GetBootloaderMode => 236,
50 LinearPotiV2BrickletFunction::SetWriteFirmwarePointer => 237,
51 LinearPotiV2BrickletFunction::WriteFirmware => 238,
52 LinearPotiV2BrickletFunction::SetStatusLedConfig => 239,
53 LinearPotiV2BrickletFunction::GetStatusLedConfig => 240,
54 LinearPotiV2BrickletFunction::GetChipTemperature => 242,
55 LinearPotiV2BrickletFunction::Reset => 243,
56 LinearPotiV2BrickletFunction::WriteUid => 248,
57 LinearPotiV2BrickletFunction::ReadUid => 249,
58 LinearPotiV2BrickletFunction::GetIdentity => 255,
59 LinearPotiV2BrickletFunction::CallbackPosition => 4,
60 }
61 }
62}
63pub const LINEAR_POTI_V2_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
64pub const LINEAR_POTI_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
65pub const LINEAR_POTI_V2_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
66pub const LINEAR_POTI_V2_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
67pub const LINEAR_POTI_V2_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
68pub const LINEAR_POTI_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
69pub const LINEAR_POTI_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
70pub const LINEAR_POTI_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
71pub const LINEAR_POTI_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
72pub const LINEAR_POTI_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
73pub const LINEAR_POTI_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
74pub const LINEAR_POTI_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
75pub const LINEAR_POTI_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
76pub const LINEAR_POTI_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
77pub const LINEAR_POTI_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
78pub const LINEAR_POTI_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
79pub const LINEAR_POTI_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
80pub const LINEAR_POTI_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
81pub const LINEAR_POTI_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
82pub const LINEAR_POTI_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
83
84#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
85pub struct PositionCallbackConfiguration {
86 pub period: u32,
87 pub value_has_to_change: bool,
88 pub option: char,
89 pub min: u8,
90 pub max: u8,
91}
92impl FromByteSlice for PositionCallbackConfiguration {
93 fn bytes_expected() -> usize {
94 8
95 }
96 fn from_le_byte_slice(bytes: &[u8]) -> PositionCallbackConfiguration {
97 PositionCallbackConfiguration {
98 period: <u32>::from_le_byte_slice(&bytes[0..4]),
99 value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
100 option: <char>::from_le_byte_slice(&bytes[5..6]),
101 min: <u8>::from_le_byte_slice(&bytes[6..7]),
102 max: <u8>::from_le_byte_slice(&bytes[7..8]),
103 }
104 }
105}
106
107#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
108pub struct SpitfpErrorCount {
109 pub error_count_ack_checksum: u32,
110 pub error_count_message_checksum: u32,
111 pub error_count_frame: u32,
112 pub error_count_overflow: u32,
113}
114impl FromByteSlice for SpitfpErrorCount {
115 fn bytes_expected() -> usize {
116 16
117 }
118 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
119 SpitfpErrorCount {
120 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
121 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
122 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
123 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
124 }
125 }
126}
127
128#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
129pub struct Identity {
130 pub uid: String,
131 pub connected_uid: String,
132 pub position: char,
133 pub hardware_version: [u8; 3],
134 pub firmware_version: [u8; 3],
135 pub device_identifier: u16,
136}
137impl FromByteSlice for Identity {
138 fn bytes_expected() -> usize {
139 25
140 }
141 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
142 Identity {
143 uid: <String>::from_le_byte_slice(&bytes[0..8]),
144 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
145 position: <char>::from_le_byte_slice(&bytes[16..17]),
146 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
147 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
148 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
149 }
150 }
151}
152
153#[derive(Clone)]
155pub struct LinearPotiV2Bricklet {
156 device: Device,
157}
158impl LinearPotiV2Bricklet {
159 pub const DEVICE_IDENTIFIER: u16 = 2139;
160 pub const DEVICE_DISPLAY_NAME: &'static str = "Linear Poti Bricklet 2.0";
161 pub fn new(uid: Uid, connection: AsyncIpConnection) -> LinearPotiV2Bricklet {
163 let mut result = LinearPotiV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
164 result.device.response_expected[u8::from(LinearPotiV2BrickletFunction::GetPosition) as usize] = ResponseExpectedFlag::AlwaysTrue;
165 result.device.response_expected[u8::from(LinearPotiV2BrickletFunction::SetPositionCallbackConfiguration) as usize] =
166 ResponseExpectedFlag::True;
167 result.device.response_expected[u8::from(LinearPotiV2BrickletFunction::GetPositionCallbackConfiguration) as usize] =
168 ResponseExpectedFlag::AlwaysTrue;
169 result.device.response_expected[u8::from(LinearPotiV2BrickletFunction::GetSpitfpErrorCount) as usize] =
170 ResponseExpectedFlag::AlwaysTrue;
171 result.device.response_expected[u8::from(LinearPotiV2BrickletFunction::SetBootloaderMode) as usize] =
172 ResponseExpectedFlag::AlwaysTrue;
173 result.device.response_expected[u8::from(LinearPotiV2BrickletFunction::GetBootloaderMode) as usize] =
174 ResponseExpectedFlag::AlwaysTrue;
175 result.device.response_expected[u8::from(LinearPotiV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
176 ResponseExpectedFlag::False;
177 result.device.response_expected[u8::from(LinearPotiV2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
178 result.device.response_expected[u8::from(LinearPotiV2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
179 result.device.response_expected[u8::from(LinearPotiV2BrickletFunction::GetStatusLedConfig) as usize] =
180 ResponseExpectedFlag::AlwaysTrue;
181 result.device.response_expected[u8::from(LinearPotiV2BrickletFunction::GetChipTemperature) as usize] =
182 ResponseExpectedFlag::AlwaysTrue;
183 result.device.response_expected[u8::from(LinearPotiV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
184 result.device.response_expected[u8::from(LinearPotiV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
185 result.device.response_expected[u8::from(LinearPotiV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
186 result.device.response_expected[u8::from(LinearPotiV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
187 result
188 }
189
190 pub fn get_response_expected(&mut self, fun: LinearPotiV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
205 self.device.get_response_expected(u8::from(fun))
206 }
207
208 pub fn set_response_expected(
217 &mut self,
218 fun: LinearPotiV2BrickletFunction,
219 response_expected: bool,
220 ) -> Result<(), SetResponseExpectedError> {
221 self.device.set_response_expected(u8::from(fun), response_expected)
222 }
223
224 pub fn set_response_expected_all(&mut self, response_expected: bool) {
226 self.device.set_response_expected_all(response_expected)
227 }
228
229 pub fn get_api_version(&self) -> [u8; 3] {
232 self.device.api_version
233 }
234
235 pub async fn get_position_callback_receiver(&mut self) -> impl Stream<Item = u8> {
243 self.device
244 .get_callback_receiver(u8::from(LinearPotiV2BrickletFunction::CallbackPosition))
245 .await
246 .map(|p| u8::from_le_byte_slice(p.body()))
247 }
248
249 pub async fn get_position(&mut self) -> Result<u8, TinkerforgeError> {
257 let payload = [0; 0];
258
259 #[allow(unused_variables)]
260 let result = self.device.get(u8::from(LinearPotiV2BrickletFunction::GetPosition), &payload).await?;
261 Ok(u8::from_le_byte_slice(result.body()))
262 }
263
264 pub async fn set_position_callback_configuration(
297 &mut self,
298 period: u32,
299 value_has_to_change: bool,
300 option: char,
301 min: u8,
302 max: u8,
303 ) -> Result<(), TinkerforgeError> {
304 let mut payload = [0; 8];
305 period.write_to_slice(&mut payload[0..4]);
306 value_has_to_change.write_to_slice(&mut payload[4..5]);
307 option.write_to_slice(&mut payload[5..6]);
308 min.write_to_slice(&mut payload[6..7]);
309 max.write_to_slice(&mut payload[7..8]);
310
311 #[allow(unused_variables)]
312 let result = self.device.set(u8::from(LinearPotiV2BrickletFunction::SetPositionCallbackConfiguration), &payload).await?;
313 Ok(())
314 }
315
316 pub async fn get_position_callback_configuration(&mut self) -> Result<PositionCallbackConfiguration, TinkerforgeError> {
325 let payload = [0; 0];
326
327 #[allow(unused_variables)]
328 let result = self.device.get(u8::from(LinearPotiV2BrickletFunction::GetPositionCallbackConfiguration), &payload).await?;
329 Ok(PositionCallbackConfiguration::from_le_byte_slice(result.body()))
330 }
331
332 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
344 let payload = [0; 0];
345
346 #[allow(unused_variables)]
347 let result = self.device.get(u8::from(LinearPotiV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
348 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
349 }
350
351 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
374 let mut payload = [0; 1];
375 mode.write_to_slice(&mut payload[0..1]);
376
377 #[allow(unused_variables)]
378 let result = self.device.get(u8::from(LinearPotiV2BrickletFunction::SetBootloaderMode), &payload).await?;
379 Ok(u8::from_le_byte_slice(result.body()))
380 }
381
382 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
391 let payload = [0; 0];
392
393 #[allow(unused_variables)]
394 let result = self.device.get(u8::from(LinearPotiV2BrickletFunction::GetBootloaderMode), &payload).await?;
395 Ok(u8::from_le_byte_slice(result.body()))
396 }
397
398 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
405 let mut payload = [0; 4];
406 pointer.write_to_slice(&mut payload[0..4]);
407
408 #[allow(unused_variables)]
409 let result = self.device.set(u8::from(LinearPotiV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
410 Ok(())
411 }
412
413 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
422 let mut payload = [0; 64];
423 data.write_to_slice(&mut payload[0..64]);
424
425 #[allow(unused_variables)]
426 let result = self.device.get(u8::from(LinearPotiV2BrickletFunction::WriteFirmware), &payload).await?;
427 Ok(u8::from_le_byte_slice(result.body()))
428 }
429
430 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
444 let mut payload = [0; 1];
445 config.write_to_slice(&mut payload[0..1]);
446
447 #[allow(unused_variables)]
448 let result = self.device.set(u8::from(LinearPotiV2BrickletFunction::SetStatusLedConfig), &payload).await?;
449 Ok(())
450 }
451
452 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
460 let payload = [0; 0];
461
462 #[allow(unused_variables)]
463 let result = self.device.get(u8::from(LinearPotiV2BrickletFunction::GetStatusLedConfig), &payload).await?;
464 Ok(u8::from_le_byte_slice(result.body()))
465 }
466
467 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
474 let payload = [0; 0];
475
476 #[allow(unused_variables)]
477 let result = self.device.get(u8::from(LinearPotiV2BrickletFunction::GetChipTemperature), &payload).await?;
478 Ok(i16::from_le_byte_slice(result.body()))
479 }
480
481 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
488 let payload = [0; 0];
489
490 #[allow(unused_variables)]
491 let result = self.device.set(u8::from(LinearPotiV2BrickletFunction::Reset), &payload).await?;
492 Ok(())
493 }
494
495 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
501 let mut payload = [0; 4];
502 uid.write_to_slice(&mut payload[0..4]);
503
504 #[allow(unused_variables)]
505 let result = self.device.set(u8::from(LinearPotiV2BrickletFunction::WriteUid), &payload).await?;
506 Ok(())
507 }
508
509 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
512 let payload = [0; 0];
513
514 #[allow(unused_variables)]
515 let result = self.device.get(u8::from(LinearPotiV2BrickletFunction::ReadUid), &payload).await?;
516 Ok(u32::from_le_byte_slice(result.body()))
517 }
518
519 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
530 let payload = [0; 0];
531
532 #[allow(unused_variables)]
533 let result = self.device.get(u8::from(LinearPotiV2BrickletFunction::GetIdentity), &payload).await?;
534 Ok(Identity::from_le_byte_slice(result.body()))
535 }
536}