1use crate::{
15    byte_converter::*, converting_callback_receiver::ConvertingCallbackReceiver, converting_receiver::ConvertingReceiver, device::*,
16    ip_connection::GetRequestSender,
17};
18pub enum IndustrialQuadRelayV2BrickletFunction {
19    SetValue,
20    GetValue,
21    SetMonoflop,
22    GetMonoflop,
23    SetSelectedValue,
24    SetChannelLedConfig,
25    GetChannelLedConfig,
26    GetSpitfpErrorCount,
27    SetBootloaderMode,
28    GetBootloaderMode,
29    SetWriteFirmwarePointer,
30    WriteFirmware,
31    SetStatusLedConfig,
32    GetStatusLedConfig,
33    GetChipTemperature,
34    Reset,
35    WriteUid,
36    ReadUid,
37    GetIdentity,
38    CallbackMonoflopDone,
39}
40impl From<IndustrialQuadRelayV2BrickletFunction> for u8 {
41    fn from(fun: IndustrialQuadRelayV2BrickletFunction) -> Self {
42        match fun {
43            IndustrialQuadRelayV2BrickletFunction::SetValue => 1,
44            IndustrialQuadRelayV2BrickletFunction::GetValue => 2,
45            IndustrialQuadRelayV2BrickletFunction::SetMonoflop => 3,
46            IndustrialQuadRelayV2BrickletFunction::GetMonoflop => 4,
47            IndustrialQuadRelayV2BrickletFunction::SetSelectedValue => 5,
48            IndustrialQuadRelayV2BrickletFunction::SetChannelLedConfig => 6,
49            IndustrialQuadRelayV2BrickletFunction::GetChannelLedConfig => 7,
50            IndustrialQuadRelayV2BrickletFunction::GetSpitfpErrorCount => 234,
51            IndustrialQuadRelayV2BrickletFunction::SetBootloaderMode => 235,
52            IndustrialQuadRelayV2BrickletFunction::GetBootloaderMode => 236,
53            IndustrialQuadRelayV2BrickletFunction::SetWriteFirmwarePointer => 237,
54            IndustrialQuadRelayV2BrickletFunction::WriteFirmware => 238,
55            IndustrialQuadRelayV2BrickletFunction::SetStatusLedConfig => 239,
56            IndustrialQuadRelayV2BrickletFunction::GetStatusLedConfig => 240,
57            IndustrialQuadRelayV2BrickletFunction::GetChipTemperature => 242,
58            IndustrialQuadRelayV2BrickletFunction::Reset => 243,
59            IndustrialQuadRelayV2BrickletFunction::WriteUid => 248,
60            IndustrialQuadRelayV2BrickletFunction::ReadUid => 249,
61            IndustrialQuadRelayV2BrickletFunction::GetIdentity => 255,
62            IndustrialQuadRelayV2BrickletFunction::CallbackMonoflopDone => 8,
63        }
64    }
65}
66pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_CHANNEL_LED_CONFIG_OFF: u8 = 0;
67pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_CHANNEL_LED_CONFIG_ON: u8 = 1;
68pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_CHANNEL_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
69pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_CHANNEL_LED_CONFIG_SHOW_CHANNEL_STATUS: u8 = 3;
70pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
71pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
72pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
73pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
74pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
75pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
76pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
77pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
78pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
79pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
80pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
81pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
82pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
83pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
84pub const INDUSTRIAL_QUAD_RELAY_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
85
86#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
87pub struct Monoflop {
88    pub value: bool,
89    pub time: u32,
90    pub time_remaining: u32,
91}
92impl FromByteSlice for Monoflop {
93    fn bytes_expected() -> usize { 9 }
94    fn from_le_byte_slice(bytes: &[u8]) -> Monoflop {
95        Monoflop {
96            value: <bool>::from_le_byte_slice(&bytes[0..1]),
97            time: <u32>::from_le_byte_slice(&bytes[1..5]),
98            time_remaining: <u32>::from_le_byte_slice(&bytes[5..9]),
99        }
100    }
101}
102
103#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
104pub struct MonoflopDoneEvent {
105    pub channel: u8,
106    pub value: bool,
107}
108impl FromByteSlice for MonoflopDoneEvent {
109    fn bytes_expected() -> usize { 2 }
110    fn from_le_byte_slice(bytes: &[u8]) -> MonoflopDoneEvent {
111        MonoflopDoneEvent { channel: <u8>::from_le_byte_slice(&bytes[0..1]), value: <bool>::from_le_byte_slice(&bytes[1..2]) }
112    }
113}
114
115#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
116pub struct SpitfpErrorCount {
117    pub error_count_ack_checksum: u32,
118    pub error_count_message_checksum: u32,
119    pub error_count_frame: u32,
120    pub error_count_overflow: u32,
121}
122impl FromByteSlice for SpitfpErrorCount {
123    fn bytes_expected() -> usize { 16 }
124    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
125        SpitfpErrorCount {
126            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
127            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
128            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
129            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
130        }
131    }
132}
133
134#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
135pub struct Identity {
136    pub uid: String,
137    pub connected_uid: String,
138    pub position: char,
139    pub hardware_version: [u8; 3],
140    pub firmware_version: [u8; 3],
141    pub device_identifier: u16,
142}
143impl FromByteSlice for Identity {
144    fn bytes_expected() -> usize { 25 }
145    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
146        Identity {
147            uid: <String>::from_le_byte_slice(&bytes[0..8]),
148            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
149            position: <char>::from_le_byte_slice(&bytes[16..17]),
150            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
151            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
152            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
153        }
154    }
155}
156
157#[derive(Clone)]
159pub struct IndustrialQuadRelayV2Bricklet {
160    device: Device,
161}
162impl IndustrialQuadRelayV2Bricklet {
163    pub const DEVICE_IDENTIFIER: u16 = 2102;
164    pub const DEVICE_DISPLAY_NAME: &'static str = "Industrial Quad Relay Bricklet 2.0";
165    pub fn new<T: GetRequestSender>(uid: &str, req_sender: T) -> IndustrialQuadRelayV2Bricklet {
167        let mut result = IndustrialQuadRelayV2Bricklet { device: Device::new([2, 0, 0], uid, req_sender, 0) };
168        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::SetValue) as usize] = ResponseExpectedFlag::False;
169        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::GetValue) as usize] =
170            ResponseExpectedFlag::AlwaysTrue;
171        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::SetMonoflop) as usize] =
172            ResponseExpectedFlag::False;
173        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::GetMonoflop) as usize] =
174            ResponseExpectedFlag::AlwaysTrue;
175        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::SetSelectedValue) as usize] =
176            ResponseExpectedFlag::False;
177        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::SetChannelLedConfig) as usize] =
178            ResponseExpectedFlag::False;
179        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::GetChannelLedConfig) as usize] =
180            ResponseExpectedFlag::AlwaysTrue;
181        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::GetSpitfpErrorCount) as usize] =
182            ResponseExpectedFlag::AlwaysTrue;
183        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::SetBootloaderMode) as usize] =
184            ResponseExpectedFlag::AlwaysTrue;
185        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::GetBootloaderMode) as usize] =
186            ResponseExpectedFlag::AlwaysTrue;
187        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
188            ResponseExpectedFlag::False;
189        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::WriteFirmware) as usize] =
190            ResponseExpectedFlag::AlwaysTrue;
191        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::SetStatusLedConfig) as usize] =
192            ResponseExpectedFlag::False;
193        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::GetStatusLedConfig) as usize] =
194            ResponseExpectedFlag::AlwaysTrue;
195        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::GetChipTemperature) as usize] =
196            ResponseExpectedFlag::AlwaysTrue;
197        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
198        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
199        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::ReadUid) as usize] =
200            ResponseExpectedFlag::AlwaysTrue;
201        result.device.response_expected[u8::from(IndustrialQuadRelayV2BrickletFunction::GetIdentity) as usize] =
202            ResponseExpectedFlag::AlwaysTrue;
203        result
204    }
205
206    pub fn get_response_expected(&mut self, fun: IndustrialQuadRelayV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
221        self.device.get_response_expected(u8::from(fun))
222    }
223
224    pub fn set_response_expected(
233        &mut self,
234        fun: IndustrialQuadRelayV2BrickletFunction,
235        response_expected: bool,
236    ) -> Result<(), SetResponseExpectedError> {
237        self.device.set_response_expected(u8::from(fun), response_expected)
238    }
239
240    pub fn set_response_expected_all(&mut self, response_expected: bool) { self.device.set_response_expected_all(response_expected) }
242
243    pub fn get_api_version(&self) -> [u8; 3] { self.device.api_version }
246
247    pub fn get_monoflop_done_callback_receiver(&self) -> ConvertingCallbackReceiver<MonoflopDoneEvent> {
251        self.device.get_callback_receiver(u8::from(IndustrialQuadRelayV2BrickletFunction::CallbackMonoflopDone))
252    }
253
254    pub fn set_value(&self, value: [bool; 4]) -> ConvertingReceiver<()> {
261        let mut payload = vec![0; 1];
262        payload[0..1].copy_from_slice(&<[bool; 4]>::to_le_byte_vec(value));
263
264        self.device.set(u8::from(IndustrialQuadRelayV2BrickletFunction::SetValue), payload)
265    }
266
267    pub fn get_value(&self) -> ConvertingReceiver<[bool; 4]> {
269        let payload = vec![0; 0];
270
271        self.device.get(u8::from(IndustrialQuadRelayV2BrickletFunction::GetValue), payload)
272    }
273
274    pub fn set_monoflop(&self, channel: u8, value: bool, time: u32) -> ConvertingReceiver<()> {
292        let mut payload = vec![0; 6];
293        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(channel));
294        payload[1..2].copy_from_slice(&<bool>::to_le_byte_vec(value));
295        payload[2..6].copy_from_slice(&<u32>::to_le_byte_vec(time));
296
297        self.device.set(u8::from(IndustrialQuadRelayV2BrickletFunction::SetMonoflop), payload)
298    }
299
300    pub fn get_monoflop(&self, channel: u8) -> ConvertingReceiver<Monoflop> {
306        let mut payload = vec![0; 1];
307        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(channel));
308
309        self.device.get(u8::from(IndustrialQuadRelayV2BrickletFunction::GetMonoflop), payload)
310    }
311
312    pub fn set_selected_value(&self, channel: u8, value: bool) -> ConvertingReceiver<()> {
318        let mut payload = vec![0; 2];
319        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(channel));
320        payload[1..2].copy_from_slice(&<bool>::to_le_byte_vec(value));
321
322        self.device.set(u8::from(IndustrialQuadRelayV2BrickletFunction::SetSelectedValue), payload)
323    }
324
325    pub fn set_channel_led_config(&self, channel: u8, config: u8) -> ConvertingReceiver<()> {
335        let mut payload = vec![0; 2];
336        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(channel));
337        payload[1..2].copy_from_slice(&<u8>::to_le_byte_vec(config));
338
339        self.device.set(u8::from(IndustrialQuadRelayV2BrickletFunction::SetChannelLedConfig), payload)
340    }
341
342    pub fn get_channel_led_config(&self, channel: u8) -> ConvertingReceiver<u8> {
350        let mut payload = vec![0; 1];
351        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(channel));
352
353        self.device.get(u8::from(IndustrialQuadRelayV2BrickletFunction::GetChannelLedConfig), payload)
354    }
355
356    pub fn get_spitfp_error_count(&self) -> ConvertingReceiver<SpitfpErrorCount> {
368        let payload = vec![0; 0];
369
370        self.device.get(u8::from(IndustrialQuadRelayV2BrickletFunction::GetSpitfpErrorCount), payload)
371    }
372
373    pub fn set_bootloader_mode(&self, mode: u8) -> ConvertingReceiver<u8> {
396        let mut payload = vec![0; 1];
397        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(mode));
398
399        self.device.get(u8::from(IndustrialQuadRelayV2BrickletFunction::SetBootloaderMode), payload)
400    }
401
402    pub fn get_bootloader_mode(&self) -> ConvertingReceiver<u8> {
411        let payload = vec![0; 0];
412
413        self.device.get(u8::from(IndustrialQuadRelayV2BrickletFunction::GetBootloaderMode), payload)
414    }
415
416    pub fn set_write_firmware_pointer(&self, pointer: u32) -> ConvertingReceiver<()> {
423        let mut payload = vec![0; 4];
424        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(pointer));
425
426        self.device.set(u8::from(IndustrialQuadRelayV2BrickletFunction::SetWriteFirmwarePointer), payload)
427    }
428
429    pub fn write_firmware(&self, data: [u8; 64]) -> ConvertingReceiver<u8> {
438        let mut payload = vec![0; 64];
439        payload[0..64].copy_from_slice(&<[u8; 64]>::to_le_byte_vec(data));
440
441        self.device.get(u8::from(IndustrialQuadRelayV2BrickletFunction::WriteFirmware), payload)
442    }
443
444    pub fn set_status_led_config(&self, config: u8) -> ConvertingReceiver<()> {
458        let mut payload = vec![0; 1];
459        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(config));
460
461        self.device.set(u8::from(IndustrialQuadRelayV2BrickletFunction::SetStatusLedConfig), payload)
462    }
463
464    pub fn get_status_led_config(&self) -> ConvertingReceiver<u8> {
472        let payload = vec![0; 0];
473
474        self.device.get(u8::from(IndustrialQuadRelayV2BrickletFunction::GetStatusLedConfig), payload)
475    }
476
477    pub fn get_chip_temperature(&self) -> ConvertingReceiver<i16> {
484        let payload = vec![0; 0];
485
486        self.device.get(u8::from(IndustrialQuadRelayV2BrickletFunction::GetChipTemperature), payload)
487    }
488
489    pub fn reset(&self) -> ConvertingReceiver<()> {
496        let payload = vec![0; 0];
497
498        self.device.set(u8::from(IndustrialQuadRelayV2BrickletFunction::Reset), payload)
499    }
500
501    pub fn write_uid(&self, uid: u32) -> ConvertingReceiver<()> {
507        let mut payload = vec![0; 4];
508        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(uid));
509
510        self.device.set(u8::from(IndustrialQuadRelayV2BrickletFunction::WriteUid), payload)
511    }
512
513    pub fn read_uid(&self) -> ConvertingReceiver<u32> {
516        let payload = vec![0; 0];
517
518        self.device.get(u8::from(IndustrialQuadRelayV2BrickletFunction::ReadUid), payload)
519    }
520
521    pub fn get_identity(&self) -> ConvertingReceiver<Identity> {
532        let payload = vec![0; 0];
533
534        self.device.get(u8::from(IndustrialQuadRelayV2BrickletFunction::GetIdentity), payload)
535    }
536}