1#[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 MotorizedLinearPotiBrickletFunction {
24 GetPosition,
25 SetPositionCallbackConfiguration,
26 GetPositionCallbackConfiguration,
27 SetMotorPosition,
28 GetMotorPosition,
29 Calibrate,
30 SetPositionReachedCallbackConfiguration,
31 GetPositionReachedCallbackConfiguration,
32 GetSpitfpErrorCount,
33 SetBootloaderMode,
34 GetBootloaderMode,
35 SetWriteFirmwarePointer,
36 WriteFirmware,
37 SetStatusLedConfig,
38 GetStatusLedConfig,
39 GetChipTemperature,
40 Reset,
41 WriteUid,
42 ReadUid,
43 GetIdentity,
44 CallbackPosition,
45 CallbackPositionReached,
46}
47impl From<MotorizedLinearPotiBrickletFunction> for u8 {
48 fn from(fun: MotorizedLinearPotiBrickletFunction) -> Self {
49 match fun {
50 MotorizedLinearPotiBrickletFunction::GetPosition => 1,
51 MotorizedLinearPotiBrickletFunction::SetPositionCallbackConfiguration => 2,
52 MotorizedLinearPotiBrickletFunction::GetPositionCallbackConfiguration => 3,
53 MotorizedLinearPotiBrickletFunction::SetMotorPosition => 5,
54 MotorizedLinearPotiBrickletFunction::GetMotorPosition => 6,
55 MotorizedLinearPotiBrickletFunction::Calibrate => 7,
56 MotorizedLinearPotiBrickletFunction::SetPositionReachedCallbackConfiguration => 8,
57 MotorizedLinearPotiBrickletFunction::GetPositionReachedCallbackConfiguration => 9,
58 MotorizedLinearPotiBrickletFunction::GetSpitfpErrorCount => 234,
59 MotorizedLinearPotiBrickletFunction::SetBootloaderMode => 235,
60 MotorizedLinearPotiBrickletFunction::GetBootloaderMode => 236,
61 MotorizedLinearPotiBrickletFunction::SetWriteFirmwarePointer => 237,
62 MotorizedLinearPotiBrickletFunction::WriteFirmware => 238,
63 MotorizedLinearPotiBrickletFunction::SetStatusLedConfig => 239,
64 MotorizedLinearPotiBrickletFunction::GetStatusLedConfig => 240,
65 MotorizedLinearPotiBrickletFunction::GetChipTemperature => 242,
66 MotorizedLinearPotiBrickletFunction::Reset => 243,
67 MotorizedLinearPotiBrickletFunction::WriteUid => 248,
68 MotorizedLinearPotiBrickletFunction::ReadUid => 249,
69 MotorizedLinearPotiBrickletFunction::GetIdentity => 255,
70 MotorizedLinearPotiBrickletFunction::CallbackPosition => 4,
71 MotorizedLinearPotiBrickletFunction::CallbackPositionReached => 10,
72 }
73 }
74}
75pub const MOTORIZED_LINEAR_POTI_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
76pub const MOTORIZED_LINEAR_POTI_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
77pub const MOTORIZED_LINEAR_POTI_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
78pub const MOTORIZED_LINEAR_POTI_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
79pub const MOTORIZED_LINEAR_POTI_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
80pub const MOTORIZED_LINEAR_POTI_BRICKLET_DRIVE_MODE_FAST: u8 = 0;
81pub const MOTORIZED_LINEAR_POTI_BRICKLET_DRIVE_MODE_SMOOTH: u8 = 1;
82pub const MOTORIZED_LINEAR_POTI_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
83pub const MOTORIZED_LINEAR_POTI_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
84pub const MOTORIZED_LINEAR_POTI_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
85pub const MOTORIZED_LINEAR_POTI_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
86pub const MOTORIZED_LINEAR_POTI_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
87pub const MOTORIZED_LINEAR_POTI_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
88pub const MOTORIZED_LINEAR_POTI_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
89pub const MOTORIZED_LINEAR_POTI_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
90pub const MOTORIZED_LINEAR_POTI_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
91pub const MOTORIZED_LINEAR_POTI_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
92pub const MOTORIZED_LINEAR_POTI_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
93pub const MOTORIZED_LINEAR_POTI_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
94pub const MOTORIZED_LINEAR_POTI_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
95pub const MOTORIZED_LINEAR_POTI_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
96pub const MOTORIZED_LINEAR_POTI_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
97
98#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
99pub struct PositionCallbackConfiguration {
100 pub period: u32,
101 pub value_has_to_change: bool,
102 pub option: char,
103 pub min: u16,
104 pub max: u16,
105}
106impl FromByteSlice for PositionCallbackConfiguration {
107 fn bytes_expected() -> usize {
108 10
109 }
110 fn from_le_byte_slice(bytes: &[u8]) -> PositionCallbackConfiguration {
111 PositionCallbackConfiguration {
112 period: <u32>::from_le_byte_slice(&bytes[0..4]),
113 value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
114 option: <char>::from_le_byte_slice(&bytes[5..6]),
115 min: <u16>::from_le_byte_slice(&bytes[6..8]),
116 max: <u16>::from_le_byte_slice(&bytes[8..10]),
117 }
118 }
119}
120
121#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
122pub struct MotorPosition {
123 pub position: u16,
124 pub drive_mode: u8,
125 pub hold_position: bool,
126 pub position_reached: bool,
127}
128impl FromByteSlice for MotorPosition {
129 fn bytes_expected() -> usize {
130 5
131 }
132 fn from_le_byte_slice(bytes: &[u8]) -> MotorPosition {
133 MotorPosition {
134 position: <u16>::from_le_byte_slice(&bytes[0..2]),
135 drive_mode: <u8>::from_le_byte_slice(&bytes[2..3]),
136 hold_position: <bool>::from_le_byte_slice(&bytes[3..4]),
137 position_reached: <bool>::from_le_byte_slice(&bytes[4..5]),
138 }
139 }
140}
141
142#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
143pub struct SpitfpErrorCount {
144 pub error_count_ack_checksum: u32,
145 pub error_count_message_checksum: u32,
146 pub error_count_frame: u32,
147 pub error_count_overflow: u32,
148}
149impl FromByteSlice for SpitfpErrorCount {
150 fn bytes_expected() -> usize {
151 16
152 }
153 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
154 SpitfpErrorCount {
155 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
156 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
157 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
158 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
159 }
160 }
161}
162
163#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
164pub struct Identity {
165 pub uid: String,
166 pub connected_uid: String,
167 pub position: char,
168 pub hardware_version: [u8; 3],
169 pub firmware_version: [u8; 3],
170 pub device_identifier: u16,
171}
172impl FromByteSlice for Identity {
173 fn bytes_expected() -> usize {
174 25
175 }
176 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
177 Identity {
178 uid: <String>::from_le_byte_slice(&bytes[0..8]),
179 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
180 position: <char>::from_le_byte_slice(&bytes[16..17]),
181 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
182 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
183 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
184 }
185 }
186}
187
188#[derive(Clone)]
190pub struct MotorizedLinearPotiBricklet {
191 device: Device,
192}
193impl MotorizedLinearPotiBricklet {
194 pub const DEVICE_IDENTIFIER: u16 = 267;
195 pub const DEVICE_DISPLAY_NAME: &'static str = "Motorized Linear Poti Bricklet";
196 pub fn new(uid: Uid, connection: AsyncIpConnection) -> MotorizedLinearPotiBricklet {
198 let mut result = MotorizedLinearPotiBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
199 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::GetPosition) as usize] =
200 ResponseExpectedFlag::AlwaysTrue;
201 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::SetPositionCallbackConfiguration) as usize] =
202 ResponseExpectedFlag::True;
203 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::GetPositionCallbackConfiguration) as usize] =
204 ResponseExpectedFlag::AlwaysTrue;
205 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::SetMotorPosition) as usize] =
206 ResponseExpectedFlag::False;
207 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::GetMotorPosition) as usize] =
208 ResponseExpectedFlag::AlwaysTrue;
209 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::Calibrate) as usize] = ResponseExpectedFlag::False;
210 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::SetPositionReachedCallbackConfiguration) as usize] =
211 ResponseExpectedFlag::True;
212 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::GetPositionReachedCallbackConfiguration) as usize] =
213 ResponseExpectedFlag::AlwaysTrue;
214 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::GetSpitfpErrorCount) as usize] =
215 ResponseExpectedFlag::AlwaysTrue;
216 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::SetBootloaderMode) as usize] =
217 ResponseExpectedFlag::AlwaysTrue;
218 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::GetBootloaderMode) as usize] =
219 ResponseExpectedFlag::AlwaysTrue;
220 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::SetWriteFirmwarePointer) as usize] =
221 ResponseExpectedFlag::False;
222 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::WriteFirmware) as usize] =
223 ResponseExpectedFlag::AlwaysTrue;
224 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::SetStatusLedConfig) as usize] =
225 ResponseExpectedFlag::False;
226 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::GetStatusLedConfig) as usize] =
227 ResponseExpectedFlag::AlwaysTrue;
228 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::GetChipTemperature) as usize] =
229 ResponseExpectedFlag::AlwaysTrue;
230 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
231 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
232 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
233 result.device.response_expected[u8::from(MotorizedLinearPotiBrickletFunction::GetIdentity) as usize] =
234 ResponseExpectedFlag::AlwaysTrue;
235 result
236 }
237
238 pub fn get_response_expected(&mut self, fun: MotorizedLinearPotiBrickletFunction) -> Result<bool, GetResponseExpectedError> {
253 self.device.get_response_expected(u8::from(fun))
254 }
255
256 pub fn set_response_expected(
265 &mut self,
266 fun: MotorizedLinearPotiBrickletFunction,
267 response_expected: bool,
268 ) -> Result<(), SetResponseExpectedError> {
269 self.device.set_response_expected(u8::from(fun), response_expected)
270 }
271
272 pub fn set_response_expected_all(&mut self, response_expected: bool) {
274 self.device.set_response_expected_all(response_expected)
275 }
276
277 pub fn get_api_version(&self) -> [u8; 3] {
280 self.device.api_version
281 }
282
283 pub async fn get_position_callback_receiver(&mut self) -> impl Stream<Item = u16> {
291 self.device
292 .get_callback_receiver(u8::from(MotorizedLinearPotiBrickletFunction::CallbackPosition))
293 .await
294 .map(|p| u16::from_le_byte_slice(p.body()))
295 }
296
297 pub async fn get_position_reached_callback_receiver(&mut self) -> impl Stream<Item = u16> {
302 self.device
303 .get_callback_receiver(u8::from(MotorizedLinearPotiBrickletFunction::CallbackPositionReached))
304 .await
305 .map(|p| u16::from_le_byte_slice(p.body()))
306 }
307
308 pub async fn get_position(&mut self) -> Result<u16, TinkerforgeError> {
316 let payload = [0; 0];
317
318 #[allow(unused_variables)]
319 let result = self.device.get(u8::from(MotorizedLinearPotiBrickletFunction::GetPosition), &payload).await?;
320 Ok(u16::from_le_byte_slice(result.body()))
321 }
322
323 pub async fn set_position_callback_configuration(
356 &mut self,
357 period: u32,
358 value_has_to_change: bool,
359 option: char,
360 min: u16,
361 max: u16,
362 ) -> Result<(), TinkerforgeError> {
363 let mut payload = [0; 10];
364 period.write_to_slice(&mut payload[0..4]);
365 value_has_to_change.write_to_slice(&mut payload[4..5]);
366 option.write_to_slice(&mut payload[5..6]);
367 min.write_to_slice(&mut payload[6..8]);
368 max.write_to_slice(&mut payload[8..10]);
369
370 #[allow(unused_variables)]
371 let result = self.device.set(u8::from(MotorizedLinearPotiBrickletFunction::SetPositionCallbackConfiguration), &payload).await?;
372 Ok(())
373 }
374
375 pub async fn get_position_callback_configuration(&mut self) -> Result<PositionCallbackConfiguration, TinkerforgeError> {
384 let payload = [0; 0];
385
386 #[allow(unused_variables)]
387 let result = self.device.get(u8::from(MotorizedLinearPotiBrickletFunction::GetPositionCallbackConfiguration), &payload).await?;
388 Ok(PositionCallbackConfiguration::from_le_byte_slice(result.body()))
389 }
390
391 pub async fn set_motor_position(&mut self, position: u16, drive_mode: u8, hold_position: bool) -> Result<(), TinkerforgeError> {
409 let mut payload = [0; 4];
410 position.write_to_slice(&mut payload[0..2]);
411 drive_mode.write_to_slice(&mut payload[2..3]);
412 hold_position.write_to_slice(&mut payload[3..4]);
413
414 #[allow(unused_variables)]
415 let result = self.device.set(u8::from(MotorizedLinearPotiBrickletFunction::SetMotorPosition), &payload).await?;
416 Ok(())
417 }
418
419 pub async fn get_motor_position(&mut self) -> Result<MotorPosition, TinkerforgeError> {
430 let payload = [0; 0];
431
432 #[allow(unused_variables)]
433 let result = self.device.get(u8::from(MotorizedLinearPotiBrickletFunction::GetMotorPosition), &payload).await?;
434 Ok(MotorPosition::from_le_byte_slice(result.body()))
435 }
436
437 pub async fn calibrate(&mut self) -> Result<(), TinkerforgeError> {
445 let payload = [0; 0];
446
447 #[allow(unused_variables)]
448 let result = self.device.set(u8::from(MotorizedLinearPotiBrickletFunction::Calibrate), &payload).await?;
449 Ok(())
450 }
451
452 pub async fn set_position_reached_callback_configuration(&mut self, enabled: bool) -> Result<(), TinkerforgeError> {
454 let mut payload = [0; 1];
455 enabled.write_to_slice(&mut payload[0..1]);
456
457 #[allow(unused_variables)]
458 let result =
459 self.device.set(u8::from(MotorizedLinearPotiBrickletFunction::SetPositionReachedCallbackConfiguration), &payload).await?;
460 Ok(())
461 }
462
463 pub async fn get_position_reached_callback_configuration(&mut self) -> Result<bool, TinkerforgeError> {
466 let payload = [0; 0];
467
468 #[allow(unused_variables)]
469 let result =
470 self.device.get(u8::from(MotorizedLinearPotiBrickletFunction::GetPositionReachedCallbackConfiguration), &payload).await?;
471 Ok(bool::from_le_byte_slice(result.body()))
472 }
473
474 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
486 let payload = [0; 0];
487
488 #[allow(unused_variables)]
489 let result = self.device.get(u8::from(MotorizedLinearPotiBrickletFunction::GetSpitfpErrorCount), &payload).await?;
490 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
491 }
492
493 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
516 let mut payload = [0; 1];
517 mode.write_to_slice(&mut payload[0..1]);
518
519 #[allow(unused_variables)]
520 let result = self.device.get(u8::from(MotorizedLinearPotiBrickletFunction::SetBootloaderMode), &payload).await?;
521 Ok(u8::from_le_byte_slice(result.body()))
522 }
523
524 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
533 let payload = [0; 0];
534
535 #[allow(unused_variables)]
536 let result = self.device.get(u8::from(MotorizedLinearPotiBrickletFunction::GetBootloaderMode), &payload).await?;
537 Ok(u8::from_le_byte_slice(result.body()))
538 }
539
540 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
547 let mut payload = [0; 4];
548 pointer.write_to_slice(&mut payload[0..4]);
549
550 #[allow(unused_variables)]
551 let result = self.device.set(u8::from(MotorizedLinearPotiBrickletFunction::SetWriteFirmwarePointer), &payload).await?;
552 Ok(())
553 }
554
555 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
564 let mut payload = [0; 64];
565 data.write_to_slice(&mut payload[0..64]);
566
567 #[allow(unused_variables)]
568 let result = self.device.get(u8::from(MotorizedLinearPotiBrickletFunction::WriteFirmware), &payload).await?;
569 Ok(u8::from_le_byte_slice(result.body()))
570 }
571
572 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
586 let mut payload = [0; 1];
587 config.write_to_slice(&mut payload[0..1]);
588
589 #[allow(unused_variables)]
590 let result = self.device.set(u8::from(MotorizedLinearPotiBrickletFunction::SetStatusLedConfig), &payload).await?;
591 Ok(())
592 }
593
594 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
602 let payload = [0; 0];
603
604 #[allow(unused_variables)]
605 let result = self.device.get(u8::from(MotorizedLinearPotiBrickletFunction::GetStatusLedConfig), &payload).await?;
606 Ok(u8::from_le_byte_slice(result.body()))
607 }
608
609 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
616 let payload = [0; 0];
617
618 #[allow(unused_variables)]
619 let result = self.device.get(u8::from(MotorizedLinearPotiBrickletFunction::GetChipTemperature), &payload).await?;
620 Ok(i16::from_le_byte_slice(result.body()))
621 }
622
623 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
630 let payload = [0; 0];
631
632 #[allow(unused_variables)]
633 let result = self.device.set(u8::from(MotorizedLinearPotiBrickletFunction::Reset), &payload).await?;
634 Ok(())
635 }
636
637 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
643 let mut payload = [0; 4];
644 uid.write_to_slice(&mut payload[0..4]);
645
646 #[allow(unused_variables)]
647 let result = self.device.set(u8::from(MotorizedLinearPotiBrickletFunction::WriteUid), &payload).await?;
648 Ok(())
649 }
650
651 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
654 let payload = [0; 0];
655
656 #[allow(unused_variables)]
657 let result = self.device.get(u8::from(MotorizedLinearPotiBrickletFunction::ReadUid), &payload).await?;
658 Ok(u32::from_le_byte_slice(result.body()))
659 }
660
661 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
672 let payload = [0; 0];
673
674 #[allow(unused_variables)]
675 let result = self.device.get(u8::from(MotorizedLinearPotiBrickletFunction::GetIdentity), &payload).await?;
676 Ok(Identity::from_le_byte_slice(result.body()))
677 }
678}