tinkerforge_async/bindings/
lcd_20x4_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//! 20x4 character alphanumeric display with blue backlight.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/LCD20x4_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 Lcd20x4BrickletFunction {
24    WriteLine,
25    ClearDisplay,
26    BacklightOn,
27    BacklightOff,
28    IsBacklightOn,
29    SetConfig,
30    GetConfig,
31    IsButtonPressed,
32    SetCustomCharacter,
33    GetCustomCharacter,
34    SetDefaultText,
35    GetDefaultText,
36    SetDefaultTextCounter,
37    GetDefaultTextCounter,
38    GetIdentity,
39    CallbackButtonPressed,
40    CallbackButtonReleased,
41}
42impl From<Lcd20x4BrickletFunction> for u8 {
43    fn from(fun: Lcd20x4BrickletFunction) -> Self {
44        match fun {
45            Lcd20x4BrickletFunction::WriteLine => 1,
46            Lcd20x4BrickletFunction::ClearDisplay => 2,
47            Lcd20x4BrickletFunction::BacklightOn => 3,
48            Lcd20x4BrickletFunction::BacklightOff => 4,
49            Lcd20x4BrickletFunction::IsBacklightOn => 5,
50            Lcd20x4BrickletFunction::SetConfig => 6,
51            Lcd20x4BrickletFunction::GetConfig => 7,
52            Lcd20x4BrickletFunction::IsButtonPressed => 8,
53            Lcd20x4BrickletFunction::SetCustomCharacter => 11,
54            Lcd20x4BrickletFunction::GetCustomCharacter => 12,
55            Lcd20x4BrickletFunction::SetDefaultText => 13,
56            Lcd20x4BrickletFunction::GetDefaultText => 14,
57            Lcd20x4BrickletFunction::SetDefaultTextCounter => 15,
58            Lcd20x4BrickletFunction::GetDefaultTextCounter => 16,
59            Lcd20x4BrickletFunction::GetIdentity => 255,
60            Lcd20x4BrickletFunction::CallbackButtonPressed => 9,
61            Lcd20x4BrickletFunction::CallbackButtonReleased => 10,
62        }
63    }
64}
65
66#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
67pub struct Config {
68    pub cursor: bool,
69    pub blinking: bool,
70}
71impl FromByteSlice for Config {
72    fn bytes_expected() -> usize {
73        2
74    }
75    fn from_le_byte_slice(bytes: &[u8]) -> Config {
76        Config { cursor: <bool>::from_le_byte_slice(&bytes[0..1]), blinking: <bool>::from_le_byte_slice(&bytes[1..2]) }
77    }
78}
79
80#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
81pub struct Identity {
82    pub uid: String,
83    pub connected_uid: String,
84    pub position: char,
85    pub hardware_version: [u8; 3],
86    pub firmware_version: [u8; 3],
87    pub device_identifier: u16,
88}
89impl FromByteSlice for Identity {
90    fn bytes_expected() -> usize {
91        25
92    }
93    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
94        Identity {
95            uid: <String>::from_le_byte_slice(&bytes[0..8]),
96            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
97            position: <char>::from_le_byte_slice(&bytes[16..17]),
98            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
99            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
100            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
101        }
102    }
103}
104
105/// 20x4 character alphanumeric display with blue backlight
106#[derive(Clone)]
107pub struct Lcd20x4Bricklet {
108    device: Device,
109}
110impl Lcd20x4Bricklet {
111    pub const DEVICE_IDENTIFIER: u16 = 212;
112    pub const DEVICE_DISPLAY_NAME: &'static str = "LCD 20x4 Bricklet";
113    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
114    pub fn new(uid: Uid, connection: AsyncIpConnection) -> Lcd20x4Bricklet {
115        let mut result = Lcd20x4Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
116        result.device.response_expected[u8::from(Lcd20x4BrickletFunction::WriteLine) as usize] = ResponseExpectedFlag::False;
117        result.device.response_expected[u8::from(Lcd20x4BrickletFunction::ClearDisplay) as usize] = ResponseExpectedFlag::False;
118        result.device.response_expected[u8::from(Lcd20x4BrickletFunction::BacklightOn) as usize] = ResponseExpectedFlag::False;
119        result.device.response_expected[u8::from(Lcd20x4BrickletFunction::BacklightOff) as usize] = ResponseExpectedFlag::False;
120        result.device.response_expected[u8::from(Lcd20x4BrickletFunction::IsBacklightOn) as usize] = ResponseExpectedFlag::AlwaysTrue;
121        result.device.response_expected[u8::from(Lcd20x4BrickletFunction::SetConfig) as usize] = ResponseExpectedFlag::False;
122        result.device.response_expected[u8::from(Lcd20x4BrickletFunction::GetConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
123        result.device.response_expected[u8::from(Lcd20x4BrickletFunction::IsButtonPressed) as usize] = ResponseExpectedFlag::AlwaysTrue;
124        result.device.response_expected[u8::from(Lcd20x4BrickletFunction::SetCustomCharacter) as usize] = ResponseExpectedFlag::False;
125        result.device.response_expected[u8::from(Lcd20x4BrickletFunction::GetCustomCharacter) as usize] = ResponseExpectedFlag::AlwaysTrue;
126        result.device.response_expected[u8::from(Lcd20x4BrickletFunction::SetDefaultText) as usize] = ResponseExpectedFlag::False;
127        result.device.response_expected[u8::from(Lcd20x4BrickletFunction::GetDefaultText) as usize] = ResponseExpectedFlag::AlwaysTrue;
128        result.device.response_expected[u8::from(Lcd20x4BrickletFunction::SetDefaultTextCounter) as usize] = ResponseExpectedFlag::False;
129        result.device.response_expected[u8::from(Lcd20x4BrickletFunction::GetDefaultTextCounter) as usize] =
130            ResponseExpectedFlag::AlwaysTrue;
131        result.device.response_expected[u8::from(Lcd20x4BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
132        result
133    }
134
135    /// Returns the response expected flag for the function specified by the function ID parameter.
136    /// It is true if the function is expected to send a response, false otherwise.
137    ///
138    /// For getter functions this is enabled by default and cannot be disabled, because those
139    /// functions will always send a response. For callback configuration functions it is enabled
140    /// by default too, but can be disabled by [`set_response_expected`](crate::lcd_20x4_bricklet::Lcd20x4Bricklet::set_response_expected).
141    /// For setter functions it is disabled by default and can be enabled.
142    ///
143    /// Enabling the response expected flag for a setter function allows to detect timeouts
144    /// and other error conditions calls of this setter as well. The device will then send a response
145    /// for this purpose. If this flag is disabled for a setter function then no response is sent
146    /// and errors are silently ignored, because they cannot be detected.
147    ///
148    /// See [`set_response_expected`](crate::lcd_20x4_bricklet::Lcd20x4Bricklet::set_response_expected) for the list of function ID constants available for this function.
149    pub fn get_response_expected(&mut self, fun: Lcd20x4BrickletFunction) -> Result<bool, GetResponseExpectedError> {
150        self.device.get_response_expected(u8::from(fun))
151    }
152
153    /// Changes the response expected flag of the function specified by the function ID parameter.
154    /// This flag can only be changed for setter (default value: false) and callback configuration
155    /// functions (default value: true). For getter functions it is always enabled.
156    ///
157    /// Enabling the response expected flag for a setter function allows to detect timeouts and
158    /// other error conditions calls of this setter as well. The device will then send a response
159    /// for this purpose. If this flag is disabled for a setter function then no response is sent
160    /// and errors are silently ignored, because they cannot be detected.
161    pub fn set_response_expected(&mut self, fun: Lcd20x4BrickletFunction, response_expected: bool) -> Result<(), SetResponseExpectedError> {
162        self.device.set_response_expected(u8::from(fun), response_expected)
163    }
164
165    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
166    pub fn set_response_expected_all(&mut self, response_expected: bool) {
167        self.device.set_response_expected_all(response_expected)
168    }
169
170    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
171    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
172    pub fn get_api_version(&self) -> [u8; 3] {
173        self.device.api_version
174    }
175
176    /// This receiver is triggered when a button is pressed. The parameter is
177    /// the number of the button (0 to 2 or 0 to 3 since hardware version 1.2).
178    pub async fn get_button_pressed_callback_receiver(&mut self) -> impl Stream<Item = u8> {
179        self.device
180            .get_callback_receiver(u8::from(Lcd20x4BrickletFunction::CallbackButtonPressed))
181            .await
182            .map(|p| u8::from_le_byte_slice(p.body()))
183    }
184
185    /// This receiver is triggered when a button is released. The parameter is
186    /// the number of the button (0 to 2 or 0 to 3 since hardware version 1.2).
187    pub async fn get_button_released_callback_receiver(&mut self) -> impl Stream<Item = u8> {
188        self.device
189            .get_callback_receiver(u8::from(Lcd20x4BrickletFunction::CallbackButtonReleased))
190            .await
191            .map(|p| u8::from_le_byte_slice(p.body()))
192    }
193
194    /// Writes text to a specific line with a specific position.
195    /// The text can have a maximum of 20 characters.
196    ///
197    /// For example: (0, 7, Hello) will write *Hello* in the middle of the
198    /// first line of the display.
199    ///
200    /// The display uses a special charset that includes all ASCII characters except
201    /// backslash and tilde. The LCD charset also includes several other non-ASCII characters, see
202    /// the [charset specification](https://github.com/Tinkerforge/lcd-20x4-bricklet/raw/master/datasheets/standard_charset.pdf)__
203    /// for details. The Unicode example above shows how to specify non-ASCII characters
204    /// and how to translate from Unicode to the LCD charset.
205    pub async fn write_line(&mut self, line: u8, position: u8, text: String) -> Result<(), TinkerforgeError> {
206        let mut payload = [0; 22];
207        line.write_to_slice(&mut payload[0..1]);
208        position.write_to_slice(&mut payload[1..2]);
209        text.try_write_to_slice(20, &mut payload)?;
210
211        #[allow(unused_variables)]
212        let result = self.device.set(u8::from(Lcd20x4BrickletFunction::WriteLine), &payload).await?;
213        Ok(())
214    }
215
216    /// Deletes all characters from the display.
217    pub async fn clear_display(&mut self) -> Result<(), TinkerforgeError> {
218        let payload = [0; 0];
219
220        #[allow(unused_variables)]
221        let result = self.device.set(u8::from(Lcd20x4BrickletFunction::ClearDisplay), &payload).await?;
222        Ok(())
223    }
224
225    /// Turns the backlight on.
226    pub async fn backlight_on(&mut self) -> Result<(), TinkerforgeError> {
227        let payload = [0; 0];
228
229        #[allow(unused_variables)]
230        let result = self.device.set(u8::from(Lcd20x4BrickletFunction::BacklightOn), &payload).await?;
231        Ok(())
232    }
233
234    /// Turns the backlight off.
235    pub async fn backlight_off(&mut self) -> Result<(), TinkerforgeError> {
236        let payload = [0; 0];
237
238        #[allow(unused_variables)]
239        let result = self.device.set(u8::from(Lcd20x4BrickletFunction::BacklightOff), &payload).await?;
240        Ok(())
241    }
242
243    /// Returns *true* if the backlight is on and *false* otherwise.
244    pub async fn is_backlight_on(&mut self) -> Result<bool, TinkerforgeError> {
245        let payload = [0; 0];
246
247        #[allow(unused_variables)]
248        let result = self.device.get(u8::from(Lcd20x4BrickletFunction::IsBacklightOn), &payload).await?;
249        Ok(bool::from_le_byte_slice(result.body()))
250    }
251
252    /// Configures if the cursor (shown as _) should be visible and if it
253    /// should be blinking (shown as a blinking block). The cursor position
254    /// is one character behind the the last text written with
255    /// [`write_line`].
256    pub async fn set_config(&mut self, cursor: bool, blinking: bool) -> Result<(), TinkerforgeError> {
257        let mut payload = [0; 2];
258        cursor.write_to_slice(&mut payload[0..1]);
259        blinking.write_to_slice(&mut payload[1..2]);
260
261        #[allow(unused_variables)]
262        let result = self.device.set(u8::from(Lcd20x4BrickletFunction::SetConfig), &payload).await?;
263        Ok(())
264    }
265
266    /// Returns the configuration as set by [`set_config`].
267    pub async fn get_config(&mut self) -> Result<Config, TinkerforgeError> {
268        let payload = [0; 0];
269
270        #[allow(unused_variables)]
271        let result = self.device.get(u8::from(Lcd20x4BrickletFunction::GetConfig), &payload).await?;
272        Ok(Config::from_le_byte_slice(result.body()))
273    }
274
275    /// Returns *true* if the button (0 to 2 or 0 to 3 since hardware version 1.2)
276    /// is pressed.
277    ///
278    /// If you want to react on button presses and releases it is recommended to use
279    /// the [`get_button_pressed_callback_receiver`] and [`get_button_released_callback_receiver`] receivers.
280    pub async fn is_button_pressed(&mut self, button: u8) -> Result<bool, TinkerforgeError> {
281        let mut payload = [0; 1];
282        button.write_to_slice(&mut payload[0..1]);
283
284        #[allow(unused_variables)]
285        let result = self.device.get(u8::from(Lcd20x4BrickletFunction::IsButtonPressed), &payload).await?;
286        Ok(bool::from_le_byte_slice(result.body()))
287    }
288
289    /// The LCD 20x4 Bricklet can store up to 8 custom characters. The characters
290    /// consist of 5x8 pixels and can be addressed with the index 0-7. To describe
291    /// the pixels, the first 5 bits of 8 bytes are used. For example, to make
292    /// a custom character H, you should transfer the following:
293    ///
294    /// * ``character[0] = 0b00010001`` (decimal value 17)
295    /// * ``character[1] = 0b00010001`` (decimal value 17)
296    /// * ``character[2] = 0b00010001`` (decimal value 17)
297    /// * ``character[3] = 0b00011111`` (decimal value 31)
298    /// * ``character[4] = 0b00010001`` (decimal value 17)
299    /// * ``character[5] = 0b00010001`` (decimal value 17)
300    /// * ``character[6] = 0b00010001`` (decimal value 17)
301    /// * ``character[7] = 0b00000000`` (decimal value 0)
302    ///
303    /// The characters can later be written with [`write_line`] by using the
304    /// characters with the byte representation 8 (\\x08 or \\u0008) to 15
305    /// (\\x0F or \\u000F).
306    ///
307    /// You can play around with the custom characters in Brick Viewer version
308    /// since 2.0.1.
309    ///
310    /// Custom characters are stored by the LCD in RAM, so they have to be set
311    /// after each startup.
312    ///
313    ///
314    /// .. versionadded:: 2.0.1$nbsp;(Plugin)
315    pub async fn set_custom_character(&mut self, index: u8, character: &[u8; 8]) -> Result<(), TinkerforgeError> {
316        let mut payload = [0; 9];
317        index.write_to_slice(&mut payload[0..1]);
318        character.write_to_slice(&mut payload[1..9]);
319
320        #[allow(unused_variables)]
321        let result = self.device.set(u8::from(Lcd20x4BrickletFunction::SetCustomCharacter), &payload).await?;
322        Ok(())
323    }
324
325    /// Returns the custom character for a given index, as set with
326    /// [`set_custom_character`].
327    ///
328    ///
329    /// .. versionadded:: 2.0.1$nbsp;(Plugin)
330    pub async fn get_custom_character(&mut self, index: u8) -> Result<Box<[u8; 8]>, TinkerforgeError> {
331        let mut payload = [0; 1];
332        index.write_to_slice(&mut payload[0..1]);
333
334        #[allow(unused_variables)]
335        let result = self.device.get(u8::from(Lcd20x4BrickletFunction::GetCustomCharacter), &payload).await?;
336        Ok(Box::<[u8; 8]>::from_le_byte_slice(result.body()))
337    }
338
339    /// Sets the default text for lines 0-3. The max number of characters
340    /// per line is 20.
341    ///
342    /// The default text is shown on the LCD, if the default text counter
343    /// expires, see [`set_default_text_counter`].
344    ///
345    ///
346    /// .. versionadded:: 2.0.2$nbsp;(Plugin)
347    pub async fn set_default_text(&mut self, line: u8, text: String) -> Result<(), TinkerforgeError> {
348        let mut payload = [0; 21];
349        line.write_to_slice(&mut payload[0..1]);
350        text.try_write_to_slice(20, &mut payload)?;
351
352        #[allow(unused_variables)]
353        let result = self.device.set(u8::from(Lcd20x4BrickletFunction::SetDefaultText), &payload).await?;
354        Ok(())
355    }
356
357    /// Returns the default text for a given line (0-3) as set by
358    /// [`set_default_text`].
359    ///
360    ///
361    /// .. versionadded:: 2.0.2$nbsp;(Plugin)
362    pub async fn get_default_text(&mut self, line: u8) -> Result<String, TinkerforgeError> {
363        let mut payload = [0; 1];
364        line.write_to_slice(&mut payload[0..1]);
365
366        #[allow(unused_variables)]
367        let result = self.device.get(u8::from(Lcd20x4BrickletFunction::GetDefaultText), &payload).await?;
368        Ok(String::from_le_byte_slice(result.body()))
369    }
370
371    /// Sets the default text counter. This counter is decremented each
372    /// ms by the LCD firmware. If the counter reaches 0, the default text
373    /// (see [`set_default_text`]) is shown on the LCD.
374    ///
375    /// This functionality can be used to show a default text if the controlling
376    /// program crashes or the connection is interrupted.
377    ///
378    /// A possible approach is to call [`set_default_text_counter`] every
379    /// minute with the parameter 1000*60*2 (2 minutes). In this case the
380    /// default text will be shown no later than 2 minutes after the
381    /// controlling program crashes.
382    ///
383    /// A negative counter turns the default text functionality off.
384    ///
385    ///
386    /// .. versionadded:: 2.0.2$nbsp;(Plugin)
387    pub async fn set_default_text_counter(&mut self, counter: i32) -> Result<(), TinkerforgeError> {
388        let mut payload = [0; 4];
389        counter.write_to_slice(&mut payload[0..4]);
390
391        #[allow(unused_variables)]
392        let result = self.device.set(u8::from(Lcd20x4BrickletFunction::SetDefaultTextCounter), &payload).await?;
393        Ok(())
394    }
395
396    /// Returns the current value of the default text counter.
397    ///
398    ///
399    /// .. versionadded:: 2.0.2$nbsp;(Plugin)
400    pub async fn get_default_text_counter(&mut self) -> Result<i32, TinkerforgeError> {
401        let payload = [0; 0];
402
403        #[allow(unused_variables)]
404        let result = self.device.get(u8::from(Lcd20x4BrickletFunction::GetDefaultTextCounter), &payload).await?;
405        Ok(i32::from_le_byte_slice(result.body()))
406    }
407
408    /// Returns the UID, the UID where the Bricklet is connected to,
409    /// the position, the hardware and firmware version as well as the
410    /// device identifier.
411    ///
412    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
413    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
414    /// position 'z'.
415    ///
416    /// The device identifier numbers can be found [here](device_identifier).
417    /// |device_identifier_constant|
418    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
419        let payload = [0; 0];
420
421        #[allow(unused_variables)]
422        let result = self.device.get(u8::from(Lcd20x4BrickletFunction::GetIdentity), &payload).await?;
423        Ok(Identity::from_le_byte_slice(result.body()))
424    }
425}