tinkerforge_async/bindings/
rgb_led_matrix_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 RgbLedMatrixBrickletFunction {
24 SetRed,
25 GetRed,
26 SetGreen,
27 GetGreen,
28 SetBlue,
29 GetBlue,
30 SetFrameDuration,
31 GetFrameDuration,
32 DrawFrame,
33 GetSupplyVoltage,
34 GetSpitfpErrorCount,
35 SetBootloaderMode,
36 GetBootloaderMode,
37 SetWriteFirmwarePointer,
38 WriteFirmware,
39 SetStatusLedConfig,
40 GetStatusLedConfig,
41 GetChipTemperature,
42 Reset,
43 WriteUid,
44 ReadUid,
45 GetIdentity,
46 CallbackFrameStarted,
47}
48impl From<RgbLedMatrixBrickletFunction> for u8 {
49 fn from(fun: RgbLedMatrixBrickletFunction) -> Self {
50 match fun {
51 RgbLedMatrixBrickletFunction::SetRed => 1,
52 RgbLedMatrixBrickletFunction::GetRed => 2,
53 RgbLedMatrixBrickletFunction::SetGreen => 3,
54 RgbLedMatrixBrickletFunction::GetGreen => 4,
55 RgbLedMatrixBrickletFunction::SetBlue => 5,
56 RgbLedMatrixBrickletFunction::GetBlue => 6,
57 RgbLedMatrixBrickletFunction::SetFrameDuration => 7,
58 RgbLedMatrixBrickletFunction::GetFrameDuration => 8,
59 RgbLedMatrixBrickletFunction::DrawFrame => 9,
60 RgbLedMatrixBrickletFunction::GetSupplyVoltage => 10,
61 RgbLedMatrixBrickletFunction::GetSpitfpErrorCount => 234,
62 RgbLedMatrixBrickletFunction::SetBootloaderMode => 235,
63 RgbLedMatrixBrickletFunction::GetBootloaderMode => 236,
64 RgbLedMatrixBrickletFunction::SetWriteFirmwarePointer => 237,
65 RgbLedMatrixBrickletFunction::WriteFirmware => 238,
66 RgbLedMatrixBrickletFunction::SetStatusLedConfig => 239,
67 RgbLedMatrixBrickletFunction::GetStatusLedConfig => 240,
68 RgbLedMatrixBrickletFunction::GetChipTemperature => 242,
69 RgbLedMatrixBrickletFunction::Reset => 243,
70 RgbLedMatrixBrickletFunction::WriteUid => 248,
71 RgbLedMatrixBrickletFunction::ReadUid => 249,
72 RgbLedMatrixBrickletFunction::GetIdentity => 255,
73 RgbLedMatrixBrickletFunction::CallbackFrameStarted => 11,
74 }
75 }
76}
77pub const RGB_LED_MATRIX_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
78pub const RGB_LED_MATRIX_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
79pub const RGB_LED_MATRIX_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
80pub const RGB_LED_MATRIX_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
81pub const RGB_LED_MATRIX_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
82pub const RGB_LED_MATRIX_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
83pub const RGB_LED_MATRIX_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
84pub const RGB_LED_MATRIX_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
85pub const RGB_LED_MATRIX_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
86pub const RGB_LED_MATRIX_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
87pub const RGB_LED_MATRIX_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
88pub const RGB_LED_MATRIX_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
89pub const RGB_LED_MATRIX_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
90pub const RGB_LED_MATRIX_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
91pub const RGB_LED_MATRIX_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
92
93#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
94pub struct SpitfpErrorCount {
95 pub error_count_ack_checksum: u32,
96 pub error_count_message_checksum: u32,
97 pub error_count_frame: u32,
98 pub error_count_overflow: u32,
99}
100impl FromByteSlice for SpitfpErrorCount {
101 fn bytes_expected() -> usize {
102 16
103 }
104 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
105 SpitfpErrorCount {
106 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
107 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
108 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
109 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
110 }
111 }
112}
113
114#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
115pub struct Identity {
116 pub uid: String,
117 pub connected_uid: String,
118 pub position: char,
119 pub hardware_version: [u8; 3],
120 pub firmware_version: [u8; 3],
121 pub device_identifier: u16,
122}
123impl FromByteSlice for Identity {
124 fn bytes_expected() -> usize {
125 25
126 }
127 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
128 Identity {
129 uid: <String>::from_le_byte_slice(&bytes[0..8]),
130 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
131 position: <char>::from_le_byte_slice(&bytes[16..17]),
132 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
133 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
134 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
135 }
136 }
137}
138
139#[derive(Clone)]
141pub struct RgbLedMatrixBricklet {
142 device: Device,
143}
144impl RgbLedMatrixBricklet {
145 pub const DEVICE_IDENTIFIER: u16 = 272;
146 pub const DEVICE_DISPLAY_NAME: &'static str = "RGB LED Matrix Bricklet";
147 pub fn new(uid: Uid, connection: AsyncIpConnection) -> RgbLedMatrixBricklet {
149 let mut result = RgbLedMatrixBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
150 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::SetRed) as usize] = ResponseExpectedFlag::False;
151 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::GetRed) as usize] = ResponseExpectedFlag::AlwaysTrue;
152 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::SetGreen) as usize] = ResponseExpectedFlag::False;
153 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::GetGreen) as usize] = ResponseExpectedFlag::AlwaysTrue;
154 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::SetBlue) as usize] = ResponseExpectedFlag::False;
155 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::GetBlue) as usize] = ResponseExpectedFlag::AlwaysTrue;
156 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::SetFrameDuration) as usize] = ResponseExpectedFlag::False;
157 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::GetFrameDuration) as usize] =
158 ResponseExpectedFlag::AlwaysTrue;
159 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::DrawFrame) as usize] = ResponseExpectedFlag::False;
160 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::GetSupplyVoltage) as usize] =
161 ResponseExpectedFlag::AlwaysTrue;
162 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::GetSpitfpErrorCount) as usize] =
163 ResponseExpectedFlag::AlwaysTrue;
164 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::SetBootloaderMode) as usize] =
165 ResponseExpectedFlag::AlwaysTrue;
166 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::GetBootloaderMode) as usize] =
167 ResponseExpectedFlag::AlwaysTrue;
168 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::SetWriteFirmwarePointer) as usize] =
169 ResponseExpectedFlag::False;
170 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
171 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
172 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::GetStatusLedConfig) as usize] =
173 ResponseExpectedFlag::AlwaysTrue;
174 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::GetChipTemperature) as usize] =
175 ResponseExpectedFlag::AlwaysTrue;
176 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
177 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
178 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
179 result.device.response_expected[u8::from(RgbLedMatrixBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
180 result
181 }
182
183 pub fn get_response_expected(&mut self, fun: RgbLedMatrixBrickletFunction) -> Result<bool, GetResponseExpectedError> {
198 self.device.get_response_expected(u8::from(fun))
199 }
200
201 pub fn set_response_expected(
210 &mut self,
211 fun: RgbLedMatrixBrickletFunction,
212 response_expected: bool,
213 ) -> Result<(), SetResponseExpectedError> {
214 self.device.set_response_expected(u8::from(fun), response_expected)
215 }
216
217 pub fn set_response_expected_all(&mut self, response_expected: bool) {
219 self.device.set_response_expected_all(response_expected)
220 }
221
222 pub fn get_api_version(&self) -> [u8; 3] {
225 self.device.api_version
226 }
227
228 pub async fn get_frame_started_callback_receiver(&mut self) -> impl Stream<Item = u32> {
232 self.device
233 .get_callback_receiver(u8::from(RgbLedMatrixBrickletFunction::CallbackFrameStarted))
234 .await
235 .map(|p| u32::from_le_byte_slice(p.body()))
236 }
237
238 pub async fn set_red(&mut self, red: &[u8; 64]) -> Result<(), TinkerforgeError> {
240 let mut payload = [0; 64];
241 red.write_to_slice(&mut payload[0..64]);
242
243 #[allow(unused_variables)]
244 let result = self.device.set(u8::from(RgbLedMatrixBrickletFunction::SetRed), &payload).await?;
245 Ok(())
246 }
247
248 pub async fn get_red(&mut self) -> Result<Box<[u8; 64]>, TinkerforgeError> {
250 let payload = [0; 0];
251
252 #[allow(unused_variables)]
253 let result = self.device.get(u8::from(RgbLedMatrixBrickletFunction::GetRed), &payload).await?;
254 Ok(Box::<[u8; 64]>::from_le_byte_slice(result.body()))
255 }
256
257 pub async fn set_green(&mut self, green: &[u8; 64]) -> Result<(), TinkerforgeError> {
259 let mut payload = [0; 64];
260 green.write_to_slice(&mut payload[0..64]);
261
262 #[allow(unused_variables)]
263 let result = self.device.set(u8::from(RgbLedMatrixBrickletFunction::SetGreen), &payload).await?;
264 Ok(())
265 }
266
267 pub async fn get_green(&mut self) -> Result<Box<[u8; 64]>, TinkerforgeError> {
269 let payload = [0; 0];
270
271 #[allow(unused_variables)]
272 let result = self.device.get(u8::from(RgbLedMatrixBrickletFunction::GetGreen), &payload).await?;
273 Ok(Box::<[u8; 64]>::from_le_byte_slice(result.body()))
274 }
275
276 pub async fn set_blue(&mut self, blue: &[u8; 64]) -> Result<(), TinkerforgeError> {
278 let mut payload = [0; 64];
279 blue.write_to_slice(&mut payload[0..64]);
280
281 #[allow(unused_variables)]
282 let result = self.device.set(u8::from(RgbLedMatrixBrickletFunction::SetBlue), &payload).await?;
283 Ok(())
284 }
285
286 pub async fn get_blue(&mut self) -> Result<Box<[u8; 64]>, TinkerforgeError> {
288 let payload = [0; 0];
289
290 #[allow(unused_variables)]
291 let result = self.device.get(u8::from(RgbLedMatrixBrickletFunction::GetBlue), &payload).await?;
292 Ok(Box::<[u8; 64]>::from_le_byte_slice(result.body()))
293 }
294
295 pub async fn set_frame_duration(&mut self, frame_duration: u16) -> Result<(), TinkerforgeError> {
313 let mut payload = [0; 2];
314 frame_duration.write_to_slice(&mut payload[0..2]);
315
316 #[allow(unused_variables)]
317 let result = self.device.set(u8::from(RgbLedMatrixBrickletFunction::SetFrameDuration), &payload).await?;
318 Ok(())
319 }
320
321 pub async fn get_frame_duration(&mut self) -> Result<u16, TinkerforgeError> {
323 let payload = [0; 0];
324
325 #[allow(unused_variables)]
326 let result = self.device.get(u8::from(RgbLedMatrixBrickletFunction::GetFrameDuration), &payload).await?;
327 Ok(u16::from_le_byte_slice(result.body()))
328 }
329
330 pub async fn draw_frame(&mut self) -> Result<(), TinkerforgeError> {
344 let payload = [0; 0];
345
346 #[allow(unused_variables)]
347 let result = self.device.set(u8::from(RgbLedMatrixBrickletFunction::DrawFrame), &payload).await?;
348 Ok(())
349 }
350
351 pub async fn get_supply_voltage(&mut self) -> Result<u16, TinkerforgeError> {
353 let payload = [0; 0];
354
355 #[allow(unused_variables)]
356 let result = self.device.get(u8::from(RgbLedMatrixBrickletFunction::GetSupplyVoltage), &payload).await?;
357 Ok(u16::from_le_byte_slice(result.body()))
358 }
359
360 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
372 let payload = [0; 0];
373
374 #[allow(unused_variables)]
375 let result = self.device.get(u8::from(RgbLedMatrixBrickletFunction::GetSpitfpErrorCount), &payload).await?;
376 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
377 }
378
379 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
402 let mut payload = [0; 1];
403 mode.write_to_slice(&mut payload[0..1]);
404
405 #[allow(unused_variables)]
406 let result = self.device.get(u8::from(RgbLedMatrixBrickletFunction::SetBootloaderMode), &payload).await?;
407 Ok(u8::from_le_byte_slice(result.body()))
408 }
409
410 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
419 let payload = [0; 0];
420
421 #[allow(unused_variables)]
422 let result = self.device.get(u8::from(RgbLedMatrixBrickletFunction::GetBootloaderMode), &payload).await?;
423 Ok(u8::from_le_byte_slice(result.body()))
424 }
425
426 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
433 let mut payload = [0; 4];
434 pointer.write_to_slice(&mut payload[0..4]);
435
436 #[allow(unused_variables)]
437 let result = self.device.set(u8::from(RgbLedMatrixBrickletFunction::SetWriteFirmwarePointer), &payload).await?;
438 Ok(())
439 }
440
441 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
450 let mut payload = [0; 64];
451 data.write_to_slice(&mut payload[0..64]);
452
453 #[allow(unused_variables)]
454 let result = self.device.get(u8::from(RgbLedMatrixBrickletFunction::WriteFirmware), &payload).await?;
455 Ok(u8::from_le_byte_slice(result.body()))
456 }
457
458 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
472 let mut payload = [0; 1];
473 config.write_to_slice(&mut payload[0..1]);
474
475 #[allow(unused_variables)]
476 let result = self.device.set(u8::from(RgbLedMatrixBrickletFunction::SetStatusLedConfig), &payload).await?;
477 Ok(())
478 }
479
480 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
488 let payload = [0; 0];
489
490 #[allow(unused_variables)]
491 let result = self.device.get(u8::from(RgbLedMatrixBrickletFunction::GetStatusLedConfig), &payload).await?;
492 Ok(u8::from_le_byte_slice(result.body()))
493 }
494
495 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
502 let payload = [0; 0];
503
504 #[allow(unused_variables)]
505 let result = self.device.get(u8::from(RgbLedMatrixBrickletFunction::GetChipTemperature), &payload).await?;
506 Ok(i16::from_le_byte_slice(result.body()))
507 }
508
509 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
516 let payload = [0; 0];
517
518 #[allow(unused_variables)]
519 let result = self.device.set(u8::from(RgbLedMatrixBrickletFunction::Reset), &payload).await?;
520 Ok(())
521 }
522
523 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
529 let mut payload = [0; 4];
530 uid.write_to_slice(&mut payload[0..4]);
531
532 #[allow(unused_variables)]
533 let result = self.device.set(u8::from(RgbLedMatrixBrickletFunction::WriteUid), &payload).await?;
534 Ok(())
535 }
536
537 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
540 let payload = [0; 0];
541
542 #[allow(unused_variables)]
543 let result = self.device.get(u8::from(RgbLedMatrixBrickletFunction::ReadUid), &payload).await?;
544 Ok(u32::from_le_byte_slice(result.body()))
545 }
546
547 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
558 let payload = [0; 0];
559
560 #[allow(unused_variables)]
561 let result = self.device.get(u8::from(RgbLedMatrixBrickletFunction::GetIdentity), &payload).await?;
562 Ok(Identity::from_le_byte_slice(result.body()))
563 }
564}