1use crate::{
15 byte_converter::*,
16 converting_receiver::{BrickletRecvTimeoutError, ConvertingReceiver},
17 device::*,
18 ip_connection::GetRequestSender,
19 low_level_traits::*,
20};
21pub enum OneWireBrickletFunction {
22 SearchBusLowLevel,
23 ResetBus,
24 Write,
25 Read,
26 WriteCommand,
27 SetCommunicationLedConfig,
28 GetCommunicationLedConfig,
29 GetSpitfpErrorCount,
30 SetBootloaderMode,
31 GetBootloaderMode,
32 SetWriteFirmwarePointer,
33 WriteFirmware,
34 SetStatusLedConfig,
35 GetStatusLedConfig,
36 GetChipTemperature,
37 Reset,
38 WriteUid,
39 ReadUid,
40 GetIdentity,
41}
42impl From<OneWireBrickletFunction> for u8 {
43 fn from(fun: OneWireBrickletFunction) -> Self {
44 match fun {
45 OneWireBrickletFunction::SearchBusLowLevel => 1,
46 OneWireBrickletFunction::ResetBus => 2,
47 OneWireBrickletFunction::Write => 3,
48 OneWireBrickletFunction::Read => 4,
49 OneWireBrickletFunction::WriteCommand => 5,
50 OneWireBrickletFunction::SetCommunicationLedConfig => 6,
51 OneWireBrickletFunction::GetCommunicationLedConfig => 7,
52 OneWireBrickletFunction::GetSpitfpErrorCount => 234,
53 OneWireBrickletFunction::SetBootloaderMode => 235,
54 OneWireBrickletFunction::GetBootloaderMode => 236,
55 OneWireBrickletFunction::SetWriteFirmwarePointer => 237,
56 OneWireBrickletFunction::WriteFirmware => 238,
57 OneWireBrickletFunction::SetStatusLedConfig => 239,
58 OneWireBrickletFunction::GetStatusLedConfig => 240,
59 OneWireBrickletFunction::GetChipTemperature => 242,
60 OneWireBrickletFunction::Reset => 243,
61 OneWireBrickletFunction::WriteUid => 248,
62 OneWireBrickletFunction::ReadUid => 249,
63 OneWireBrickletFunction::GetIdentity => 255,
64 }
65 }
66}
67pub const ONE_WIRE_BRICKLET_STATUS_OK: u8 = 0;
68pub const ONE_WIRE_BRICKLET_STATUS_BUSY: u8 = 1;
69pub const ONE_WIRE_BRICKLET_STATUS_NO_PRESENCE: u8 = 2;
70pub const ONE_WIRE_BRICKLET_STATUS_TIMEOUT: u8 = 3;
71pub const ONE_WIRE_BRICKLET_STATUS_ERROR: u8 = 4;
72pub const ONE_WIRE_BRICKLET_COMMUNICATION_LED_CONFIG_OFF: u8 = 0;
73pub const ONE_WIRE_BRICKLET_COMMUNICATION_LED_CONFIG_ON: u8 = 1;
74pub const ONE_WIRE_BRICKLET_COMMUNICATION_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
75pub const ONE_WIRE_BRICKLET_COMMUNICATION_LED_CONFIG_SHOW_COMMUNICATION: u8 = 3;
76pub const ONE_WIRE_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
77pub const ONE_WIRE_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
78pub const ONE_WIRE_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
79pub const ONE_WIRE_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
80pub const ONE_WIRE_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
81pub const ONE_WIRE_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
82pub const ONE_WIRE_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
83pub const ONE_WIRE_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
84pub const ONE_WIRE_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
85pub const ONE_WIRE_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
86pub const ONE_WIRE_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
87pub const ONE_WIRE_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
88pub const ONE_WIRE_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
89pub const ONE_WIRE_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
90pub const ONE_WIRE_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
91
92#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
93pub struct SearchBusLowLevel {
94 pub identifier_length: u16,
95 pub identifier_chunk_offset: u16,
96 pub identifier_chunk_data: [u64; 7],
97 pub status: u8,
98}
99impl FromByteSlice for SearchBusLowLevel {
100 fn bytes_expected() -> usize { 61 }
101 fn from_le_byte_slice(bytes: &[u8]) -> SearchBusLowLevel {
102 SearchBusLowLevel {
103 identifier_length: <u16>::from_le_byte_slice(&bytes[0..2]),
104 identifier_chunk_offset: <u16>::from_le_byte_slice(&bytes[2..4]),
105 identifier_chunk_data: <[u64; 7]>::from_le_byte_slice(&bytes[4..60]),
106 status: <u8>::from_le_byte_slice(&bytes[60..61]),
107 }
108 }
109}
110impl LowLevelRead<u64, SearchBusResult> for SearchBusLowLevel {
111 fn ll_message_length(&self) -> usize { self.identifier_length as usize }
112
113 fn ll_message_chunk_offset(&self) -> usize { self.identifier_chunk_offset as usize }
114
115 fn ll_message_chunk_data(&self) -> &[u64] { &self.identifier_chunk_data }
116
117 fn get_result(&self) -> SearchBusResult { SearchBusResult { status: self.status } }
118}
119
120#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
121pub struct Read {
122 pub data: u8,
123 pub status: u8,
124}
125impl FromByteSlice for Read {
126 fn bytes_expected() -> usize { 2 }
127 fn from_le_byte_slice(bytes: &[u8]) -> Read {
128 Read { data: <u8>::from_le_byte_slice(&bytes[0..1]), status: <u8>::from_le_byte_slice(&bytes[1..2]) }
129 }
130}
131
132#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
133pub struct SpitfpErrorCount {
134 pub error_count_ack_checksum: u32,
135 pub error_count_message_checksum: u32,
136 pub error_count_frame: u32,
137 pub error_count_overflow: u32,
138}
139impl FromByteSlice for SpitfpErrorCount {
140 fn bytes_expected() -> usize { 16 }
141 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
142 SpitfpErrorCount {
143 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
144 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
145 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
146 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
147 }
148 }
149}
150
151#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
152pub struct Identity {
153 pub uid: String,
154 pub connected_uid: String,
155 pub position: char,
156 pub hardware_version: [u8; 3],
157 pub firmware_version: [u8; 3],
158 pub device_identifier: u16,
159}
160impl FromByteSlice for Identity {
161 fn bytes_expected() -> usize { 25 }
162 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
163 Identity {
164 uid: <String>::from_le_byte_slice(&bytes[0..8]),
165 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
166 position: <char>::from_le_byte_slice(&bytes[16..17]),
167 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
168 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
169 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
170 }
171 }
172}
173
174#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
175pub struct SearchBusResult {
176 pub status: u8,
177}
178
179#[derive(Clone)]
181pub struct OneWireBricklet {
182 device: Device,
183}
184impl OneWireBricklet {
185 pub const DEVICE_IDENTIFIER: u16 = 2123;
186 pub const DEVICE_DISPLAY_NAME: &'static str = "One Wire Bricklet";
187 pub fn new<T: GetRequestSender>(uid: &str, req_sender: T) -> OneWireBricklet {
189 let mut result = OneWireBricklet { device: Device::new([2, 0, 0], uid, req_sender, 1) };
190 result.device.response_expected[u8::from(OneWireBrickletFunction::SearchBusLowLevel) as usize] = ResponseExpectedFlag::AlwaysTrue;
191 result.device.response_expected[u8::from(OneWireBrickletFunction::ResetBus) as usize] = ResponseExpectedFlag::AlwaysTrue;
192 result.device.response_expected[u8::from(OneWireBrickletFunction::Write) as usize] = ResponseExpectedFlag::AlwaysTrue;
193 result.device.response_expected[u8::from(OneWireBrickletFunction::Read) as usize] = ResponseExpectedFlag::AlwaysTrue;
194 result.device.response_expected[u8::from(OneWireBrickletFunction::WriteCommand) as usize] = ResponseExpectedFlag::AlwaysTrue;
195 result.device.response_expected[u8::from(OneWireBrickletFunction::SetCommunicationLedConfig) as usize] =
196 ResponseExpectedFlag::False;
197 result.device.response_expected[u8::from(OneWireBrickletFunction::GetCommunicationLedConfig) as usize] =
198 ResponseExpectedFlag::AlwaysTrue;
199 result.device.response_expected[u8::from(OneWireBrickletFunction::GetSpitfpErrorCount) as usize] = ResponseExpectedFlag::AlwaysTrue;
200 result.device.response_expected[u8::from(OneWireBrickletFunction::SetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
201 result.device.response_expected[u8::from(OneWireBrickletFunction::GetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
202 result.device.response_expected[u8::from(OneWireBrickletFunction::SetWriteFirmwarePointer) as usize] = ResponseExpectedFlag::False;
203 result.device.response_expected[u8::from(OneWireBrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
204 result.device.response_expected[u8::from(OneWireBrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
205 result.device.response_expected[u8::from(OneWireBrickletFunction::GetStatusLedConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
206 result.device.response_expected[u8::from(OneWireBrickletFunction::GetChipTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
207 result.device.response_expected[u8::from(OneWireBrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
208 result.device.response_expected[u8::from(OneWireBrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
209 result.device.response_expected[u8::from(OneWireBrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
210 result.device.response_expected[u8::from(OneWireBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
211 result
212 }
213
214 pub fn get_response_expected(&mut self, fun: OneWireBrickletFunction) -> Result<bool, GetResponseExpectedError> {
229 self.device.get_response_expected(u8::from(fun))
230 }
231
232 pub fn set_response_expected(&mut self, fun: OneWireBrickletFunction, response_expected: bool) -> 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) { self.device.set_response_expected_all(response_expected) }
246
247 pub fn get_api_version(&self) -> [u8; 3] { self.device.api_version }
250
251 pub fn search_bus_low_level(&self) -> ConvertingReceiver<SearchBusLowLevel> {
266 let payload = vec![0; 0];
267
268 self.device.get(u8::from(OneWireBrickletFunction::SearchBusLowLevel), payload)
269 }
270
271 pub fn search_bus(&self) -> Result<(Vec<u64>, u8), BrickletRecvTimeoutError> {
279 let ll_result = self.device.get_high_level(0, &mut || self.search_bus_low_level().recv())?;
280 Ok((ll_result.0, ll_result.1.status))
281 }
282
283 pub fn reset_bus(&self) -> ConvertingReceiver<u8> {
292 let payload = vec![0; 0];
293
294 self.device.get(u8::from(OneWireBrickletFunction::ResetBus), payload)
295 }
296
297 pub fn write(&self, data: u8) -> ConvertingReceiver<u8> {
306 let mut payload = vec![0; 1];
307 payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(data));
308
309 self.device.get(u8::from(OneWireBrickletFunction::Write), payload)
310 }
311
312 pub fn read(&self) -> ConvertingReceiver<Read> {
321 let payload = vec![0; 0];
322
323 self.device.get(u8::from(OneWireBrickletFunction::Read), payload)
324 }
325
326 pub fn write_command(&self, identifier: u64, command: u8) -> ConvertingReceiver<u8> {
341 let mut payload = vec![0; 9];
342 payload[0..8].copy_from_slice(&<u64>::to_le_byte_vec(identifier));
343 payload[8..9].copy_from_slice(&<u8>::to_le_byte_vec(command));
344
345 self.device.get(u8::from(OneWireBrickletFunction::WriteCommand), payload)
346 }
347
348 pub fn set_communication_led_config(&self, config: u8) -> ConvertingReceiver<()> {
361 let mut payload = vec![0; 1];
362 payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(config));
363
364 self.device.set(u8::from(OneWireBrickletFunction::SetCommunicationLedConfig), payload)
365 }
366
367 pub fn get_communication_led_config(&self) -> ConvertingReceiver<u8> {
375 let payload = vec![0; 0];
376
377 self.device.get(u8::from(OneWireBrickletFunction::GetCommunicationLedConfig), payload)
378 }
379
380 pub fn get_spitfp_error_count(&self) -> ConvertingReceiver<SpitfpErrorCount> {
392 let payload = vec![0; 0];
393
394 self.device.get(u8::from(OneWireBrickletFunction::GetSpitfpErrorCount), payload)
395 }
396
397 pub fn set_bootloader_mode(&self, mode: u8) -> ConvertingReceiver<u8> {
420 let mut payload = vec![0; 1];
421 payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(mode));
422
423 self.device.get(u8::from(OneWireBrickletFunction::SetBootloaderMode), payload)
424 }
425
426 pub fn get_bootloader_mode(&self) -> ConvertingReceiver<u8> {
435 let payload = vec![0; 0];
436
437 self.device.get(u8::from(OneWireBrickletFunction::GetBootloaderMode), payload)
438 }
439
440 pub fn set_write_firmware_pointer(&self, pointer: u32) -> ConvertingReceiver<()> {
447 let mut payload = vec![0; 4];
448 payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(pointer));
449
450 self.device.set(u8::from(OneWireBrickletFunction::SetWriteFirmwarePointer), payload)
451 }
452
453 pub fn write_firmware(&self, data: [u8; 64]) -> ConvertingReceiver<u8> {
462 let mut payload = vec![0; 64];
463 payload[0..64].copy_from_slice(&<[u8; 64]>::to_le_byte_vec(data));
464
465 self.device.get(u8::from(OneWireBrickletFunction::WriteFirmware), payload)
466 }
467
468 pub fn set_status_led_config(&self, config: u8) -> ConvertingReceiver<()> {
482 let mut payload = vec![0; 1];
483 payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(config));
484
485 self.device.set(u8::from(OneWireBrickletFunction::SetStatusLedConfig), payload)
486 }
487
488 pub fn get_status_led_config(&self) -> ConvertingReceiver<u8> {
496 let payload = vec![0; 0];
497
498 self.device.get(u8::from(OneWireBrickletFunction::GetStatusLedConfig), payload)
499 }
500
501 pub fn get_chip_temperature(&self) -> ConvertingReceiver<i16> {
508 let payload = vec![0; 0];
509
510 self.device.get(u8::from(OneWireBrickletFunction::GetChipTemperature), payload)
511 }
512
513 pub fn reset(&self) -> ConvertingReceiver<()> {
520 let payload = vec![0; 0];
521
522 self.device.set(u8::from(OneWireBrickletFunction::Reset), payload)
523 }
524
525 pub fn write_uid(&self, uid: u32) -> ConvertingReceiver<()> {
531 let mut payload = vec![0; 4];
532 payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(uid));
533
534 self.device.set(u8::from(OneWireBrickletFunction::WriteUid), payload)
535 }
536
537 pub fn read_uid(&self) -> ConvertingReceiver<u32> {
540 let payload = vec![0; 0];
541
542 self.device.get(u8::from(OneWireBrickletFunction::ReadUid), payload)
543 }
544
545 pub fn get_identity(&self) -> ConvertingReceiver<Identity> {
556 let payload = vec![0; 0];
557
558 self.device.get(u8::from(OneWireBrickletFunction::GetIdentity), payload)
559 }
560}