tinkerforge_async/bindings/
lcd_20x4_bricklet.rs1#[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#[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 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 pub fn get_response_expected(&mut self, fun: Lcd20x4BrickletFunction) -> Result<bool, GetResponseExpectedError> {
150 self.device.get_response_expected(u8::from(fun))
151 }
152
153 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 pub fn set_response_expected_all(&mut self, response_expected: bool) {
167 self.device.set_response_expected_all(response_expected)
168 }
169
170 pub fn get_api_version(&self) -> [u8; 3] {
173 self.device.api_version
174 }
175
176 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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}