tinkerforge_async/bindings/
oled_128x64_bricklet.rs

1/* ***********************************************************
2 * This file was automatically generated on 2024-02-16.      *
3 *                                                           *
4 * Rust Bindings Version 2.0.20                              *
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/OLED128x64_Bricklet_Rust.html).
14#[allow(unused_imports)]
15use crate::{
16    base58::Uid, byte_converter::*, converting_receiver::BrickletError, device::*, error::TinkerforgeError,
17    ip_connection::async_io::AsyncIpConnection, low_level_traits::LowLevelRead,
18};
19#[allow(unused_imports)]
20use futures_core::Stream;
21#[allow(unused_imports)]
22use tokio_stream::StreamExt;
23pub enum Oled128x64BrickletFunction {
24    Write,
25    NewWindow,
26    ClearDisplay,
27    SetDisplayConfiguration,
28    GetDisplayConfiguration,
29    WriteLine,
30    GetIdentity,
31}
32impl From<Oled128x64BrickletFunction> for u8 {
33    fn from(fun: Oled128x64BrickletFunction) -> Self {
34        match fun {
35            Oled128x64BrickletFunction::Write => 1,
36            Oled128x64BrickletFunction::NewWindow => 2,
37            Oled128x64BrickletFunction::ClearDisplay => 3,
38            Oled128x64BrickletFunction::SetDisplayConfiguration => 4,
39            Oled128x64BrickletFunction::GetDisplayConfiguration => 5,
40            Oled128x64BrickletFunction::WriteLine => 6,
41            Oled128x64BrickletFunction::GetIdentity => 255,
42        }
43    }
44}
45
46#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
47pub struct DisplayConfiguration {
48    pub contrast: u8,
49    pub invert: bool,
50}
51impl FromByteSlice for DisplayConfiguration {
52    fn bytes_expected() -> usize {
53        2
54    }
55    fn from_le_byte_slice(bytes: &[u8]) -> DisplayConfiguration {
56        DisplayConfiguration { contrast: <u8>::from_le_byte_slice(&bytes[0..1]), invert: <bool>::from_le_byte_slice(&bytes[1..2]) }
57    }
58}
59
60#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
61pub struct Identity {
62    pub uid: String,
63    pub connected_uid: String,
64    pub position: char,
65    pub hardware_version: [u8; 3],
66    pub firmware_version: [u8; 3],
67    pub device_identifier: u16,
68}
69impl FromByteSlice for Identity {
70    fn bytes_expected() -> usize {
71        25
72    }
73    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
74        Identity {
75            uid: <String>::from_le_byte_slice(&bytes[0..8]),
76            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
77            position: <char>::from_le_byte_slice(&bytes[16..17]),
78            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
79            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
80            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
81        }
82    }
83}
84
85/// 3.3cm (1.3") OLED display with 128x64 pixels
86#[derive(Clone)]
87pub struct Oled128x64Bricklet {
88    device: Device,
89}
90impl Oled128x64Bricklet {
91    pub const DEVICE_IDENTIFIER: u16 = 263;
92    pub const DEVICE_DISPLAY_NAME: &'static str = "OLED 128x64 Bricklet";
93    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
94    pub fn new(uid: Uid, connection: AsyncIpConnection) -> Oled128x64Bricklet {
95        let mut result = Oled128x64Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
96        result.device.response_expected[u8::from(Oled128x64BrickletFunction::Write) as usize] = ResponseExpectedFlag::False;
97        result.device.response_expected[u8::from(Oled128x64BrickletFunction::NewWindow) as usize] = ResponseExpectedFlag::False;
98        result.device.response_expected[u8::from(Oled128x64BrickletFunction::ClearDisplay) as usize] = ResponseExpectedFlag::False;
99        result.device.response_expected[u8::from(Oled128x64BrickletFunction::SetDisplayConfiguration) as usize] =
100            ResponseExpectedFlag::False;
101        result.device.response_expected[u8::from(Oled128x64BrickletFunction::GetDisplayConfiguration) as usize] =
102            ResponseExpectedFlag::AlwaysTrue;
103        result.device.response_expected[u8::from(Oled128x64BrickletFunction::WriteLine) as usize] = ResponseExpectedFlag::False;
104        result.device.response_expected[u8::from(Oled128x64BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
105        result
106    }
107
108    /// Returns the response expected flag for the function specified by the function ID parameter.
109    /// It is true if the function is expected to send a response, false otherwise.
110    ///
111    /// For getter functions this is enabled by default and cannot be disabled, because those
112    /// functions will always send a response. For callback configuration functions it is enabled
113    /// by default too, but can be disabled by [`set_response_expected`](crate::oled_128x64_bricklet::Oled128x64Bricklet::set_response_expected).
114    /// For setter functions it is disabled by default and can be enabled.
115    ///
116    /// Enabling the response expected flag for a setter function allows to detect timeouts
117    /// and other error conditions calls of this setter as well. The device will then send a response
118    /// for this purpose. If this flag is disabled for a setter function then no response is sent
119    /// and errors are silently ignored, because they cannot be detected.
120    ///
121    /// See [`set_response_expected`](crate::oled_128x64_bricklet::Oled128x64Bricklet::set_response_expected) for the list of function ID constants available for this function.
122    pub fn get_response_expected(&mut self, fun: Oled128x64BrickletFunction) -> Result<bool, GetResponseExpectedError> {
123        self.device.get_response_expected(u8::from(fun))
124    }
125
126    /// Changes the response expected flag of the function specified by the function ID parameter.
127    /// This flag can only be changed for setter (default value: false) and callback configuration
128    /// functions (default value: true). For getter functions it is always enabled.
129    ///
130    /// Enabling the response expected flag for a setter function allows to detect timeouts and
131    /// other error conditions calls of this setter as well. The device will then send a response
132    /// for this purpose. If this flag is disabled for a setter function then no response is sent
133    /// and errors are silently ignored, because they cannot be detected.
134    pub fn set_response_expected(
135        &mut self,
136        fun: Oled128x64BrickletFunction,
137        response_expected: bool,
138    ) -> Result<(), SetResponseExpectedError> {
139        self.device.set_response_expected(u8::from(fun), response_expected)
140    }
141
142    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
143    pub fn set_response_expected_all(&mut self, response_expected: bool) {
144        self.device.set_response_expected_all(response_expected)
145    }
146
147    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
148    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
149    pub fn get_api_version(&self) -> [u8; 3] {
150        self.device.api_version
151    }
152
153    /// Appends 64 byte of data to the window as set by [`new_window`].
154    ///
155    /// Each row has a height of 8 pixels which corresponds to one byte of data.
156    ///
157    /// Example: if you call [`new_window`] with column from 0 to 127 and row
158    /// from 0 to 7 (the whole display) each call of [`write`] (red arrow) will
159    /// write half of a row.
160    ///
161    /// .. image:: /Images/Bricklets/bricklet_oled_128x64_display.png
162    ///    :scale: 100 %
163    ///    :alt: Display pixel order
164    ///    :align: center
165    ///    :target: ../../_images/Bricklets/bricklet_oled_128x64_display.png
166    ///
167    /// The LSB (D0) of each data byte is at the top and the MSB (D7) is at the
168    /// bottom of the row.
169    ///
170    /// The next call of [`write`] will write the second half of the row
171    /// and the next two the second row and so on. To fill the whole display
172    /// you need to call [`write`] 16 times.
173    ///
174    /// [`write`]: #method.write
175    /// [`new_window`]: #method.new_window
176    pub async fn write(&mut self, data: &[u8; 64]) -> Result<(), TinkerforgeError> {
177        let mut payload = [0; 64];
178        data.write_to_slice(&mut payload[0..64]);
179
180        #[allow(unused_variables)]
181        let result = self.device.set(u8::from(Oled128x64BrickletFunction::Write), &payload).await?;
182        Ok(())
183    }
184
185    /// Sets the window in which you can write with [`write`]. One row
186    /// has a height of 8 pixels.
187    pub async fn new_window(&mut self, column_from: u8, column_to: u8, row_from: u8, row_to: u8) -> Result<(), TinkerforgeError> {
188        let mut payload = [0; 4];
189        column_from.write_to_slice(&mut payload[0..1]);
190        column_to.write_to_slice(&mut payload[1..2]);
191        row_from.write_to_slice(&mut payload[2..3]);
192        row_to.write_to_slice(&mut payload[3..4]);
193
194        #[allow(unused_variables)]
195        let result = self.device.set(u8::from(Oled128x64BrickletFunction::NewWindow), &payload).await?;
196        Ok(())
197    }
198
199    /// Clears the current content of the window as set by [`new_window`].
200    pub async fn clear_display(&mut self) -> Result<(), TinkerforgeError> {
201        let payload = [0; 0];
202
203        #[allow(unused_variables)]
204        let result = self.device.set(u8::from(Oled128x64BrickletFunction::ClearDisplay), &payload).await?;
205        Ok(())
206    }
207
208    /// Sets the configuration of the display.
209    ///
210    /// You can set a contrast value from 0 to 255 and you can invert the color
211    /// (black/white) of the display.
212    pub async fn set_display_configuration(&mut self, contrast: u8, invert: bool) -> Result<(), TinkerforgeError> {
213        let mut payload = [0; 2];
214        contrast.write_to_slice(&mut payload[0..1]);
215        invert.write_to_slice(&mut payload[1..2]);
216
217        #[allow(unused_variables)]
218        let result = self.device.set(u8::from(Oled128x64BrickletFunction::SetDisplayConfiguration), &payload).await?;
219        Ok(())
220    }
221
222    /// Returns the configuration as set by [`set_display_configuration`].
223    pub async fn get_display_configuration(&mut self) -> Result<DisplayConfiguration, TinkerforgeError> {
224        let payload = [0; 0];
225
226        #[allow(unused_variables)]
227        let result = self.device.get(u8::from(Oled128x64BrickletFunction::GetDisplayConfiguration), &payload).await?;
228        Ok(DisplayConfiguration::from_le_byte_slice(result.body()))
229    }
230
231    /// Writes text to a specific line with a specific position.
232    /// The text can have a maximum of 26 characters.
233    ///
234    /// For example: (1, 10, Hello) will write *Hello* in the middle of the
235    /// second line of the display.
236    ///
237    /// You can draw to the display with [`write`] and then add text to it
238    /// afterwards.
239    ///
240    /// The display uses a special 5x7 pixel charset. You can view the characters
241    /// of the charset in Brick Viewer.
242    ///
243    /// The font conforms to code page 437.
244    pub async fn write_line(&mut self, line: u8, position: u8, text: String) -> Result<(), TinkerforgeError> {
245        let mut payload = [0; 28];
246        line.write_to_slice(&mut payload[0..1]);
247        position.write_to_slice(&mut payload[1..2]);
248        text.try_write_to_slice(26, &mut payload)?;
249
250        #[allow(unused_variables)]
251        let result = self.device.set(u8::from(Oled128x64BrickletFunction::WriteLine), &payload).await?;
252        Ok(())
253    }
254
255    /// Returns the UID, the UID where the Bricklet is connected to,
256    /// the position, the hardware and firmware version as well as the
257    /// device identifier.
258    ///
259    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
260    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
261    /// position 'z'.
262    ///
263    /// The device identifier numbers can be found [here](device_identifier).
264    /// |device_identifier_constant|
265    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
266        let payload = [0; 0];
267
268        #[allow(unused_variables)]
269        let result = self.device.get(u8::from(Oled128x64BrickletFunction::GetIdentity), &payload).await?;
270        Ok(Identity::from_le_byte_slice(result.body()))
271    }
272}