tinkerforge/bindings/
oled_128x64_v2_bricklet.rs

1/* ***********************************************************
2 * This file was automatically generated on 2024-02-27.      *
3 *                                                           *
4 * Rust Bindings Version 2.0.21                              *
5 *                                                           *
6 * If you have a bugfix for this file and want to commit it, *
7 * please fix the bug in the generator. You can find a link  *
8 * to the generators git repository on tinkerforge.com       *
9 *************************************************************/
10
11//! 3.3cm (1.3") OLED display with 128x64 pixels.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/OLED128x64V2_Bricklet_Rust.html).
14use crate::{
15    byte_converter::*,
16    converting_receiver::{BrickletError, BrickletRecvTimeoutError, ConvertingReceiver},
17    device::*,
18    ip_connection::GetRequestSender,
19    low_level_traits::*,
20};
21pub enum Oled128x64V2BrickletFunction {
22    WritePixelsLowLevel,
23    ReadPixelsLowLevel,
24    ClearDisplay,
25    SetDisplayConfiguration,
26    GetDisplayConfiguration,
27    WriteLine,
28    DrawBufferedFrame,
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}
42impl From<Oled128x64V2BrickletFunction> for u8 {
43    fn from(fun: Oled128x64V2BrickletFunction) -> Self {
44        match fun {
45            Oled128x64V2BrickletFunction::WritePixelsLowLevel => 1,
46            Oled128x64V2BrickletFunction::ReadPixelsLowLevel => 2,
47            Oled128x64V2BrickletFunction::ClearDisplay => 3,
48            Oled128x64V2BrickletFunction::SetDisplayConfiguration => 4,
49            Oled128x64V2BrickletFunction::GetDisplayConfiguration => 5,
50            Oled128x64V2BrickletFunction::WriteLine => 6,
51            Oled128x64V2BrickletFunction::DrawBufferedFrame => 7,
52            Oled128x64V2BrickletFunction::GetSpitfpErrorCount => 234,
53            Oled128x64V2BrickletFunction::SetBootloaderMode => 235,
54            Oled128x64V2BrickletFunction::GetBootloaderMode => 236,
55            Oled128x64V2BrickletFunction::SetWriteFirmwarePointer => 237,
56            Oled128x64V2BrickletFunction::WriteFirmware => 238,
57            Oled128x64V2BrickletFunction::SetStatusLedConfig => 239,
58            Oled128x64V2BrickletFunction::GetStatusLedConfig => 240,
59            Oled128x64V2BrickletFunction::GetChipTemperature => 242,
60            Oled128x64V2BrickletFunction::Reset => 243,
61            Oled128x64V2BrickletFunction::WriteUid => 248,
62            Oled128x64V2BrickletFunction::ReadUid => 249,
63            Oled128x64V2BrickletFunction::GetIdentity => 255,
64        }
65    }
66}
67pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
68pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
69pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
70pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
71pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
72pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
73pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
74pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
75pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
76pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
77pub const OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
78pub const OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
79pub const OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
80pub const OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
81pub const OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
82
83#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
84pub struct WritePixelsLowLevel {}
85impl FromByteSlice for WritePixelsLowLevel {
86    fn bytes_expected() -> usize { 0 }
87    fn from_le_byte_slice(_bytes: &[u8]) -> WritePixelsLowLevel { WritePixelsLowLevel {} }
88}
89impl LowLevelWrite<WritePixelsResult> for WritePixelsLowLevel {
90    fn ll_message_written(&self) -> usize { 448 }
91
92    fn get_result(&self) -> WritePixelsResult { WritePixelsResult {} }
93}
94
95#[derive(Clone, Copy)]
96pub struct ReadPixelsLowLevel {
97    pub pixels_length: u16,
98    pub pixels_chunk_offset: u16,
99    pub pixels_chunk_data: [bool; 480],
100}
101impl FromByteSlice for ReadPixelsLowLevel {
102    fn bytes_expected() -> usize { 64 }
103    fn from_le_byte_slice(bytes: &[u8]) -> ReadPixelsLowLevel {
104        ReadPixelsLowLevel {
105            pixels_length: <u16>::from_le_byte_slice(&bytes[0..2]),
106            pixels_chunk_offset: <u16>::from_le_byte_slice(&bytes[2..4]),
107            pixels_chunk_data: <[bool; 480]>::from_le_byte_slice(&bytes[4..64]),
108        }
109    }
110}
111impl LowLevelRead<bool, ReadPixelsResult> for ReadPixelsLowLevel {
112    fn ll_message_length(&self) -> usize { self.pixels_length as usize }
113
114    fn ll_message_chunk_offset(&self) -> usize { self.pixels_chunk_offset as usize }
115
116    fn ll_message_chunk_data(&self) -> &[bool] { &self.pixels_chunk_data }
117
118    fn get_result(&self) -> ReadPixelsResult { ReadPixelsResult {} }
119}
120
121#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
122pub struct DisplayConfiguration {
123    pub contrast: u8,
124    pub invert: bool,
125    pub automatic_draw: bool,
126}
127impl FromByteSlice for DisplayConfiguration {
128    fn bytes_expected() -> usize { 3 }
129    fn from_le_byte_slice(bytes: &[u8]) -> DisplayConfiguration {
130        DisplayConfiguration {
131            contrast: <u8>::from_le_byte_slice(&bytes[0..1]),
132            invert: <bool>::from_le_byte_slice(&bytes[1..2]),
133            automatic_draw: <bool>::from_le_byte_slice(&bytes[2..3]),
134        }
135    }
136}
137
138#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
139pub struct SpitfpErrorCount {
140    pub error_count_ack_checksum: u32,
141    pub error_count_message_checksum: u32,
142    pub error_count_frame: u32,
143    pub error_count_overflow: u32,
144}
145impl FromByteSlice for SpitfpErrorCount {
146    fn bytes_expected() -> usize { 16 }
147    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
148        SpitfpErrorCount {
149            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
150            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
151            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
152            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
153        }
154    }
155}
156
157#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
158pub struct Identity {
159    pub uid: String,
160    pub connected_uid: String,
161    pub position: char,
162    pub hardware_version: [u8; 3],
163    pub firmware_version: [u8; 3],
164    pub device_identifier: u16,
165}
166impl FromByteSlice for Identity {
167    fn bytes_expected() -> usize { 25 }
168    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
169        Identity {
170            uid: <String>::from_le_byte_slice(&bytes[0..8]),
171            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
172            position: <char>::from_le_byte_slice(&bytes[16..17]),
173            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
174            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
175            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
176        }
177    }
178}
179
180#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
181pub struct WritePixelsResult {}
182
183#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
184pub struct ReadPixelsResult {}
185
186/// 3.3cm (1.3") OLED display with 128x64 pixels
187#[derive(Clone)]
188pub struct Oled128x64V2Bricklet {
189    device: Device,
190}
191impl Oled128x64V2Bricklet {
192    pub const DEVICE_IDENTIFIER: u16 = 2112;
193    pub const DEVICE_DISPLAY_NAME: &'static str = "OLED 128x64 Bricklet 2.0";
194    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
195    pub fn new<T: GetRequestSender>(uid: &str, req_sender: T) -> Oled128x64V2Bricklet {
196        let mut result = Oled128x64V2Bricklet { device: Device::new([2, 0, 0], uid, req_sender, 2) };
197        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::WritePixelsLowLevel) as usize] = ResponseExpectedFlag::True;
198        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::ReadPixelsLowLevel) as usize] =
199            ResponseExpectedFlag::AlwaysTrue;
200        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::ClearDisplay) as usize] = ResponseExpectedFlag::False;
201        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::SetDisplayConfiguration) as usize] =
202            ResponseExpectedFlag::False;
203        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::GetDisplayConfiguration) as usize] =
204            ResponseExpectedFlag::AlwaysTrue;
205        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::WriteLine) as usize] = ResponseExpectedFlag::False;
206        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::DrawBufferedFrame) as usize] = ResponseExpectedFlag::False;
207        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::GetSpitfpErrorCount) as usize] =
208            ResponseExpectedFlag::AlwaysTrue;
209        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::SetBootloaderMode) as usize] =
210            ResponseExpectedFlag::AlwaysTrue;
211        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::GetBootloaderMode) as usize] =
212            ResponseExpectedFlag::AlwaysTrue;
213        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::SetWriteFirmwarePointer) as usize] =
214            ResponseExpectedFlag::False;
215        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
216        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
217        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::GetStatusLedConfig) as usize] =
218            ResponseExpectedFlag::AlwaysTrue;
219        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::GetChipTemperature) as usize] =
220            ResponseExpectedFlag::AlwaysTrue;
221        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
222        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
223        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
224        result.device.response_expected[u8::from(Oled128x64V2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
225        result
226    }
227
228    /// Returns the response expected flag for the function specified by the function ID parameter.
229    /// It is true if the function is expected to send a response, false otherwise.
230    ///
231    /// For getter functions this is enabled by default and cannot be disabled, because those
232    /// functions will always send a response. For callback configuration functions it is enabled
233    /// by default too, but can be disabled by [`set_response_expected`](crate::oled_128x64_v2_bricklet::Oled128x64V2Bricklet::set_response_expected).
234    /// For setter functions it is disabled by default and can be enabled.
235    ///
236    /// Enabling the response expected flag for a setter function allows to detect timeouts
237    /// and other error conditions calls of this setter as well. The device will then send a response
238    /// for this purpose. If this flag is disabled for a setter function then no response is sent
239    /// and errors are silently ignored, because they cannot be detected.
240    ///
241    /// See [`set_response_expected`](crate::oled_128x64_v2_bricklet::Oled128x64V2Bricklet::set_response_expected) for the list of function ID constants available for this function.
242    pub fn get_response_expected(&mut self, fun: Oled128x64V2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
243        self.device.get_response_expected(u8::from(fun))
244    }
245
246    /// Changes the response expected flag of the function specified by the function ID parameter.
247    /// This flag can only be changed for setter (default value: false) and callback configuration
248    /// functions (default value: true). For getter functions it is always enabled.
249    ///
250    /// Enabling the response expected flag for a setter function allows to detect timeouts and
251    /// other error conditions calls of this setter as well. The device will then send a response
252    /// for this purpose. If this flag is disabled for a setter function then no response is sent
253    /// and errors are silently ignored, because they cannot be detected.
254    pub fn set_response_expected(
255        &mut self,
256        fun: Oled128x64V2BrickletFunction,
257        response_expected: bool,
258    ) -> Result<(), SetResponseExpectedError> {
259        self.device.set_response_expected(u8::from(fun), response_expected)
260    }
261
262    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
263    pub fn set_response_expected_all(&mut self, response_expected: bool) { self.device.set_response_expected_all(response_expected) }
264
265    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
266    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
267    pub fn get_api_version(&self) -> [u8; 3] { self.device.api_version }
268
269    /// Writes pixels to the specified window.
270    ///
271    /// The pixels are written into the window line by line top to bottom
272    /// and each line is written from left to right.
273    ///
274    /// If automatic draw is enabled (default) the pixels are directly written to
275    /// the screen. Only pixels that have actually changed are updated on the screen,
276    /// the rest stays the same.
277    ///
278    /// If automatic draw is disabled the pixels are written to an internal buffer and
279    /// the buffer is transferred to the display only after [`draw_buffered_frame`]
280    /// is called. This can be used to avoid flicker when drawing a complex frame in
281    /// multiple steps.
282    ///
283    /// Automatic draw can be configured with the [`set_display_configuration`]
284    /// function.
285    ///
286    /// [`set_display_configuration`]: #method.set_display_configuration
287    /// [`draw_buffered_frame`]: #method.draw_buffered_frame
288    pub fn write_pixels_low_level(
289        &self,
290        x_start: u8,
291        y_start: u8,
292        x_end: u8,
293        y_end: u8,
294        pixels_length: u16,
295        pixels_chunk_offset: u16,
296        pixels_chunk_data: [bool; 448],
297    ) -> ConvertingReceiver<WritePixelsLowLevel> {
298        let mut payload = vec![0; 64];
299        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(x_start));
300        payload[1..2].copy_from_slice(&<u8>::to_le_byte_vec(y_start));
301        payload[2..3].copy_from_slice(&<u8>::to_le_byte_vec(x_end));
302        payload[3..4].copy_from_slice(&<u8>::to_le_byte_vec(y_end));
303        payload[4..6].copy_from_slice(&<u16>::to_le_byte_vec(pixels_length));
304        payload[6..8].copy_from_slice(&<u16>::to_le_byte_vec(pixels_chunk_offset));
305        payload[8..64].copy_from_slice(&<[bool; 448]>::to_le_byte_vec(pixels_chunk_data));
306
307        self.device.set(u8::from(Oled128x64V2BrickletFunction::WritePixelsLowLevel), payload)
308    }
309
310    /// Writes pixels to the specified window.
311    ///
312    /// The pixels are written into the window line by line top to bottom
313    /// and each line is written from left to right.
314    ///
315    /// If automatic draw is enabled (default) the pixels are directly written to
316    /// the screen. Only pixels that have actually changed are updated on the screen,
317    /// the rest stays the same.
318    ///
319    /// If automatic draw is disabled the pixels are written to an internal buffer and
320    /// the buffer is transferred to the display only after [`draw_buffered_frame`]
321    /// is called. This can be used to avoid flicker when drawing a complex frame in
322    /// multiple steps.
323    ///
324    /// Automatic draw can be configured with the [`set_display_configuration`]
325    /// function.
326    pub fn write_pixels(&self, x_start: u8, y_start: u8, x_end: u8, y_end: u8, pixels: &[bool]) -> Result<(), BrickletRecvTimeoutError> {
327        let _ll_result = self.device.set_high_level(0, pixels, 65535, 448, &mut |length: usize, chunk_offset: usize, chunk: &[bool]| {
328            let chunk_length = chunk.len() as u16;
329            let mut chunk_array = [<bool>::default(); 448];
330            chunk_array[0..chunk_length as usize].copy_from_slice(&chunk);
331
332            let result =
333                self.write_pixels_low_level(x_start, y_start, x_end, y_end, length as u16, chunk_offset as u16, chunk_array).recv();
334            if let Err(BrickletRecvTimeoutError::SuccessButResponseExpectedIsDisabled) = result {
335                Ok(Default::default())
336            } else {
337                result
338            }
339        })?;
340        Ok(())
341    }
342
343    /// Reads pixels from the specified window.
344    ///
345    /// The pixels are read from the window line by line top to bottom
346    /// and each line is read from left to right.
347    ///
348    /// If automatic draw is enabled (default) the pixels that are read are always the
349    /// same that are shown on the display.
350    ///
351    /// If automatic draw is disabled the pixels are read from the internal buffer
352    /// (see [`draw_buffered_frame`]).
353    ///
354    /// Automatic draw can be configured with the [`set_display_configuration`]
355    /// function.
356    pub fn read_pixels_low_level(&self, x_start: u8, y_start: u8, x_end: u8, y_end: u8) -> ConvertingReceiver<ReadPixelsLowLevel> {
357        let mut payload = vec![0; 4];
358        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(x_start));
359        payload[1..2].copy_from_slice(&<u8>::to_le_byte_vec(y_start));
360        payload[2..3].copy_from_slice(&<u8>::to_le_byte_vec(x_end));
361        payload[3..4].copy_from_slice(&<u8>::to_le_byte_vec(y_end));
362
363        self.device.get(u8::from(Oled128x64V2BrickletFunction::ReadPixelsLowLevel), payload)
364    }
365
366    /// Reads pixels from the specified window.
367    ///
368    /// The pixels are read from the window line by line top to bottom
369    /// and each line is read from left to right.
370    ///
371    /// If automatic draw is enabled (default) the pixels that are read are always the
372    /// same that are shown on the display.
373    ///
374    /// If automatic draw is disabled the pixels are read from the internal buffer
375    /// (see [`draw_buffered_frame`]).
376    ///
377    /// Automatic draw can be configured with the [`set_display_configuration`]
378    /// function.
379    pub fn read_pixels(&self, x_start: u8, y_start: u8, x_end: u8, y_end: u8) -> Result<Vec<bool>, BrickletRecvTimeoutError> {
380        let ll_result = self.device.get_high_level(1, &mut || self.read_pixels_low_level(x_start, y_start, x_end, y_end).recv())?;
381        Ok(ll_result.0)
382    }
383
384    /// Clears the complete content of the display.
385    ///
386    /// If automatic draw is enabled (default) the pixels are directly cleared.
387    ///
388    /// If automatic draw is disabled the the internal buffer is cleared and
389    /// the buffer is transferred to the display only after [`draw_buffered_frame`]
390    /// is called. This can be used to avoid flicker when drawing a complex frame in
391    /// multiple steps.
392    ///
393    /// Automatic draw can be configured with the [`set_display_configuration`]
394    /// function.
395    pub fn clear_display(&self) -> ConvertingReceiver<()> {
396        let payload = vec![0; 0];
397
398        self.device.set(u8::from(Oled128x64V2BrickletFunction::ClearDisplay), payload)
399    }
400
401    /// Sets the configuration of the display.
402    ///
403    /// You can set a contrast value from 0 to 255 and you can invert the color
404    /// (white/black) of the display.
405    ///
406    /// If automatic draw is set to *true*, the display is automatically updated with every
407    /// call of [`write_pixels`] or [`write_line`]. If it is set to false, the
408    /// changes are written into an internal buffer and only shown on the display after
409    /// a call of [`draw_buffered_frame`].
410    pub fn set_display_configuration(&self, contrast: u8, invert: bool, automatic_draw: bool) -> ConvertingReceiver<()> {
411        let mut payload = vec![0; 3];
412        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(contrast));
413        payload[1..2].copy_from_slice(&<bool>::to_le_byte_vec(invert));
414        payload[2..3].copy_from_slice(&<bool>::to_le_byte_vec(automatic_draw));
415
416        self.device.set(u8::from(Oled128x64V2BrickletFunction::SetDisplayConfiguration), payload)
417    }
418
419    /// Returns the configuration as set by [`set_display_configuration`].
420    pub fn get_display_configuration(&self) -> ConvertingReceiver<DisplayConfiguration> {
421        let payload = vec![0; 0];
422
423        self.device.get(u8::from(Oled128x64V2BrickletFunction::GetDisplayConfiguration), payload)
424    }
425
426    /// Writes text to a specific line with a specific position.
427    /// The text can have a maximum of 22 characters.
428    ///
429    /// For example: (1, 10, Hello) will write *Hello* in the middle of the
430    /// second line of the display.
431    ///
432    /// The display uses a special 5x7 pixel charset. You can view the characters
433    /// of the charset in Brick Viewer.
434    ///
435    /// If automatic draw is enabled (default) the text is directly written to
436    /// the screen. Only pixels that have actually changed are updated on the screen,
437    /// the rest stays the same.
438    ///
439    /// If automatic draw is disabled the text is written to an internal buffer and
440    /// the buffer is transferred to the display only after [`draw_buffered_frame`]
441    /// is called. This can be used to avoid flicker when drawing a complex frame in
442    /// multiple steps.
443    ///
444    /// Automatic draw can be configured with the [`set_display_configuration`]
445    /// function.
446    ///
447    /// The font conforms to code page 437.
448    pub fn write_line(&self, line: u8, position: u8, text: String) -> ConvertingReceiver<()> {
449        let mut payload = vec![0; 24];
450        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(line));
451        payload[1..2].copy_from_slice(&<u8>::to_le_byte_vec(position));
452        match <String>::try_to_le_byte_vec(text, 22) {
453            Err(e) => {
454                let (tx, rx) = std::sync::mpsc::channel::<Result<Vec<u8>, BrickletError>>();
455                let _ = tx.send(Err(e));
456                return ConvertingReceiver::new(rx, std::time::Duration::new(1, 0));
457            }
458            Ok(bytes) => payload[2..24].copy_from_slice(&bytes),
459        }
460
461        self.device.set(u8::from(Oled128x64V2BrickletFunction::WriteLine), payload)
462    }
463
464    /// Draws the currently buffered frame. Normally each call of [`write_pixels`] and
465    /// [`write_line`] draws directly onto the display. If you turn automatic draw off
466    /// ([`set_display_configuration`]), the data is written in an internal buffer and
467    /// only transferred to the display by calling this function. This can be used to
468    /// avoid flicker when drawing a complex frame in multiple steps.
469    ///
470    /// Set the `force complete redraw` to *true* to redraw the whole display
471    /// instead of only the changed parts. Normally it should not be necessary to set this to
472    /// *true*. It may only become necessary in case of stuck pixels because of errors.
473    pub fn draw_buffered_frame(&self, force_complete_redraw: bool) -> ConvertingReceiver<()> {
474        let mut payload = vec![0; 1];
475        payload[0..1].copy_from_slice(&<bool>::to_le_byte_vec(force_complete_redraw));
476
477        self.device.set(u8::from(Oled128x64V2BrickletFunction::DrawBufferedFrame), payload)
478    }
479
480    /// Returns the error count for the communication between Brick and Bricklet.
481    ///
482    /// The errors are divided into
483    ///
484    /// * ACK checksum errors,
485    /// * message checksum errors,
486    /// * framing errors and
487    /// * overflow errors.
488    ///
489    /// The errors counts are for errors that occur on the Bricklet side. All
490    /// Bricks have a similar function that returns the errors on the Brick side.
491    pub fn get_spitfp_error_count(&self) -> ConvertingReceiver<SpitfpErrorCount> {
492        let payload = vec![0; 0];
493
494        self.device.get(u8::from(Oled128x64V2BrickletFunction::GetSpitfpErrorCount), payload)
495    }
496
497    /// Sets the bootloader mode and returns the status after the requested
498    /// mode change was instigated.
499    ///
500    /// You can change from bootloader mode to firmware mode and vice versa. A change
501    /// from bootloader mode to firmware mode will only take place if the entry function,
502    /// device identifier and CRC are present and correct.
503    ///
504    /// This function is used by Brick Viewer during flashing. It should not be
505    /// necessary to call it in a normal user program.
506    ///
507    /// Associated constants:
508    /// * OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
509    ///	* OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
510    ///	* OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
511    ///	* OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
512    ///	* OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
513    ///	* OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_OK
514    ///	* OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
515    ///	* OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
516    ///	* OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
517    ///	* OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
518    ///	* OLED_128X64_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
519    pub fn set_bootloader_mode(&self, mode: u8) -> ConvertingReceiver<u8> {
520        let mut payload = vec![0; 1];
521        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(mode));
522
523        self.device.get(u8::from(Oled128x64V2BrickletFunction::SetBootloaderMode), payload)
524    }
525
526    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
527    ///
528    /// Associated constants:
529    /// * OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
530    ///	* OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
531    ///	* OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
532    ///	* OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
533    ///	* OLED_128X64_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
534    pub fn get_bootloader_mode(&self) -> ConvertingReceiver<u8> {
535        let payload = vec![0; 0];
536
537        self.device.get(u8::from(Oled128x64V2BrickletFunction::GetBootloaderMode), payload)
538    }
539
540    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
541    /// to be increased by chunks of size 64. The data is written to flash
542    /// every 4 chunks (which equals to one page of size 256).
543    ///
544    /// This function is used by Brick Viewer during flashing. It should not be
545    /// necessary to call it in a normal user program.
546    pub fn set_write_firmware_pointer(&self, pointer: u32) -> ConvertingReceiver<()> {
547        let mut payload = vec![0; 4];
548        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(pointer));
549
550        self.device.set(u8::from(Oled128x64V2BrickletFunction::SetWriteFirmwarePointer), payload)
551    }
552
553    /// Writes 64 Bytes of firmware at the position as written by
554    /// [`set_write_firmware_pointer`] before. The firmware is written
555    /// to flash every 4 chunks.
556    ///
557    /// You can only write firmware in bootloader mode.
558    ///
559    /// This function is used by Brick Viewer during flashing. It should not be
560    /// necessary to call it in a normal user program.
561    pub fn write_firmware(&self, data: [u8; 64]) -> ConvertingReceiver<u8> {
562        let mut payload = vec![0; 64];
563        payload[0..64].copy_from_slice(&<[u8; 64]>::to_le_byte_vec(data));
564
565        self.device.get(u8::from(Oled128x64V2BrickletFunction::WriteFirmware), payload)
566    }
567
568    /// Sets the status LED configuration. By default the LED shows
569    /// communication traffic between Brick and Bricklet, it flickers once
570    /// for every 10 received data packets.
571    ///
572    /// You can also turn the LED permanently on/off or show a heartbeat.
573    ///
574    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
575    ///
576    /// Associated constants:
577    /// * OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_OFF
578    ///	* OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_ON
579    ///	* OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
580    ///	* OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
581    pub fn set_status_led_config(&self, config: u8) -> ConvertingReceiver<()> {
582        let mut payload = vec![0; 1];
583        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(config));
584
585        self.device.set(u8::from(Oled128x64V2BrickletFunction::SetStatusLedConfig), payload)
586    }
587
588    /// Returns the configuration as set by [`set_status_led_config`]
589    ///
590    /// Associated constants:
591    /// * OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_OFF
592    ///	* OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_ON
593    ///	* OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
594    ///	* OLED_128X64_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
595    pub fn get_status_led_config(&self) -> ConvertingReceiver<u8> {
596        let payload = vec![0; 0];
597
598        self.device.get(u8::from(Oled128x64V2BrickletFunction::GetStatusLedConfig), payload)
599    }
600
601    /// Returns the temperature as measured inside the microcontroller. The
602    /// value returned is not the ambient temperature!
603    ///
604    /// The temperature is only proportional to the real temperature and it has bad
605    /// accuracy. Practically it is only useful as an indicator for
606    /// temperature changes.
607    pub fn get_chip_temperature(&self) -> ConvertingReceiver<i16> {
608        let payload = vec![0; 0];
609
610        self.device.get(u8::from(Oled128x64V2BrickletFunction::GetChipTemperature), payload)
611    }
612
613    /// Calling this function will reset the Bricklet. All configurations
614    /// will be lost.
615    ///
616    /// After a reset you have to create new device objects,
617    /// calling functions on the existing ones will result in
618    /// undefined behavior!
619    pub fn reset(&self) -> ConvertingReceiver<()> {
620        let payload = vec![0; 0];
621
622        self.device.set(u8::from(Oled128x64V2BrickletFunction::Reset), payload)
623    }
624
625    /// Writes a new UID into flash. If you want to set a new UID
626    /// you have to decode the Base58 encoded UID string into an
627    /// integer first.
628    ///
629    /// We recommend that you use Brick Viewer to change the UID.
630    pub fn write_uid(&self, uid: u32) -> ConvertingReceiver<()> {
631        let mut payload = vec![0; 4];
632        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(uid));
633
634        self.device.set(u8::from(Oled128x64V2BrickletFunction::WriteUid), payload)
635    }
636
637    /// Returns the current UID as an integer. Encode as
638    /// Base58 to get the usual string version.
639    pub fn read_uid(&self) -> ConvertingReceiver<u32> {
640        let payload = vec![0; 0];
641
642        self.device.get(u8::from(Oled128x64V2BrickletFunction::ReadUid), payload)
643    }
644
645    /// Returns the UID, the UID where the Bricklet is connected to,
646    /// the position, the hardware and firmware version as well as the
647    /// device identifier.
648    ///
649    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
650    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
651    /// position 'z'.
652    ///
653    /// The device identifier numbers can be found [here](device_identifier).
654    /// |device_identifier_constant|
655    pub fn get_identity(&self) -> ConvertingReceiver<Identity> {
656        let payload = vec![0; 0];
657
658        self.device.get(u8::from(Oled128x64V2BrickletFunction::GetIdentity), payload)
659    }
660}