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 IndustrialDualAcRelayBrickletFunction {
24 SetValue,
25 GetValue,
26 SetChannelLedConfig,
27 GetChannelLedConfig,
28 SetMonoflop,
29 GetMonoflop,
30 SetSelectedValue,
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 CallbackMonoflopDone,
44}
45impl From<IndustrialDualAcRelayBrickletFunction> for u8 {
46 fn from(fun: IndustrialDualAcRelayBrickletFunction) -> Self {
47 match fun {
48 IndustrialDualAcRelayBrickletFunction::SetValue => 1,
49 IndustrialDualAcRelayBrickletFunction::GetValue => 2,
50 IndustrialDualAcRelayBrickletFunction::SetChannelLedConfig => 3,
51 IndustrialDualAcRelayBrickletFunction::GetChannelLedConfig => 4,
52 IndustrialDualAcRelayBrickletFunction::SetMonoflop => 5,
53 IndustrialDualAcRelayBrickletFunction::GetMonoflop => 6,
54 IndustrialDualAcRelayBrickletFunction::SetSelectedValue => 8,
55 IndustrialDualAcRelayBrickletFunction::GetSpitfpErrorCount => 234,
56 IndustrialDualAcRelayBrickletFunction::SetBootloaderMode => 235,
57 IndustrialDualAcRelayBrickletFunction::GetBootloaderMode => 236,
58 IndustrialDualAcRelayBrickletFunction::SetWriteFirmwarePointer => 237,
59 IndustrialDualAcRelayBrickletFunction::WriteFirmware => 238,
60 IndustrialDualAcRelayBrickletFunction::SetStatusLedConfig => 239,
61 IndustrialDualAcRelayBrickletFunction::GetStatusLedConfig => 240,
62 IndustrialDualAcRelayBrickletFunction::GetChipTemperature => 242,
63 IndustrialDualAcRelayBrickletFunction::Reset => 243,
64 IndustrialDualAcRelayBrickletFunction::WriteUid => 248,
65 IndustrialDualAcRelayBrickletFunction::ReadUid => 249,
66 IndustrialDualAcRelayBrickletFunction::GetIdentity => 255,
67 IndustrialDualAcRelayBrickletFunction::CallbackMonoflopDone => 7,
68 }
69 }
70}
71pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_CHANNEL_LED_CONFIG_OFF: u8 = 0;
72pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_CHANNEL_LED_CONFIG_ON: u8 = 1;
73pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_CHANNEL_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
74pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_CHANNEL_LED_CONFIG_SHOW_CHANNEL_STATUS: u8 = 3;
75pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
76pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
77pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
78pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
79pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
80pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
81pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
82pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
83pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
84pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
85pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
86pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
87pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
88pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
89pub const INDUSTRIAL_DUAL_AC_RELAY_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
90
91#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
92pub struct Value {
93 pub channel0: bool,
94 pub channel1: bool,
95}
96impl FromByteSlice for Value {
97 fn bytes_expected() -> usize {
98 2
99 }
100 fn from_le_byte_slice(bytes: &[u8]) -> Value {
101 Value { channel0: <bool>::from_le_byte_slice(&bytes[0..1]), channel1: <bool>::from_le_byte_slice(&bytes[1..2]) }
102 }
103}
104
105#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
106pub struct Monoflop {
107 pub value: bool,
108 pub time: u32,
109 pub time_remaining: u32,
110}
111impl FromByteSlice for Monoflop {
112 fn bytes_expected() -> usize {
113 9
114 }
115 fn from_le_byte_slice(bytes: &[u8]) -> Monoflop {
116 Monoflop {
117 value: <bool>::from_le_byte_slice(&bytes[0..1]),
118 time: <u32>::from_le_byte_slice(&bytes[1..5]),
119 time_remaining: <u32>::from_le_byte_slice(&bytes[5..9]),
120 }
121 }
122}
123
124#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
125pub struct MonoflopDoneEvent {
126 pub channel: u8,
127 pub value: bool,
128}
129impl FromByteSlice for MonoflopDoneEvent {
130 fn bytes_expected() -> usize {
131 2
132 }
133 fn from_le_byte_slice(bytes: &[u8]) -> MonoflopDoneEvent {
134 MonoflopDoneEvent { channel: <u8>::from_le_byte_slice(&bytes[0..1]), value: <bool>::from_le_byte_slice(&bytes[1..2]) }
135 }
136}
137
138#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
139pub struct SpitfpErrorCount {
140 pub error_count_ack_checksum: u32,
141 pub error_count_message_checksum: u32,
142 pub error_count_frame: u32,
143 pub error_count_overflow: u32,
144}
145impl FromByteSlice for SpitfpErrorCount {
146 fn bytes_expected() -> usize {
147 16
148 }
149 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
150 SpitfpErrorCount {
151 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
152 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
153 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
154 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
155 }
156 }
157}
158
159#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
160pub struct Identity {
161 pub uid: String,
162 pub connected_uid: String,
163 pub position: char,
164 pub hardware_version: [u8; 3],
165 pub firmware_version: [u8; 3],
166 pub device_identifier: u16,
167}
168impl FromByteSlice for Identity {
169 fn bytes_expected() -> usize {
170 25
171 }
172 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
173 Identity {
174 uid: <String>::from_le_byte_slice(&bytes[0..8]),
175 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
176 position: <char>::from_le_byte_slice(&bytes[16..17]),
177 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
178 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
179 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
180 }
181 }
182}
183
184#[derive(Clone)]
186pub struct IndustrialDualAcRelayBricklet {
187 device: Device,
188}
189impl IndustrialDualAcRelayBricklet {
190 pub const DEVICE_IDENTIFIER: u16 = 2162;
191 pub const DEVICE_DISPLAY_NAME: &'static str = "Industrial Dual AC Relay Bricklet";
192 pub fn new(uid: Uid, connection: AsyncIpConnection) -> IndustrialDualAcRelayBricklet {
194 let mut result = IndustrialDualAcRelayBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
195 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::SetValue) as usize] = ResponseExpectedFlag::False;
196 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::GetValue) as usize] =
197 ResponseExpectedFlag::AlwaysTrue;
198 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::SetChannelLedConfig) as usize] =
199 ResponseExpectedFlag::False;
200 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::GetChannelLedConfig) as usize] =
201 ResponseExpectedFlag::AlwaysTrue;
202 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::SetMonoflop) as usize] =
203 ResponseExpectedFlag::False;
204 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::GetMonoflop) as usize] =
205 ResponseExpectedFlag::AlwaysTrue;
206 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::SetSelectedValue) as usize] =
207 ResponseExpectedFlag::False;
208 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::GetSpitfpErrorCount) as usize] =
209 ResponseExpectedFlag::AlwaysTrue;
210 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::SetBootloaderMode) as usize] =
211 ResponseExpectedFlag::AlwaysTrue;
212 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::GetBootloaderMode) as usize] =
213 ResponseExpectedFlag::AlwaysTrue;
214 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::SetWriteFirmwarePointer) as usize] =
215 ResponseExpectedFlag::False;
216 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::WriteFirmware) as usize] =
217 ResponseExpectedFlag::AlwaysTrue;
218 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::SetStatusLedConfig) as usize] =
219 ResponseExpectedFlag::False;
220 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::GetStatusLedConfig) as usize] =
221 ResponseExpectedFlag::AlwaysTrue;
222 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::GetChipTemperature) as usize] =
223 ResponseExpectedFlag::AlwaysTrue;
224 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
225 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
226 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::ReadUid) as usize] =
227 ResponseExpectedFlag::AlwaysTrue;
228 result.device.response_expected[u8::from(IndustrialDualAcRelayBrickletFunction::GetIdentity) as usize] =
229 ResponseExpectedFlag::AlwaysTrue;
230 result
231 }
232
233 pub fn get_response_expected(&mut self, fun: IndustrialDualAcRelayBrickletFunction) -> Result<bool, GetResponseExpectedError> {
248 self.device.get_response_expected(u8::from(fun))
249 }
250
251 pub fn set_response_expected(
260 &mut self,
261 fun: IndustrialDualAcRelayBrickletFunction,
262 response_expected: bool,
263 ) -> Result<(), SetResponseExpectedError> {
264 self.device.set_response_expected(u8::from(fun), response_expected)
265 }
266
267 pub fn set_response_expected_all(&mut self, response_expected: bool) {
269 self.device.set_response_expected_all(response_expected)
270 }
271
272 pub fn get_api_version(&self) -> [u8; 3] {
275 self.device.api_version
276 }
277
278 pub async fn get_monoflop_done_callback_receiver(&mut self) -> impl Stream<Item = MonoflopDoneEvent> {
282 self.device
283 .get_callback_receiver(u8::from(IndustrialDualAcRelayBrickletFunction::CallbackMonoflopDone))
284 .await
285 .map(|p| MonoflopDoneEvent::from_le_byte_slice(p.body()))
286 }
287
288 pub async fn set_value(&mut self, channel0: bool, channel1: bool) -> Result<(), TinkerforgeError> {
297 let mut payload = [0; 2];
298 channel0.write_to_slice(&mut payload[0..1]);
299 channel1.write_to_slice(&mut payload[1..2]);
300
301 #[allow(unused_variables)]
302 let result = self.device.set(u8::from(IndustrialDualAcRelayBrickletFunction::SetValue), &payload).await?;
303 Ok(())
304 }
305
306 pub async fn get_value(&mut self) -> Result<Value, TinkerforgeError> {
308 let payload = [0; 0];
309
310 #[allow(unused_variables)]
311 let result = self.device.get(u8::from(IndustrialDualAcRelayBrickletFunction::GetValue), &payload).await?;
312 Ok(Value::from_le_byte_slice(result.body()))
313 }
314
315 pub async fn set_channel_led_config(&mut self, channel: u8, config: u8) -> Result<(), TinkerforgeError> {
325 let mut payload = [0; 2];
326 channel.write_to_slice(&mut payload[0..1]);
327 config.write_to_slice(&mut payload[1..2]);
328
329 #[allow(unused_variables)]
330 let result = self.device.set(u8::from(IndustrialDualAcRelayBrickletFunction::SetChannelLedConfig), &payload).await?;
331 Ok(())
332 }
333
334 pub async fn get_channel_led_config(&mut self, channel: u8) -> Result<u8, TinkerforgeError> {
342 let mut payload = [0; 1];
343 channel.write_to_slice(&mut payload[0..1]);
344
345 #[allow(unused_variables)]
346 let result = self.device.get(u8::from(IndustrialDualAcRelayBrickletFunction::GetChannelLedConfig), &payload).await?;
347 Ok(u8::from_le_byte_slice(result.body()))
348 }
349
350 pub async fn set_monoflop(&mut self, channel: u8, value: bool, time: u32) -> Result<(), TinkerforgeError> {
364 let mut payload = [0; 6];
365 channel.write_to_slice(&mut payload[0..1]);
366 value.write_to_slice(&mut payload[1..2]);
367 time.write_to_slice(&mut payload[2..6]);
368
369 #[allow(unused_variables)]
370 let result = self.device.set(u8::from(IndustrialDualAcRelayBrickletFunction::SetMonoflop), &payload).await?;
371 Ok(())
372 }
373
374 pub async fn get_monoflop(&mut self, channel: u8) -> Result<Monoflop, TinkerforgeError> {
380 let mut payload = [0; 1];
381 channel.write_to_slice(&mut payload[0..1]);
382
383 #[allow(unused_variables)]
384 let result = self.device.get(u8::from(IndustrialDualAcRelayBrickletFunction::GetMonoflop), &payload).await?;
385 Ok(Monoflop::from_le_byte_slice(result.body()))
386 }
387
388 pub async fn set_selected_value(&mut self, channel: u8, value: bool) -> Result<(), TinkerforgeError> {
396 let mut payload = [0; 2];
397 channel.write_to_slice(&mut payload[0..1]);
398 value.write_to_slice(&mut payload[1..2]);
399
400 #[allow(unused_variables)]
401 let result = self.device.set(u8::from(IndustrialDualAcRelayBrickletFunction::SetSelectedValue), &payload).await?;
402 Ok(())
403 }
404
405 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
417 let payload = [0; 0];
418
419 #[allow(unused_variables)]
420 let result = self.device.get(u8::from(IndustrialDualAcRelayBrickletFunction::GetSpitfpErrorCount), &payload).await?;
421 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
422 }
423
424 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
447 let mut payload = [0; 1];
448 mode.write_to_slice(&mut payload[0..1]);
449
450 #[allow(unused_variables)]
451 let result = self.device.get(u8::from(IndustrialDualAcRelayBrickletFunction::SetBootloaderMode), &payload).await?;
452 Ok(u8::from_le_byte_slice(result.body()))
453 }
454
455 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
464 let payload = [0; 0];
465
466 #[allow(unused_variables)]
467 let result = self.device.get(u8::from(IndustrialDualAcRelayBrickletFunction::GetBootloaderMode), &payload).await?;
468 Ok(u8::from_le_byte_slice(result.body()))
469 }
470
471 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
478 let mut payload = [0; 4];
479 pointer.write_to_slice(&mut payload[0..4]);
480
481 #[allow(unused_variables)]
482 let result = self.device.set(u8::from(IndustrialDualAcRelayBrickletFunction::SetWriteFirmwarePointer), &payload).await?;
483 Ok(())
484 }
485
486 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
495 let mut payload = [0; 64];
496 data.write_to_slice(&mut payload[0..64]);
497
498 #[allow(unused_variables)]
499 let result = self.device.get(u8::from(IndustrialDualAcRelayBrickletFunction::WriteFirmware), &payload).await?;
500 Ok(u8::from_le_byte_slice(result.body()))
501 }
502
503 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
517 let mut payload = [0; 1];
518 config.write_to_slice(&mut payload[0..1]);
519
520 #[allow(unused_variables)]
521 let result = self.device.set(u8::from(IndustrialDualAcRelayBrickletFunction::SetStatusLedConfig), &payload).await?;
522 Ok(())
523 }
524
525 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
533 let payload = [0; 0];
534
535 #[allow(unused_variables)]
536 let result = self.device.get(u8::from(IndustrialDualAcRelayBrickletFunction::GetStatusLedConfig), &payload).await?;
537 Ok(u8::from_le_byte_slice(result.body()))
538 }
539
540 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
547 let payload = [0; 0];
548
549 #[allow(unused_variables)]
550 let result = self.device.get(u8::from(IndustrialDualAcRelayBrickletFunction::GetChipTemperature), &payload).await?;
551 Ok(i16::from_le_byte_slice(result.body()))
552 }
553
554 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
561 let payload = [0; 0];
562
563 #[allow(unused_variables)]
564 let result = self.device.set(u8::from(IndustrialDualAcRelayBrickletFunction::Reset), &payload).await?;
565 Ok(())
566 }
567
568 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
574 let mut payload = [0; 4];
575 uid.write_to_slice(&mut payload[0..4]);
576
577 #[allow(unused_variables)]
578 let result = self.device.set(u8::from(IndustrialDualAcRelayBrickletFunction::WriteUid), &payload).await?;
579 Ok(())
580 }
581
582 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
585 let payload = [0; 0];
586
587 #[allow(unused_variables)]
588 let result = self.device.get(u8::from(IndustrialDualAcRelayBrickletFunction::ReadUid), &payload).await?;
589 Ok(u32::from_le_byte_slice(result.body()))
590 }
591
592 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
603 let payload = [0; 0];
604
605 #[allow(unused_variables)]
606 let result = self.device.get(u8::from(IndustrialDualAcRelayBrickletFunction::GetIdentity), &payload).await?;
607 Ok(Identity::from_le_byte_slice(result.body()))
608 }
609}