tinkerforge_async/bindings/
segment_display_4x7_bricklet.rs1#[allow(unused_imports)]
15use crate::{
16 base58::Uid, byte_converter::*, device::*, error::TinkerforgeError, ip_connection::async_io::AsyncIpConnection,
17 low_level_traits::LowLevelRead,
18};
19#[allow(unused_imports)]
20use futures_core::Stream;
21#[allow(unused_imports)]
22use tokio_stream::StreamExt;
23pub enum SegmentDisplay4x7BrickletFunction {
24 SetSegments,
25 GetSegments,
26 StartCounter,
27 GetCounterValue,
28 GetIdentity,
29 CallbackCounterFinished,
30}
31impl From<SegmentDisplay4x7BrickletFunction> for u8 {
32 fn from(fun: SegmentDisplay4x7BrickletFunction) -> Self {
33 match fun {
34 SegmentDisplay4x7BrickletFunction::SetSegments => 1,
35 SegmentDisplay4x7BrickletFunction::GetSegments => 2,
36 SegmentDisplay4x7BrickletFunction::StartCounter => 3,
37 SegmentDisplay4x7BrickletFunction::GetCounterValue => 4,
38 SegmentDisplay4x7BrickletFunction::GetIdentity => 255,
39 SegmentDisplay4x7BrickletFunction::CallbackCounterFinished => 5,
40 }
41 }
42}
43
44#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
45pub struct Segments {
46 pub segments: [u8; 4],
47 pub brightness: u8,
48 pub colon: bool,
49}
50impl FromByteSlice for Segments {
51 fn bytes_expected() -> usize {
52 6
53 }
54 fn from_le_byte_slice(bytes: &[u8]) -> Segments {
55 Segments {
56 segments: <[u8; 4]>::from_le_byte_slice(&bytes[0..4]),
57 brightness: <u8>::from_le_byte_slice(&bytes[4..5]),
58 colon: <bool>::from_le_byte_slice(&bytes[5..6]),
59 }
60 }
61}
62
63#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
64pub struct Identity {
65 pub uid: String,
66 pub connected_uid: String,
67 pub position: char,
68 pub hardware_version: [u8; 3],
69 pub firmware_version: [u8; 3],
70 pub device_identifier: u16,
71}
72impl FromByteSlice for Identity {
73 fn bytes_expected() -> usize {
74 25
75 }
76 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
77 Identity {
78 uid: <String>::from_le_byte_slice(&bytes[0..8]),
79 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
80 position: <char>::from_le_byte_slice(&bytes[16..17]),
81 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
82 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
83 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
84 }
85 }
86}
87
88#[derive(Clone)]
90pub struct SegmentDisplay4x7Bricklet {
91 device: Device,
92}
93impl SegmentDisplay4x7Bricklet {
94 pub const DEVICE_IDENTIFIER: u16 = 237;
95 pub const DEVICE_DISPLAY_NAME: &'static str = "Segment Display 4x7 Bricklet";
96 pub fn new(uid: Uid, connection: AsyncIpConnection) -> SegmentDisplay4x7Bricklet {
98 let mut result = SegmentDisplay4x7Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
99 result.device.response_expected[u8::from(SegmentDisplay4x7BrickletFunction::SetSegments) as usize] = ResponseExpectedFlag::False;
100 result.device.response_expected[u8::from(SegmentDisplay4x7BrickletFunction::GetSegments) as usize] =
101 ResponseExpectedFlag::AlwaysTrue;
102 result.device.response_expected[u8::from(SegmentDisplay4x7BrickletFunction::StartCounter) as usize] = ResponseExpectedFlag::False;
103 result.device.response_expected[u8::from(SegmentDisplay4x7BrickletFunction::GetCounterValue) as usize] =
104 ResponseExpectedFlag::AlwaysTrue;
105 result.device.response_expected[u8::from(SegmentDisplay4x7BrickletFunction::GetIdentity) as usize] =
106 ResponseExpectedFlag::AlwaysTrue;
107 result
108 }
109
110 pub fn get_response_expected(&mut self, fun: SegmentDisplay4x7BrickletFunction) -> Result<bool, GetResponseExpectedError> {
125 self.device.get_response_expected(u8::from(fun))
126 }
127
128 pub fn set_response_expected(
137 &mut self,
138 fun: SegmentDisplay4x7BrickletFunction,
139 response_expected: bool,
140 ) -> Result<(), SetResponseExpectedError> {
141 self.device.set_response_expected(u8::from(fun), response_expected)
142 }
143
144 pub fn set_response_expected_all(&mut self, response_expected: bool) {
146 self.device.set_response_expected_all(response_expected)
147 }
148
149 pub fn get_api_version(&self) -> [u8; 3] {
152 self.device.api_version
153 }
154
155 pub async fn get_counter_finished_callback_receiver(&mut self) -> impl Stream<Item = ()> {
160 self.device.get_callback_receiver(u8::from(SegmentDisplay4x7BrickletFunction::CallbackCounterFinished)).await.map(|_p| ())
161 }
162
163 pub async fn set_segments(&mut self, segments: &[u8; 4], brightness: u8, colon: bool) -> Result<(), TinkerforgeError> {
177 let mut payload = [0; 6];
178 segments.write_to_slice(&mut payload[0..4]);
179 brightness.write_to_slice(&mut payload[4..5]);
180 colon.write_to_slice(&mut payload[5..6]);
181
182 #[allow(unused_variables)]
183 let result = self.device.set(u8::from(SegmentDisplay4x7BrickletFunction::SetSegments), &payload).await?;
184 Ok(())
185 }
186
187 pub async fn get_segments(&mut self) -> Result<Segments, TinkerforgeError> {
190 let payload = [0; 0];
191
192 #[allow(unused_variables)]
193 let result = self.device.get(u8::from(SegmentDisplay4x7BrickletFunction::GetSegments), &payload).await?;
194 Ok(Segments::from_le_byte_slice(result.body()))
195 }
196
197 pub async fn start_counter(&mut self, value_from: i16, value_to: i16, increment: i16, length: u32) -> Result<(), TinkerforgeError> {
209 let mut payload = [0; 10];
210 value_from.write_to_slice(&mut payload[0..2]);
211 value_to.write_to_slice(&mut payload[2..4]);
212 increment.write_to_slice(&mut payload[4..6]);
213 length.write_to_slice(&mut payload[6..10]);
214
215 #[allow(unused_variables)]
216 let result = self.device.set(u8::from(SegmentDisplay4x7BrickletFunction::StartCounter), &payload).await?;
217 Ok(())
218 }
219
220 pub async fn get_counter_value(&mut self) -> Result<u16, TinkerforgeError> {
224 let payload = [0; 0];
225
226 #[allow(unused_variables)]
227 let result = self.device.get(u8::from(SegmentDisplay4x7BrickletFunction::GetCounterValue), &payload).await?;
228 Ok(u16::from_le_byte_slice(result.body()))
229 }
230
231 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
242 let payload = [0; 0];
243
244 #[allow(unused_variables)]
245 let result = self.device.get(u8::from(SegmentDisplay4x7BrickletFunction::GetIdentity), &payload).await?;
246 Ok(Identity::from_le_byte_slice(result.body()))
247 }
248}