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 DistanceUsV2BrickletFunction {
24 GetDistance,
25 SetDistanceCallbackConfiguration,
26 GetDistanceCallbackConfiguration,
27 SetUpdateRate,
28 GetUpdateRate,
29 SetDistanceLedConfig,
30 GetDistanceLedConfig,
31 GetSpitfpErrorCount,
32 SetBootloaderMode,
33 GetBootloaderMode,
34 SetWriteFirmwarePointer,
35 WriteFirmware,
36 SetStatusLedConfig,
37 GetStatusLedConfig,
38 GetChipTemperature,
39 Reset,
40 WriteUid,
41 ReadUid,
42 GetIdentity,
43 CallbackDistance,
44}
45impl From<DistanceUsV2BrickletFunction> for u8 {
46 fn from(fun: DistanceUsV2BrickletFunction) -> Self {
47 match fun {
48 DistanceUsV2BrickletFunction::GetDistance => 1,
49 DistanceUsV2BrickletFunction::SetDistanceCallbackConfiguration => 2,
50 DistanceUsV2BrickletFunction::GetDistanceCallbackConfiguration => 3,
51 DistanceUsV2BrickletFunction::SetUpdateRate => 5,
52 DistanceUsV2BrickletFunction::GetUpdateRate => 6,
53 DistanceUsV2BrickletFunction::SetDistanceLedConfig => 7,
54 DistanceUsV2BrickletFunction::GetDistanceLedConfig => 8,
55 DistanceUsV2BrickletFunction::GetSpitfpErrorCount => 234,
56 DistanceUsV2BrickletFunction::SetBootloaderMode => 235,
57 DistanceUsV2BrickletFunction::GetBootloaderMode => 236,
58 DistanceUsV2BrickletFunction::SetWriteFirmwarePointer => 237,
59 DistanceUsV2BrickletFunction::WriteFirmware => 238,
60 DistanceUsV2BrickletFunction::SetStatusLedConfig => 239,
61 DistanceUsV2BrickletFunction::GetStatusLedConfig => 240,
62 DistanceUsV2BrickletFunction::GetChipTemperature => 242,
63 DistanceUsV2BrickletFunction::Reset => 243,
64 DistanceUsV2BrickletFunction::WriteUid => 248,
65 DistanceUsV2BrickletFunction::ReadUid => 249,
66 DistanceUsV2BrickletFunction::GetIdentity => 255,
67 DistanceUsV2BrickletFunction::CallbackDistance => 4,
68 }
69 }
70}
71pub const DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
72pub const DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
73pub const DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
74pub const DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
75pub const DISTANCE_US_V2_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
76pub const DISTANCE_US_V2_BRICKLET_UPDATE_RATE_2_HZ: u8 = 0;
77pub const DISTANCE_US_V2_BRICKLET_UPDATE_RATE_10_HZ: u8 = 1;
78pub const DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_OFF: u8 = 0;
79pub const DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_ON: u8 = 1;
80pub const DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
81pub const DISTANCE_US_V2_BRICKLET_DISTANCE_LED_CONFIG_SHOW_DISTANCE: u8 = 3;
82pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
83pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
84pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
85pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
86pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
87pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
88pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
89pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
90pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
91pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
92pub const DISTANCE_US_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
93pub const DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
94pub const DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
95pub const DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
96pub const DISTANCE_US_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
97
98#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
99pub struct DistanceCallbackConfiguration {
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 DistanceCallbackConfiguration {
107 fn bytes_expected() -> usize {
108 10
109 }
110 fn from_le_byte_slice(bytes: &[u8]) -> DistanceCallbackConfiguration {
111 DistanceCallbackConfiguration {
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 SpitfpErrorCount {
123 pub error_count_ack_checksum: u32,
124 pub error_count_message_checksum: u32,
125 pub error_count_frame: u32,
126 pub error_count_overflow: u32,
127}
128impl FromByteSlice for SpitfpErrorCount {
129 fn bytes_expected() -> usize {
130 16
131 }
132 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
133 SpitfpErrorCount {
134 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
135 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
136 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
137 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
138 }
139 }
140}
141
142#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
143pub struct Identity {
144 pub uid: String,
145 pub connected_uid: String,
146 pub position: char,
147 pub hardware_version: [u8; 3],
148 pub firmware_version: [u8; 3],
149 pub device_identifier: u16,
150}
151impl FromByteSlice for Identity {
152 fn bytes_expected() -> usize {
153 25
154 }
155 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
156 Identity {
157 uid: <String>::from_le_byte_slice(&bytes[0..8]),
158 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
159 position: <char>::from_le_byte_slice(&bytes[16..17]),
160 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
161 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
162 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
163 }
164 }
165}
166
167#[derive(Clone)]
169pub struct DistanceUsV2Bricklet {
170 device: Device,
171}
172impl DistanceUsV2Bricklet {
173 pub const DEVICE_IDENTIFIER: u16 = 299;
174 pub const DEVICE_DISPLAY_NAME: &'static str = "Distance US Bricklet 2.0";
175 pub fn new(uid: Uid, connection: AsyncIpConnection) -> DistanceUsV2Bricklet {
177 let mut result = DistanceUsV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
178 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetDistance) as usize] = ResponseExpectedFlag::AlwaysTrue;
179 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::SetDistanceCallbackConfiguration) as usize] =
180 ResponseExpectedFlag::True;
181 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetDistanceCallbackConfiguration) as usize] =
182 ResponseExpectedFlag::AlwaysTrue;
183 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::SetUpdateRate) as usize] = ResponseExpectedFlag::False;
184 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetUpdateRate) as usize] = ResponseExpectedFlag::AlwaysTrue;
185 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::SetDistanceLedConfig) as usize] =
186 ResponseExpectedFlag::False;
187 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetDistanceLedConfig) as usize] =
188 ResponseExpectedFlag::AlwaysTrue;
189 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetSpitfpErrorCount) as usize] =
190 ResponseExpectedFlag::AlwaysTrue;
191 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::SetBootloaderMode) as usize] =
192 ResponseExpectedFlag::AlwaysTrue;
193 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetBootloaderMode) as usize] =
194 ResponseExpectedFlag::AlwaysTrue;
195 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
196 ResponseExpectedFlag::False;
197 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
198 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
199 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetStatusLedConfig) as usize] =
200 ResponseExpectedFlag::AlwaysTrue;
201 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetChipTemperature) as usize] =
202 ResponseExpectedFlag::AlwaysTrue;
203 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
204 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
205 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
206 result.device.response_expected[u8::from(DistanceUsV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
207 result
208 }
209
210 pub fn get_response_expected(&mut self, fun: DistanceUsV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
225 self.device.get_response_expected(u8::from(fun))
226 }
227
228 pub fn set_response_expected(
237 &mut self,
238 fun: DistanceUsV2BrickletFunction,
239 response_expected: bool,
240 ) -> Result<(), SetResponseExpectedError> {
241 self.device.set_response_expected(u8::from(fun), response_expected)
242 }
243
244 pub fn set_response_expected_all(&mut self, response_expected: bool) {
246 self.device.set_response_expected_all(response_expected)
247 }
248
249 pub fn get_api_version(&self) -> [u8; 3] {
252 self.device.api_version
253 }
254
255 pub async fn get_distance_callback_receiver(&mut self) -> impl Stream<Item = u16> {
263 self.device
264 .get_callback_receiver(u8::from(DistanceUsV2BrickletFunction::CallbackDistance))
265 .await
266 .map(|p| u16::from_le_byte_slice(p.body()))
267 }
268
269 pub async fn get_distance(&mut self) -> Result<u16, TinkerforgeError> {
276 let payload = [0; 0];
277
278 #[allow(unused_variables)]
279 let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetDistance), &payload).await?;
280 Ok(u16::from_le_byte_slice(result.body()))
281 }
282
283 pub async fn set_distance_callback_configuration(
316 &mut self,
317 period: u32,
318 value_has_to_change: bool,
319 option: char,
320 min: u16,
321 max: u16,
322 ) -> Result<(), TinkerforgeError> {
323 let mut payload = [0; 10];
324 period.write_to_slice(&mut payload[0..4]);
325 value_has_to_change.write_to_slice(&mut payload[4..5]);
326 option.write_to_slice(&mut payload[5..6]);
327 min.write_to_slice(&mut payload[6..8]);
328 max.write_to_slice(&mut payload[8..10]);
329
330 #[allow(unused_variables)]
331 let result = self.device.set(u8::from(DistanceUsV2BrickletFunction::SetDistanceCallbackConfiguration), &payload).await?;
332 Ok(())
333 }
334
335 pub async fn get_distance_callback_configuration(&mut self) -> Result<DistanceCallbackConfiguration, TinkerforgeError> {
344 let payload = [0; 0];
345
346 #[allow(unused_variables)]
347 let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetDistanceCallbackConfiguration), &payload).await?;
348 Ok(DistanceCallbackConfiguration::from_le_byte_slice(result.body()))
349 }
350
351 pub async fn set_update_rate(&mut self, update_rate: u8) -> Result<(), TinkerforgeError> {
360 let mut payload = [0; 1];
361 update_rate.write_to_slice(&mut payload[0..1]);
362
363 #[allow(unused_variables)]
364 let result = self.device.set(u8::from(DistanceUsV2BrickletFunction::SetUpdateRate), &payload).await?;
365 Ok(())
366 }
367
368 pub async fn get_update_rate(&mut self) -> Result<u8, TinkerforgeError> {
374 let payload = [0; 0];
375
376 #[allow(unused_variables)]
377 let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetUpdateRate), &payload).await?;
378 Ok(u8::from_le_byte_slice(result.body()))
379 }
380
381 pub async fn set_distance_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
390 let mut payload = [0; 1];
391 config.write_to_slice(&mut payload[0..1]);
392
393 #[allow(unused_variables)]
394 let result = self.device.set(u8::from(DistanceUsV2BrickletFunction::SetDistanceLedConfig), &payload).await?;
395 Ok(())
396 }
397
398 pub async fn get_distance_led_config(&mut self) -> Result<u8, TinkerforgeError> {
406 let payload = [0; 0];
407
408 #[allow(unused_variables)]
409 let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetDistanceLedConfig), &payload).await?;
410 Ok(u8::from_le_byte_slice(result.body()))
411 }
412
413 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
425 let payload = [0; 0];
426
427 #[allow(unused_variables)]
428 let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
429 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
430 }
431
432 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
455 let mut payload = [0; 1];
456 mode.write_to_slice(&mut payload[0..1]);
457
458 #[allow(unused_variables)]
459 let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::SetBootloaderMode), &payload).await?;
460 Ok(u8::from_le_byte_slice(result.body()))
461 }
462
463 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
472 let payload = [0; 0];
473
474 #[allow(unused_variables)]
475 let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetBootloaderMode), &payload).await?;
476 Ok(u8::from_le_byte_slice(result.body()))
477 }
478
479 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
486 let mut payload = [0; 4];
487 pointer.write_to_slice(&mut payload[0..4]);
488
489 #[allow(unused_variables)]
490 let result = self.device.set(u8::from(DistanceUsV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
491 Ok(())
492 }
493
494 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
503 let mut payload = [0; 64];
504 data.write_to_slice(&mut payload[0..64]);
505
506 #[allow(unused_variables)]
507 let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::WriteFirmware), &payload).await?;
508 Ok(u8::from_le_byte_slice(result.body()))
509 }
510
511 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
525 let mut payload = [0; 1];
526 config.write_to_slice(&mut payload[0..1]);
527
528 #[allow(unused_variables)]
529 let result = self.device.set(u8::from(DistanceUsV2BrickletFunction::SetStatusLedConfig), &payload).await?;
530 Ok(())
531 }
532
533 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
541 let payload = [0; 0];
542
543 #[allow(unused_variables)]
544 let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetStatusLedConfig), &payload).await?;
545 Ok(u8::from_le_byte_slice(result.body()))
546 }
547
548 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
555 let payload = [0; 0];
556
557 #[allow(unused_variables)]
558 let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetChipTemperature), &payload).await?;
559 Ok(i16::from_le_byte_slice(result.body()))
560 }
561
562 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
569 let payload = [0; 0];
570
571 #[allow(unused_variables)]
572 let result = self.device.set(u8::from(DistanceUsV2BrickletFunction::Reset), &payload).await?;
573 Ok(())
574 }
575
576 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
582 let mut payload = [0; 4];
583 uid.write_to_slice(&mut payload[0..4]);
584
585 #[allow(unused_variables)]
586 let result = self.device.set(u8::from(DistanceUsV2BrickletFunction::WriteUid), &payload).await?;
587 Ok(())
588 }
589
590 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
593 let payload = [0; 0];
594
595 #[allow(unused_variables)]
596 let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::ReadUid), &payload).await?;
597 Ok(u32::from_le_byte_slice(result.body()))
598 }
599
600 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
611 let payload = [0; 0];
612
613 #[allow(unused_variables)]
614 let result = self.device.get(u8::from(DistanceUsV2BrickletFunction::GetIdentity), &payload).await?;
615 Ok(Identity::from_le_byte_slice(result.body()))
616 }
617}