tinkerforge_async/bindings/
remote_switch_v2_bricklet.rs

1/* ***********************************************************
2 * This file was automatically generated on 2024-02-16.      *
3 *                                                           *
4 * Rust Bindings Version 2.0.20                              *
5 *                                                           *
6 * If you have a bugfix for this file and want to commit it, *
7 * please fix the bug in the generator. You can find a link  *
8 * to the generators git repository on tinkerforge.com       *
9 *************************************************************/
10
11//! Controls remote mains switches and receives signals from remotes.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/RemoteSwitchV2_Bricklet_Rust.html).
14#[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 RemoteSwitchV2BrickletFunction {
24    GetSwitchingState,
25    SetRepeats,
26    GetRepeats,
27    SwitchSocketA,
28    SwitchSocketB,
29    DimSocketB,
30    SwitchSocketC,
31    SetRemoteConfiguration,
32    GetRemoteConfiguration,
33    GetRemoteStatusA,
34    GetRemoteStatusB,
35    GetRemoteStatusC,
36    GetSpitfpErrorCount,
37    SetBootloaderMode,
38    GetBootloaderMode,
39    SetWriteFirmwarePointer,
40    WriteFirmware,
41    SetStatusLedConfig,
42    GetStatusLedConfig,
43    GetChipTemperature,
44    Reset,
45    WriteUid,
46    ReadUid,
47    GetIdentity,
48    CallbackSwitchingDone,
49    CallbackRemoteStatusA,
50    CallbackRemoteStatusB,
51    CallbackRemoteStatusC,
52}
53impl From<RemoteSwitchV2BrickletFunction> for u8 {
54    fn from(fun: RemoteSwitchV2BrickletFunction) -> Self {
55        match fun {
56            RemoteSwitchV2BrickletFunction::GetSwitchingState => 1,
57            RemoteSwitchV2BrickletFunction::SetRepeats => 3,
58            RemoteSwitchV2BrickletFunction::GetRepeats => 4,
59            RemoteSwitchV2BrickletFunction::SwitchSocketA => 5,
60            RemoteSwitchV2BrickletFunction::SwitchSocketB => 6,
61            RemoteSwitchV2BrickletFunction::DimSocketB => 7,
62            RemoteSwitchV2BrickletFunction::SwitchSocketC => 8,
63            RemoteSwitchV2BrickletFunction::SetRemoteConfiguration => 9,
64            RemoteSwitchV2BrickletFunction::GetRemoteConfiguration => 10,
65            RemoteSwitchV2BrickletFunction::GetRemoteStatusA => 11,
66            RemoteSwitchV2BrickletFunction::GetRemoteStatusB => 12,
67            RemoteSwitchV2BrickletFunction::GetRemoteStatusC => 13,
68            RemoteSwitchV2BrickletFunction::GetSpitfpErrorCount => 234,
69            RemoteSwitchV2BrickletFunction::SetBootloaderMode => 235,
70            RemoteSwitchV2BrickletFunction::GetBootloaderMode => 236,
71            RemoteSwitchV2BrickletFunction::SetWriteFirmwarePointer => 237,
72            RemoteSwitchV2BrickletFunction::WriteFirmware => 238,
73            RemoteSwitchV2BrickletFunction::SetStatusLedConfig => 239,
74            RemoteSwitchV2BrickletFunction::GetStatusLedConfig => 240,
75            RemoteSwitchV2BrickletFunction::GetChipTemperature => 242,
76            RemoteSwitchV2BrickletFunction::Reset => 243,
77            RemoteSwitchV2BrickletFunction::WriteUid => 248,
78            RemoteSwitchV2BrickletFunction::ReadUid => 249,
79            RemoteSwitchV2BrickletFunction::GetIdentity => 255,
80            RemoteSwitchV2BrickletFunction::CallbackSwitchingDone => 2,
81            RemoteSwitchV2BrickletFunction::CallbackRemoteStatusA => 14,
82            RemoteSwitchV2BrickletFunction::CallbackRemoteStatusB => 15,
83            RemoteSwitchV2BrickletFunction::CallbackRemoteStatusC => 16,
84        }
85    }
86}
87pub const REMOTE_SWITCH_V2_BRICKLET_SWITCHING_STATE_READY: u8 = 0;
88pub const REMOTE_SWITCH_V2_BRICKLET_SWITCHING_STATE_BUSY: u8 = 1;
89pub const REMOTE_SWITCH_V2_BRICKLET_SWITCH_TO_OFF: u8 = 0;
90pub const REMOTE_SWITCH_V2_BRICKLET_SWITCH_TO_ON: u8 = 1;
91pub const REMOTE_SWITCH_V2_BRICKLET_REMOTE_TYPE_A: u8 = 0;
92pub const REMOTE_SWITCH_V2_BRICKLET_REMOTE_TYPE_B: u8 = 1;
93pub const REMOTE_SWITCH_V2_BRICKLET_REMOTE_TYPE_C: u8 = 2;
94pub const REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
95pub const REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
96pub const REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
97pub const REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
98pub const REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
99pub const REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
100pub const REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
101pub const REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
102pub const REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
103pub const REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
104pub const REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
105pub const REMOTE_SWITCH_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
106pub const REMOTE_SWITCH_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
107pub const REMOTE_SWITCH_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
108pub const REMOTE_SWITCH_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
109
110#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
111pub struct RemoteConfiguration {
112    pub remote_type: u8,
113    pub minimum_repeats: u16,
114    pub callback_enabled: bool,
115}
116impl FromByteSlice for RemoteConfiguration {
117    fn bytes_expected() -> usize {
118        4
119    }
120    fn from_le_byte_slice(bytes: &[u8]) -> RemoteConfiguration {
121        RemoteConfiguration {
122            remote_type: <u8>::from_le_byte_slice(&bytes[0..1]),
123            minimum_repeats: <u16>::from_le_byte_slice(&bytes[1..3]),
124            callback_enabled: <bool>::from_le_byte_slice(&bytes[3..4]),
125        }
126    }
127}
128
129#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
130pub struct RemoteStatusA {
131    pub house_code: u8,
132    pub receiver_code: u8,
133    pub switch_to: u8,
134    pub repeats: u16,
135}
136impl FromByteSlice for RemoteStatusA {
137    fn bytes_expected() -> usize {
138        5
139    }
140    fn from_le_byte_slice(bytes: &[u8]) -> RemoteStatusA {
141        RemoteStatusA {
142            house_code: <u8>::from_le_byte_slice(&bytes[0..1]),
143            receiver_code: <u8>::from_le_byte_slice(&bytes[1..2]),
144            switch_to: <u8>::from_le_byte_slice(&bytes[2..3]),
145            repeats: <u16>::from_le_byte_slice(&bytes[3..5]),
146        }
147    }
148}
149
150#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
151pub struct RemoteStatusB {
152    pub address: u32,
153    pub unit: u8,
154    pub switch_to: u8,
155    pub dim_value: u8,
156    pub repeats: u16,
157}
158impl FromByteSlice for RemoteStatusB {
159    fn bytes_expected() -> usize {
160        9
161    }
162    fn from_le_byte_slice(bytes: &[u8]) -> RemoteStatusB {
163        RemoteStatusB {
164            address: <u32>::from_le_byte_slice(&bytes[0..4]),
165            unit: <u8>::from_le_byte_slice(&bytes[4..5]),
166            switch_to: <u8>::from_le_byte_slice(&bytes[5..6]),
167            dim_value: <u8>::from_le_byte_slice(&bytes[6..7]),
168            repeats: <u16>::from_le_byte_slice(&bytes[7..9]),
169        }
170    }
171}
172
173#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
174pub struct RemoteStatusC {
175    pub system_code: char,
176    pub device_code: u8,
177    pub switch_to: u8,
178    pub repeats: u16,
179}
180impl FromByteSlice for RemoteStatusC {
181    fn bytes_expected() -> usize {
182        5
183    }
184    fn from_le_byte_slice(bytes: &[u8]) -> RemoteStatusC {
185        RemoteStatusC {
186            system_code: <char>::from_le_byte_slice(&bytes[0..1]),
187            device_code: <u8>::from_le_byte_slice(&bytes[1..2]),
188            switch_to: <u8>::from_le_byte_slice(&bytes[2..3]),
189            repeats: <u16>::from_le_byte_slice(&bytes[3..5]),
190        }
191    }
192}
193
194#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
195pub struct RemoteStatusAEvent {
196    pub house_code: u8,
197    pub receiver_code: u8,
198    pub switch_to: u8,
199    pub repeats: u16,
200}
201impl FromByteSlice for RemoteStatusAEvent {
202    fn bytes_expected() -> usize {
203        5
204    }
205    fn from_le_byte_slice(bytes: &[u8]) -> RemoteStatusAEvent {
206        RemoteStatusAEvent {
207            house_code: <u8>::from_le_byte_slice(&bytes[0..1]),
208            receiver_code: <u8>::from_le_byte_slice(&bytes[1..2]),
209            switch_to: <u8>::from_le_byte_slice(&bytes[2..3]),
210            repeats: <u16>::from_le_byte_slice(&bytes[3..5]),
211        }
212    }
213}
214
215#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
216pub struct RemoteStatusBEvent {
217    pub address: u32,
218    pub unit: u8,
219    pub switch_to: u8,
220    pub dim_value: u8,
221    pub repeats: u16,
222}
223impl FromByteSlice for RemoteStatusBEvent {
224    fn bytes_expected() -> usize {
225        9
226    }
227    fn from_le_byte_slice(bytes: &[u8]) -> RemoteStatusBEvent {
228        RemoteStatusBEvent {
229            address: <u32>::from_le_byte_slice(&bytes[0..4]),
230            unit: <u8>::from_le_byte_slice(&bytes[4..5]),
231            switch_to: <u8>::from_le_byte_slice(&bytes[5..6]),
232            dim_value: <u8>::from_le_byte_slice(&bytes[6..7]),
233            repeats: <u16>::from_le_byte_slice(&bytes[7..9]),
234        }
235    }
236}
237
238#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
239pub struct RemoteStatusCEvent {
240    pub system_code: char,
241    pub device_code: u8,
242    pub switch_to: u8,
243    pub repeats: u16,
244}
245impl FromByteSlice for RemoteStatusCEvent {
246    fn bytes_expected() -> usize {
247        5
248    }
249    fn from_le_byte_slice(bytes: &[u8]) -> RemoteStatusCEvent {
250        RemoteStatusCEvent {
251            system_code: <char>::from_le_byte_slice(&bytes[0..1]),
252            device_code: <u8>::from_le_byte_slice(&bytes[1..2]),
253            switch_to: <u8>::from_le_byte_slice(&bytes[2..3]),
254            repeats: <u16>::from_le_byte_slice(&bytes[3..5]),
255        }
256    }
257}
258
259#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
260pub struct SpitfpErrorCount {
261    pub error_count_ack_checksum: u32,
262    pub error_count_message_checksum: u32,
263    pub error_count_frame: u32,
264    pub error_count_overflow: u32,
265}
266impl FromByteSlice for SpitfpErrorCount {
267    fn bytes_expected() -> usize {
268        16
269    }
270    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
271        SpitfpErrorCount {
272            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
273            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
274            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
275            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
276        }
277    }
278}
279
280#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
281pub struct Identity {
282    pub uid: String,
283    pub connected_uid: String,
284    pub position: char,
285    pub hardware_version: [u8; 3],
286    pub firmware_version: [u8; 3],
287    pub device_identifier: u16,
288}
289impl FromByteSlice for Identity {
290    fn bytes_expected() -> usize {
291        25
292    }
293    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
294        Identity {
295            uid: <String>::from_le_byte_slice(&bytes[0..8]),
296            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
297            position: <char>::from_le_byte_slice(&bytes[16..17]),
298            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
299            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
300            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
301        }
302    }
303}
304
305/// Controls remote mains switches and receives signals from remotes
306#[derive(Clone)]
307pub struct RemoteSwitchV2Bricklet {
308    device: Device,
309}
310impl RemoteSwitchV2Bricklet {
311    pub const DEVICE_IDENTIFIER: u16 = 289;
312    pub const DEVICE_DISPLAY_NAME: &'static str = "Remote Switch Bricklet 2.0";
313    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
314    pub fn new(uid: Uid, connection: AsyncIpConnection) -> RemoteSwitchV2Bricklet {
315        let mut result = RemoteSwitchV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
316        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::GetSwitchingState) as usize] =
317            ResponseExpectedFlag::AlwaysTrue;
318        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::SetRepeats) as usize] = ResponseExpectedFlag::False;
319        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::GetRepeats) as usize] = ResponseExpectedFlag::AlwaysTrue;
320        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::SwitchSocketA) as usize] = ResponseExpectedFlag::False;
321        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::SwitchSocketB) as usize] = ResponseExpectedFlag::False;
322        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::DimSocketB) as usize] = ResponseExpectedFlag::False;
323        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::SwitchSocketC) as usize] = ResponseExpectedFlag::False;
324        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::SetRemoteConfiguration) as usize] =
325            ResponseExpectedFlag::False;
326        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::GetRemoteConfiguration) as usize] =
327            ResponseExpectedFlag::AlwaysTrue;
328        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::GetRemoteStatusA) as usize] =
329            ResponseExpectedFlag::AlwaysTrue;
330        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::GetRemoteStatusB) as usize] =
331            ResponseExpectedFlag::AlwaysTrue;
332        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::GetRemoteStatusC) as usize] =
333            ResponseExpectedFlag::AlwaysTrue;
334        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::GetSpitfpErrorCount) as usize] =
335            ResponseExpectedFlag::AlwaysTrue;
336        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::SetBootloaderMode) as usize] =
337            ResponseExpectedFlag::AlwaysTrue;
338        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::GetBootloaderMode) as usize] =
339            ResponseExpectedFlag::AlwaysTrue;
340        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
341            ResponseExpectedFlag::False;
342        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::WriteFirmware) as usize] =
343            ResponseExpectedFlag::AlwaysTrue;
344        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::SetStatusLedConfig) as usize] =
345            ResponseExpectedFlag::False;
346        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::GetStatusLedConfig) as usize] =
347            ResponseExpectedFlag::AlwaysTrue;
348        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::GetChipTemperature) as usize] =
349            ResponseExpectedFlag::AlwaysTrue;
350        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
351        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
352        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
353        result.device.response_expected[u8::from(RemoteSwitchV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
354        result
355    }
356
357    /// Returns the response expected flag for the function specified by the function ID parameter.
358    /// It is true if the function is expected to send a response, false otherwise.
359    ///
360    /// For getter functions this is enabled by default and cannot be disabled, because those
361    /// functions will always send a response. For callback configuration functions it is enabled
362    /// by default too, but can be disabled by [`set_response_expected`](crate::remote_switch_v2_bricklet::RemoteSwitchV2Bricklet::set_response_expected).
363    /// For setter functions it is disabled by default and can be enabled.
364    ///
365    /// Enabling the response expected flag for a setter function allows to detect timeouts
366    /// and other error conditions calls of this setter as well. The device will then send a response
367    /// for this purpose. If this flag is disabled for a setter function then no response is sent
368    /// and errors are silently ignored, because they cannot be detected.
369    ///
370    /// See [`set_response_expected`](crate::remote_switch_v2_bricklet::RemoteSwitchV2Bricklet::set_response_expected) for the list of function ID constants available for this function.
371    pub fn get_response_expected(&mut self, fun: RemoteSwitchV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
372        self.device.get_response_expected(u8::from(fun))
373    }
374
375    /// Changes the response expected flag of the function specified by the function ID parameter.
376    /// This flag can only be changed for setter (default value: false) and callback configuration
377    /// functions (default value: true). For getter functions it is always enabled.
378    ///
379    /// Enabling the response expected flag for a setter function allows to detect timeouts and
380    /// other error conditions calls of this setter as well. The device will then send a response
381    /// for this purpose. If this flag is disabled for a setter function then no response is sent
382    /// and errors are silently ignored, because they cannot be detected.
383    pub fn set_response_expected(
384        &mut self,
385        fun: RemoteSwitchV2BrickletFunction,
386        response_expected: bool,
387    ) -> Result<(), SetResponseExpectedError> {
388        self.device.set_response_expected(u8::from(fun), response_expected)
389    }
390
391    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
392    pub fn set_response_expected_all(&mut self, response_expected: bool) {
393        self.device.set_response_expected_all(response_expected)
394    }
395
396    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
397    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
398    pub fn get_api_version(&self) -> [u8; 3] {
399        self.device.api_version
400    }
401
402    /// This receiver is triggered whenever the switching state changes
403    /// from busy to ready, see [`get_switching_state`].
404    ///
405    /// [`get_switching_state`]: #method.get_switching_state
406    pub async fn get_switching_done_callback_receiver(&mut self) -> impl Stream<Item = ()> {
407        self.device.get_callback_receiver(u8::from(RemoteSwitchV2BrickletFunction::CallbackSwitchingDone)).await.map(|_p| ())
408    }
409
410    /// Returns the house code, receiver code, switch state (on/off) and number of repeats for
411    /// remote type A.
412    ///
413    /// The repeats are the number of received identical data packets. The longer the button is pressed,
414    /// the higher the repeat number. The receiver is triggered with every repeat.
415    ///
416    /// You have to enable the receiver with [`set_remote_configuration`]. The number
417    /// of repeats that you can set in the configuration is the minimum number of repeats that have
418    /// to be seen before the receiver is triggered for the first time.
419    pub async fn get_remote_status_a_callback_receiver(&mut self) -> impl Stream<Item = RemoteStatusAEvent> {
420        self.device
421            .get_callback_receiver(u8::from(RemoteSwitchV2BrickletFunction::CallbackRemoteStatusA))
422            .await
423            .map(|p| RemoteStatusAEvent::from_le_byte_slice(p.body()))
424    }
425
426    /// Returns the address (unique per remote), unit (button number), switch state (on/off) and number of repeats for
427    /// remote type B.
428    ///
429    /// If the remote supports dimming the dim value is used instead of the switch state.
430    ///
431    /// The repeats are the number of received identical data packets. The longer the button is pressed,
432    /// the higher the repeat number. The receiver is triggered with every repeat.
433    ///
434    /// You have to enable the receiver with [`set_remote_configuration`]. The number
435    /// of repeats that you can set in the configuration is the minimum number of repeats that have
436    /// to be seen before the receiver is triggered for the first time.
437    pub async fn get_remote_status_b_callback_receiver(&mut self) -> impl Stream<Item = RemoteStatusBEvent> {
438        self.device
439            .get_callback_receiver(u8::from(RemoteSwitchV2BrickletFunction::CallbackRemoteStatusB))
440            .await
441            .map(|p| RemoteStatusBEvent::from_le_byte_slice(p.body()))
442    }
443
444    /// Returns the system code, device code, switch state (on/off) and number of repeats for
445    /// remote type C.
446    ///
447    /// The repeats are the number of received identical data packets. The longer the button is pressed,
448    /// the higher the repeat number. The receiver is triggered with every repeat.
449    ///
450    /// You have to enable the receiver with [`set_remote_configuration`]. The number
451    /// of repeats that you can set in the configuration is the minimum number of repeats that have
452    /// to be seen before the receiver is triggered for the first time.
453    pub async fn get_remote_status_c_callback_receiver(&mut self) -> impl Stream<Item = RemoteStatusCEvent> {
454        self.device
455            .get_callback_receiver(u8::from(RemoteSwitchV2BrickletFunction::CallbackRemoteStatusC))
456            .await
457            .map(|p| RemoteStatusCEvent::from_le_byte_slice(p.body()))
458    }
459
460    /// Returns the current switching state. If the current state is busy, the
461    /// Bricklet is currently sending a code to switch a socket. It will not
462    /// accept any calls of switch socket functions until the state changes to ready.
463    ///
464    /// How long the switching takes is dependent on the number of repeats, see
465    /// [`set_repeats`].
466    ///
467    /// Associated constants:
468    /// * REMOTE_SWITCH_V2_BRICKLET_SWITCHING_STATE_READY
469    ///	* REMOTE_SWITCH_V2_BRICKLET_SWITCHING_STATE_BUSY
470    pub async fn get_switching_state(&mut self) -> Result<u8, TinkerforgeError> {
471        let payload = [0; 0];
472
473        #[allow(unused_variables)]
474        let result = self.device.get(u8::from(RemoteSwitchV2BrickletFunction::GetSwitchingState), &payload).await?;
475        Ok(u8::from_le_byte_slice(result.body()))
476    }
477
478    /// Sets the number of times the code is sent when one of the Switch Socket
479    /// functions is called. The repeats basically correspond to the amount of time
480    /// that a button of the remote is pressed.
481    ///
482    /// Some dimmers are controlled by the length of a button pressed,
483    /// this can be simulated by increasing the repeats.
484    pub async fn set_repeats(&mut self, repeats: u8) -> Result<(), TinkerforgeError> {
485        let mut payload = [0; 1];
486        repeats.write_to_slice(&mut payload[0..1]);
487
488        #[allow(unused_variables)]
489        let result = self.device.set(u8::from(RemoteSwitchV2BrickletFunction::SetRepeats), &payload).await?;
490        Ok(())
491    }
492
493    /// Returns the number of repeats as set by [`set_repeats`].
494    pub async fn get_repeats(&mut self) -> Result<u8, TinkerforgeError> {
495        let payload = [0; 0];
496
497        #[allow(unused_variables)]
498        let result = self.device.get(u8::from(RemoteSwitchV2BrickletFunction::GetRepeats), &payload).await?;
499        Ok(u8::from_le_byte_slice(result.body()))
500    }
501
502    /// To switch a type A socket you have to give the house code, receiver code and the
503    /// state (on or off) you want to switch to.
504    ///
505    /// A detailed description on how you can figure out the house and receiver code
506    /// can be found [here](remote_switch_bricklet_type_a_house_and_receiver_code).
507    ///
508    /// Associated constants:
509    /// * REMOTE_SWITCH_V2_BRICKLET_SWITCH_TO_OFF
510    ///	* REMOTE_SWITCH_V2_BRICKLET_SWITCH_TO_ON
511    pub async fn switch_socket_a(&mut self, house_code: u8, receiver_code: u8, switch_to: u8) -> Result<(), TinkerforgeError> {
512        let mut payload = [0; 3];
513        house_code.write_to_slice(&mut payload[0..1]);
514        receiver_code.write_to_slice(&mut payload[1..2]);
515        switch_to.write_to_slice(&mut payload[2..3]);
516
517        #[allow(unused_variables)]
518        let result = self.device.set(u8::from(RemoteSwitchV2BrickletFunction::SwitchSocketA), &payload).await?;
519        Ok(())
520    }
521
522    /// To switch a type B socket you have to give the address, unit and the state
523    /// (on or off) you want to switch to.
524    ///
525    /// To switch all devices with the same address use 255 for the unit.
526    ///
527    /// A detailed description on how you can teach a socket the address and unit can
528    /// be found [here](remote_switch_bricklet_type_b_address_and_unit).
529    ///
530    /// Associated constants:
531    /// * REMOTE_SWITCH_V2_BRICKLET_SWITCH_TO_OFF
532    ///	* REMOTE_SWITCH_V2_BRICKLET_SWITCH_TO_ON
533    pub async fn switch_socket_b(&mut self, address: u32, unit: u8, switch_to: u8) -> Result<(), TinkerforgeError> {
534        let mut payload = [0; 6];
535        address.write_to_slice(&mut payload[0..4]);
536        unit.write_to_slice(&mut payload[4..5]);
537        switch_to.write_to_slice(&mut payload[5..6]);
538
539        #[allow(unused_variables)]
540        let result = self.device.set(u8::from(RemoteSwitchV2BrickletFunction::SwitchSocketB), &payload).await?;
541        Ok(())
542    }
543
544    /// To control a type B dimmer you have to give the address, unit and the
545    /// dim value you want to set the dimmer to.
546    ///
547    /// A detailed description on how you can teach a dimmer the address and unit can
548    /// be found [here](remote_switch_bricklet_type_b_address_and_unit).
549    pub async fn dim_socket_b(&mut self, address: u32, unit: u8, dim_value: u8) -> Result<(), TinkerforgeError> {
550        let mut payload = [0; 6];
551        address.write_to_slice(&mut payload[0..4]);
552        unit.write_to_slice(&mut payload[4..5]);
553        dim_value.write_to_slice(&mut payload[5..6]);
554
555        #[allow(unused_variables)]
556        let result = self.device.set(u8::from(RemoteSwitchV2BrickletFunction::DimSocketB), &payload).await?;
557        Ok(())
558    }
559
560    /// To switch a type C socket you have to give the system code, device code and the
561    /// state (on or off) you want to switch to.
562    ///
563    /// A detailed description on how you can figure out the system and device code
564    /// can be found [here](remote_switch_bricklet_type_c_system_and_device_code).
565    ///
566    /// Associated constants:
567    /// * REMOTE_SWITCH_V2_BRICKLET_SWITCH_TO_OFF
568    ///	* REMOTE_SWITCH_V2_BRICKLET_SWITCH_TO_ON
569    pub async fn switch_socket_c(&mut self, system_code: char, device_code: u8, switch_to: u8) -> Result<(), TinkerforgeError> {
570        let mut payload = [0; 3];
571        system_code.write_to_slice(&mut payload[0..1]);
572        device_code.write_to_slice(&mut payload[1..2]);
573        switch_to.write_to_slice(&mut payload[2..3]);
574
575        #[allow(unused_variables)]
576        let result = self.device.set(u8::from(RemoteSwitchV2BrickletFunction::SwitchSocketC), &payload).await?;
577        Ok(())
578    }
579
580    /// Sets the configuration for **receiving** data from a remote of type A, B or C.
581    ///
582    /// * Remote Type: A, B or C depending on the type of remote you want to receive.
583    /// * Minimum Repeats: The minimum number of repeated data packets until the receiver
584    ///   is triggered (if enabled).
585    /// * Receiver Enabled: Enable or disable receiver (see [`get_remote_status_a_callback_receiver`] receiver,
586    ///   [`get_remote_status_b_callback_receiver`] receiver and [`get_remote_status_c_callback_receiver`] receiver).
587    ///
588    /// Associated constants:
589    /// * REMOTE_SWITCH_V2_BRICKLET_REMOTE_TYPE_A
590    ///	* REMOTE_SWITCH_V2_BRICKLET_REMOTE_TYPE_B
591    ///	* REMOTE_SWITCH_V2_BRICKLET_REMOTE_TYPE_C
592    pub async fn set_remote_configuration(
593        &mut self,
594        remote_type: u8,
595        minimum_repeats: u16,
596        callback_enabled: bool,
597    ) -> Result<(), TinkerforgeError> {
598        let mut payload = [0; 4];
599        remote_type.write_to_slice(&mut payload[0..1]);
600        minimum_repeats.write_to_slice(&mut payload[1..3]);
601        callback_enabled.write_to_slice(&mut payload[3..4]);
602
603        #[allow(unused_variables)]
604        let result = self.device.set(u8::from(RemoteSwitchV2BrickletFunction::SetRemoteConfiguration), &payload).await?;
605        Ok(())
606    }
607
608    /// Returns the remote configuration as set by [`set_remote_configuration`]
609    ///
610    /// Associated constants:
611    /// * REMOTE_SWITCH_V2_BRICKLET_REMOTE_TYPE_A
612    ///	* REMOTE_SWITCH_V2_BRICKLET_REMOTE_TYPE_B
613    ///	* REMOTE_SWITCH_V2_BRICKLET_REMOTE_TYPE_C
614    pub async fn get_remote_configuration(&mut self) -> Result<RemoteConfiguration, TinkerforgeError> {
615        let payload = [0; 0];
616
617        #[allow(unused_variables)]
618        let result = self.device.get(u8::from(RemoteSwitchV2BrickletFunction::GetRemoteConfiguration), &payload).await?;
619        Ok(RemoteConfiguration::from_le_byte_slice(result.body()))
620    }
621
622    /// Returns the house code, receiver code, switch state (on/off) and number of
623    /// repeats for remote type A.
624    ///
625    /// Repeats == 0 means there was no button press. Repeats >= 1 means there
626    /// was a button press with the specified house/receiver code. The repeats are the
627    /// number of received identical data packets. The longer the button is pressed,
628    /// the higher the repeat number.
629    ///
630    /// Use the receiver to get this data automatically when a button is pressed,
631    /// see [`set_remote_configuration`] and [`get_remote_status_a_callback_receiver`] receiver.
632    ///
633    /// Associated constants:
634    /// * REMOTE_SWITCH_V2_BRICKLET_SWITCH_TO_OFF
635    ///	* REMOTE_SWITCH_V2_BRICKLET_SWITCH_TO_ON
636    pub async fn get_remote_status_a(&mut self) -> Result<RemoteStatusA, TinkerforgeError> {
637        let payload = [0; 0];
638
639        #[allow(unused_variables)]
640        let result = self.device.get(u8::from(RemoteSwitchV2BrickletFunction::GetRemoteStatusA), &payload).await?;
641        Ok(RemoteStatusA::from_le_byte_slice(result.body()))
642    }
643
644    /// Returns the address (unique per remote), unit (button number), switch state
645    /// (on/off) and number of repeats for remote type B.
646    ///
647    /// If the remote supports dimming the dim value is used instead of the switch state.
648    ///
649    /// If repeats=0 there was no button press. If repeats >= 1 there
650    /// was a button press with the specified address/unit. The repeats are the number of received
651    /// identical data packets. The longer the button is pressed, the higher the repeat number.
652    ///
653    /// Use the receiver to get this data automatically when a button is pressed,
654    /// see [`set_remote_configuration`] and [`get_remote_status_b_callback_receiver`] receiver.
655    ///
656    /// Associated constants:
657    /// * REMOTE_SWITCH_V2_BRICKLET_SWITCH_TO_OFF
658    ///	* REMOTE_SWITCH_V2_BRICKLET_SWITCH_TO_ON
659    pub async fn get_remote_status_b(&mut self) -> Result<RemoteStatusB, TinkerforgeError> {
660        let payload = [0; 0];
661
662        #[allow(unused_variables)]
663        let result = self.device.get(u8::from(RemoteSwitchV2BrickletFunction::GetRemoteStatusB), &payload).await?;
664        Ok(RemoteStatusB::from_le_byte_slice(result.body()))
665    }
666
667    /// Returns the system code, device code, switch state (on/off) and number of repeats for
668    /// remote type C.
669    ///
670    /// If repeats=0 there was no button press. If repeats >= 1 there
671    /// was a button press with the specified system/device code. The repeats are the number of received
672    /// identical data packets. The longer the button is pressed, the higher the repeat number.
673    ///
674    /// Use the receiver to get this data automatically when a button is pressed,
675    /// see [`set_remote_configuration`] and [`get_remote_status_c_callback_receiver`] receiver.
676    ///
677    /// Associated constants:
678    /// * REMOTE_SWITCH_V2_BRICKLET_SWITCH_TO_OFF
679    ///	* REMOTE_SWITCH_V2_BRICKLET_SWITCH_TO_ON
680    pub async fn get_remote_status_c(&mut self) -> Result<RemoteStatusC, TinkerforgeError> {
681        let payload = [0; 0];
682
683        #[allow(unused_variables)]
684        let result = self.device.get(u8::from(RemoteSwitchV2BrickletFunction::GetRemoteStatusC), &payload).await?;
685        Ok(RemoteStatusC::from_le_byte_slice(result.body()))
686    }
687
688    /// Returns the error count for the communication between Brick and Bricklet.
689    ///
690    /// The errors are divided into
691    ///
692    /// * ACK checksum errors,
693    /// * message checksum errors,
694    /// * framing errors and
695    /// * overflow errors.
696    ///
697    /// The errors counts are for errors that occur on the Bricklet side. All
698    /// Bricks have a similar function that returns the errors on the Brick side.
699    pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
700        let payload = [0; 0];
701
702        #[allow(unused_variables)]
703        let result = self.device.get(u8::from(RemoteSwitchV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
704        Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
705    }
706
707    /// Sets the bootloader mode and returns the status after the requested
708    /// mode change was instigated.
709    ///
710    /// You can change from bootloader mode to firmware mode and vice versa. A change
711    /// from bootloader mode to firmware mode will only take place if the entry function,
712    /// device identifier and CRC are present and correct.
713    ///
714    /// This function is used by Brick Viewer during flashing. It should not be
715    /// necessary to call it in a normal user program.
716    ///
717    /// Associated constants:
718    /// * REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
719    ///	* REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
720    ///	* REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
721    ///	* REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
722    ///	* REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
723    ///	* REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_STATUS_OK
724    ///	* REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
725    ///	* REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
726    ///	* REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
727    ///	* REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
728    ///	* REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
729    pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
730        let mut payload = [0; 1];
731        mode.write_to_slice(&mut payload[0..1]);
732
733        #[allow(unused_variables)]
734        let result = self.device.get(u8::from(RemoteSwitchV2BrickletFunction::SetBootloaderMode), &payload).await?;
735        Ok(u8::from_le_byte_slice(result.body()))
736    }
737
738    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
739    ///
740    /// Associated constants:
741    /// * REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
742    ///	* REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
743    ///	* REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
744    ///	* REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
745    ///	* REMOTE_SWITCH_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
746    pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
747        let payload = [0; 0];
748
749        #[allow(unused_variables)]
750        let result = self.device.get(u8::from(RemoteSwitchV2BrickletFunction::GetBootloaderMode), &payload).await?;
751        Ok(u8::from_le_byte_slice(result.body()))
752    }
753
754    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
755    /// to be increased by chunks of size 64. The data is written to flash
756    /// every 4 chunks (which equals to one page of size 256).
757    ///
758    /// This function is used by Brick Viewer during flashing. It should not be
759    /// necessary to call it in a normal user program.
760    pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
761        let mut payload = [0; 4];
762        pointer.write_to_slice(&mut payload[0..4]);
763
764        #[allow(unused_variables)]
765        let result = self.device.set(u8::from(RemoteSwitchV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
766        Ok(())
767    }
768
769    /// Writes 64 Bytes of firmware at the position as written by
770    /// [`set_write_firmware_pointer`] before. The firmware is written
771    /// to flash every 4 chunks.
772    ///
773    /// You can only write firmware in bootloader mode.
774    ///
775    /// This function is used by Brick Viewer during flashing. It should not be
776    /// necessary to call it in a normal user program.
777    pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
778        let mut payload = [0; 64];
779        data.write_to_slice(&mut payload[0..64]);
780
781        #[allow(unused_variables)]
782        let result = self.device.get(u8::from(RemoteSwitchV2BrickletFunction::WriteFirmware), &payload).await?;
783        Ok(u8::from_le_byte_slice(result.body()))
784    }
785
786    /// Sets the status LED configuration. By default the LED shows
787    /// communication traffic between Brick and Bricklet, it flickers once
788    /// for every 10 received data packets.
789    ///
790    /// You can also turn the LED permanently on/off or show a heartbeat.
791    ///
792    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
793    ///
794    /// Associated constants:
795    /// * REMOTE_SWITCH_V2_BRICKLET_STATUS_LED_CONFIG_OFF
796    ///	* REMOTE_SWITCH_V2_BRICKLET_STATUS_LED_CONFIG_ON
797    ///	* REMOTE_SWITCH_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
798    ///	* REMOTE_SWITCH_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
799    pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
800        let mut payload = [0; 1];
801        config.write_to_slice(&mut payload[0..1]);
802
803        #[allow(unused_variables)]
804        let result = self.device.set(u8::from(RemoteSwitchV2BrickletFunction::SetStatusLedConfig), &payload).await?;
805        Ok(())
806    }
807
808    /// Returns the configuration as set by [`set_status_led_config`]
809    ///
810    /// Associated constants:
811    /// * REMOTE_SWITCH_V2_BRICKLET_STATUS_LED_CONFIG_OFF
812    ///	* REMOTE_SWITCH_V2_BRICKLET_STATUS_LED_CONFIG_ON
813    ///	* REMOTE_SWITCH_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
814    ///	* REMOTE_SWITCH_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
815    pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
816        let payload = [0; 0];
817
818        #[allow(unused_variables)]
819        let result = self.device.get(u8::from(RemoteSwitchV2BrickletFunction::GetStatusLedConfig), &payload).await?;
820        Ok(u8::from_le_byte_slice(result.body()))
821    }
822
823    /// Returns the temperature as measured inside the microcontroller. The
824    /// value returned is not the ambient temperature!
825    ///
826    /// The temperature is only proportional to the real temperature and it has bad
827    /// accuracy. Practically it is only useful as an indicator for
828    /// temperature changes.
829    pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
830        let payload = [0; 0];
831
832        #[allow(unused_variables)]
833        let result = self.device.get(u8::from(RemoteSwitchV2BrickletFunction::GetChipTemperature), &payload).await?;
834        Ok(i16::from_le_byte_slice(result.body()))
835    }
836
837    /// Calling this function will reset the Bricklet. All configurations
838    /// will be lost.
839    ///
840    /// After a reset you have to create new device objects,
841    /// calling functions on the existing ones will result in
842    /// undefined behavior!
843    pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
844        let payload = [0; 0];
845
846        #[allow(unused_variables)]
847        let result = self.device.set(u8::from(RemoteSwitchV2BrickletFunction::Reset), &payload).await?;
848        Ok(())
849    }
850
851    /// Writes a new UID into flash. If you want to set a new UID
852    /// you have to decode the Base58 encoded UID string into an
853    /// integer first.
854    ///
855    /// We recommend that you use Brick Viewer to change the UID.
856    pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
857        let mut payload = [0; 4];
858        uid.write_to_slice(&mut payload[0..4]);
859
860        #[allow(unused_variables)]
861        let result = self.device.set(u8::from(RemoteSwitchV2BrickletFunction::WriteUid), &payload).await?;
862        Ok(())
863    }
864
865    /// Returns the current UID as an integer. Encode as
866    /// Base58 to get the usual string version.
867    pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
868        let payload = [0; 0];
869
870        #[allow(unused_variables)]
871        let result = self.device.get(u8::from(RemoteSwitchV2BrickletFunction::ReadUid), &payload).await?;
872        Ok(u32::from_le_byte_slice(result.body()))
873    }
874
875    /// Returns the UID, the UID where the Bricklet is connected to,
876    /// the position, the hardware and firmware version as well as the
877    /// device identifier.
878    ///
879    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
880    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
881    /// position 'z'.
882    ///
883    /// The device identifier numbers can be found [here](device_identifier).
884    /// |device_identifier_constant|
885    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
886        let payload = [0; 0];
887
888        #[allow(unused_variables)]
889        let result = self.device.get(u8::from(RemoteSwitchV2BrickletFunction::GetIdentity), &payload).await?;
890        Ok(Identity::from_le_byte_slice(result.body()))
891    }
892}