1#[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 RgbLedButtonBrickletFunction {
24 SetColor,
25 GetColor,
26 GetButtonState,
27 SetColorCalibration,
28 GetColorCalibration,
29 GetSpitfpErrorCount,
30 SetBootloaderMode,
31 GetBootloaderMode,
32 SetWriteFirmwarePointer,
33 WriteFirmware,
34 SetStatusLedConfig,
35 GetStatusLedConfig,
36 GetChipTemperature,
37 Reset,
38 WriteUid,
39 ReadUid,
40 GetIdentity,
41 CallbackButtonStateChanged,
42}
43impl From<RgbLedButtonBrickletFunction> for u8 {
44 fn from(fun: RgbLedButtonBrickletFunction) -> Self {
45 match fun {
46 RgbLedButtonBrickletFunction::SetColor => 1,
47 RgbLedButtonBrickletFunction::GetColor => 2,
48 RgbLedButtonBrickletFunction::GetButtonState => 3,
49 RgbLedButtonBrickletFunction::SetColorCalibration => 5,
50 RgbLedButtonBrickletFunction::GetColorCalibration => 6,
51 RgbLedButtonBrickletFunction::GetSpitfpErrorCount => 234,
52 RgbLedButtonBrickletFunction::SetBootloaderMode => 235,
53 RgbLedButtonBrickletFunction::GetBootloaderMode => 236,
54 RgbLedButtonBrickletFunction::SetWriteFirmwarePointer => 237,
55 RgbLedButtonBrickletFunction::WriteFirmware => 238,
56 RgbLedButtonBrickletFunction::SetStatusLedConfig => 239,
57 RgbLedButtonBrickletFunction::GetStatusLedConfig => 240,
58 RgbLedButtonBrickletFunction::GetChipTemperature => 242,
59 RgbLedButtonBrickletFunction::Reset => 243,
60 RgbLedButtonBrickletFunction::WriteUid => 248,
61 RgbLedButtonBrickletFunction::ReadUid => 249,
62 RgbLedButtonBrickletFunction::GetIdentity => 255,
63 RgbLedButtonBrickletFunction::CallbackButtonStateChanged => 4,
64 }
65 }
66}
67pub const RGB_LED_BUTTON_BRICKLET_BUTTON_STATE_PRESSED: u8 = 0;
68pub const RGB_LED_BUTTON_BRICKLET_BUTTON_STATE_RELEASED: u8 = 1;
69pub const RGB_LED_BUTTON_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
70pub const RGB_LED_BUTTON_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
71pub const RGB_LED_BUTTON_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
72pub const RGB_LED_BUTTON_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
73pub const RGB_LED_BUTTON_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
74pub const RGB_LED_BUTTON_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
75pub const RGB_LED_BUTTON_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
76pub const RGB_LED_BUTTON_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
77pub const RGB_LED_BUTTON_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
78pub const RGB_LED_BUTTON_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
79pub const RGB_LED_BUTTON_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
80pub const RGB_LED_BUTTON_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
81pub const RGB_LED_BUTTON_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
82pub const RGB_LED_BUTTON_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
83pub const RGB_LED_BUTTON_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
84
85#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
86pub struct Color {
87 pub red: u8,
88 pub green: u8,
89 pub blue: u8,
90}
91impl FromByteSlice for Color {
92 fn bytes_expected() -> usize {
93 3
94 }
95 fn from_le_byte_slice(bytes: &[u8]) -> Color {
96 Color {
97 red: <u8>::from_le_byte_slice(&bytes[0..1]),
98 green: <u8>::from_le_byte_slice(&bytes[1..2]),
99 blue: <u8>::from_le_byte_slice(&bytes[2..3]),
100 }
101 }
102}
103
104#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
105pub struct ColorCalibration {
106 pub red: u8,
107 pub green: u8,
108 pub blue: u8,
109}
110impl FromByteSlice for ColorCalibration {
111 fn bytes_expected() -> usize {
112 3
113 }
114 fn from_le_byte_slice(bytes: &[u8]) -> ColorCalibration {
115 ColorCalibration {
116 red: <u8>::from_le_byte_slice(&bytes[0..1]),
117 green: <u8>::from_le_byte_slice(&bytes[1..2]),
118 blue: <u8>::from_le_byte_slice(&bytes[2..3]),
119 }
120 }
121}
122
123#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
124pub struct SpitfpErrorCount {
125 pub error_count_ack_checksum: u32,
126 pub error_count_message_checksum: u32,
127 pub error_count_frame: u32,
128 pub error_count_overflow: u32,
129}
130impl FromByteSlice for SpitfpErrorCount {
131 fn bytes_expected() -> usize {
132 16
133 }
134 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
135 SpitfpErrorCount {
136 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
137 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
138 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
139 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
140 }
141 }
142}
143
144#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
145pub struct Identity {
146 pub uid: String,
147 pub connected_uid: String,
148 pub position: char,
149 pub hardware_version: [u8; 3],
150 pub firmware_version: [u8; 3],
151 pub device_identifier: u16,
152}
153impl FromByteSlice for Identity {
154 fn bytes_expected() -> usize {
155 25
156 }
157 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
158 Identity {
159 uid: <String>::from_le_byte_slice(&bytes[0..8]),
160 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
161 position: <char>::from_le_byte_slice(&bytes[16..17]),
162 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
163 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
164 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
165 }
166 }
167}
168
169#[derive(Clone)]
171pub struct RgbLedButtonBricklet {
172 device: Device,
173}
174impl RgbLedButtonBricklet {
175 pub const DEVICE_IDENTIFIER: u16 = 282;
176 pub const DEVICE_DISPLAY_NAME: &'static str = "RGB LED Button Bricklet";
177 pub fn new(uid: Uid, connection: AsyncIpConnection) -> RgbLedButtonBricklet {
179 let mut result = RgbLedButtonBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
180 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::SetColor) as usize] = ResponseExpectedFlag::False;
181 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::GetColor) as usize] = ResponseExpectedFlag::AlwaysTrue;
182 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::GetButtonState) as usize] = ResponseExpectedFlag::AlwaysTrue;
183 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::SetColorCalibration) as usize] = ResponseExpectedFlag::False;
184 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::GetColorCalibration) as usize] =
185 ResponseExpectedFlag::AlwaysTrue;
186 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::GetSpitfpErrorCount) as usize] =
187 ResponseExpectedFlag::AlwaysTrue;
188 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::SetBootloaderMode) as usize] =
189 ResponseExpectedFlag::AlwaysTrue;
190 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::GetBootloaderMode) as usize] =
191 ResponseExpectedFlag::AlwaysTrue;
192 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::SetWriteFirmwarePointer) as usize] =
193 ResponseExpectedFlag::False;
194 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
195 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
196 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::GetStatusLedConfig) as usize] =
197 ResponseExpectedFlag::AlwaysTrue;
198 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::GetChipTemperature) as usize] =
199 ResponseExpectedFlag::AlwaysTrue;
200 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
201 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
202 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
203 result.device.response_expected[u8::from(RgbLedButtonBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
204 result
205 }
206
207 pub fn get_response_expected(&mut self, fun: RgbLedButtonBrickletFunction) -> Result<bool, GetResponseExpectedError> {
222 self.device.get_response_expected(u8::from(fun))
223 }
224
225 pub fn set_response_expected(
234 &mut self,
235 fun: RgbLedButtonBrickletFunction,
236 response_expected: bool,
237 ) -> Result<(), SetResponseExpectedError> {
238 self.device.set_response_expected(u8::from(fun), response_expected)
239 }
240
241 pub fn set_response_expected_all(&mut self, response_expected: bool) {
243 self.device.set_response_expected_all(response_expected)
244 }
245
246 pub fn get_api_version(&self) -> [u8; 3] {
249 self.device.api_version
250 }
251
252 pub async fn get_button_state_changed_callback_receiver(&mut self) -> impl Stream<Item = u8> {
257 self.device
258 .get_callback_receiver(u8::from(RgbLedButtonBrickletFunction::CallbackButtonStateChanged))
259 .await
260 .map(|p| u8::from_le_byte_slice(p.body()))
261 }
262
263 pub async fn set_color(&mut self, red: u8, green: u8, blue: u8) -> Result<(), TinkerforgeError> {
265 let mut payload = [0; 3];
266 red.write_to_slice(&mut payload[0..1]);
267 green.write_to_slice(&mut payload[1..2]);
268 blue.write_to_slice(&mut payload[2..3]);
269
270 #[allow(unused_variables)]
271 let result = self.device.set(u8::from(RgbLedButtonBrickletFunction::SetColor), &payload).await?;
272 Ok(())
273 }
274
275 pub async fn get_color(&mut self) -> Result<Color, TinkerforgeError> {
277 let payload = [0; 0];
278
279 #[allow(unused_variables)]
280 let result = self.device.get(u8::from(RgbLedButtonBrickletFunction::GetColor), &payload).await?;
281 Ok(Color::from_le_byte_slice(result.body()))
282 }
283
284 pub async fn get_button_state(&mut self) -> Result<u8, TinkerforgeError> {
290 let payload = [0; 0];
291
292 #[allow(unused_variables)]
293 let result = self.device.get(u8::from(RgbLedButtonBrickletFunction::GetButtonState), &payload).await?;
294 Ok(u8::from_le_byte_slice(result.body()))
295 }
296
297 pub async fn set_color_calibration(&mut self, red: u8, green: u8, blue: u8) -> Result<(), TinkerforgeError> {
303 let mut payload = [0; 3];
304 red.write_to_slice(&mut payload[0..1]);
305 green.write_to_slice(&mut payload[1..2]);
306 blue.write_to_slice(&mut payload[2..3]);
307
308 #[allow(unused_variables)]
309 let result = self.device.set(u8::from(RgbLedButtonBrickletFunction::SetColorCalibration), &payload).await?;
310 Ok(())
311 }
312
313 pub async fn get_color_calibration(&mut self) -> Result<ColorCalibration, TinkerforgeError> {
315 let payload = [0; 0];
316
317 #[allow(unused_variables)]
318 let result = self.device.get(u8::from(RgbLedButtonBrickletFunction::GetColorCalibration), &payload).await?;
319 Ok(ColorCalibration::from_le_byte_slice(result.body()))
320 }
321
322 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
334 let payload = [0; 0];
335
336 #[allow(unused_variables)]
337 let result = self.device.get(u8::from(RgbLedButtonBrickletFunction::GetSpitfpErrorCount), &payload).await?;
338 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
339 }
340
341 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
364 let mut payload = [0; 1];
365 mode.write_to_slice(&mut payload[0..1]);
366
367 #[allow(unused_variables)]
368 let result = self.device.get(u8::from(RgbLedButtonBrickletFunction::SetBootloaderMode), &payload).await?;
369 Ok(u8::from_le_byte_slice(result.body()))
370 }
371
372 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
381 let payload = [0; 0];
382
383 #[allow(unused_variables)]
384 let result = self.device.get(u8::from(RgbLedButtonBrickletFunction::GetBootloaderMode), &payload).await?;
385 Ok(u8::from_le_byte_slice(result.body()))
386 }
387
388 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
395 let mut payload = [0; 4];
396 pointer.write_to_slice(&mut payload[0..4]);
397
398 #[allow(unused_variables)]
399 let result = self.device.set(u8::from(RgbLedButtonBrickletFunction::SetWriteFirmwarePointer), &payload).await?;
400 Ok(())
401 }
402
403 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
412 let mut payload = [0; 64];
413 data.write_to_slice(&mut payload[0..64]);
414
415 #[allow(unused_variables)]
416 let result = self.device.get(u8::from(RgbLedButtonBrickletFunction::WriteFirmware), &payload).await?;
417 Ok(u8::from_le_byte_slice(result.body()))
418 }
419
420 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
434 let mut payload = [0; 1];
435 config.write_to_slice(&mut payload[0..1]);
436
437 #[allow(unused_variables)]
438 let result = self.device.set(u8::from(RgbLedButtonBrickletFunction::SetStatusLedConfig), &payload).await?;
439 Ok(())
440 }
441
442 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
450 let payload = [0; 0];
451
452 #[allow(unused_variables)]
453 let result = self.device.get(u8::from(RgbLedButtonBrickletFunction::GetStatusLedConfig), &payload).await?;
454 Ok(u8::from_le_byte_slice(result.body()))
455 }
456
457 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
464 let payload = [0; 0];
465
466 #[allow(unused_variables)]
467 let result = self.device.get(u8::from(RgbLedButtonBrickletFunction::GetChipTemperature), &payload).await?;
468 Ok(i16::from_le_byte_slice(result.body()))
469 }
470
471 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
478 let payload = [0; 0];
479
480 #[allow(unused_variables)]
481 let result = self.device.set(u8::from(RgbLedButtonBrickletFunction::Reset), &payload).await?;
482 Ok(())
483 }
484
485 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
491 let mut payload = [0; 4];
492 uid.write_to_slice(&mut payload[0..4]);
493
494 #[allow(unused_variables)]
495 let result = self.device.set(u8::from(RgbLedButtonBrickletFunction::WriteUid), &payload).await?;
496 Ok(())
497 }
498
499 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
502 let payload = [0; 0];
503
504 #[allow(unused_variables)]
505 let result = self.device.get(u8::from(RgbLedButtonBrickletFunction::ReadUid), &payload).await?;
506 Ok(u32::from_le_byte_slice(result.body()))
507 }
508
509 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
520 let payload = [0; 0];
521
522 #[allow(unused_variables)]
523 let result = self.device.get(u8::from(RgbLedButtonBrickletFunction::GetIdentity), &payload).await?;
524 Ok(Identity::from_le_byte_slice(result.body()))
525 }
526}