tinkerforge_async/bindings/
uv_light_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//! Measures UV light.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/UVLight_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 UvLightBrickletFunction {
24    GetUvLight,
25    SetUvLightCallbackPeriod,
26    GetUvLightCallbackPeriod,
27    SetUvLightCallbackThreshold,
28    GetUvLightCallbackThreshold,
29    SetDebouncePeriod,
30    GetDebouncePeriod,
31    GetIdentity,
32    CallbackUvLight,
33    CallbackUvLightReached,
34}
35impl From<UvLightBrickletFunction> for u8 {
36    fn from(fun: UvLightBrickletFunction) -> Self {
37        match fun {
38            UvLightBrickletFunction::GetUvLight => 1,
39            UvLightBrickletFunction::SetUvLightCallbackPeriod => 2,
40            UvLightBrickletFunction::GetUvLightCallbackPeriod => 3,
41            UvLightBrickletFunction::SetUvLightCallbackThreshold => 4,
42            UvLightBrickletFunction::GetUvLightCallbackThreshold => 5,
43            UvLightBrickletFunction::SetDebouncePeriod => 6,
44            UvLightBrickletFunction::GetDebouncePeriod => 7,
45            UvLightBrickletFunction::GetIdentity => 255,
46            UvLightBrickletFunction::CallbackUvLight => 8,
47            UvLightBrickletFunction::CallbackUvLightReached => 9,
48        }
49    }
50}
51pub const UV_LIGHT_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
52pub const UV_LIGHT_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
53pub const UV_LIGHT_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
54pub const UV_LIGHT_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
55pub const UV_LIGHT_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
56
57#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
58pub struct UvLightCallbackThreshold {
59    pub option: char,
60    pub min: u32,
61    pub max: u32,
62}
63impl FromByteSlice for UvLightCallbackThreshold {
64    fn bytes_expected() -> usize {
65        9
66    }
67    fn from_le_byte_slice(bytes: &[u8]) -> UvLightCallbackThreshold {
68        UvLightCallbackThreshold {
69            option: <char>::from_le_byte_slice(&bytes[0..1]),
70            min: <u32>::from_le_byte_slice(&bytes[1..5]),
71            max: <u32>::from_le_byte_slice(&bytes[5..9]),
72        }
73    }
74}
75
76#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
77pub struct Identity {
78    pub uid: String,
79    pub connected_uid: String,
80    pub position: char,
81    pub hardware_version: [u8; 3],
82    pub firmware_version: [u8; 3],
83    pub device_identifier: u16,
84}
85impl FromByteSlice for Identity {
86    fn bytes_expected() -> usize {
87        25
88    }
89    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
90        Identity {
91            uid: <String>::from_le_byte_slice(&bytes[0..8]),
92            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
93            position: <char>::from_le_byte_slice(&bytes[16..17]),
94            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
95            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
96            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
97        }
98    }
99}
100
101/// Measures UV light
102#[derive(Clone)]
103pub struct UvLightBricklet {
104    device: Device,
105}
106impl UvLightBricklet {
107    pub const DEVICE_IDENTIFIER: u16 = 265;
108    pub const DEVICE_DISPLAY_NAME: &'static str = "UV Light Bricklet";
109    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
110    pub fn new(uid: Uid, connection: AsyncIpConnection) -> UvLightBricklet {
111        let mut result = UvLightBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
112        result.device.response_expected[u8::from(UvLightBrickletFunction::GetUvLight) as usize] = ResponseExpectedFlag::AlwaysTrue;
113        result.device.response_expected[u8::from(UvLightBrickletFunction::SetUvLightCallbackPeriod) as usize] = ResponseExpectedFlag::True;
114        result.device.response_expected[u8::from(UvLightBrickletFunction::GetUvLightCallbackPeriod) as usize] =
115            ResponseExpectedFlag::AlwaysTrue;
116        result.device.response_expected[u8::from(UvLightBrickletFunction::SetUvLightCallbackThreshold) as usize] =
117            ResponseExpectedFlag::True;
118        result.device.response_expected[u8::from(UvLightBrickletFunction::GetUvLightCallbackThreshold) as usize] =
119            ResponseExpectedFlag::AlwaysTrue;
120        result.device.response_expected[u8::from(UvLightBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
121        result.device.response_expected[u8::from(UvLightBrickletFunction::GetDebouncePeriod) as usize] = ResponseExpectedFlag::AlwaysTrue;
122        result.device.response_expected[u8::from(UvLightBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
123        result
124    }
125
126    /// Returns the response expected flag for the function specified by the function ID parameter.
127    /// It is true if the function is expected to send a response, false otherwise.
128    ///
129    /// For getter functions this is enabled by default and cannot be disabled, because those
130    /// functions will always send a response. For callback configuration functions it is enabled
131    /// by default too, but can be disabled by [`set_response_expected`](crate::uv_light_bricklet::UvLightBricklet::set_response_expected).
132    /// For setter functions it is disabled by default and can be enabled.
133    ///
134    /// Enabling the response expected flag for a setter function allows to detect timeouts
135    /// and other error conditions calls of this setter as well. The device will then send a response
136    /// for this purpose. If this flag is disabled for a setter function then no response is sent
137    /// and errors are silently ignored, because they cannot be detected.
138    ///
139    /// See [`set_response_expected`](crate::uv_light_bricklet::UvLightBricklet::set_response_expected) for the list of function ID constants available for this function.
140    pub fn get_response_expected(&mut self, fun: UvLightBrickletFunction) -> Result<bool, GetResponseExpectedError> {
141        self.device.get_response_expected(u8::from(fun))
142    }
143
144    /// Changes the response expected flag of the function specified by the function ID parameter.
145    /// This flag can only be changed for setter (default value: false) and callback configuration
146    /// functions (default value: true). For getter functions it is always enabled.
147    ///
148    /// Enabling the response expected flag for a setter function allows to detect timeouts and
149    /// other error conditions calls of this setter as well. The device will then send a response
150    /// for this purpose. If this flag is disabled for a setter function then no response is sent
151    /// and errors are silently ignored, because they cannot be detected.
152    pub fn set_response_expected(&mut self, fun: UvLightBrickletFunction, response_expected: bool) -> Result<(), SetResponseExpectedError> {
153        self.device.set_response_expected(u8::from(fun), response_expected)
154    }
155
156    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
157    pub fn set_response_expected_all(&mut self, response_expected: bool) {
158        self.device.set_response_expected_all(response_expected)
159    }
160
161    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
162    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
163    pub fn get_api_version(&self) -> [u8; 3] {
164        self.device.api_version
165    }
166
167    /// This receiver is triggered periodically with the period that is set by
168    /// [`set_uv_light_callback_period`]. The parameter is the UV Light
169    /// intensity of the sensor.
170    ///
171    /// The [`get_uv_light_callback_receiver`] receiver is only triggered if the intensity has changed
172    /// since the last triggering.
173    ///
174    /// [`set_uv_light_callback_period`]: #method.set_uv_light_callback_period
175    /// [`get_uv_light_callback_receiver`]: #method.get_uv_light_callback_receiver
176    pub async fn get_uv_light_callback_receiver(&mut self) -> impl Stream<Item = u32> {
177        self.device
178            .get_callback_receiver(u8::from(UvLightBrickletFunction::CallbackUvLight))
179            .await
180            .map(|p| u32::from_le_byte_slice(p.body()))
181    }
182
183    /// This receiver is triggered when the threshold as set by
184    /// [`set_uv_light_callback_threshold`] is reached.
185    /// The parameter is the UV Light intensity of the sensor.
186    ///
187    /// If the threshold keeps being reached, the receiver is triggered periodically
188    /// with the period as set by [`set_debounce_period`].
189    pub async fn get_uv_light_reached_callback_receiver(&mut self) -> impl Stream<Item = u32> {
190        self.device
191            .get_callback_receiver(u8::from(UvLightBrickletFunction::CallbackUvLightReached))
192            .await
193            .map(|p| u32::from_le_byte_slice(p.body()))
194    }
195
196    /// Returns the UV light intensity of the sensor.
197    /// The sensor has already weighted the intensity with the erythemal
198    /// action spectrum to get the skin-affecting irradiation.
199    ///
200    /// To get UV index you just have to divide the value by 250. For example, a UV
201    /// light intensity of 500 is equivalent to an UV index of 2.
202    ///
203    /// If you want to get the intensity periodically, it is recommended to use the
204    /// [`get_uv_light_callback_receiver`] receiver and set the period with
205    /// [`set_uv_light_callback_period`].
206    pub async fn get_uv_light(&mut self) -> Result<u32, TinkerforgeError> {
207        let payload = [0; 0];
208
209        #[allow(unused_variables)]
210        let result = self.device.get(u8::from(UvLightBrickletFunction::GetUvLight), &payload).await?;
211        Ok(u32::from_le_byte_slice(result.body()))
212    }
213
214    /// Sets the period with which the [`get_uv_light_callback_receiver`] receiver is triggered
215    /// periodically. A value of 0 turns the receiver off.
216    ///
217    /// The [`get_uv_light_callback_receiver`] receiver is only triggered if the intensity has changed since
218    /// the last triggering.
219    pub async fn set_uv_light_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
220        let mut payload = [0; 4];
221        period.write_to_slice(&mut payload[0..4]);
222
223        #[allow(unused_variables)]
224        let result = self.device.set(u8::from(UvLightBrickletFunction::SetUvLightCallbackPeriod), &payload).await?;
225        Ok(())
226    }
227
228    /// Returns the period as set by [`set_uv_light_callback_period`].
229    pub async fn get_uv_light_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
230        let payload = [0; 0];
231
232        #[allow(unused_variables)]
233        let result = self.device.get(u8::from(UvLightBrickletFunction::GetUvLightCallbackPeriod), &payload).await?;
234        Ok(u32::from_le_byte_slice(result.body()))
235    }
236
237    /// Sets the thresholds for the [`get_uv_light_reached_callback_receiver`] receiver.
238    ///
239    /// The following options are possible:
240    ///
241    ///  Option| Description
242    ///  --- | ---
243    ///  'x'|    Receiver is turned off
244    ///  'o'|    Receiver is triggered when the intensity is *outside* the min and max values
245    ///  'i'|    Receiver is triggered when the intensity is *inside* the min and max values
246    ///  '<'|    Receiver is triggered when the intensity is smaller than the min value (max is ignored)
247    ///  '>'|    Receiver is triggered when the intensity is greater than the min value (max is ignored)
248    ///
249    /// Associated constants:
250    /// * UV_LIGHT_BRICKLET_THRESHOLD_OPTION_OFF
251    ///	* UV_LIGHT_BRICKLET_THRESHOLD_OPTION_OUTSIDE
252    ///	* UV_LIGHT_BRICKLET_THRESHOLD_OPTION_INSIDE
253    ///	* UV_LIGHT_BRICKLET_THRESHOLD_OPTION_SMALLER
254    ///	* UV_LIGHT_BRICKLET_THRESHOLD_OPTION_GREATER
255    pub async fn set_uv_light_callback_threshold(&mut self, option: char, min: u32, max: u32) -> Result<(), TinkerforgeError> {
256        let mut payload = [0; 9];
257        option.write_to_slice(&mut payload[0..1]);
258        min.write_to_slice(&mut payload[1..5]);
259        max.write_to_slice(&mut payload[5..9]);
260
261        #[allow(unused_variables)]
262        let result = self.device.set(u8::from(UvLightBrickletFunction::SetUvLightCallbackThreshold), &payload).await?;
263        Ok(())
264    }
265
266    /// Returns the threshold as set by [`set_uv_light_callback_threshold`].
267    ///
268    /// Associated constants:
269    /// * UV_LIGHT_BRICKLET_THRESHOLD_OPTION_OFF
270    ///	* UV_LIGHT_BRICKLET_THRESHOLD_OPTION_OUTSIDE
271    ///	* UV_LIGHT_BRICKLET_THRESHOLD_OPTION_INSIDE
272    ///	* UV_LIGHT_BRICKLET_THRESHOLD_OPTION_SMALLER
273    ///	* UV_LIGHT_BRICKLET_THRESHOLD_OPTION_GREATER
274    pub async fn get_uv_light_callback_threshold(&mut self) -> Result<UvLightCallbackThreshold, TinkerforgeError> {
275        let payload = [0; 0];
276
277        #[allow(unused_variables)]
278        let result = self.device.get(u8::from(UvLightBrickletFunction::GetUvLightCallbackThreshold), &payload).await?;
279        Ok(UvLightCallbackThreshold::from_le_byte_slice(result.body()))
280    }
281
282    /// Sets the period with which the threshold receivers
283    ///
284    /// * [`get_uv_light_reached_callback_receiver`],
285    ///
286    /// are triggered, if the thresholds
287    ///
288    /// * [`set_uv_light_callback_threshold`],
289    ///
290    /// keep being reached.
291    pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
292        let mut payload = [0; 4];
293        debounce.write_to_slice(&mut payload[0..4]);
294
295        #[allow(unused_variables)]
296        let result = self.device.set(u8::from(UvLightBrickletFunction::SetDebouncePeriod), &payload).await?;
297        Ok(())
298    }
299
300    /// Returns the debounce period as set by [`set_debounce_period`].
301    pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
302        let payload = [0; 0];
303
304        #[allow(unused_variables)]
305        let result = self.device.get(u8::from(UvLightBrickletFunction::GetDebouncePeriod), &payload).await?;
306        Ok(u32::from_le_byte_slice(result.body()))
307    }
308
309    /// Returns the UID, the UID where the Bricklet is connected to,
310    /// the position, the hardware and firmware version as well as the
311    /// device identifier.
312    ///
313    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
314    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
315    /// position 'z'.
316    ///
317    /// The device identifier numbers can be found [here](device_identifier).
318    /// |device_identifier_constant|
319    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
320        let payload = [0; 0];
321
322        #[allow(unused_variables)]
323        let result = self.device.get(u8::from(UvLightBrickletFunction::GetIdentity), &payload).await?;
324        Ok(Identity::from_le_byte_slice(result.body()))
325    }
326}