tinkerforge_async/bindings/
oled_128x64_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 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#[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 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 pub fn get_response_expected(&mut self, fun: Oled128x64BrickletFunction) -> Result<bool, GetResponseExpectedError> {
123 self.device.get_response_expected(u8::from(fun))
124 }
125
126 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 pub fn set_response_expected_all(&mut self, response_expected: bool) {
144 self.device.set_response_expected_all(response_expected)
145 }
146
147 pub fn get_api_version(&self) -> [u8; 3] {
150 self.device.api_version
151 }
152
153 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 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 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 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 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 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 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}