tinkerforge_async/bindings/
segment_display_4x7_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//! Four 7-segment displays with switchable colon.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/SegmentDisplay4x7_Bricklet_Rust.html).
14#[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/// Four 7-segment displays with switchable colon
89#[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    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
97    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    /// Returns the response expected flag for the function specified by the function ID parameter.
111    /// It is true if the function is expected to send a response, false otherwise.
112    ///
113    /// For getter functions this is enabled by default and cannot be disabled, because those
114    /// functions will always send a response. For callback configuration functions it is enabled
115    /// by default too, but can be disabled by [`set_response_expected`](crate::segment_display_4x7_bricklet::SegmentDisplay4x7Bricklet::set_response_expected).
116    /// For setter functions it is disabled by default and can be enabled.
117    ///
118    /// Enabling the response expected flag for a setter function allows to detect timeouts
119    /// and other error conditions calls of this setter as well. The device will then send a response
120    /// for this purpose. If this flag is disabled for a setter function then no response is sent
121    /// and errors are silently ignored, because they cannot be detected.
122    ///
123    /// See [`set_response_expected`](crate::segment_display_4x7_bricklet::SegmentDisplay4x7Bricklet::set_response_expected) for the list of function ID constants available for this function.
124    pub fn get_response_expected(&mut self, fun: SegmentDisplay4x7BrickletFunction) -> Result<bool, GetResponseExpectedError> {
125        self.device.get_response_expected(u8::from(fun))
126    }
127
128    /// Changes the response expected flag of the function specified by the function ID parameter.
129    /// This flag can only be changed for setter (default value: false) and callback configuration
130    /// functions (default value: true). For getter functions it is always enabled.
131    ///
132    /// Enabling the response expected flag for a setter function allows to detect timeouts and
133    /// other error conditions calls of this setter as well. The device will then send a response
134    /// for this purpose. If this flag is disabled for a setter function then no response is sent
135    /// and errors are silently ignored, because they cannot be detected.
136    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    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
145    pub fn set_response_expected_all(&mut self, response_expected: bool) {
146        self.device.set_response_expected_all(response_expected)
147    }
148
149    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
150    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
151    pub fn get_api_version(&self) -> [u8; 3] {
152        self.device.api_version
153    }
154
155    /// This receiver is triggered when the counter (see [`start_counter`]) is
156    /// finished.
157    ///
158    /// [`start_counter`]: #method.start_counter
159    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    /// The 7-segment display can be set with bitmaps. Every bit controls one
164    /// segment:
165    ///
166    /// .. image:: /Images/Bricklets/bricklet_segment_display_4x7_bit_order.png
167    ///    :scale: 100 %
168    ///    :alt: Bit order of one segment
169    ///    :align: center
170    ///
171    /// For example to set a 5 you would want to activate segments 0, 2, 3, 5 and 6.
172    /// This is represented by the number 0b01101101 = 0x6d = 109.
173    ///
174    /// The brightness can be set between 0 (dark) and 7 (bright). The colon
175    /// parameter turns the colon of the display on or off.
176    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    /// Returns the segment, brightness and color data as set by
188    /// [`set_segments`].
189    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    /// Starts a counter with the *from* value that counts to the *to*
198    /// value with the each step incremented by *increment*.
199    /// *length* is the pause between each increment.
200    ///
201    /// Example: If you set *from* to 0, *to* to 100, *increment* to 1 and
202    /// *length* to 1000, a counter that goes from 0 to 100 with one second
203    /// pause between each increment will be started.
204    ///
205    /// Using a negative increment allows to count backwards.
206    ///
207    /// You can stop the counter at every time by calling [`set_segments`].
208    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    /// Returns the counter value that is currently shown on the display.
221    ///
222    /// If there is no counter running a 0 will be returned.
223    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    /// Returns the UID, the UID where the Bricklet is connected to,
232    /// the position, the hardware and firmware version as well as the
233    /// device identifier.
234    ///
235    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
236    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
237    /// position 'z'.
238    ///
239    /// The device identifier numbers can be found [here](device_identifier).
240    /// |device_identifier_constant|
241    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}