tinkerforge_async/bindings/
oled_64x48_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//! 1.68cm (0.66") OLED display with 64x48 pixels.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/OLED64x48_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 Oled64x48BrickletFunction {
24    Write,
25    NewWindow,
26    ClearDisplay,
27    SetDisplayConfiguration,
28    GetDisplayConfiguration,
29    WriteLine,
30    GetIdentity,
31}
32impl From<Oled64x48BrickletFunction> for u8 {
33    fn from(fun: Oled64x48BrickletFunction) -> Self {
34        match fun {
35            Oled64x48BrickletFunction::Write => 1,
36            Oled64x48BrickletFunction::NewWindow => 2,
37            Oled64x48BrickletFunction::ClearDisplay => 3,
38            Oled64x48BrickletFunction::SetDisplayConfiguration => 4,
39            Oled64x48BrickletFunction::GetDisplayConfiguration => 5,
40            Oled64x48BrickletFunction::WriteLine => 6,
41            Oled64x48BrickletFunction::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/// 1.68cm (0.66") OLED display with 64x48 pixels
86#[derive(Clone)]
87pub struct Oled64x48Bricklet {
88    device: Device,
89}
90impl Oled64x48Bricklet {
91    pub const DEVICE_IDENTIFIER: u16 = 264;
92    pub const DEVICE_DISPLAY_NAME: &'static str = "OLED 64x48 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) -> Oled64x48Bricklet {
95        let mut result = Oled64x48Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
96        result.device.response_expected[u8::from(Oled64x48BrickletFunction::Write) as usize] = ResponseExpectedFlag::False;
97        result.device.response_expected[u8::from(Oled64x48BrickletFunction::NewWindow) as usize] = ResponseExpectedFlag::False;
98        result.device.response_expected[u8::from(Oled64x48BrickletFunction::ClearDisplay) as usize] = ResponseExpectedFlag::False;
99        result.device.response_expected[u8::from(Oled64x48BrickletFunction::SetDisplayConfiguration) as usize] =
100            ResponseExpectedFlag::False;
101        result.device.response_expected[u8::from(Oled64x48BrickletFunction::GetDisplayConfiguration) as usize] =
102            ResponseExpectedFlag::AlwaysTrue;
103        result.device.response_expected[u8::from(Oled64x48BrickletFunction::WriteLine) as usize] = ResponseExpectedFlag::False;
104        result.device.response_expected[u8::from(Oled64x48BrickletFunction::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_64x48_bricklet::Oled64x48Bricklet::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_64x48_bricklet::Oled64x48Bricklet::set_response_expected) for the list of function ID constants available for this function.
122    pub fn get_response_expected(&mut self, fun: Oled64x48BrickletFunction) -> 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: Oled64x48BrickletFunction,
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 63 and row
158    /// from 0 to 5 (the whole display) each call of [`write`] (red arrow) will
159    /// write one row.
160    ///
161    /// .. image:: /Images/Bricklets/bricklet_oled_64x48_display.png
162    ///    :scale: 100 %
163    ///    :alt: Display pixel order
164    ///    :align: center
165    ///    :target: ../../_images/Bricklets/bricklet_oled_64x48_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 row and so on. To
171    /// fill the whole display you need to call [`write`] 6 times.
172    ///
173    /// [`write`]: #method.write
174    /// [`new_window`]: #method.new_window
175    pub async fn write(&mut self, data: &[u8; 64]) -> Result<(), TinkerforgeError> {
176        let mut payload = [0; 64];
177        data.write_to_slice(&mut payload[0..64]);
178
179        #[allow(unused_variables)]
180        let result = self.device.set(u8::from(Oled64x48BrickletFunction::Write), &payload).await?;
181        Ok(())
182    }
183
184    /// Sets the window in which you can write with [`write`]. One row
185    /// has a height of 8 pixels.
186    pub async fn new_window(&mut self, column_from: u8, column_to: u8, row_from: u8, row_to: u8) -> Result<(), TinkerforgeError> {
187        let mut payload = [0; 4];
188        column_from.write_to_slice(&mut payload[0..1]);
189        column_to.write_to_slice(&mut payload[1..2]);
190        row_from.write_to_slice(&mut payload[2..3]);
191        row_to.write_to_slice(&mut payload[3..4]);
192
193        #[allow(unused_variables)]
194        let result = self.device.set(u8::from(Oled64x48BrickletFunction::NewWindow), &payload).await?;
195        Ok(())
196    }
197
198    /// Clears the current content of the window as set by [`new_window`].
199    pub async fn clear_display(&mut self) -> Result<(), TinkerforgeError> {
200        let payload = [0; 0];
201
202        #[allow(unused_variables)]
203        let result = self.device.set(u8::from(Oled64x48BrickletFunction::ClearDisplay), &payload).await?;
204        Ok(())
205    }
206
207    /// Sets the configuration of the display.
208    ///
209    /// You can set a contrast value from 0 to 255 and you can invert the color
210    /// (black/white) of the display.
211    pub async fn set_display_configuration(&mut self, contrast: u8, invert: bool) -> Result<(), TinkerforgeError> {
212        let mut payload = [0; 2];
213        contrast.write_to_slice(&mut payload[0..1]);
214        invert.write_to_slice(&mut payload[1..2]);
215
216        #[allow(unused_variables)]
217        let result = self.device.set(u8::from(Oled64x48BrickletFunction::SetDisplayConfiguration), &payload).await?;
218        Ok(())
219    }
220
221    /// Returns the configuration as set by [`set_display_configuration`].
222    pub async fn get_display_configuration(&mut self) -> Result<DisplayConfiguration, TinkerforgeError> {
223        let payload = [0; 0];
224
225        #[allow(unused_variables)]
226        let result = self.device.get(u8::from(Oled64x48BrickletFunction::GetDisplayConfiguration), &payload).await?;
227        Ok(DisplayConfiguration::from_le_byte_slice(result.body()))
228    }
229
230    /// Writes text to a specific line with a specific position.
231    /// The text can have a maximum of 13 characters.
232    ///
233    /// For example: (1, 4, Hello) will write *Hello* in the middle of the
234    /// second line of the display.
235    ///
236    /// You can draw to the display with [`write`] and then add text to it
237    /// afterwards.
238    ///
239    /// The display uses a special 5x7 pixel charset. You can view the characters
240    /// of the charset in Brick Viewer.
241    ///
242    /// The font conforms to code page 437.
243    pub async fn write_line(&mut self, line: u8, position: u8, text: String) -> Result<(), TinkerforgeError> {
244        let mut payload = [0; 15];
245        line.write_to_slice(&mut payload[0..1]);
246        position.write_to_slice(&mut payload[1..2]);
247        text.try_write_to_slice(13, &mut payload)?;
248
249        #[allow(unused_variables)]
250        let result = self.device.set(u8::from(Oled64x48BrickletFunction::WriteLine), &payload).await?;
251        Ok(())
252    }
253
254    /// Returns the UID, the UID where the Bricklet is connected to,
255    /// the position, the hardware and firmware version as well as the
256    /// device identifier.
257    ///
258    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
259    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
260    /// position 'z'.
261    ///
262    /// The device identifier numbers can be found [here](device_identifier).
263    /// |device_identifier_constant|
264    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
265        let payload = [0; 0];
266
267        #[allow(unused_variables)]
268        let result = self.device.get(u8::from(Oled64x48BrickletFunction::GetIdentity), &payload).await?;
269        Ok(Identity::from_le_byte_slice(result.body()))
270    }
271}