tinkerforge_async/bindings/motion_detector_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//! Passive infrared (PIR) motion sensor with 7m range.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/MotionDetector_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 MotionDetectorBrickletFunction {
24 GetMotionDetected,
25 SetStatusLedConfig,
26 GetStatusLedConfig,
27 GetIdentity,
28 CallbackMotionDetected,
29 CallbackDetectionCycleEnded,
30}
31impl From<MotionDetectorBrickletFunction> for u8 {
32 fn from(fun: MotionDetectorBrickletFunction) -> Self {
33 match fun {
34 MotionDetectorBrickletFunction::GetMotionDetected => 1,
35 MotionDetectorBrickletFunction::SetStatusLedConfig => 4,
36 MotionDetectorBrickletFunction::GetStatusLedConfig => 5,
37 MotionDetectorBrickletFunction::GetIdentity => 255,
38 MotionDetectorBrickletFunction::CallbackMotionDetected => 2,
39 MotionDetectorBrickletFunction::CallbackDetectionCycleEnded => 3,
40 }
41 }
42}
43pub const MOTION_DETECTOR_BRICKLET_MOTION_NOT_DETECTED: u8 = 0;
44pub const MOTION_DETECTOR_BRICKLET_MOTION_DETECTED: u8 = 1;
45pub const MOTION_DETECTOR_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
46pub const MOTION_DETECTOR_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
47pub const MOTION_DETECTOR_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 2;
48
49#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
50pub struct Identity {
51 pub uid: String,
52 pub connected_uid: String,
53 pub position: char,
54 pub hardware_version: [u8; 3],
55 pub firmware_version: [u8; 3],
56 pub device_identifier: u16,
57}
58impl FromByteSlice for Identity {
59 fn bytes_expected() -> usize {
60 25
61 }
62 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
63 Identity {
64 uid: <String>::from_le_byte_slice(&bytes[0..8]),
65 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
66 position: <char>::from_le_byte_slice(&bytes[16..17]),
67 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
68 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
69 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
70 }
71 }
72}
73
74/// Passive infrared (PIR) motion sensor with 7m range
75#[derive(Clone)]
76pub struct MotionDetectorBricklet {
77 device: Device,
78}
79impl MotionDetectorBricklet {
80 pub const DEVICE_IDENTIFIER: u16 = 233;
81 pub const DEVICE_DISPLAY_NAME: &'static str = "Motion Detector Bricklet";
82 /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
83 pub fn new(uid: Uid, connection: AsyncIpConnection) -> MotionDetectorBricklet {
84 let mut result = MotionDetectorBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
85 result.device.response_expected[u8::from(MotionDetectorBrickletFunction::GetMotionDetected) as usize] =
86 ResponseExpectedFlag::AlwaysTrue;
87 result.device.response_expected[u8::from(MotionDetectorBrickletFunction::SetStatusLedConfig) as usize] =
88 ResponseExpectedFlag::False;
89 result.device.response_expected[u8::from(MotionDetectorBrickletFunction::GetStatusLedConfig) as usize] =
90 ResponseExpectedFlag::AlwaysTrue;
91 result.device.response_expected[u8::from(MotionDetectorBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
92 result
93 }
94
95 /// Returns the response expected flag for the function specified by the function ID parameter.
96 /// It is true if the function is expected to send a response, false otherwise.
97 ///
98 /// For getter functions this is enabled by default and cannot be disabled, because those
99 /// functions will always send a response. For callback configuration functions it is enabled
100 /// by default too, but can be disabled by [`set_response_expected`](crate::motion_detector_bricklet::MotionDetectorBricklet::set_response_expected).
101 /// For setter functions it is disabled by default and can be enabled.
102 ///
103 /// Enabling the response expected flag for a setter function allows to detect timeouts
104 /// and other error conditions calls of this setter as well. The device will then send a response
105 /// for this purpose. If this flag is disabled for a setter function then no response is sent
106 /// and errors are silently ignored, because they cannot be detected.
107 ///
108 /// See [`set_response_expected`](crate::motion_detector_bricklet::MotionDetectorBricklet::set_response_expected) for the list of function ID constants available for this function.
109 pub fn get_response_expected(&mut self, fun: MotionDetectorBrickletFunction) -> Result<bool, GetResponseExpectedError> {
110 self.device.get_response_expected(u8::from(fun))
111 }
112
113 /// Changes the response expected flag of the function specified by the function ID parameter.
114 /// This flag can only be changed for setter (default value: false) and callback configuration
115 /// functions (default value: true). For getter functions it is always enabled.
116 ///
117 /// Enabling the response expected flag for a setter function allows to detect timeouts and
118 /// other error conditions calls of this setter as well. The device will then send a response
119 /// for this purpose. If this flag is disabled for a setter function then no response is sent
120 /// and errors are silently ignored, because they cannot be detected.
121 pub fn set_response_expected(
122 &mut self,
123 fun: MotionDetectorBrickletFunction,
124 response_expected: bool,
125 ) -> Result<(), SetResponseExpectedError> {
126 self.device.set_response_expected(u8::from(fun), response_expected)
127 }
128
129 /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
130 pub fn set_response_expected_all(&mut self, response_expected: bool) {
131 self.device.set_response_expected_all(response_expected)
132 }
133
134 /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
135 /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
136 pub fn get_api_version(&self) -> [u8; 3] {
137 self.device.api_version
138 }
139
140 /// This receiver is called after a motion was detected.
141 pub async fn get_motion_detected_callback_receiver(&mut self) -> impl Stream<Item = ()> {
142 self.device.get_callback_receiver(u8::from(MotionDetectorBrickletFunction::CallbackMotionDetected)).await.map(|_p| ())
143 }
144
145 /// This receiver is called when the detection cycle ended. When this
146 /// receiver is called, a new motion can be detected again after approximately 2
147 /// seconds.
148 pub async fn get_detection_cycle_ended_callback_receiver(&mut self) -> impl Stream<Item = ()> {
149 self.device.get_callback_receiver(u8::from(MotionDetectorBrickletFunction::CallbackDetectionCycleEnded)).await.map(|_p| ())
150 }
151
152 /// Returns 1 if a motion was detected. How long this returns 1 after a motion
153 /// was detected can be adjusted with one of the small potentiometers on the
154 /// Motion Detector Bricklet, see `here
155 /// <motion_detector_bricklet_sensitivity_delay_block_time>`.
156 ///
157 /// There is also a blue LED on the Bricklet that is on as long as the Bricklet is
158 /// in the motion detected state.
159 ///
160 /// Associated constants:
161 /// * MOTION_DETECTOR_BRICKLET_MOTION_NOT_DETECTED
162 /// * MOTION_DETECTOR_BRICKLET_MOTION_DETECTED
163 pub async fn get_motion_detected(&mut self) -> Result<u8, TinkerforgeError> {
164 let payload = [0; 0];
165
166 #[allow(unused_variables)]
167 let result = self.device.get(u8::from(MotionDetectorBrickletFunction::GetMotionDetected), &payload).await?;
168 Ok(u8::from_le_byte_slice(result.body()))
169 }
170
171 /// Sets the status led configuration.
172 ///
173 /// By default the status LED turns on if a motion is detected and off is no motion
174 /// is detected.
175 ///
176 /// You can also turn the LED permanently on/off.
177 ///
178 ///
179 /// .. versionadded:: 2.0.1$nbsp;(Plugin)
180 ///
181 /// Associated constants:
182 /// * MOTION_DETECTOR_BRICKLET_STATUS_LED_CONFIG_OFF
183 /// * MOTION_DETECTOR_BRICKLET_STATUS_LED_CONFIG_ON
184 /// * MOTION_DETECTOR_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
185 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
186 let mut payload = [0; 1];
187 config.write_to_slice(&mut payload[0..1]);
188
189 #[allow(unused_variables)]
190 let result = self.device.set(u8::from(MotionDetectorBrickletFunction::SetStatusLedConfig), &payload).await?;
191 Ok(())
192 }
193
194 /// Returns the configuration as set by [`set_status_led_config`].
195 ///
196 ///
197 /// .. versionadded:: 2.0.1$nbsp;(Plugin)
198 ///
199 /// Associated constants:
200 /// * MOTION_DETECTOR_BRICKLET_STATUS_LED_CONFIG_OFF
201 /// * MOTION_DETECTOR_BRICKLET_STATUS_LED_CONFIG_ON
202 /// * MOTION_DETECTOR_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
203 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
204 let payload = [0; 0];
205
206 #[allow(unused_variables)]
207 let result = self.device.get(u8::from(MotionDetectorBrickletFunction::GetStatusLedConfig), &payload).await?;
208 Ok(u8::from_le_byte_slice(result.body()))
209 }
210
211 /// Returns the UID, the UID where the Bricklet is connected to,
212 /// the position, the hardware and firmware version as well as the
213 /// device identifier.
214 ///
215 /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
216 /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
217 /// position 'z'.
218 ///
219 /// The device identifier numbers can be found [here](device_identifier).
220 /// |device_identifier_constant|
221 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
222 let payload = [0; 0];
223
224 #[allow(unused_variables)]
225 let result = self.device.get(u8::from(MotionDetectorBrickletFunction::GetIdentity), &payload).await?;
226 Ok(Identity::from_le_byte_slice(result.body()))
227 }
228}