tinkerforge_async/bindings/
rgb_led_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 one RGB LED.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/RGBLED_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 RgbLedBrickletFunction {
24    SetRgbValue,
25    GetRgbValue,
26    GetIdentity,
27}
28impl From<RgbLedBrickletFunction> for u8 {
29    fn from(fun: RgbLedBrickletFunction) -> Self {
30        match fun {
31            RgbLedBrickletFunction::SetRgbValue => 1,
32            RgbLedBrickletFunction::GetRgbValue => 2,
33            RgbLedBrickletFunction::GetIdentity => 255,
34        }
35    }
36}
37
38#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
39pub struct RgbValue {
40    pub r: u8,
41    pub g: u8,
42    pub b: u8,
43}
44impl FromByteSlice for RgbValue {
45    fn bytes_expected() -> usize {
46        3
47    }
48    fn from_le_byte_slice(bytes: &[u8]) -> RgbValue {
49        RgbValue {
50            r: <u8>::from_le_byte_slice(&bytes[0..1]),
51            g: <u8>::from_le_byte_slice(&bytes[1..2]),
52            b: <u8>::from_le_byte_slice(&bytes[2..3]),
53        }
54    }
55}
56
57#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
58pub struct Identity {
59    pub uid: String,
60    pub connected_uid: String,
61    pub position: char,
62    pub hardware_version: [u8; 3],
63    pub firmware_version: [u8; 3],
64    pub device_identifier: u16,
65}
66impl FromByteSlice for Identity {
67    fn bytes_expected() -> usize {
68        25
69    }
70    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
71        Identity {
72            uid: <String>::from_le_byte_slice(&bytes[0..8]),
73            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
74            position: <char>::from_le_byte_slice(&bytes[16..17]),
75            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
76            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
77            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
78        }
79    }
80}
81
82/// Controls one RGB LED
83#[derive(Clone)]
84pub struct RgbLedBricklet {
85    device: Device,
86}
87impl RgbLedBricklet {
88    pub const DEVICE_IDENTIFIER: u16 = 271;
89    pub const DEVICE_DISPLAY_NAME: &'static str = "RGB LED Bricklet";
90    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
91    pub fn new(uid: Uid, connection: AsyncIpConnection) -> RgbLedBricklet {
92        let mut result = RgbLedBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
93        result.device.response_expected[u8::from(RgbLedBrickletFunction::SetRgbValue) as usize] = ResponseExpectedFlag::False;
94        result.device.response_expected[u8::from(RgbLedBrickletFunction::GetRgbValue) as usize] = ResponseExpectedFlag::AlwaysTrue;
95        result.device.response_expected[u8::from(RgbLedBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
96        result
97    }
98
99    /// Returns the response expected flag for the function specified by the function ID parameter.
100    /// It is true if the function is expected to send a response, false otherwise.
101    ///
102    /// For getter functions this is enabled by default and cannot be disabled, because those
103    /// functions will always send a response. For callback configuration functions it is enabled
104    /// by default too, but can be disabled by [`set_response_expected`](crate::rgb_led_bricklet::RgbLedBricklet::set_response_expected).
105    /// For setter functions it is disabled by default and can be enabled.
106    ///
107    /// Enabling the response expected flag for a setter function allows to detect timeouts
108    /// and other error conditions calls of this setter as well. The device will then send a response
109    /// for this purpose. If this flag is disabled for a setter function then no response is sent
110    /// and errors are silently ignored, because they cannot be detected.
111    ///
112    /// See [`set_response_expected`](crate::rgb_led_bricklet::RgbLedBricklet::set_response_expected) for the list of function ID constants available for this function.
113    pub fn get_response_expected(&mut self, fun: RgbLedBrickletFunction) -> Result<bool, GetResponseExpectedError> {
114        self.device.get_response_expected(u8::from(fun))
115    }
116
117    /// Changes the response expected flag of the function specified by the function ID parameter.
118    /// This flag can only be changed for setter (default value: false) and callback configuration
119    /// functions (default value: true). For getter functions it is always enabled.
120    ///
121    /// Enabling the response expected flag for a setter function allows to detect timeouts and
122    /// other error conditions calls of this setter as well. The device will then send a response
123    /// for this purpose. If this flag is disabled for a setter function then no response is sent
124    /// and errors are silently ignored, because they cannot be detected.
125    pub fn set_response_expected(&mut self, fun: RgbLedBrickletFunction, response_expected: bool) -> 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    /// Sets the *r*, *g* and *b* values for the LED.
141    pub async fn set_rgb_value(&mut self, r: u8, g: u8, b: u8) -> Result<(), TinkerforgeError> {
142        let mut payload = [0; 3];
143        r.write_to_slice(&mut payload[0..1]);
144        g.write_to_slice(&mut payload[1..2]);
145        b.write_to_slice(&mut payload[2..3]);
146
147        #[allow(unused_variables)]
148        let result = self.device.set(u8::from(RgbLedBrickletFunction::SetRgbValue), &payload).await?;
149        Ok(())
150    }
151
152    /// Returns the *r*, *g* and *b* values of the LED as set by [`set_rgb_value`].
153    pub async fn get_rgb_value(&mut self) -> Result<RgbValue, TinkerforgeError> {
154        let payload = [0; 0];
155
156        #[allow(unused_variables)]
157        let result = self.device.get(u8::from(RgbLedBrickletFunction::GetRgbValue), &payload).await?;
158        Ok(RgbValue::from_le_byte_slice(result.body()))
159    }
160
161    /// Returns the UID, the UID where the Bricklet is connected to,
162    /// the position, the hardware and firmware version as well as the
163    /// device identifier.
164    ///
165    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
166    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
167    /// position 'z'.
168    ///
169    /// The device identifier numbers can be found [here](device_identifier).
170    /// |device_identifier_constant|
171    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
172        let payload = [0; 0];
173
174        #[allow(unused_variables)]
175        let result = self.device.get(u8::from(RgbLedBrickletFunction::GetIdentity), &payload).await?;
176        Ok(Identity::from_le_byte_slice(result.body()))
177    }
178}