tinkerforge_async/bindings/solid_state_relay_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 AC and DC Solid State Relays.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/SolidStateRelay_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 SolidStateRelayBrickletFunction {
24 SetState,
25 GetState,
26 SetMonoflop,
27 GetMonoflop,
28 GetIdentity,
29 CallbackMonoflopDone,
30}
31impl From<SolidStateRelayBrickletFunction> for u8 {
32 fn from(fun: SolidStateRelayBrickletFunction) -> Self {
33 match fun {
34 SolidStateRelayBrickletFunction::SetState => 1,
35 SolidStateRelayBrickletFunction::GetState => 2,
36 SolidStateRelayBrickletFunction::SetMonoflop => 3,
37 SolidStateRelayBrickletFunction::GetMonoflop => 4,
38 SolidStateRelayBrickletFunction::GetIdentity => 255,
39 SolidStateRelayBrickletFunction::CallbackMonoflopDone => 5,
40 }
41 }
42}
43
44#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
45pub struct Monoflop {
46 pub state: bool,
47 pub time: u32,
48 pub time_remaining: u32,
49}
50impl FromByteSlice for Monoflop {
51 fn bytes_expected() -> usize {
52 9
53 }
54 fn from_le_byte_slice(bytes: &[u8]) -> Monoflop {
55 Monoflop {
56 state: <bool>::from_le_byte_slice(&bytes[0..1]),
57 time: <u32>::from_le_byte_slice(&bytes[1..5]),
58 time_remaining: <u32>::from_le_byte_slice(&bytes[5..9]),
59 }
60 }
61}
62
63#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
64pub struct Identity {
65 pub uid: String,
66 pub connected_uid: String,
67 pub position: char,
68 pub hardware_version: [u8; 3],
69 pub firmware_version: [u8; 3],
70 pub device_identifier: u16,
71}
72impl FromByteSlice for Identity {
73 fn bytes_expected() -> usize {
74 25
75 }
76 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
77 Identity {
78 uid: <String>::from_le_byte_slice(&bytes[0..8]),
79 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
80 position: <char>::from_le_byte_slice(&bytes[16..17]),
81 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
82 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
83 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
84 }
85 }
86}
87
88/// Controls AC and DC Solid State Relays
89#[derive(Clone)]
90pub struct SolidStateRelayBricklet {
91 device: Device,
92}
93impl SolidStateRelayBricklet {
94 pub const DEVICE_IDENTIFIER: u16 = 244;
95 pub const DEVICE_DISPLAY_NAME: &'static str = "Solid State Relay Bricklet";
96 /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
97 pub fn new(uid: Uid, connection: AsyncIpConnection) -> SolidStateRelayBricklet {
98 let mut result = SolidStateRelayBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
99 result.device.response_expected[u8::from(SolidStateRelayBrickletFunction::SetState) as usize] = ResponseExpectedFlag::False;
100 result.device.response_expected[u8::from(SolidStateRelayBrickletFunction::GetState) as usize] = ResponseExpectedFlag::AlwaysTrue;
101 result.device.response_expected[u8::from(SolidStateRelayBrickletFunction::SetMonoflop) as usize] = ResponseExpectedFlag::False;
102 result.device.response_expected[u8::from(SolidStateRelayBrickletFunction::GetMonoflop) as usize] = ResponseExpectedFlag::AlwaysTrue;
103 result.device.response_expected[u8::from(SolidStateRelayBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
104 result
105 }
106
107 /// Returns the response expected flag for the function specified by the function ID parameter.
108 /// It is true if the function is expected to send a response, false otherwise.
109 ///
110 /// For getter functions this is enabled by default and cannot be disabled, because those
111 /// functions will always send a response. For callback configuration functions it is enabled
112 /// by default too, but can be disabled by [`set_response_expected`](crate::solid_state_relay_bricklet::SolidStateRelayBricklet::set_response_expected).
113 /// For setter functions it is disabled by default and can be enabled.
114 ///
115 /// Enabling the response expected flag for a setter function allows to detect timeouts
116 /// and other error conditions calls of this setter as well. The device will then send a response
117 /// for this purpose. If this flag is disabled for a setter function then no response is sent
118 /// and errors are silently ignored, because they cannot be detected.
119 ///
120 /// See [`set_response_expected`](crate::solid_state_relay_bricklet::SolidStateRelayBricklet::set_response_expected) for the list of function ID constants available for this function.
121 pub fn get_response_expected(&mut self, fun: SolidStateRelayBrickletFunction) -> Result<bool, GetResponseExpectedError> {
122 self.device.get_response_expected(u8::from(fun))
123 }
124
125 /// Changes the response expected flag of the function specified by the function ID parameter.
126 /// This flag can only be changed for setter (default value: false) and callback configuration
127 /// functions (default value: true). For getter functions it is always enabled.
128 ///
129 /// Enabling the response expected flag for a setter function allows to detect timeouts and
130 /// other error conditions calls of this setter as well. The device will then send a response
131 /// for this purpose. If this flag is disabled for a setter function then no response is sent
132 /// and errors are silently ignored, because they cannot be detected.
133 pub fn set_response_expected(
134 &mut self,
135 fun: SolidStateRelayBrickletFunction,
136 response_expected: bool,
137 ) -> Result<(), SetResponseExpectedError> {
138 self.device.set_response_expected(u8::from(fun), response_expected)
139 }
140
141 /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
142 pub fn set_response_expected_all(&mut self, response_expected: bool) {
143 self.device.set_response_expected_all(response_expected)
144 }
145
146 /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
147 /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
148 pub fn get_api_version(&self) -> [u8; 3] {
149 self.device.api_version
150 }
151
152 /// This receiver is triggered whenever the monoflop timer reaches 0.
153 /// The parameter is the current state of the relay
154 /// (the state after the monoflop).
155 pub async fn get_monoflop_done_callback_receiver(&mut self) -> impl Stream<Item = bool> {
156 self.device
157 .get_callback_receiver(u8::from(SolidStateRelayBrickletFunction::CallbackMonoflopDone))
158 .await
159 .map(|p| bool::from_le_byte_slice(p.body()))
160 }
161
162 /// Sets the state of the relays *true* means on and *false* means off.
163 ///
164 /// A running monoflop timer will be aborted if this function is called.
165 pub async fn set_state(&mut self, state: bool) -> Result<(), TinkerforgeError> {
166 let mut payload = [0; 1];
167 state.write_to_slice(&mut payload[0..1]);
168
169 #[allow(unused_variables)]
170 let result = self.device.set(u8::from(SolidStateRelayBrickletFunction::SetState), &payload).await?;
171 Ok(())
172 }
173
174 /// Returns the state of the relay, *true* means on and *false* means off.
175 pub async fn get_state(&mut self) -> Result<bool, TinkerforgeError> {
176 let payload = [0; 0];
177
178 #[allow(unused_variables)]
179 let result = self.device.get(u8::from(SolidStateRelayBrickletFunction::GetState), &payload).await?;
180 Ok(bool::from_le_byte_slice(result.body()))
181 }
182
183 /// The first parameter is the desired state of the relay (*true* means on
184 /// and *false* means off). The second parameter indicates the time that
185 /// the relay should hold the state.
186 ///
187 /// If this function is called with the parameters (true, 1500):
188 /// The relay will turn on and in 1.5s it will turn off again.
189 ///
190 /// A monoflop can be used as a failsafe mechanism. For example: Lets assume you
191 /// have a RS485 bus and a Solid State Relay Bricklet connected to one of the slave
192 /// stacks. You can now call this function every second, with a time parameter
193 /// of two seconds. The relay will be on all the time. If now the RS485
194 /// connection is lost, the relay will turn off in at most two seconds.
195 pub async fn set_monoflop(&mut self, state: bool, time: u32) -> Result<(), TinkerforgeError> {
196 let mut payload = [0; 5];
197 state.write_to_slice(&mut payload[0..1]);
198 time.write_to_slice(&mut payload[1..5]);
199
200 #[allow(unused_variables)]
201 let result = self.device.set(u8::from(SolidStateRelayBrickletFunction::SetMonoflop), &payload).await?;
202 Ok(())
203 }
204
205 /// Returns the current state and the time as set by
206 /// [`set_monoflop`] as well as the remaining time until the state flips.
207 ///
208 /// If the timer is not running currently, the remaining time will be returned
209 /// as 0.
210 pub async fn get_monoflop(&mut self) -> Result<Monoflop, TinkerforgeError> {
211 let payload = [0; 0];
212
213 #[allow(unused_variables)]
214 let result = self.device.get(u8::from(SolidStateRelayBrickletFunction::GetMonoflop), &payload).await?;
215 Ok(Monoflop::from_le_byte_slice(result.body()))
216 }
217
218 /// Returns the UID, the UID where the Bricklet is connected to,
219 /// the position, the hardware and firmware version as well as the
220 /// device identifier.
221 ///
222 /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
223 /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
224 /// position 'z'.
225 ///
226 /// The device identifier numbers can be found [here](device_identifier).
227 /// |device_identifier_constant|
228 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
229 let payload = [0; 0];
230
231 #[allow(unused_variables)]
232 let result = self.device.get(u8::from(SolidStateRelayBrickletFunction::GetIdentity), &payload).await?;
233 Ok(Identity::from_le_byte_slice(result.body()))
234 }
235}