tinkerforge_async/bindings/
rgb_led_v2_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 RgbLedV2BrickletFunction {
24 SetRgbValue,
25 GetRgbValue,
26 GetSpitfpErrorCount,
27 SetBootloaderMode,
28 GetBootloaderMode,
29 SetWriteFirmwarePointer,
30 WriteFirmware,
31 SetStatusLedConfig,
32 GetStatusLedConfig,
33 GetChipTemperature,
34 Reset,
35 WriteUid,
36 ReadUid,
37 GetIdentity,
38}
39impl From<RgbLedV2BrickletFunction> for u8 {
40 fn from(fun: RgbLedV2BrickletFunction) -> Self {
41 match fun {
42 RgbLedV2BrickletFunction::SetRgbValue => 1,
43 RgbLedV2BrickletFunction::GetRgbValue => 2,
44 RgbLedV2BrickletFunction::GetSpitfpErrorCount => 234,
45 RgbLedV2BrickletFunction::SetBootloaderMode => 235,
46 RgbLedV2BrickletFunction::GetBootloaderMode => 236,
47 RgbLedV2BrickletFunction::SetWriteFirmwarePointer => 237,
48 RgbLedV2BrickletFunction::WriteFirmware => 238,
49 RgbLedV2BrickletFunction::SetStatusLedConfig => 239,
50 RgbLedV2BrickletFunction::GetStatusLedConfig => 240,
51 RgbLedV2BrickletFunction::GetChipTemperature => 242,
52 RgbLedV2BrickletFunction::Reset => 243,
53 RgbLedV2BrickletFunction::WriteUid => 248,
54 RgbLedV2BrickletFunction::ReadUid => 249,
55 RgbLedV2BrickletFunction::GetIdentity => 255,
56 }
57 }
58}
59pub const RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
60pub const RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
61pub const RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
62pub const RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
63pub const RGB_LED_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
64pub const RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
65pub const RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
66pub const RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
67pub const RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
68pub const RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
69pub const RGB_LED_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
70pub const RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
71pub const RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
72pub const RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
73pub const RGB_LED_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
74
75#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
76pub struct RgbValue {
77 pub r: u8,
78 pub g: u8,
79 pub b: u8,
80}
81impl FromByteSlice for RgbValue {
82 fn bytes_expected() -> usize {
83 3
84 }
85 fn from_le_byte_slice(bytes: &[u8]) -> RgbValue {
86 RgbValue {
87 r: <u8>::from_le_byte_slice(&bytes[0..1]),
88 g: <u8>::from_le_byte_slice(&bytes[1..2]),
89 b: <u8>::from_le_byte_slice(&bytes[2..3]),
90 }
91 }
92}
93
94#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
95pub struct SpitfpErrorCount {
96 pub error_count_ack_checksum: u32,
97 pub error_count_message_checksum: u32,
98 pub error_count_frame: u32,
99 pub error_count_overflow: u32,
100}
101impl FromByteSlice for SpitfpErrorCount {
102 fn bytes_expected() -> usize {
103 16
104 }
105 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
106 SpitfpErrorCount {
107 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
108 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
109 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
110 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
111 }
112 }
113}
114
115#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
116pub struct Identity {
117 pub uid: String,
118 pub connected_uid: String,
119 pub position: char,
120 pub hardware_version: [u8; 3],
121 pub firmware_version: [u8; 3],
122 pub device_identifier: u16,
123}
124impl FromByteSlice for Identity {
125 fn bytes_expected() -> usize {
126 25
127 }
128 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
129 Identity {
130 uid: <String>::from_le_byte_slice(&bytes[0..8]),
131 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
132 position: <char>::from_le_byte_slice(&bytes[16..17]),
133 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
134 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
135 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
136 }
137 }
138}
139
140#[derive(Clone)]
142pub struct RgbLedV2Bricklet {
143 device: Device,
144}
145impl RgbLedV2Bricklet {
146 pub const DEVICE_IDENTIFIER: u16 = 2127;
147 pub const DEVICE_DISPLAY_NAME: &'static str = "RGB LED Bricklet 2.0";
148 pub fn new(uid: Uid, connection: AsyncIpConnection) -> RgbLedV2Bricklet {
150 let mut result = RgbLedV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
151 result.device.response_expected[u8::from(RgbLedV2BrickletFunction::SetRgbValue) as usize] = ResponseExpectedFlag::False;
152 result.device.response_expected[u8::from(RgbLedV2BrickletFunction::GetRgbValue) as usize] = ResponseExpectedFlag::AlwaysTrue;
153 result.device.response_expected[u8::from(RgbLedV2BrickletFunction::GetSpitfpErrorCount) as usize] =
154 ResponseExpectedFlag::AlwaysTrue;
155 result.device.response_expected[u8::from(RgbLedV2BrickletFunction::SetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
156 result.device.response_expected[u8::from(RgbLedV2BrickletFunction::GetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
157 result.device.response_expected[u8::from(RgbLedV2BrickletFunction::SetWriteFirmwarePointer) as usize] = ResponseExpectedFlag::False;
158 result.device.response_expected[u8::from(RgbLedV2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
159 result.device.response_expected[u8::from(RgbLedV2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
160 result.device.response_expected[u8::from(RgbLedV2BrickletFunction::GetStatusLedConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
161 result.device.response_expected[u8::from(RgbLedV2BrickletFunction::GetChipTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
162 result.device.response_expected[u8::from(RgbLedV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
163 result.device.response_expected[u8::from(RgbLedV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
164 result.device.response_expected[u8::from(RgbLedV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
165 result.device.response_expected[u8::from(RgbLedV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
166 result
167 }
168
169 pub fn get_response_expected(&mut self, fun: RgbLedV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
184 self.device.get_response_expected(u8::from(fun))
185 }
186
187 pub fn set_response_expected(
196 &mut self,
197 fun: RgbLedV2BrickletFunction,
198 response_expected: bool,
199 ) -> Result<(), SetResponseExpectedError> {
200 self.device.set_response_expected(u8::from(fun), response_expected)
201 }
202
203 pub fn set_response_expected_all(&mut self, response_expected: bool) {
205 self.device.set_response_expected_all(response_expected)
206 }
207
208 pub fn get_api_version(&self) -> [u8; 3] {
211 self.device.api_version
212 }
213
214 pub async fn set_rgb_value(&mut self, r: u8, g: u8, b: u8) -> Result<(), TinkerforgeError> {
216 let mut payload = [0; 3];
217 r.write_to_slice(&mut payload[0..1]);
218 g.write_to_slice(&mut payload[1..2]);
219 b.write_to_slice(&mut payload[2..3]);
220
221 #[allow(unused_variables)]
222 let result = self.device.set(u8::from(RgbLedV2BrickletFunction::SetRgbValue), &payload).await?;
223 Ok(())
224 }
225
226 pub async fn get_rgb_value(&mut self) -> Result<RgbValue, TinkerforgeError> {
228 let payload = [0; 0];
229
230 #[allow(unused_variables)]
231 let result = self.device.get(u8::from(RgbLedV2BrickletFunction::GetRgbValue), &payload).await?;
232 Ok(RgbValue::from_le_byte_slice(result.body()))
233 }
234
235 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
247 let payload = [0; 0];
248
249 #[allow(unused_variables)]
250 let result = self.device.get(u8::from(RgbLedV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
251 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
252 }
253
254 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
277 let mut payload = [0; 1];
278 mode.write_to_slice(&mut payload[0..1]);
279
280 #[allow(unused_variables)]
281 let result = self.device.get(u8::from(RgbLedV2BrickletFunction::SetBootloaderMode), &payload).await?;
282 Ok(u8::from_le_byte_slice(result.body()))
283 }
284
285 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
294 let payload = [0; 0];
295
296 #[allow(unused_variables)]
297 let result = self.device.get(u8::from(RgbLedV2BrickletFunction::GetBootloaderMode), &payload).await?;
298 Ok(u8::from_le_byte_slice(result.body()))
299 }
300
301 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
308 let mut payload = [0; 4];
309 pointer.write_to_slice(&mut payload[0..4]);
310
311 #[allow(unused_variables)]
312 let result = self.device.set(u8::from(RgbLedV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
313 Ok(())
314 }
315
316 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
325 let mut payload = [0; 64];
326 data.write_to_slice(&mut payload[0..64]);
327
328 #[allow(unused_variables)]
329 let result = self.device.get(u8::from(RgbLedV2BrickletFunction::WriteFirmware), &payload).await?;
330 Ok(u8::from_le_byte_slice(result.body()))
331 }
332
333 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
347 let mut payload = [0; 1];
348 config.write_to_slice(&mut payload[0..1]);
349
350 #[allow(unused_variables)]
351 let result = self.device.set(u8::from(RgbLedV2BrickletFunction::SetStatusLedConfig), &payload).await?;
352 Ok(())
353 }
354
355 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
363 let payload = [0; 0];
364
365 #[allow(unused_variables)]
366 let result = self.device.get(u8::from(RgbLedV2BrickletFunction::GetStatusLedConfig), &payload).await?;
367 Ok(u8::from_le_byte_slice(result.body()))
368 }
369
370 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
377 let payload = [0; 0];
378
379 #[allow(unused_variables)]
380 let result = self.device.get(u8::from(RgbLedV2BrickletFunction::GetChipTemperature), &payload).await?;
381 Ok(i16::from_le_byte_slice(result.body()))
382 }
383
384 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
391 let payload = [0; 0];
392
393 #[allow(unused_variables)]
394 let result = self.device.set(u8::from(RgbLedV2BrickletFunction::Reset), &payload).await?;
395 Ok(())
396 }
397
398 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
404 let mut payload = [0; 4];
405 uid.write_to_slice(&mut payload[0..4]);
406
407 #[allow(unused_variables)]
408 let result = self.device.set(u8::from(RgbLedV2BrickletFunction::WriteUid), &payload).await?;
409 Ok(())
410 }
411
412 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
415 let payload = [0; 0];
416
417 #[allow(unused_variables)]
418 let result = self.device.get(u8::from(RgbLedV2BrickletFunction::ReadUid), &payload).await?;
419 Ok(u32::from_le_byte_slice(result.body()))
420 }
421
422 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
433 let payload = [0; 0];
434
435 #[allow(unused_variables)]
436 let result = self.device.get(u8::from(RgbLedV2BrickletFunction::GetIdentity), &payload).await?;
437 Ok(Identity::from_le_byte_slice(result.body()))
438 }
439}