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 IsolatorBrickletFunction {
24 GetStatistics,
25 SetSpitfpBaudrateConfig,
26 GetSpitfpBaudrateConfig,
27 SetSpitfpBaudrate,
28 GetSpitfpBaudrate,
29 GetIsolatorSpitfpErrorCount,
30 SetStatisticsCallbackConfiguration,
31 GetStatisticsCallbackConfiguration,
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 CallbackStatistics,
45}
46impl From<IsolatorBrickletFunction> for u8 {
47 fn from(fun: IsolatorBrickletFunction) -> Self {
48 match fun {
49 IsolatorBrickletFunction::GetStatistics => 1,
50 IsolatorBrickletFunction::SetSpitfpBaudrateConfig => 2,
51 IsolatorBrickletFunction::GetSpitfpBaudrateConfig => 3,
52 IsolatorBrickletFunction::SetSpitfpBaudrate => 4,
53 IsolatorBrickletFunction::GetSpitfpBaudrate => 5,
54 IsolatorBrickletFunction::GetIsolatorSpitfpErrorCount => 6,
55 IsolatorBrickletFunction::SetStatisticsCallbackConfiguration => 7,
56 IsolatorBrickletFunction::GetStatisticsCallbackConfiguration => 8,
57 IsolatorBrickletFunction::GetSpitfpErrorCount => 234,
58 IsolatorBrickletFunction::SetBootloaderMode => 235,
59 IsolatorBrickletFunction::GetBootloaderMode => 236,
60 IsolatorBrickletFunction::SetWriteFirmwarePointer => 237,
61 IsolatorBrickletFunction::WriteFirmware => 238,
62 IsolatorBrickletFunction::SetStatusLedConfig => 239,
63 IsolatorBrickletFunction::GetStatusLedConfig => 240,
64 IsolatorBrickletFunction::GetChipTemperature => 242,
65 IsolatorBrickletFunction::Reset => 243,
66 IsolatorBrickletFunction::WriteUid => 248,
67 IsolatorBrickletFunction::ReadUid => 249,
68 IsolatorBrickletFunction::GetIdentity => 255,
69 IsolatorBrickletFunction::CallbackStatistics => 9,
70 }
71 }
72}
73pub const ISOLATOR_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
74pub const ISOLATOR_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
75pub const ISOLATOR_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
76pub const ISOLATOR_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
77pub const ISOLATOR_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
78pub const ISOLATOR_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
79pub const ISOLATOR_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
80pub const ISOLATOR_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
81pub const ISOLATOR_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
82pub const ISOLATOR_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
83pub const ISOLATOR_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
84pub const ISOLATOR_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
85pub const ISOLATOR_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
86pub const ISOLATOR_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
87pub const ISOLATOR_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
88
89#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
90pub struct Statistics {
91 pub messages_from_brick: u32,
92 pub messages_from_bricklet: u32,
93 pub connected_bricklet_device_identifier: u16,
94 pub connected_bricklet_uid: String,
95}
96impl FromByteSlice for Statistics {
97 fn bytes_expected() -> usize {
98 18
99 }
100 fn from_le_byte_slice(bytes: &[u8]) -> Statistics {
101 Statistics {
102 messages_from_brick: <u32>::from_le_byte_slice(&bytes[0..4]),
103 messages_from_bricklet: <u32>::from_le_byte_slice(&bytes[4..8]),
104 connected_bricklet_device_identifier: <u16>::from_le_byte_slice(&bytes[8..10]),
105 connected_bricklet_uid: <String>::from_le_byte_slice(&bytes[10..18]),
106 }
107 }
108}
109
110#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
111pub struct SpitfpBaudrateConfig {
112 pub enable_dynamic_baudrate: bool,
113 pub minimum_dynamic_baudrate: u32,
114}
115impl FromByteSlice for SpitfpBaudrateConfig {
116 fn bytes_expected() -> usize {
117 5
118 }
119 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpBaudrateConfig {
120 SpitfpBaudrateConfig {
121 enable_dynamic_baudrate: <bool>::from_le_byte_slice(&bytes[0..1]),
122 minimum_dynamic_baudrate: <u32>::from_le_byte_slice(&bytes[1..5]),
123 }
124 }
125}
126
127#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
128pub struct IsolatorSpitfpErrorCount {
129 pub error_count_ack_checksum: u32,
130 pub error_count_message_checksum: u32,
131 pub error_count_frame: u32,
132 pub error_count_overflow: u32,
133}
134impl FromByteSlice for IsolatorSpitfpErrorCount {
135 fn bytes_expected() -> usize {
136 16
137 }
138 fn from_le_byte_slice(bytes: &[u8]) -> IsolatorSpitfpErrorCount {
139 IsolatorSpitfpErrorCount {
140 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
141 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
142 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
143 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
144 }
145 }
146}
147
148#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
149pub struct StatisticsCallbackConfiguration {
150 pub period: u32,
151 pub value_has_to_change: bool,
152}
153impl FromByteSlice for StatisticsCallbackConfiguration {
154 fn bytes_expected() -> usize {
155 5
156 }
157 fn from_le_byte_slice(bytes: &[u8]) -> StatisticsCallbackConfiguration {
158 StatisticsCallbackConfiguration {
159 period: <u32>::from_le_byte_slice(&bytes[0..4]),
160 value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
161 }
162 }
163}
164
165#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
166pub struct StatisticsEvent {
167 pub messages_from_brick: u32,
168 pub messages_from_bricklet: u32,
169 pub connected_bricklet_device_identifier: u16,
170 pub connected_bricklet_uid: String,
171}
172impl FromByteSlice for StatisticsEvent {
173 fn bytes_expected() -> usize {
174 18
175 }
176 fn from_le_byte_slice(bytes: &[u8]) -> StatisticsEvent {
177 StatisticsEvent {
178 messages_from_brick: <u32>::from_le_byte_slice(&bytes[0..4]),
179 messages_from_bricklet: <u32>::from_le_byte_slice(&bytes[4..8]),
180 connected_bricklet_device_identifier: <u16>::from_le_byte_slice(&bytes[8..10]),
181 connected_bricklet_uid: <String>::from_le_byte_slice(&bytes[10..18]),
182 }
183 }
184}
185
186#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
187pub struct SpitfpErrorCount {
188 pub error_count_ack_checksum: u32,
189 pub error_count_message_checksum: u32,
190 pub error_count_frame: u32,
191 pub error_count_overflow: u32,
192}
193impl FromByteSlice for SpitfpErrorCount {
194 fn bytes_expected() -> usize {
195 16
196 }
197 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
198 SpitfpErrorCount {
199 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
200 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
201 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
202 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
203 }
204 }
205}
206
207#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
208pub struct Identity {
209 pub uid: String,
210 pub connected_uid: String,
211 pub position: char,
212 pub hardware_version: [u8; 3],
213 pub firmware_version: [u8; 3],
214 pub device_identifier: u16,
215}
216impl FromByteSlice for Identity {
217 fn bytes_expected() -> usize {
218 25
219 }
220 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
221 Identity {
222 uid: <String>::from_le_byte_slice(&bytes[0..8]),
223 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
224 position: <char>::from_le_byte_slice(&bytes[16..17]),
225 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
226 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
227 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
228 }
229 }
230}
231
232#[derive(Clone)]
234pub struct IsolatorBricklet {
235 device: Device,
236}
237impl IsolatorBricklet {
238 pub const DEVICE_IDENTIFIER: u16 = 2122;
239 pub const DEVICE_DISPLAY_NAME: &'static str = "Isolator Bricklet";
240 pub fn new(uid: Uid, connection: AsyncIpConnection) -> IsolatorBricklet {
242 let mut result = IsolatorBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
243 result.device.response_expected[u8::from(IsolatorBrickletFunction::GetStatistics) as usize] = ResponseExpectedFlag::AlwaysTrue;
244 result.device.response_expected[u8::from(IsolatorBrickletFunction::SetSpitfpBaudrateConfig) as usize] = ResponseExpectedFlag::False;
245 result.device.response_expected[u8::from(IsolatorBrickletFunction::GetSpitfpBaudrateConfig) as usize] =
246 ResponseExpectedFlag::AlwaysTrue;
247 result.device.response_expected[u8::from(IsolatorBrickletFunction::SetSpitfpBaudrate) as usize] = ResponseExpectedFlag::False;
248 result.device.response_expected[u8::from(IsolatorBrickletFunction::GetSpitfpBaudrate) as usize] = ResponseExpectedFlag::AlwaysTrue;
249 result.device.response_expected[u8::from(IsolatorBrickletFunction::GetIsolatorSpitfpErrorCount) as usize] =
250 ResponseExpectedFlag::AlwaysTrue;
251 result.device.response_expected[u8::from(IsolatorBrickletFunction::SetStatisticsCallbackConfiguration) as usize] =
252 ResponseExpectedFlag::True;
253 result.device.response_expected[u8::from(IsolatorBrickletFunction::GetStatisticsCallbackConfiguration) as usize] =
254 ResponseExpectedFlag::AlwaysTrue;
255 result.device.response_expected[u8::from(IsolatorBrickletFunction::GetSpitfpErrorCount) as usize] =
256 ResponseExpectedFlag::AlwaysTrue;
257 result.device.response_expected[u8::from(IsolatorBrickletFunction::SetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
258 result.device.response_expected[u8::from(IsolatorBrickletFunction::GetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
259 result.device.response_expected[u8::from(IsolatorBrickletFunction::SetWriteFirmwarePointer) as usize] = ResponseExpectedFlag::False;
260 result.device.response_expected[u8::from(IsolatorBrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
261 result.device.response_expected[u8::from(IsolatorBrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
262 result.device.response_expected[u8::from(IsolatorBrickletFunction::GetStatusLedConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
263 result.device.response_expected[u8::from(IsolatorBrickletFunction::GetChipTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
264 result.device.response_expected[u8::from(IsolatorBrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
265 result.device.response_expected[u8::from(IsolatorBrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
266 result.device.response_expected[u8::from(IsolatorBrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
267 result.device.response_expected[u8::from(IsolatorBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
268 result
269 }
270
271 pub fn get_response_expected(&mut self, fun: IsolatorBrickletFunction) -> Result<bool, GetResponseExpectedError> {
286 self.device.get_response_expected(u8::from(fun))
287 }
288
289 pub fn set_response_expected(
298 &mut self,
299 fun: IsolatorBrickletFunction,
300 response_expected: bool,
301 ) -> Result<(), SetResponseExpectedError> {
302 self.device.set_response_expected(u8::from(fun), response_expected)
303 }
304
305 pub fn set_response_expected_all(&mut self, response_expected: bool) {
307 self.device.set_response_expected_all(response_expected)
308 }
309
310 pub fn get_api_version(&self) -> [u8; 3] {
313 self.device.api_version
314 }
315
316 pub async fn get_statistics_callback_receiver(&mut self) -> impl Stream<Item = StatisticsEvent> {
325 self.device
326 .get_callback_receiver(u8::from(IsolatorBrickletFunction::CallbackStatistics))
327 .await
328 .map(|p| StatisticsEvent::from_le_byte_slice(p.body()))
329 }
330
331 pub async fn get_statistics(&mut self) -> Result<Statistics, TinkerforgeError> {
333 let payload = [0; 0];
334
335 #[allow(unused_variables)]
336 let result = self.device.get(u8::from(IsolatorBrickletFunction::GetStatistics), &payload).await?;
337 Ok(Statistics::from_le_byte_slice(result.body()))
338 }
339
340 pub async fn set_spitfp_baudrate_config(
362 &mut self,
363 enable_dynamic_baudrate: bool,
364 minimum_dynamic_baudrate: u32,
365 ) -> Result<(), TinkerforgeError> {
366 let mut payload = [0; 5];
367 enable_dynamic_baudrate.write_to_slice(&mut payload[0..1]);
368 minimum_dynamic_baudrate.write_to_slice(&mut payload[1..5]);
369
370 #[allow(unused_variables)]
371 let result = self.device.set(u8::from(IsolatorBrickletFunction::SetSpitfpBaudrateConfig), &payload).await?;
372 Ok(())
373 }
374
375 pub async fn get_spitfp_baudrate_config(&mut self) -> Result<SpitfpBaudrateConfig, TinkerforgeError> {
377 let payload = [0; 0];
378
379 #[allow(unused_variables)]
380 let result = self.device.get(u8::from(IsolatorBrickletFunction::GetSpitfpBaudrateConfig), &payload).await?;
381 Ok(SpitfpBaudrateConfig::from_le_byte_slice(result.body()))
382 }
383
384 pub async fn set_spitfp_baudrate(&mut self, baudrate: u32) -> Result<(), TinkerforgeError> {
400 let mut payload = [0; 4];
401 baudrate.write_to_slice(&mut payload[0..4]);
402
403 #[allow(unused_variables)]
404 let result = self.device.set(u8::from(IsolatorBrickletFunction::SetSpitfpBaudrate), &payload).await?;
405 Ok(())
406 }
407
408 pub async fn get_spitfp_baudrate(&mut self) -> Result<u32, TinkerforgeError> {
410 let payload = [0; 0];
411
412 #[allow(unused_variables)]
413 let result = self.device.get(u8::from(IsolatorBrickletFunction::GetSpitfpBaudrate), &payload).await?;
414 Ok(u32::from_le_byte_slice(result.body()))
415 }
416
417 pub async fn get_isolator_spitfp_error_count(&mut self) -> Result<IsolatorSpitfpErrorCount, TinkerforgeError> {
428 let payload = [0; 0];
429
430 #[allow(unused_variables)]
431 let result = self.device.get(u8::from(IsolatorBrickletFunction::GetIsolatorSpitfpErrorCount), &payload).await?;
432 Ok(IsolatorSpitfpErrorCount::from_le_byte_slice(result.body()))
433 }
434
435 pub async fn set_statistics_callback_configuration(&mut self, period: u32, value_has_to_change: bool) -> Result<(), TinkerforgeError> {
448 let mut payload = [0; 5];
449 period.write_to_slice(&mut payload[0..4]);
450 value_has_to_change.write_to_slice(&mut payload[4..5]);
451
452 #[allow(unused_variables)]
453 let result = self.device.set(u8::from(IsolatorBrickletFunction::SetStatisticsCallbackConfiguration), &payload).await?;
454 Ok(())
455 }
456
457 pub async fn get_statistics_callback_configuration(&mut self) -> Result<StatisticsCallbackConfiguration, TinkerforgeError> {
463 let payload = [0; 0];
464
465 #[allow(unused_variables)]
466 let result = self.device.get(u8::from(IsolatorBrickletFunction::GetStatisticsCallbackConfiguration), &payload).await?;
467 Ok(StatisticsCallbackConfiguration::from_le_byte_slice(result.body()))
468 }
469
470 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
482 let payload = [0; 0];
483
484 #[allow(unused_variables)]
485 let result = self.device.get(u8::from(IsolatorBrickletFunction::GetSpitfpErrorCount), &payload).await?;
486 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
487 }
488
489 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
512 let mut payload = [0; 1];
513 mode.write_to_slice(&mut payload[0..1]);
514
515 #[allow(unused_variables)]
516 let result = self.device.get(u8::from(IsolatorBrickletFunction::SetBootloaderMode), &payload).await?;
517 Ok(u8::from_le_byte_slice(result.body()))
518 }
519
520 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
529 let payload = [0; 0];
530
531 #[allow(unused_variables)]
532 let result = self.device.get(u8::from(IsolatorBrickletFunction::GetBootloaderMode), &payload).await?;
533 Ok(u8::from_le_byte_slice(result.body()))
534 }
535
536 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
543 let mut payload = [0; 4];
544 pointer.write_to_slice(&mut payload[0..4]);
545
546 #[allow(unused_variables)]
547 let result = self.device.set(u8::from(IsolatorBrickletFunction::SetWriteFirmwarePointer), &payload).await?;
548 Ok(())
549 }
550
551 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
560 let mut payload = [0; 64];
561 data.write_to_slice(&mut payload[0..64]);
562
563 #[allow(unused_variables)]
564 let result = self.device.get(u8::from(IsolatorBrickletFunction::WriteFirmware), &payload).await?;
565 Ok(u8::from_le_byte_slice(result.body()))
566 }
567
568 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
582 let mut payload = [0; 1];
583 config.write_to_slice(&mut payload[0..1]);
584
585 #[allow(unused_variables)]
586 let result = self.device.set(u8::from(IsolatorBrickletFunction::SetStatusLedConfig), &payload).await?;
587 Ok(())
588 }
589
590 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
598 let payload = [0; 0];
599
600 #[allow(unused_variables)]
601 let result = self.device.get(u8::from(IsolatorBrickletFunction::GetStatusLedConfig), &payload).await?;
602 Ok(u8::from_le_byte_slice(result.body()))
603 }
604
605 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
612 let payload = [0; 0];
613
614 #[allow(unused_variables)]
615 let result = self.device.get(u8::from(IsolatorBrickletFunction::GetChipTemperature), &payload).await?;
616 Ok(i16::from_le_byte_slice(result.body()))
617 }
618
619 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
626 let payload = [0; 0];
627
628 #[allow(unused_variables)]
629 let result = self.device.set(u8::from(IsolatorBrickletFunction::Reset), &payload).await?;
630 Ok(())
631 }
632
633 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
639 let mut payload = [0; 4];
640 uid.write_to_slice(&mut payload[0..4]);
641
642 #[allow(unused_variables)]
643 let result = self.device.set(u8::from(IsolatorBrickletFunction::WriteUid), &payload).await?;
644 Ok(())
645 }
646
647 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
650 let payload = [0; 0];
651
652 #[allow(unused_variables)]
653 let result = self.device.get(u8::from(IsolatorBrickletFunction::ReadUid), &payload).await?;
654 Ok(u32::from_le_byte_slice(result.body()))
655 }
656
657 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
668 let payload = [0; 0];
669
670 #[allow(unused_variables)]
671 let result = self.device.get(u8::from(IsolatorBrickletFunction::GetIdentity), &payload).await?;
672 Ok(Identity::from_le_byte_slice(result.body()))
673 }
674}