1#[allow(unused_imports)]
15use crate::{
16 base58::Uid,
17 byte_converter::*,
18 converting_receiver::{BrickletError, BrickletRecvTimeoutError},
19 device::*,
20 error::TinkerforgeError,
21 ip_connection::async_io::AsyncIpConnection,
22 low_level_traits::LowLevelRead,
23};
24#[allow(unused_imports)]
25use futures_core::Stream;
26#[allow(unused_imports)]
27use tokio_stream::StreamExt;
28pub enum Oled128x64V2BrickletFunction {
29 WritePixelsLowLevel,
30 ReadPixelsLowLevel,
31 ClearDisplay,
32 SetDisplayConfiguration,
33 GetDisplayConfiguration,
34 WriteLine,
35 DrawBufferedFrame,
36 GetSpitfpErrorCount,
37 SetBootloaderMode,
38 GetBootloaderMode,
39 SetWriteFirmwarePointer,
40 WriteFirmware,
41 SetStatusLedConfig,
42 GetStatusLedConfig,
43 GetChipTemperature,
44 Reset,
45 WriteUid,
46 ReadUid,
47 GetIdentity,
48}
49impl From<Oled128x64V2BrickletFunction> for u8 {
50 fn from(fun: Oled128x64V2BrickletFunction) -> Self {
51 match fun {
52 Oled128x64V2BrickletFunction::WritePixelsLowLevel => 1,
53 Oled128x64V2BrickletFunction::ReadPixelsLowLevel => 2,
54 Oled128x64V2BrickletFunction::ClearDisplay => 3,
55 Oled128x64V2BrickletFunction::SetDisplayConfiguration => 4,
56 Oled128x64V2BrickletFunction::GetDisplayConfiguration => 5,
57 Oled128x64V2BrickletFunction::WriteLine => 6,
58 Oled128x64V2BrickletFunction::DrawBufferedFrame => 7,
59 Oled128x64V2BrickletFunction::GetSpitfpErrorCount => 234,
60 Oled128x64V2BrickletFunction::SetBootloaderMode => 235,
61 Oled128x64V2BrickletFunction::GetBootloaderMode => 236,
62 Oled128x64V2BrickletFunction::SetWriteFirmwarePointer => 237,
63 Oled128x64V2BrickletFunction::WriteFirmware => 238,
64 Oled128x64V2BrickletFunction::SetStatusLedConfig => 239,
65 Oled128x64V2BrickletFunction::GetStatusLedConfig => 240,
66 Oled128x64V2BrickletFunction::GetChipTemperature => 242,
67 Oled128x64V2BrickletFunction::Reset => 243,
68 Oled128x64V2BrickletFunction::WriteUid => 248,
69 Oled128x64V2BrickletFunction::ReadUid => 249,
70 Oled128x64V2BrickletFunction::GetIdentity => 255,
71 }
72 }
73}
74pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
75pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
76pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
77pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
78pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
79pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
80pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
81pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
82pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
83pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
84pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
85pub const OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
86pub const OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
87pub const OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
88pub const OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
89
90#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
91pub struct WritePixelsLowLevel {}
92impl FromByteSlice for WritePixelsLowLevel {
93 fn bytes_expected() -> usize {
94 0
95 }
96 fn from_le_byte_slice(_bytes: &[u8]) -> WritePixelsLowLevel {
97 WritePixelsLowLevel {}
98 }
99}
100
101#[derive(Clone, Copy)]
102pub struct ReadPixelsLowLevel {
103 pub pixels_length: u16,
104 pub pixels_chunk_offset: u16,
105 pub pixels_chunk_data: [bool; 480],
106}
107impl FromByteSlice for ReadPixelsLowLevel {
108 fn bytes_expected() -> usize {
109 64
110 }
111 fn from_le_byte_slice(bytes: &[u8]) -> ReadPixelsLowLevel {
112 ReadPixelsLowLevel {
113 pixels_length: <u16>::from_le_byte_slice(&bytes[0..2]),
114 pixels_chunk_offset: <u16>::from_le_byte_slice(&bytes[2..4]),
115 pixels_chunk_data: <[bool; 480]>::from_le_byte_slice(&bytes[4..64]),
116 }
117 }
118}
119impl LowLevelRead<bool, ReadPixelsResult> for ReadPixelsLowLevel {
120 fn ll_message_length(&self) -> usize {
121 self.pixels_length as usize
122 }
123
124 fn ll_message_chunk_offset(&self) -> usize {
125 self.pixels_chunk_offset as usize
126 }
127
128 fn ll_message_chunk_data(&self) -> &[bool] {
129 &self.pixels_chunk_data
130 }
131
132 fn get_result(&self) -> ReadPixelsResult {
133 ReadPixelsResult {}
134 }
135}
136
137#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
138pub struct DisplayConfiguration {
139 pub contrast: u8,
140 pub invert: bool,
141 pub automatic_draw: bool,
142}
143impl FromByteSlice for DisplayConfiguration {
144 fn bytes_expected() -> usize {
145 3
146 }
147 fn from_le_byte_slice(bytes: &[u8]) -> DisplayConfiguration {
148 DisplayConfiguration {
149 contrast: <u8>::from_le_byte_slice(&bytes[0..1]),
150 invert: <bool>::from_le_byte_slice(&bytes[1..2]),
151 automatic_draw: <bool>::from_le_byte_slice(&bytes[2..3]),
152 }
153 }
154}
155
156#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
157pub struct SpitfpErrorCount {
158 pub error_count_ack_checksum: u32,
159 pub error_count_message_checksum: u32,
160 pub error_count_frame: u32,
161 pub error_count_overflow: u32,
162}
163impl FromByteSlice for SpitfpErrorCount {
164 fn bytes_expected() -> usize {
165 16
166 }
167 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
168 SpitfpErrorCount {
169 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
170 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
171 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
172 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
173 }
174 }
175}
176
177#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
178pub struct Identity {
179 pub uid: String,
180 pub connected_uid: String,
181 pub position: char,
182 pub hardware_version: [u8; 3],
183 pub firmware_version: [u8; 3],
184 pub device_identifier: u16,
185}
186impl FromByteSlice for Identity {
187 fn bytes_expected() -> usize {
188 25
189 }
190 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
191 Identity {
192 uid: <String>::from_le_byte_slice(&bytes[0..8]),
193 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
194 position: <char>::from_le_byte_slice(&bytes[16..17]),
195 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
196 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
197 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
198 }
199 }
200}
201
202#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
203pub struct WritePixelsResult {}
204
205#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
206pub struct ReadPixelsResult {}
207
208#[derive(Clone)]
210pub struct Oled128x64V2Bricklet {
211 device: Device,
212}
213impl Oled128x64V2Bricklet {
214 pub const DEVICE_IDENTIFIER: u16 = 2112;
215 pub const DEVICE_DISPLAY_NAME: &'static str = "OLED 128x64 Bricklet 2.0";
216 pub fn new(uid: Uid, connection: AsyncIpConnection) -> Oled128x64V2Bricklet {
218 let mut result = Oled128x64V2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
219 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::WritePixelsLowLevel) as usize] = ResponseExpectedFlag::True;
220 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::ReadPixelsLowLevel) as usize] =
221 ResponseExpectedFlag::AlwaysTrue;
222 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::ClearDisplay) as usize] = ResponseExpectedFlag::False;
223 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::SetDisplayConfiguration) as usize] =
224 ResponseExpectedFlag::False;
225 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::GetDisplayConfiguration) as usize] =
226 ResponseExpectedFlag::AlwaysTrue;
227 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::WriteLine) as usize] = ResponseExpectedFlag::False;
228 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::DrawBufferedFrame) as usize] = ResponseExpectedFlag::False;
229 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::GetSpitfpErrorCount) as usize] =
230 ResponseExpectedFlag::AlwaysTrue;
231 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::SetBootloaderMode) as usize] =
232 ResponseExpectedFlag::AlwaysTrue;
233 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::GetBootloaderMode) as usize] =
234 ResponseExpectedFlag::AlwaysTrue;
235 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::SetWriteFirmwarePointer) as usize] =
236 ResponseExpectedFlag::False;
237 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
238 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
239 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::GetStatusLedConfig) as usize] =
240 ResponseExpectedFlag::AlwaysTrue;
241 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::GetChipTemperature) as usize] =
242 ResponseExpectedFlag::AlwaysTrue;
243 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
244 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
245 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
246 result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
247 result
248 }
249
250 pub fn get_response_expected(&mut self, fun: Oled128x64V2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
265 self.device.get_response_expected(u8::from(fun))
266 }
267
268 pub fn set_response_expected(
277 &mut self,
278 fun: Oled128x64V2BrickletFunction,
279 response_expected: bool,
280 ) -> Result<(), SetResponseExpectedError> {
281 self.device.set_response_expected(u8::from(fun), response_expected)
282 }
283
284 pub fn set_response_expected_all(&mut self, response_expected: bool) {
286 self.device.set_response_expected_all(response_expected)
287 }
288
289 pub fn get_api_version(&self) -> [u8; 3] {
292 self.device.api_version
293 }
294
295 pub async fn write_pixels_low_level(
315 &mut self,
316 x_start: u8,
317 y_start: u8,
318 x_end: u8,
319 y_end: u8,
320 pixels_length: u16,
321 pixels_chunk_offset: u16,
322 pixels_chunk_data: &[bool; 448],
323 ) -> Result<WritePixelsLowLevel, TinkerforgeError> {
324 let mut payload = [0; 64];
325 x_start.write_to_slice(&mut payload[0..1]);
326 y_start.write_to_slice(&mut payload[1..2]);
327 x_end.write_to_slice(&mut payload[2..3]);
328 y_end.write_to_slice(&mut payload[3..4]);
329 pixels_length.write_to_slice(&mut payload[4..6]);
330 pixels_chunk_offset.write_to_slice(&mut payload[6..8]);
331 pixels_chunk_data.write_to_slice(&mut payload[8..64]);
332
333 #[allow(unused_variables)]
334 let result = self.device.set(u8::from(Oled128x64V2BrickletFunction::WritePixelsLowLevel), &payload).await?.unwrap();
335 Ok(WritePixelsLowLevel::from_le_byte_slice(result.body()))
336 }
337
338 pub async fn read_pixels_low_level(
352 &mut self,
353 x_start: u8,
354 y_start: u8,
355 x_end: u8,
356 y_end: u8,
357 ) -> Result<ReadPixelsLowLevel, TinkerforgeError> {
358 let mut payload = [0; 4];
359 x_start.write_to_slice(&mut payload[0..1]);
360 y_start.write_to_slice(&mut payload[1..2]);
361 x_end.write_to_slice(&mut payload[2..3]);
362 y_end.write_to_slice(&mut payload[3..4]);
363
364 #[allow(unused_variables)]
365 let result = self.device.get(u8::from(Oled128x64V2BrickletFunction::ReadPixelsLowLevel), &payload).await?;
366 Ok(ReadPixelsLowLevel::from_le_byte_slice(result.body()))
367 }
368
369 pub async fn clear_display(&mut self) -> Result<(), TinkerforgeError> {
381 let payload = [0; 0];
382
383 #[allow(unused_variables)]
384 let result = self.device.set(u8::from(Oled128x64V2BrickletFunction::ClearDisplay), &payload).await?;
385 Ok(())
386 }
387
388 pub async fn set_display_configuration(&mut self, contrast: u8, invert: bool, automatic_draw: bool) -> Result<(), TinkerforgeError> {
398 let mut payload = [0; 3];
399 contrast.write_to_slice(&mut payload[0..1]);
400 invert.write_to_slice(&mut payload[1..2]);
401 automatic_draw.write_to_slice(&mut payload[2..3]);
402
403 #[allow(unused_variables)]
404 let result = self.device.set(u8::from(Oled128x64V2BrickletFunction::SetDisplayConfiguration), &payload).await?;
405 Ok(())
406 }
407
408 pub async fn get_display_configuration(&mut self) -> Result<DisplayConfiguration, TinkerforgeError> {
410 let payload = [0; 0];
411
412 #[allow(unused_variables)]
413 let result = self.device.get(u8::from(Oled128x64V2BrickletFunction::GetDisplayConfiguration), &payload).await?;
414 Ok(DisplayConfiguration::from_le_byte_slice(result.body()))
415 }
416
417 pub async fn write_line(&mut self, line: u8, position: u8, text: String) -> Result<(), TinkerforgeError> {
440 let mut payload = [0; 24];
441 line.write_to_slice(&mut payload[0..1]);
442 position.write_to_slice(&mut payload[1..2]);
443 text.try_write_to_slice(22, &mut payload)?;
444
445 #[allow(unused_variables)]
446 let result = self.device.set(u8::from(Oled128x64V2BrickletFunction::WriteLine), &payload).await?;
447 Ok(())
448 }
449
450 pub async fn draw_buffered_frame(&mut self, force_complete_redraw: bool) -> Result<(), TinkerforgeError> {
460 let mut payload = [0; 1];
461 force_complete_redraw.write_to_slice(&mut payload[0..1]);
462
463 #[allow(unused_variables)]
464 let result = self.device.set(u8::from(Oled128x64V2BrickletFunction::DrawBufferedFrame), &payload).await?;
465 Ok(())
466 }
467
468 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
480 let payload = [0; 0];
481
482 #[allow(unused_variables)]
483 let result = self.device.get(u8::from(Oled128x64V2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
484 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
485 }
486
487 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
510 let mut payload = [0; 1];
511 mode.write_to_slice(&mut payload[0..1]);
512
513 #[allow(unused_variables)]
514 let result = self.device.get(u8::from(Oled128x64V2BrickletFunction::SetBootloaderMode), &payload).await?;
515 Ok(u8::from_le_byte_slice(result.body()))
516 }
517
518 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
527 let payload = [0; 0];
528
529 #[allow(unused_variables)]
530 let result = self.device.get(u8::from(Oled128x64V2BrickletFunction::GetBootloaderMode), &payload).await?;
531 Ok(u8::from_le_byte_slice(result.body()))
532 }
533
534 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
541 let mut payload = [0; 4];
542 pointer.write_to_slice(&mut payload[0..4]);
543
544 #[allow(unused_variables)]
545 let result = self.device.set(u8::from(Oled128x64V2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
546 Ok(())
547 }
548
549 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
558 let mut payload = [0; 64];
559 data.write_to_slice(&mut payload[0..64]);
560
561 #[allow(unused_variables)]
562 let result = self.device.get(u8::from(Oled128x64V2BrickletFunction::WriteFirmware), &payload).await?;
563 Ok(u8::from_le_byte_slice(result.body()))
564 }
565
566 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
580 let mut payload = [0; 1];
581 config.write_to_slice(&mut payload[0..1]);
582
583 #[allow(unused_variables)]
584 let result = self.device.set(u8::from(Oled128x64V2BrickletFunction::SetStatusLedConfig), &payload).await?;
585 Ok(())
586 }
587
588 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
596 let payload = [0; 0];
597
598 #[allow(unused_variables)]
599 let result = self.device.get(u8::from(Oled128x64V2BrickletFunction::GetStatusLedConfig), &payload).await?;
600 Ok(u8::from_le_byte_slice(result.body()))
601 }
602
603 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
610 let payload = [0; 0];
611
612 #[allow(unused_variables)]
613 let result = self.device.get(u8::from(Oled128x64V2BrickletFunction::GetChipTemperature), &payload).await?;
614 Ok(i16::from_le_byte_slice(result.body()))
615 }
616
617 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
624 let payload = [0; 0];
625
626 #[allow(unused_variables)]
627 let result = self.device.set(u8::from(Oled128x64V2BrickletFunction::Reset), &payload).await?;
628 Ok(())
629 }
630
631 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
637 let mut payload = [0; 4];
638 uid.write_to_slice(&mut payload[0..4]);
639
640 #[allow(unused_variables)]
641 let result = self.device.set(u8::from(Oled128x64V2BrickletFunction::WriteUid), &payload).await?;
642 Ok(())
643 }
644
645 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
648 let payload = [0; 0];
649
650 #[allow(unused_variables)]
651 let result = self.device.get(u8::from(Oled128x64V2BrickletFunction::ReadUid), &payload).await?;
652 Ok(u32::from_le_byte_slice(result.body()))
653 }
654
655 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
666 let payload = [0; 0];
667
668 #[allow(unused_variables)]
669 let result = self.device.get(u8::from(Oled128x64V2BrickletFunction::GetIdentity), &payload).await?;
670 Ok(Identity::from_le_byte_slice(result.body()))
671 }
672}