1#[allow(unused_imports)]
15use crate::{
16 base58::Uid, byte_converter::*, converting_receiver::BrickletRecvTimeoutError, device::*, error::TinkerforgeError,
17 ip_connection::async_io::AsyncIpConnection, low_level_traits::LowLevelRead,
18};
19#[allow(unused_imports)]
20use futures_core::Stream;
21#[allow(unused_imports)]
22use tokio_stream::StreamExt;
23pub enum OneWireBrickletFunction {
24 SearchBusLowLevel,
25 ResetBus,
26 Write,
27 Read,
28 WriteCommand,
29 SetCommunicationLedConfig,
30 GetCommunicationLedConfig,
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}
44impl From<OneWireBrickletFunction> for u8 {
45 fn from(fun: OneWireBrickletFunction) -> Self {
46 match fun {
47 OneWireBrickletFunction::SearchBusLowLevel => 1,
48 OneWireBrickletFunction::ResetBus => 2,
49 OneWireBrickletFunction::Write => 3,
50 OneWireBrickletFunction::Read => 4,
51 OneWireBrickletFunction::WriteCommand => 5,
52 OneWireBrickletFunction::SetCommunicationLedConfig => 6,
53 OneWireBrickletFunction::GetCommunicationLedConfig => 7,
54 OneWireBrickletFunction::GetSpitfpErrorCount => 234,
55 OneWireBrickletFunction::SetBootloaderMode => 235,
56 OneWireBrickletFunction::GetBootloaderMode => 236,
57 OneWireBrickletFunction::SetWriteFirmwarePointer => 237,
58 OneWireBrickletFunction::WriteFirmware => 238,
59 OneWireBrickletFunction::SetStatusLedConfig => 239,
60 OneWireBrickletFunction::GetStatusLedConfig => 240,
61 OneWireBrickletFunction::GetChipTemperature => 242,
62 OneWireBrickletFunction::Reset => 243,
63 OneWireBrickletFunction::WriteUid => 248,
64 OneWireBrickletFunction::ReadUid => 249,
65 OneWireBrickletFunction::GetIdentity => 255,
66 }
67 }
68}
69pub const ONE_WIRE_BRICKLET_STATUS_OK: u8 = 0;
70pub const ONE_WIRE_BRICKLET_STATUS_BUSY: u8 = 1;
71pub const ONE_WIRE_BRICKLET_STATUS_NO_PRESENCE: u8 = 2;
72pub const ONE_WIRE_BRICKLET_STATUS_TIMEOUT: u8 = 3;
73pub const ONE_WIRE_BRICKLET_STATUS_ERROR: u8 = 4;
74pub const ONE_WIRE_BRICKLET_COMMUNICATION_LED_CONFIG_OFF: u8 = 0;
75pub const ONE_WIRE_BRICKLET_COMMUNICATION_LED_CONFIG_ON: u8 = 1;
76pub const ONE_WIRE_BRICKLET_COMMUNICATION_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
77pub const ONE_WIRE_BRICKLET_COMMUNICATION_LED_CONFIG_SHOW_COMMUNICATION: u8 = 3;
78pub const ONE_WIRE_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
79pub const ONE_WIRE_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
80pub const ONE_WIRE_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
81pub const ONE_WIRE_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
82pub const ONE_WIRE_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
83pub const ONE_WIRE_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
84pub const ONE_WIRE_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
85pub const ONE_WIRE_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
86pub const ONE_WIRE_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
87pub const ONE_WIRE_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
88pub const ONE_WIRE_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
89pub const ONE_WIRE_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
90pub const ONE_WIRE_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
91pub const ONE_WIRE_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
92pub const ONE_WIRE_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
93
94#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
95pub struct SearchBusLowLevel {
96 pub identifier_length: u16,
97 pub identifier_chunk_offset: u16,
98 pub identifier_chunk_data: [u64; 7],
99 pub status: u8,
100}
101impl FromByteSlice for SearchBusLowLevel {
102 fn bytes_expected() -> usize {
103 61
104 }
105 fn from_le_byte_slice(bytes: &[u8]) -> SearchBusLowLevel {
106 SearchBusLowLevel {
107 identifier_length: <u16>::from_le_byte_slice(&bytes[0..2]),
108 identifier_chunk_offset: <u16>::from_le_byte_slice(&bytes[2..4]),
109 identifier_chunk_data: <[u64; 7]>::from_le_byte_slice(&bytes[4..60]),
110 status: <u8>::from_le_byte_slice(&bytes[60..61]),
111 }
112 }
113}
114impl LowLevelRead<u64, SearchBusResult> for SearchBusLowLevel {
115 fn ll_message_length(&self) -> usize {
116 self.identifier_length as usize
117 }
118
119 fn ll_message_chunk_offset(&self) -> usize {
120 self.identifier_chunk_offset as usize
121 }
122
123 fn ll_message_chunk_data(&self) -> &[u64] {
124 &self.identifier_chunk_data
125 }
126
127 fn get_result(&self) -> SearchBusResult {
128 SearchBusResult { status: self.status }
129 }
130}
131
132#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
133pub struct Read {
134 pub data: u8,
135 pub status: u8,
136}
137impl FromByteSlice for Read {
138 fn bytes_expected() -> usize {
139 2
140 }
141 fn from_le_byte_slice(bytes: &[u8]) -> Read {
142 Read { data: <u8>::from_le_byte_slice(&bytes[0..1]), status: <u8>::from_le_byte_slice(&bytes[1..2]) }
143 }
144}
145
146#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
147pub struct SpitfpErrorCount {
148 pub error_count_ack_checksum: u32,
149 pub error_count_message_checksum: u32,
150 pub error_count_frame: u32,
151 pub error_count_overflow: u32,
152}
153impl FromByteSlice for SpitfpErrorCount {
154 fn bytes_expected() -> usize {
155 16
156 }
157 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
158 SpitfpErrorCount {
159 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
160 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
161 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
162 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
163 }
164 }
165}
166
167#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
168pub struct Identity {
169 pub uid: String,
170 pub connected_uid: String,
171 pub position: char,
172 pub hardware_version: [u8; 3],
173 pub firmware_version: [u8; 3],
174 pub device_identifier: u16,
175}
176impl FromByteSlice for Identity {
177 fn bytes_expected() -> usize {
178 25
179 }
180 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
181 Identity {
182 uid: <String>::from_le_byte_slice(&bytes[0..8]),
183 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
184 position: <char>::from_le_byte_slice(&bytes[16..17]),
185 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
186 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
187 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
188 }
189 }
190}
191
192#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
193pub struct SearchBusResult {
194 pub status: u8,
195}
196
197#[derive(Clone)]
199pub struct OneWireBricklet {
200 device: Device,
201}
202impl OneWireBricklet {
203 pub const DEVICE_IDENTIFIER: u16 = 2123;
204 pub const DEVICE_DISPLAY_NAME: &'static str = "One Wire Bricklet";
205 pub fn new(uid: Uid, connection: AsyncIpConnection) -> OneWireBricklet {
207 let mut result = OneWireBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
208 result.device.response_expected[u8::from(OneWireBrickletFunction::SearchBusLowLevel) as usize] = ResponseExpectedFlag::AlwaysTrue;
209 result.device.response_expected[u8::from(OneWireBrickletFunction::ResetBus) as usize] = ResponseExpectedFlag::AlwaysTrue;
210 result.device.response_expected[u8::from(OneWireBrickletFunction::Write) as usize] = ResponseExpectedFlag::AlwaysTrue;
211 result.device.response_expected[u8::from(OneWireBrickletFunction::Read) as usize] = ResponseExpectedFlag::AlwaysTrue;
212 result.device.response_expected[u8::from(OneWireBrickletFunction::WriteCommand) as usize] = ResponseExpectedFlag::AlwaysTrue;
213 result.device.response_expected[u8::from(OneWireBrickletFunction::SetCommunicationLedConfig) as usize] =
214 ResponseExpectedFlag::False;
215 result.device.response_expected[u8::from(OneWireBrickletFunction::GetCommunicationLedConfig) as usize] =
216 ResponseExpectedFlag::AlwaysTrue;
217 result.device.response_expected[u8::from(OneWireBrickletFunction::GetSpitfpErrorCount) as usize] = ResponseExpectedFlag::AlwaysTrue;
218 result.device.response_expected[u8::from(OneWireBrickletFunction::SetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
219 result.device.response_expected[u8::from(OneWireBrickletFunction::GetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
220 result.device.response_expected[u8::from(OneWireBrickletFunction::SetWriteFirmwarePointer) as usize] = ResponseExpectedFlag::False;
221 result.device.response_expected[u8::from(OneWireBrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
222 result.device.response_expected[u8::from(OneWireBrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
223 result.device.response_expected[u8::from(OneWireBrickletFunction::GetStatusLedConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
224 result.device.response_expected[u8::from(OneWireBrickletFunction::GetChipTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
225 result.device.response_expected[u8::from(OneWireBrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
226 result.device.response_expected[u8::from(OneWireBrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
227 result.device.response_expected[u8::from(OneWireBrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
228 result.device.response_expected[u8::from(OneWireBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
229 result
230 }
231
232 pub fn get_response_expected(&mut self, fun: OneWireBrickletFunction) -> Result<bool, GetResponseExpectedError> {
247 self.device.get_response_expected(u8::from(fun))
248 }
249
250 pub fn set_response_expected(&mut self, fun: OneWireBrickletFunction, response_expected: bool) -> Result<(), SetResponseExpectedError> {
259 self.device.set_response_expected(u8::from(fun), response_expected)
260 }
261
262 pub fn set_response_expected_all(&mut self, response_expected: bool) {
264 self.device.set_response_expected_all(response_expected)
265 }
266
267 pub fn get_api_version(&self) -> [u8; 3] {
270 self.device.api_version
271 }
272
273 pub async fn search_bus_low_level(&mut self) -> Result<SearchBusLowLevel, TinkerforgeError> {
288 let payload = [0; 0];
289
290 #[allow(unused_variables)]
291 let result = self.device.get(u8::from(OneWireBrickletFunction::SearchBusLowLevel), &payload).await?;
292 Ok(SearchBusLowLevel::from_le_byte_slice(result.body()))
293 }
294
295 pub async fn reset_bus(&mut self) -> Result<u8, TinkerforgeError> {
304 let payload = [0; 0];
305
306 #[allow(unused_variables)]
307 let result = self.device.get(u8::from(OneWireBrickletFunction::ResetBus), &payload).await?;
308 Ok(u8::from_le_byte_slice(result.body()))
309 }
310
311 pub async fn write(&mut self, data: u8) -> Result<u8, TinkerforgeError> {
320 let mut payload = [0; 1];
321 data.write_to_slice(&mut payload[0..1]);
322
323 #[allow(unused_variables)]
324 let result = self.device.get(u8::from(OneWireBrickletFunction::Write), &payload).await?;
325 Ok(u8::from_le_byte_slice(result.body()))
326 }
327
328 pub async fn read(&mut self) -> Result<Read, TinkerforgeError> {
337 let payload = [0; 0];
338
339 #[allow(unused_variables)]
340 let result = self.device.get(u8::from(OneWireBrickletFunction::Read), &payload).await?;
341 Ok(Read::from_le_byte_slice(result.body()))
342 }
343
344 pub async fn write_command(&mut self, identifier: u64, command: u8) -> Result<u8, TinkerforgeError> {
359 let mut payload = [0; 9];
360 identifier.write_to_slice(&mut payload[0..8]);
361 command.write_to_slice(&mut payload[8..9]);
362
363 #[allow(unused_variables)]
364 let result = self.device.get(u8::from(OneWireBrickletFunction::WriteCommand), &payload).await?;
365 Ok(u8::from_le_byte_slice(result.body()))
366 }
367
368 pub async fn set_communication_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
381 let mut payload = [0; 1];
382 config.write_to_slice(&mut payload[0..1]);
383
384 #[allow(unused_variables)]
385 let result = self.device.set(u8::from(OneWireBrickletFunction::SetCommunicationLedConfig), &payload).await?;
386 Ok(())
387 }
388
389 pub async fn get_communication_led_config(&mut self) -> Result<u8, TinkerforgeError> {
397 let payload = [0; 0];
398
399 #[allow(unused_variables)]
400 let result = self.device.get(u8::from(OneWireBrickletFunction::GetCommunicationLedConfig), &payload).await?;
401 Ok(u8::from_le_byte_slice(result.body()))
402 }
403
404 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
416 let payload = [0; 0];
417
418 #[allow(unused_variables)]
419 let result = self.device.get(u8::from(OneWireBrickletFunction::GetSpitfpErrorCount), &payload).await?;
420 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
421 }
422
423 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
446 let mut payload = [0; 1];
447 mode.write_to_slice(&mut payload[0..1]);
448
449 #[allow(unused_variables)]
450 let result = self.device.get(u8::from(OneWireBrickletFunction::SetBootloaderMode), &payload).await?;
451 Ok(u8::from_le_byte_slice(result.body()))
452 }
453
454 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
463 let payload = [0; 0];
464
465 #[allow(unused_variables)]
466 let result = self.device.get(u8::from(OneWireBrickletFunction::GetBootloaderMode), &payload).await?;
467 Ok(u8::from_le_byte_slice(result.body()))
468 }
469
470 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
477 let mut payload = [0; 4];
478 pointer.write_to_slice(&mut payload[0..4]);
479
480 #[allow(unused_variables)]
481 let result = self.device.set(u8::from(OneWireBrickletFunction::SetWriteFirmwarePointer), &payload).await?;
482 Ok(())
483 }
484
485 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
494 let mut payload = [0; 64];
495 data.write_to_slice(&mut payload[0..64]);
496
497 #[allow(unused_variables)]
498 let result = self.device.get(u8::from(OneWireBrickletFunction::WriteFirmware), &payload).await?;
499 Ok(u8::from_le_byte_slice(result.body()))
500 }
501
502 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
516 let mut payload = [0; 1];
517 config.write_to_slice(&mut payload[0..1]);
518
519 #[allow(unused_variables)]
520 let result = self.device.set(u8::from(OneWireBrickletFunction::SetStatusLedConfig), &payload).await?;
521 Ok(())
522 }
523
524 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
532 let payload = [0; 0];
533
534 #[allow(unused_variables)]
535 let result = self.device.get(u8::from(OneWireBrickletFunction::GetStatusLedConfig), &payload).await?;
536 Ok(u8::from_le_byte_slice(result.body()))
537 }
538
539 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
546 let payload = [0; 0];
547
548 #[allow(unused_variables)]
549 let result = self.device.get(u8::from(OneWireBrickletFunction::GetChipTemperature), &payload).await?;
550 Ok(i16::from_le_byte_slice(result.body()))
551 }
552
553 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
560 let payload = [0; 0];
561
562 #[allow(unused_variables)]
563 let result = self.device.set(u8::from(OneWireBrickletFunction::Reset), &payload).await?;
564 Ok(())
565 }
566
567 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
573 let mut payload = [0; 4];
574 uid.write_to_slice(&mut payload[0..4]);
575
576 #[allow(unused_variables)]
577 let result = self.device.set(u8::from(OneWireBrickletFunction::WriteUid), &payload).await?;
578 Ok(())
579 }
580
581 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
584 let payload = [0; 0];
585
586 #[allow(unused_variables)]
587 let result = self.device.get(u8::from(OneWireBrickletFunction::ReadUid), &payload).await?;
588 Ok(u32::from_le_byte_slice(result.body()))
589 }
590
591 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
602 let payload = [0; 0];
603
604 #[allow(unused_variables)]
605 let result = self.device.get(u8::from(OneWireBrickletFunction::GetIdentity), &payload).await?;
606 Ok(Identity::from_le_byte_slice(result.body()))
607 }
608}