tinkerforge_async/bindings/
piezo_buzzer_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//! Creates 1kHz beep.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/PiezoBuzzer_Bricklet_Rust.html).
14#[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 PiezoBuzzerBrickletFunction {
24    Beep,
25    MorseCode,
26    GetIdentity,
27    CallbackBeepFinished,
28    CallbackMorseCodeFinished,
29}
30impl From<PiezoBuzzerBrickletFunction> for u8 {
31    fn from(fun: PiezoBuzzerBrickletFunction) -> Self {
32        match fun {
33            PiezoBuzzerBrickletFunction::Beep => 1,
34            PiezoBuzzerBrickletFunction::MorseCode => 2,
35            PiezoBuzzerBrickletFunction::GetIdentity => 255,
36            PiezoBuzzerBrickletFunction::CallbackBeepFinished => 3,
37            PiezoBuzzerBrickletFunction::CallbackMorseCodeFinished => 4,
38        }
39    }
40}
41
42#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
43pub struct Identity {
44    pub uid: String,
45    pub connected_uid: String,
46    pub position: char,
47    pub hardware_version: [u8; 3],
48    pub firmware_version: [u8; 3],
49    pub device_identifier: u16,
50}
51impl FromByteSlice for Identity {
52    fn bytes_expected() -> usize {
53        25
54    }
55    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
56        Identity {
57            uid: <String>::from_le_byte_slice(&bytes[0..8]),
58            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
59            position: <char>::from_le_byte_slice(&bytes[16..17]),
60            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
61            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
62            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
63        }
64    }
65}
66
67/// Creates 1kHz beep
68#[derive(Clone)]
69pub struct PiezoBuzzerBricklet {
70    device: Device,
71}
72impl PiezoBuzzerBricklet {
73    pub const DEVICE_IDENTIFIER: u16 = 214;
74    pub const DEVICE_DISPLAY_NAME: &'static str = "Piezo Buzzer Bricklet";
75    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
76    pub fn new(uid: Uid, connection: AsyncIpConnection) -> PiezoBuzzerBricklet {
77        let mut result = PiezoBuzzerBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
78        result.device.response_expected[u8::from(PiezoBuzzerBrickletFunction::Beep) as usize] = ResponseExpectedFlag::False;
79        result.device.response_expected[u8::from(PiezoBuzzerBrickletFunction::MorseCode) as usize] = ResponseExpectedFlag::False;
80        result.device.response_expected[u8::from(PiezoBuzzerBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
81        result
82    }
83
84    /// Returns the response expected flag for the function specified by the function ID parameter.
85    /// It is true if the function is expected to send a response, false otherwise.
86    ///
87    /// For getter functions this is enabled by default and cannot be disabled, because those
88    /// functions will always send a response. For callback configuration functions it is enabled
89    /// by default too, but can be disabled by [`set_response_expected`](crate::piezo_buzzer_bricklet::PiezoBuzzerBricklet::set_response_expected).
90    /// For setter functions it is disabled by default and can be enabled.
91    ///
92    /// Enabling the response expected flag for a setter function allows to detect timeouts
93    /// and other error conditions calls of this setter as well. The device will then send a response
94    /// for this purpose. If this flag is disabled for a setter function then no response is sent
95    /// and errors are silently ignored, because they cannot be detected.
96    ///
97    /// See [`set_response_expected`](crate::piezo_buzzer_bricklet::PiezoBuzzerBricklet::set_response_expected) for the list of function ID constants available for this function.
98    pub fn get_response_expected(&mut self, fun: PiezoBuzzerBrickletFunction) -> Result<bool, GetResponseExpectedError> {
99        self.device.get_response_expected(u8::from(fun))
100    }
101
102    /// Changes the response expected flag of the function specified by the function ID parameter.
103    /// This flag can only be changed for setter (default value: false) and callback configuration
104    /// functions (default value: true). For getter functions it is always enabled.
105    ///
106    /// Enabling the response expected flag for a setter function allows to detect timeouts and
107    /// other error conditions calls of this setter as well. The device will then send a response
108    /// for this purpose. If this flag is disabled for a setter function then no response is sent
109    /// and errors are silently ignored, because they cannot be detected.
110    pub fn set_response_expected(
111        &mut self,
112        fun: PiezoBuzzerBrickletFunction,
113        response_expected: bool,
114    ) -> Result<(), SetResponseExpectedError> {
115        self.device.set_response_expected(u8::from(fun), response_expected)
116    }
117
118    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
119    pub fn set_response_expected_all(&mut self, response_expected: bool) {
120        self.device.set_response_expected_all(response_expected)
121    }
122
123    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
124    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
125    pub fn get_api_version(&self) -> [u8; 3] {
126        self.device.api_version
127    }
128
129    /// This receiver is triggered if a beep set by [`beep`] is finished
130    ///
131    /// [`beep`]: #method.beep
132    pub async fn get_beep_finished_callback_receiver(&mut self) -> impl Stream<Item = ()> {
133        self.device.get_callback_receiver(u8::from(PiezoBuzzerBrickletFunction::CallbackBeepFinished)).await.map(|_p| ())
134    }
135
136    /// This receiver is triggered if the playback of the morse code set by
137    /// [`morse_code`] is finished.
138    pub async fn get_morse_code_finished_callback_receiver(&mut self) -> impl Stream<Item = ()> {
139        self.device.get_callback_receiver(u8::from(PiezoBuzzerBrickletFunction::CallbackMorseCodeFinished)).await.map(|_p| ())
140    }
141
142    /// Beeps for the given duration.
143    pub async fn beep(&mut self, duration: u32) -> Result<(), TinkerforgeError> {
144        let mut payload = [0; 4];
145        duration.write_to_slice(&mut payload[0..4]);
146
147        #[allow(unused_variables)]
148        let result = self.device.set(u8::from(PiezoBuzzerBrickletFunction::Beep), &payload).await?;
149        Ok(())
150    }
151
152    /// Sets morse code that will be played by the piezo buzzer. The morse code
153    /// is given as a string consisting of . (dot), - (minus) and   (space)
154    /// for *dits*, *dahs* and *pauses*. Every other character is ignored.
155    ///
156    /// For example: If you set the string ...---..., the piezo buzzer will beep
157    /// nine times with the durations short short short long long long short
158    /// short short.
159    pub async fn morse_code(&mut self, morse: String) -> Result<(), TinkerforgeError> {
160        let mut payload = [0; 60];
161        morse.try_write_to_slice(60, &mut payload)?;
162
163        #[allow(unused_variables)]
164        let result = self.device.set(u8::from(PiezoBuzzerBrickletFunction::MorseCode), &payload).await?;
165        Ok(())
166    }
167
168    /// Returns the UID, the UID where the Bricklet is connected to,
169    /// the position, the hardware and firmware version as well as the
170    /// device identifier.
171    ///
172    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
173    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
174    /// position 'z'.
175    ///
176    /// The device identifier numbers can be found [here](device_identifier).
177    /// |device_identifier_constant|
178    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
179        let payload = [0; 0];
180
181        #[allow(unused_variables)]
182        let result = self.device.get(u8::from(PiezoBuzzerBrickletFunction::GetIdentity), &payload).await?;
183        Ok(Identity::from_le_byte_slice(result.body()))
184    }
185}