tinkerforge_async/bindings/
rgb_led_bricklet.rs1#[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#[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 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 pub fn get_response_expected(&mut self, fun: RgbLedBrickletFunction) -> Result<bool, GetResponseExpectedError> {
114 self.device.get_response_expected(u8::from(fun))
115 }
116
117 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 pub fn set_response_expected_all(&mut self, response_expected: bool) {
131 self.device.set_response_expected_all(response_expected)
132 }
133
134 pub fn get_api_version(&self) -> [u8; 3] {
137 self.device.api_version
138 }
139
140 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 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 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}