tinkerforge/bindings/
segment_display_4x7_bricklet.rs

1/* ***********************************************************
2 * This file was automatically generated on 2024-02-27.      *
3 *                                                           *
4 * Rust Bindings Version 2.0.21                              *
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).
14use crate::{
15    byte_converter::*, converting_callback_receiver::ConvertingCallbackReceiver, converting_receiver::ConvertingReceiver, device::*,
16    ip_connection::GetRequestSender,
17};
18pub enum SegmentDisplay4x7BrickletFunction {
19    SetSegments,
20    GetSegments,
21    StartCounter,
22    GetCounterValue,
23    GetIdentity,
24    CallbackCounterFinished,
25}
26impl From<SegmentDisplay4x7BrickletFunction> for u8 {
27    fn from(fun: SegmentDisplay4x7BrickletFunction) -> Self {
28        match fun {
29            SegmentDisplay4x7BrickletFunction::SetSegments => 1,
30            SegmentDisplay4x7BrickletFunction::GetSegments => 2,
31            SegmentDisplay4x7BrickletFunction::StartCounter => 3,
32            SegmentDisplay4x7BrickletFunction::GetCounterValue => 4,
33            SegmentDisplay4x7BrickletFunction::GetIdentity => 255,
34            SegmentDisplay4x7BrickletFunction::CallbackCounterFinished => 5,
35        }
36    }
37}
38
39#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
40pub struct Segments {
41    pub segments: [u8; 4],
42    pub brightness: u8,
43    pub colon: bool,
44}
45impl FromByteSlice for Segments {
46    fn bytes_expected() -> usize { 6 }
47    fn from_le_byte_slice(bytes: &[u8]) -> Segments {
48        Segments {
49            segments: <[u8; 4]>::from_le_byte_slice(&bytes[0..4]),
50            brightness: <u8>::from_le_byte_slice(&bytes[4..5]),
51            colon: <bool>::from_le_byte_slice(&bytes[5..6]),
52        }
53    }
54}
55
56#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
57pub struct Identity {
58    pub uid: String,
59    pub connected_uid: String,
60    pub position: char,
61    pub hardware_version: [u8; 3],
62    pub firmware_version: [u8; 3],
63    pub device_identifier: u16,
64}
65impl FromByteSlice for Identity {
66    fn bytes_expected() -> usize { 25 }
67    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
68        Identity {
69            uid: <String>::from_le_byte_slice(&bytes[0..8]),
70            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
71            position: <char>::from_le_byte_slice(&bytes[16..17]),
72            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
73            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
74            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
75        }
76    }
77}
78
79/// Four 7-segment displays with switchable colon
80#[derive(Clone)]
81pub struct SegmentDisplay4x7Bricklet {
82    device: Device,
83}
84impl SegmentDisplay4x7Bricklet {
85    pub const DEVICE_IDENTIFIER: u16 = 237;
86    pub const DEVICE_DISPLAY_NAME: &'static str = "Segment Display 4x7 Bricklet";
87    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
88    pub fn new<T: GetRequestSender>(uid: &str, req_sender: T) -> SegmentDisplay4x7Bricklet {
89        let mut result = SegmentDisplay4x7Bricklet { device: Device::new([2, 0, 0], uid, req_sender, 0) };
90        result.device.response_expected[u8::from(SegmentDisplay4x7BrickletFunction::SetSegments) as usize] = ResponseExpectedFlag::False;
91        result.device.response_expected[u8::from(SegmentDisplay4x7BrickletFunction::GetSegments) as usize] =
92            ResponseExpectedFlag::AlwaysTrue;
93        result.device.response_expected[u8::from(SegmentDisplay4x7BrickletFunction::StartCounter) as usize] = ResponseExpectedFlag::False;
94        result.device.response_expected[u8::from(SegmentDisplay4x7BrickletFunction::GetCounterValue) as usize] =
95            ResponseExpectedFlag::AlwaysTrue;
96        result.device.response_expected[u8::from(SegmentDisplay4x7BrickletFunction::GetIdentity) as usize] =
97            ResponseExpectedFlag::AlwaysTrue;
98        result
99    }
100
101    /// Returns the response expected flag for the function specified by the function ID parameter.
102    /// It is true if the function is expected to send a response, false otherwise.
103    ///
104    /// For getter functions this is enabled by default and cannot be disabled, because those
105    /// functions will always send a response. For callback configuration functions it is enabled
106    /// by default too, but can be disabled by [`set_response_expected`](crate::segment_display_4x7_bricklet::SegmentDisplay4x7Bricklet::set_response_expected).
107    /// For setter functions it is disabled by default and can be enabled.
108    ///
109    /// Enabling the response expected flag for a setter function allows to detect timeouts
110    /// and other error conditions calls of this setter as well. The device will then send a response
111    /// for this purpose. If this flag is disabled for a setter function then no response is sent
112    /// and errors are silently ignored, because they cannot be detected.
113    ///
114    /// See [`set_response_expected`](crate::segment_display_4x7_bricklet::SegmentDisplay4x7Bricklet::set_response_expected) for the list of function ID constants available for this function.
115    pub fn get_response_expected(&mut self, fun: SegmentDisplay4x7BrickletFunction) -> Result<bool, GetResponseExpectedError> {
116        self.device.get_response_expected(u8::from(fun))
117    }
118
119    /// Changes the response expected flag of the function specified by the function ID parameter.
120    /// This flag can only be changed for setter (default value: false) and callback configuration
121    /// functions (default value: true). For getter functions it is always enabled.
122    ///
123    /// Enabling the response expected flag for a setter function allows to detect timeouts and
124    /// other error conditions calls of this setter as well. The device will then send a response
125    /// for this purpose. If this flag is disabled for a setter function then no response is sent
126    /// and errors are silently ignored, because they cannot be detected.
127    pub fn set_response_expected(
128        &mut self,
129        fun: SegmentDisplay4x7BrickletFunction,
130        response_expected: bool,
131    ) -> Result<(), SetResponseExpectedError> {
132        self.device.set_response_expected(u8::from(fun), response_expected)
133    }
134
135    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
136    pub fn set_response_expected_all(&mut self, response_expected: bool) { self.device.set_response_expected_all(response_expected) }
137
138    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
139    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
140    pub fn get_api_version(&self) -> [u8; 3] { self.device.api_version }
141
142    /// This receiver is triggered when the counter (see [`start_counter`]) is
143    /// finished.
144    ///
145    /// [`start_counter`]: #method.start_counter
146    pub fn get_counter_finished_callback_receiver(&self) -> ConvertingCallbackReceiver<()> {
147        self.device.get_callback_receiver(u8::from(SegmentDisplay4x7BrickletFunction::CallbackCounterFinished))
148    }
149
150    /// The 7-segment display can be set with bitmaps. Every bit controls one
151    /// segment:
152    ///
153    /// .. image:: /Images/Bricklets/bricklet_segment_display_4x7_bit_order.png
154    ///    :scale: 100 %
155    ///    :alt: Bit order of one segment
156    ///    :align: center
157    ///
158    /// For example to set a 5 you would want to activate segments 0, 2, 3, 5 and 6.
159    /// This is represented by the number 0b01101101 = 0x6d = 109.
160    ///
161    /// The brightness can be set between 0 (dark) and 7 (bright). The colon
162    /// parameter turns the colon of the display on or off.
163    pub fn set_segments(&self, segments: [u8; 4], brightness: u8, colon: bool) -> ConvertingReceiver<()> {
164        let mut payload = vec![0; 6];
165        payload[0..4].copy_from_slice(&<[u8; 4]>::to_le_byte_vec(segments));
166        payload[4..5].copy_from_slice(&<u8>::to_le_byte_vec(brightness));
167        payload[5..6].copy_from_slice(&<bool>::to_le_byte_vec(colon));
168
169        self.device.set(u8::from(SegmentDisplay4x7BrickletFunction::SetSegments), payload)
170    }
171
172    /// Returns the segment, brightness and color data as set by
173    /// [`set_segments`].
174    pub fn get_segments(&self) -> ConvertingReceiver<Segments> {
175        let payload = vec![0; 0];
176
177        self.device.get(u8::from(SegmentDisplay4x7BrickletFunction::GetSegments), payload)
178    }
179
180    /// Starts a counter with the *from* value that counts to the *to*
181    /// value with the each step incremented by *increment*.
182    /// *length* is the pause between each increment.
183    ///
184    /// Example: If you set *from* to 0, *to* to 100, *increment* to 1 and
185    /// *length* to 1000, a counter that goes from 0 to 100 with one second
186    /// pause between each increment will be started.
187    ///
188    /// Using a negative increment allows to count backwards.
189    ///
190    /// You can stop the counter at every time by calling [`set_segments`].
191    pub fn start_counter(&self, value_from: i16, value_to: i16, increment: i16, length: u32) -> ConvertingReceiver<()> {
192        let mut payload = vec![0; 10];
193        payload[0..2].copy_from_slice(&<i16>::to_le_byte_vec(value_from));
194        payload[2..4].copy_from_slice(&<i16>::to_le_byte_vec(value_to));
195        payload[4..6].copy_from_slice(&<i16>::to_le_byte_vec(increment));
196        payload[6..10].copy_from_slice(&<u32>::to_le_byte_vec(length));
197
198        self.device.set(u8::from(SegmentDisplay4x7BrickletFunction::StartCounter), payload)
199    }
200
201    /// Returns the counter value that is currently shown on the display.
202    ///
203    /// If there is no counter running a 0 will be returned.
204    pub fn get_counter_value(&self) -> ConvertingReceiver<u16> {
205        let payload = vec![0; 0];
206
207        self.device.get(u8::from(SegmentDisplay4x7BrickletFunction::GetCounterValue), payload)
208    }
209
210    /// Returns the UID, the UID where the Bricklet is connected to,
211    /// the position, the hardware and firmware version as well as the
212    /// device identifier.
213    ///
214    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
215    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
216    /// position 'z'.
217    ///
218    /// The device identifier numbers can be found [here](device_identifier).
219    /// |device_identifier_constant|
220    pub fn get_identity(&self) -> ConvertingReceiver<Identity> {
221        let payload = vec![0; 0];
222
223        self.device.get(u8::from(SegmentDisplay4x7BrickletFunction::GetIdentity), payload)
224    }
225}