tinkerforge_async/bindings/tilt_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//! Detects inclination of Bricklet (tilt switch open/closed).
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/Tilt_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 TiltBrickletFunction {
24 GetTiltState,
25 EnableTiltStateCallback,
26 DisableTiltStateCallback,
27 IsTiltStateCallbackEnabled,
28 GetIdentity,
29 CallbackTiltState,
30}
31impl From<TiltBrickletFunction> for u8 {
32 fn from(fun: TiltBrickletFunction) -> Self {
33 match fun {
34 TiltBrickletFunction::GetTiltState => 1,
35 TiltBrickletFunction::EnableTiltStateCallback => 2,
36 TiltBrickletFunction::DisableTiltStateCallback => 3,
37 TiltBrickletFunction::IsTiltStateCallbackEnabled => 4,
38 TiltBrickletFunction::GetIdentity => 255,
39 TiltBrickletFunction::CallbackTiltState => 5,
40 }
41 }
42}
43pub const TILT_BRICKLET_TILT_STATE_CLOSED: u8 = 0;
44pub const TILT_BRICKLET_TILT_STATE_OPEN: u8 = 1;
45pub const TILT_BRICKLET_TILT_STATE_CLOSED_VIBRATING: u8 = 2;
46
47#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
48pub struct Identity {
49 pub uid: String,
50 pub connected_uid: String,
51 pub position: char,
52 pub hardware_version: [u8; 3],
53 pub firmware_version: [u8; 3],
54 pub device_identifier: u16,
55}
56impl FromByteSlice for Identity {
57 fn bytes_expected() -> usize {
58 25
59 }
60 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
61 Identity {
62 uid: <String>::from_le_byte_slice(&bytes[0..8]),
63 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
64 position: <char>::from_le_byte_slice(&bytes[16..17]),
65 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
66 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
67 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
68 }
69 }
70}
71
72/// Detects inclination of Bricklet (tilt switch open/closed)
73#[derive(Clone)]
74pub struct TiltBricklet {
75 device: Device,
76}
77impl TiltBricklet {
78 pub const DEVICE_IDENTIFIER: u16 = 239;
79 pub const DEVICE_DISPLAY_NAME: &'static str = "Tilt Bricklet";
80 /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
81 pub fn new(uid: Uid, connection: AsyncIpConnection) -> TiltBricklet {
82 let mut result = TiltBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
83 result.device.response_expected[u8::from(TiltBrickletFunction::GetTiltState) as usize] = ResponseExpectedFlag::AlwaysTrue;
84 result.device.response_expected[u8::from(TiltBrickletFunction::EnableTiltStateCallback) as usize] = ResponseExpectedFlag::True;
85 result.device.response_expected[u8::from(TiltBrickletFunction::DisableTiltStateCallback) as usize] = ResponseExpectedFlag::True;
86 result.device.response_expected[u8::from(TiltBrickletFunction::IsTiltStateCallbackEnabled) as usize] =
87 ResponseExpectedFlag::AlwaysTrue;
88 result.device.response_expected[u8::from(TiltBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
89 result
90 }
91
92 /// Returns the response expected flag for the function specified by the function ID parameter.
93 /// It is true if the function is expected to send a response, false otherwise.
94 ///
95 /// For getter functions this is enabled by default and cannot be disabled, because those
96 /// functions will always send a response. For callback configuration functions it is enabled
97 /// by default too, but can be disabled by [`set_response_expected`](crate::tilt_bricklet::TiltBricklet::set_response_expected).
98 /// For setter functions it is disabled by default and can be enabled.
99 ///
100 /// Enabling the response expected flag for a setter function allows to detect timeouts
101 /// and other error conditions calls of this setter as well. The device will then send a response
102 /// for this purpose. If this flag is disabled for a setter function then no response is sent
103 /// and errors are silently ignored, because they cannot be detected.
104 ///
105 /// See [`set_response_expected`](crate::tilt_bricklet::TiltBricklet::set_response_expected) for the list of function ID constants available for this function.
106 pub fn get_response_expected(&mut self, fun: TiltBrickletFunction) -> Result<bool, GetResponseExpectedError> {
107 self.device.get_response_expected(u8::from(fun))
108 }
109
110 /// Changes the response expected flag of the function specified by the function ID parameter.
111 /// This flag can only be changed for setter (default value: false) and callback configuration
112 /// functions (default value: true). For getter functions it is always enabled.
113 ///
114 /// Enabling the response expected flag for a setter function allows to detect timeouts and
115 /// other error conditions calls of this setter as well. The device will then send a response
116 /// for this purpose. If this flag is disabled for a setter function then no response is sent
117 /// and errors are silently ignored, because they cannot be detected.
118 pub fn set_response_expected(&mut self, fun: TiltBrickletFunction, response_expected: bool) -> Result<(), SetResponseExpectedError> {
119 self.device.set_response_expected(u8::from(fun), response_expected)
120 }
121
122 /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
123 pub fn set_response_expected_all(&mut self, response_expected: bool) {
124 self.device.set_response_expected_all(response_expected)
125 }
126
127 /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
128 /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
129 pub fn get_api_version(&self) -> [u8; 3] {
130 self.device.api_version
131 }
132
133 /// This receiver provides the current tilt state. It is called every time the
134 /// state changes.
135 ///
136 /// See [`get_tilt_state`] for a description of the states.
137 ///
138 /// [`get_tilt_state`]: #method.get_tilt_state
139 pub async fn get_tilt_state_callback_receiver(&mut self) -> impl Stream<Item = u8> {
140 self.device.get_callback_receiver(u8::from(TiltBrickletFunction::CallbackTiltState)).await.map(|p| u8::from_le_byte_slice(p.body()))
141 }
142
143 /// Returns the current tilt state. The state can either be
144 ///
145 /// * 0 = Closed: The ball in the tilt switch closes the circuit.
146 /// * 1 = Open: The ball in the tilt switch does not close the circuit.
147 /// * 2 = Closed Vibrating: The tilt switch is in motion (rapid change between open and close).
148 ///
149 /// .. image:: /Images/Bricklets/bricklet_tilt_mechanics.jpg
150 /// :scale: 100 %
151 /// :alt: Tilt states
152 /// :align: center
153 /// :target: ../../_images/Bricklets/bricklet_tilt_mechanics.jpg
154 ///
155 /// Associated constants:
156 /// * TILT_BRICKLET_TILT_STATE_CLOSED
157 /// * TILT_BRICKLET_TILT_STATE_OPEN
158 /// * TILT_BRICKLET_TILT_STATE_CLOSED_VIBRATING
159 pub async fn get_tilt_state(&mut self) -> Result<u8, TinkerforgeError> {
160 let payload = [0; 0];
161
162 #[allow(unused_variables)]
163 let result = self.device.get(u8::from(TiltBrickletFunction::GetTiltState), &payload).await?;
164 Ok(u8::from_le_byte_slice(result.body()))
165 }
166
167 /// Enables the [`get_tilt_state_callback_receiver`] receiver.
168 pub async fn enable_tilt_state_callback(&mut self) -> Result<(), TinkerforgeError> {
169 let payload = [0; 0];
170
171 #[allow(unused_variables)]
172 let result = self.device.set(u8::from(TiltBrickletFunction::EnableTiltStateCallback), &payload).await?;
173 Ok(())
174 }
175
176 /// Disables the [`get_tilt_state_callback_receiver`] receiver.
177 pub async fn disable_tilt_state_callback(&mut self) -> Result<(), TinkerforgeError> {
178 let payload = [0; 0];
179
180 #[allow(unused_variables)]
181 let result = self.device.set(u8::from(TiltBrickletFunction::DisableTiltStateCallback), &payload).await?;
182 Ok(())
183 }
184
185 /// Returns *true* if the [`get_tilt_state_callback_receiver`] receiver is enabled.
186 pub async fn is_tilt_state_callback_enabled(&mut self) -> Result<bool, TinkerforgeError> {
187 let payload = [0; 0];
188
189 #[allow(unused_variables)]
190 let result = self.device.get(u8::from(TiltBrickletFunction::IsTiltStateCallbackEnabled), &payload).await?;
191 Ok(bool::from_le_byte_slice(result.body()))
192 }
193
194 /// Returns the UID, the UID where the Bricklet is connected to,
195 /// the position, the hardware and firmware version as well as the
196 /// device identifier.
197 ///
198 /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
199 /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
200 /// position 'z'.
201 ///
202 /// The device identifier numbers can be found [here](device_identifier).
203 /// |device_identifier_constant|
204 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
205 let payload = [0; 0];
206
207 #[allow(unused_variables)]
208 let result = self.device.get(u8::from(TiltBrickletFunction::GetIdentity), &payload).await?;
209 Ok(Identity::from_le_byte_slice(result.body()))
210 }
211}