tinkerforge_async/bindings/
dual_button_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//! Two tactile buttons with built-in blue LEDs.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/DualButton_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 DualButtonBrickletFunction {
24    SetLedState,
25    GetLedState,
26    GetButtonState,
27    SetSelectedLedState,
28    GetIdentity,
29    CallbackStateChanged,
30}
31impl From<DualButtonBrickletFunction> for u8 {
32    fn from(fun: DualButtonBrickletFunction) -> Self {
33        match fun {
34            DualButtonBrickletFunction::SetLedState => 1,
35            DualButtonBrickletFunction::GetLedState => 2,
36            DualButtonBrickletFunction::GetButtonState => 3,
37            DualButtonBrickletFunction::SetSelectedLedState => 5,
38            DualButtonBrickletFunction::GetIdentity => 255,
39            DualButtonBrickletFunction::CallbackStateChanged => 4,
40        }
41    }
42}
43pub const DUAL_BUTTON_BRICKLET_LED_STATE_AUTO_TOGGLE_ON: u8 = 0;
44pub const DUAL_BUTTON_BRICKLET_LED_STATE_AUTO_TOGGLE_OFF: u8 = 1;
45pub const DUAL_BUTTON_BRICKLET_LED_STATE_ON: u8 = 2;
46pub const DUAL_BUTTON_BRICKLET_LED_STATE_OFF: u8 = 3;
47pub const DUAL_BUTTON_BRICKLET_BUTTON_STATE_PRESSED: u8 = 0;
48pub const DUAL_BUTTON_BRICKLET_BUTTON_STATE_RELEASED: u8 = 1;
49pub const DUAL_BUTTON_BRICKLET_LED_LEFT: u8 = 0;
50pub const DUAL_BUTTON_BRICKLET_LED_RIGHT: u8 = 1;
51
52#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
53pub struct LedState {
54    pub led_l: u8,
55    pub led_r: u8,
56}
57impl FromByteSlice for LedState {
58    fn bytes_expected() -> usize {
59        2
60    }
61    fn from_le_byte_slice(bytes: &[u8]) -> LedState {
62        LedState { led_l: <u8>::from_le_byte_slice(&bytes[0..1]), led_r: <u8>::from_le_byte_slice(&bytes[1..2]) }
63    }
64}
65
66#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
67pub struct ButtonState {
68    pub button_l: u8,
69    pub button_r: u8,
70}
71impl FromByteSlice for ButtonState {
72    fn bytes_expected() -> usize {
73        2
74    }
75    fn from_le_byte_slice(bytes: &[u8]) -> ButtonState {
76        ButtonState { button_l: <u8>::from_le_byte_slice(&bytes[0..1]), button_r: <u8>::from_le_byte_slice(&bytes[1..2]) }
77    }
78}
79
80#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
81pub struct StateChangedEvent {
82    pub button_l: u8,
83    pub button_r: u8,
84    pub led_l: u8,
85    pub led_r: u8,
86}
87impl FromByteSlice for StateChangedEvent {
88    fn bytes_expected() -> usize {
89        4
90    }
91    fn from_le_byte_slice(bytes: &[u8]) -> StateChangedEvent {
92        StateChangedEvent {
93            button_l: <u8>::from_le_byte_slice(&bytes[0..1]),
94            button_r: <u8>::from_le_byte_slice(&bytes[1..2]),
95            led_l: <u8>::from_le_byte_slice(&bytes[2..3]),
96            led_r: <u8>::from_le_byte_slice(&bytes[3..4]),
97        }
98    }
99}
100
101#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
102pub struct Identity {
103    pub uid: String,
104    pub connected_uid: String,
105    pub position: char,
106    pub hardware_version: [u8; 3],
107    pub firmware_version: [u8; 3],
108    pub device_identifier: u16,
109}
110impl FromByteSlice for Identity {
111    fn bytes_expected() -> usize {
112        25
113    }
114    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
115        Identity {
116            uid: <String>::from_le_byte_slice(&bytes[0..8]),
117            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
118            position: <char>::from_le_byte_slice(&bytes[16..17]),
119            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
120            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
121            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
122        }
123    }
124}
125
126/// Two tactile buttons with built-in blue LEDs
127#[derive(Clone)]
128pub struct DualButtonBricklet {
129    device: Device,
130}
131impl DualButtonBricklet {
132    pub const DEVICE_IDENTIFIER: u16 = 230;
133    pub const DEVICE_DISPLAY_NAME: &'static str = "Dual Button Bricklet";
134    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
135    pub fn new(uid: Uid, connection: AsyncIpConnection) -> DualButtonBricklet {
136        let mut result = DualButtonBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
137        result.device.response_expected[u8::from(DualButtonBrickletFunction::SetLedState) as usize] = ResponseExpectedFlag::False;
138        result.device.response_expected[u8::from(DualButtonBrickletFunction::GetLedState) as usize] = ResponseExpectedFlag::AlwaysTrue;
139        result.device.response_expected[u8::from(DualButtonBrickletFunction::GetButtonState) as usize] = ResponseExpectedFlag::AlwaysTrue;
140        result.device.response_expected[u8::from(DualButtonBrickletFunction::SetSelectedLedState) as usize] = ResponseExpectedFlag::False;
141        result.device.response_expected[u8::from(DualButtonBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
142        result
143    }
144
145    /// Returns the response expected flag for the function specified by the function ID parameter.
146    /// It is true if the function is expected to send a response, false otherwise.
147    ///
148    /// For getter functions this is enabled by default and cannot be disabled, because those
149    /// functions will always send a response. For callback configuration functions it is enabled
150    /// by default too, but can be disabled by [`set_response_expected`](crate::dual_button_bricklet::DualButtonBricklet::set_response_expected).
151    /// For setter functions it is disabled by default and can be enabled.
152    ///
153    /// Enabling the response expected flag for a setter function allows to detect timeouts
154    /// and other error conditions calls of this setter as well. The device will then send a response
155    /// for this purpose. If this flag is disabled for a setter function then no response is sent
156    /// and errors are silently ignored, because they cannot be detected.
157    ///
158    /// See [`set_response_expected`](crate::dual_button_bricklet::DualButtonBricklet::set_response_expected) for the list of function ID constants available for this function.
159    pub fn get_response_expected(&mut self, fun: DualButtonBrickletFunction) -> Result<bool, GetResponseExpectedError> {
160        self.device.get_response_expected(u8::from(fun))
161    }
162
163    /// Changes the response expected flag of the function specified by the function ID parameter.
164    /// This flag can only be changed for setter (default value: false) and callback configuration
165    /// functions (default value: true). For getter functions it is always enabled.
166    ///
167    /// Enabling the response expected flag for a setter function allows to detect timeouts and
168    /// other error conditions calls of this setter as well. The device will then send a response
169    /// for this purpose. If this flag is disabled for a setter function then no response is sent
170    /// and errors are silently ignored, because they cannot be detected.
171    pub fn set_response_expected(
172        &mut self,
173        fun: DualButtonBrickletFunction,
174        response_expected: bool,
175    ) -> Result<(), SetResponseExpectedError> {
176        self.device.set_response_expected(u8::from(fun), response_expected)
177    }
178
179    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
180    pub fn set_response_expected_all(&mut self, response_expected: bool) {
181        self.device.set_response_expected_all(response_expected)
182    }
183
184    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
185    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
186    pub fn get_api_version(&self) -> [u8; 3] {
187        self.device.api_version
188    }
189
190    /// This receiver is called whenever a button is pressed.
191    ///
192    /// Possible states for buttons are:
193    ///
194    /// * 0 = pressed
195    /// * 1 = released
196    ///
197    /// Possible states for LEDs are:
198    ///
199    /// * 0 = AutoToggleOn: Auto toggle enabled and LED on.
200    /// * 1 = AutoToggleOff: Auto toggle enabled and LED off.
201    /// * 2 = On: LED on (auto toggle is disabled).
202    /// * 3 = Off: LED off (auto toggle is disabled).
203    pub async fn get_state_changed_callback_receiver(&mut self) -> impl Stream<Item = StateChangedEvent> {
204        self.device
205            .get_callback_receiver(u8::from(DualButtonBrickletFunction::CallbackStateChanged))
206            .await
207            .map(|p| StateChangedEvent::from_le_byte_slice(p.body()))
208    }
209
210    /// Sets the state of the LEDs. Possible states are:
211    ///
212    /// * 0 = AutoToggleOn: Enables auto toggle with initially enabled LED.
213    /// * 1 = AutoToggleOff: Activates auto toggle with initially disabled LED.
214    /// * 2 = On: Enables LED (auto toggle is disabled).
215    /// * 3 = Off: Disables LED (auto toggle is disabled).
216    ///
217    /// In auto toggle mode the LED is toggled automatically at each press of a button.
218    ///
219    /// If you just want to set one of the LEDs and don't know the current state
220    /// of the other LED, you can get the state with [`get_led_state`] or you
221    /// can use [`set_selected_led_state`].
222    ///
223    /// Associated constants:
224    /// * DUAL_BUTTON_BRICKLET_LED_STATE_AUTO_TOGGLE_ON
225    ///	* DUAL_BUTTON_BRICKLET_LED_STATE_AUTO_TOGGLE_OFF
226    ///	* DUAL_BUTTON_BRICKLET_LED_STATE_ON
227    ///	* DUAL_BUTTON_BRICKLET_LED_STATE_OFF
228    pub async fn set_led_state(&mut self, led_l: u8, led_r: u8) -> Result<(), TinkerforgeError> {
229        let mut payload = [0; 2];
230        led_l.write_to_slice(&mut payload[0..1]);
231        led_r.write_to_slice(&mut payload[1..2]);
232
233        #[allow(unused_variables)]
234        let result = self.device.set(u8::from(DualButtonBrickletFunction::SetLedState), &payload).await?;
235        Ok(())
236    }
237
238    /// Returns the current state of the LEDs, as set by [`set_led_state`].
239    ///
240    /// Associated constants:
241    /// * DUAL_BUTTON_BRICKLET_LED_STATE_AUTO_TOGGLE_ON
242    ///	* DUAL_BUTTON_BRICKLET_LED_STATE_AUTO_TOGGLE_OFF
243    ///	* DUAL_BUTTON_BRICKLET_LED_STATE_ON
244    ///	* DUAL_BUTTON_BRICKLET_LED_STATE_OFF
245    pub async fn get_led_state(&mut self) -> Result<LedState, TinkerforgeError> {
246        let payload = [0; 0];
247
248        #[allow(unused_variables)]
249        let result = self.device.get(u8::from(DualButtonBrickletFunction::GetLedState), &payload).await?;
250        Ok(LedState::from_le_byte_slice(result.body()))
251    }
252
253    /// Returns the current state for both buttons. Possible states are:
254    ///
255    /// * 0 = pressed
256    /// * 1 = released
257    ///
258    /// Associated constants:
259    /// * DUAL_BUTTON_BRICKLET_BUTTON_STATE_PRESSED
260    ///	* DUAL_BUTTON_BRICKLET_BUTTON_STATE_RELEASED
261    pub async fn get_button_state(&mut self) -> Result<ButtonState, TinkerforgeError> {
262        let payload = [0; 0];
263
264        #[allow(unused_variables)]
265        let result = self.device.get(u8::from(DualButtonBrickletFunction::GetButtonState), &payload).await?;
266        Ok(ButtonState::from_le_byte_slice(result.body()))
267    }
268
269    /// Sets the state of the selected LED (0 or 1).
270    ///
271    /// The other LED remains untouched.
272    ///
273    /// Associated constants:
274    /// * DUAL_BUTTON_BRICKLET_LED_LEFT
275    ///	* DUAL_BUTTON_BRICKLET_LED_RIGHT
276    ///	* DUAL_BUTTON_BRICKLET_LED_STATE_AUTO_TOGGLE_ON
277    ///	* DUAL_BUTTON_BRICKLET_LED_STATE_AUTO_TOGGLE_OFF
278    ///	* DUAL_BUTTON_BRICKLET_LED_STATE_ON
279    ///	* DUAL_BUTTON_BRICKLET_LED_STATE_OFF
280    pub async fn set_selected_led_state(&mut self, led: u8, state: u8) -> Result<(), TinkerforgeError> {
281        let mut payload = [0; 2];
282        led.write_to_slice(&mut payload[0..1]);
283        state.write_to_slice(&mut payload[1..2]);
284
285        #[allow(unused_variables)]
286        let result = self.device.set(u8::from(DualButtonBrickletFunction::SetSelectedLedState), &payload).await?;
287        Ok(())
288    }
289
290    /// Returns the UID, the UID where the Bricklet is connected to,
291    /// the position, the hardware and firmware version as well as the
292    /// device identifier.
293    ///
294    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
295    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
296    /// position 'z'.
297    ///
298    /// The device identifier numbers can be found [here](device_identifier).
299    /// |device_identifier_constant|
300    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
301        let payload = [0; 0];
302
303        #[allow(unused_variables)]
304        let result = self.device.get(u8::from(DualButtonBrickletFunction::GetIdentity), &payload).await?;
305        Ok(Identity::from_le_byte_slice(result.body()))
306    }
307}