tinkerforge_async/bindings/
piezo_speaker_v2_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 beep and alarm with configurable volume and frequency.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/PiezoSpeakerV2_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 PiezoSpeakerV2BrickletFunction {
24    SetBeep,
25    GetBeep,
26    SetAlarm,
27    GetAlarm,
28    UpdateVolume,
29    UpdateFrequency,
30    GetSpitfpErrorCount,
31    SetBootloaderMode,
32    GetBootloaderMode,
33    SetWriteFirmwarePointer,
34    WriteFirmware,
35    SetStatusLedConfig,
36    GetStatusLedConfig,
37    GetChipTemperature,
38    Reset,
39    WriteUid,
40    ReadUid,
41    GetIdentity,
42    CallbackBeepFinished,
43    CallbackAlarmFinished,
44}
45impl From<PiezoSpeakerV2BrickletFunction> for u8 {
46    fn from(fun: PiezoSpeakerV2BrickletFunction) -> Self {
47        match fun {
48            PiezoSpeakerV2BrickletFunction::SetBeep => 1,
49            PiezoSpeakerV2BrickletFunction::GetBeep => 2,
50            PiezoSpeakerV2BrickletFunction::SetAlarm => 3,
51            PiezoSpeakerV2BrickletFunction::GetAlarm => 4,
52            PiezoSpeakerV2BrickletFunction::UpdateVolume => 5,
53            PiezoSpeakerV2BrickletFunction::UpdateFrequency => 6,
54            PiezoSpeakerV2BrickletFunction::GetSpitfpErrorCount => 234,
55            PiezoSpeakerV2BrickletFunction::SetBootloaderMode => 235,
56            PiezoSpeakerV2BrickletFunction::GetBootloaderMode => 236,
57            PiezoSpeakerV2BrickletFunction::SetWriteFirmwarePointer => 237,
58            PiezoSpeakerV2BrickletFunction::WriteFirmware => 238,
59            PiezoSpeakerV2BrickletFunction::SetStatusLedConfig => 239,
60            PiezoSpeakerV2BrickletFunction::GetStatusLedConfig => 240,
61            PiezoSpeakerV2BrickletFunction::GetChipTemperature => 242,
62            PiezoSpeakerV2BrickletFunction::Reset => 243,
63            PiezoSpeakerV2BrickletFunction::WriteUid => 248,
64            PiezoSpeakerV2BrickletFunction::ReadUid => 249,
65            PiezoSpeakerV2BrickletFunction::GetIdentity => 255,
66            PiezoSpeakerV2BrickletFunction::CallbackBeepFinished => 7,
67            PiezoSpeakerV2BrickletFunction::CallbackAlarmFinished => 8,
68        }
69    }
70}
71pub const PIEZO_SPEAKER_V2_BRICKLET_BEEP_DURATION_OFF: u32 = 0;
72pub const PIEZO_SPEAKER_V2_BRICKLET_BEEP_DURATION_INFINITE: u32 = 4_294_967_295;
73pub const PIEZO_SPEAKER_V2_BRICKLET_ALARM_DURATION_OFF: u32 = 0;
74pub const PIEZO_SPEAKER_V2_BRICKLET_ALARM_DURATION_INFINITE: u32 = 4_294_967_295;
75pub const PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
76pub const PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
77pub const PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
78pub const PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
79pub const PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
80pub const PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
81pub const PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
82pub const PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
83pub const PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
84pub const PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
85pub const PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
86pub const PIEZO_SPEAKER_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
87pub const PIEZO_SPEAKER_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
88pub const PIEZO_SPEAKER_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
89pub const PIEZO_SPEAKER_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
90
91#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
92pub struct Beep {
93    pub frequency: u16,
94    pub volume: u8,
95    pub duration: u32,
96    pub duration_remaining: u32,
97}
98impl FromByteSlice for Beep {
99    fn bytes_expected() -> usize {
100        11
101    }
102    fn from_le_byte_slice(bytes: &[u8]) -> Beep {
103        Beep {
104            frequency: <u16>::from_le_byte_slice(&bytes[0..2]),
105            volume: <u8>::from_le_byte_slice(&bytes[2..3]),
106            duration: <u32>::from_le_byte_slice(&bytes[3..7]),
107            duration_remaining: <u32>::from_le_byte_slice(&bytes[7..11]),
108        }
109    }
110}
111
112#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
113pub struct Alarm {
114    pub start_frequency: u16,
115    pub end_frequency: u16,
116    pub step_size: u16,
117    pub step_delay: u16,
118    pub volume: u8,
119    pub duration: u32,
120    pub duration_remaining: u32,
121    pub current_frequency: u16,
122}
123impl FromByteSlice for Alarm {
124    fn bytes_expected() -> usize {
125        19
126    }
127    fn from_le_byte_slice(bytes: &[u8]) -> Alarm {
128        Alarm {
129            start_frequency: <u16>::from_le_byte_slice(&bytes[0..2]),
130            end_frequency: <u16>::from_le_byte_slice(&bytes[2..4]),
131            step_size: <u16>::from_le_byte_slice(&bytes[4..6]),
132            step_delay: <u16>::from_le_byte_slice(&bytes[6..8]),
133            volume: <u8>::from_le_byte_slice(&bytes[8..9]),
134            duration: <u32>::from_le_byte_slice(&bytes[9..13]),
135            duration_remaining: <u32>::from_le_byte_slice(&bytes[13..17]),
136            current_frequency: <u16>::from_le_byte_slice(&bytes[17..19]),
137        }
138    }
139}
140
141#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
142pub struct SpitfpErrorCount {
143    pub error_count_ack_checksum: u32,
144    pub error_count_message_checksum: u32,
145    pub error_count_frame: u32,
146    pub error_count_overflow: u32,
147}
148impl FromByteSlice for SpitfpErrorCount {
149    fn bytes_expected() -> usize {
150        16
151    }
152    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
153        SpitfpErrorCount {
154            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
155            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
156            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
157            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
158        }
159    }
160}
161
162#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
163pub struct Identity {
164    pub uid: String,
165    pub connected_uid: String,
166    pub position: char,
167    pub hardware_version: [u8; 3],
168    pub firmware_version: [u8; 3],
169    pub device_identifier: u16,
170}
171impl FromByteSlice for Identity {
172    fn bytes_expected() -> usize {
173        25
174    }
175    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
176        Identity {
177            uid: <String>::from_le_byte_slice(&bytes[0..8]),
178            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
179            position: <char>::from_le_byte_slice(&bytes[16..17]),
180            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
181            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
182            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
183        }
184    }
185}
186
187/// Creates beep and alarm with configurable volume and frequency
188#[derive(Clone)]
189pub struct PiezoSpeakerV2Bricklet {
190    device: Device,
191}
192impl PiezoSpeakerV2Bricklet {
193    pub const DEVICE_IDENTIFIER: u16 = 2145;
194    pub const DEVICE_DISPLAY_NAME: &'static str = "Piezo Speaker Bricklet 2.0";
195    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
196    pub fn new(uid: Uid, connection: AsyncIpConnection) -> PiezoSpeakerV2Bricklet {
197        let mut result = PiezoSpeakerV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
198        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::SetBeep) as usize] = ResponseExpectedFlag::False;
199        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::GetBeep) as usize] = ResponseExpectedFlag::AlwaysTrue;
200        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::SetAlarm) as usize] = ResponseExpectedFlag::False;
201        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::GetAlarm) as usize] = ResponseExpectedFlag::AlwaysTrue;
202        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::UpdateVolume) as usize] = ResponseExpectedFlag::False;
203        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::UpdateFrequency) as usize] = ResponseExpectedFlag::False;
204        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::GetSpitfpErrorCount) as usize] =
205            ResponseExpectedFlag::AlwaysTrue;
206        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::SetBootloaderMode) as usize] =
207            ResponseExpectedFlag::AlwaysTrue;
208        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::GetBootloaderMode) as usize] =
209            ResponseExpectedFlag::AlwaysTrue;
210        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
211            ResponseExpectedFlag::False;
212        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::WriteFirmware) as usize] =
213            ResponseExpectedFlag::AlwaysTrue;
214        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::SetStatusLedConfig) as usize] =
215            ResponseExpectedFlag::False;
216        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::GetStatusLedConfig) as usize] =
217            ResponseExpectedFlag::AlwaysTrue;
218        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::GetChipTemperature) as usize] =
219            ResponseExpectedFlag::AlwaysTrue;
220        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
221        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
222        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
223        result.device.response_expected[u8::from(PiezoSpeakerV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
224        result
225    }
226
227    /// Returns the response expected flag for the function specified by the function ID parameter.
228    /// It is true if the function is expected to send a response, false otherwise.
229    ///
230    /// For getter functions this is enabled by default and cannot be disabled, because those
231    /// functions will always send a response. For callback configuration functions it is enabled
232    /// by default too, but can be disabled by [`set_response_expected`](crate::piezo_speaker_v2_bricklet::PiezoSpeakerV2Bricklet::set_response_expected).
233    /// For setter functions it is disabled by default and can be enabled.
234    ///
235    /// Enabling the response expected flag for a setter function allows to detect timeouts
236    /// and other error conditions calls of this setter as well. The device will then send a response
237    /// for this purpose. If this flag is disabled for a setter function then no response is sent
238    /// and errors are silently ignored, because they cannot be detected.
239    ///
240    /// See [`set_response_expected`](crate::piezo_speaker_v2_bricklet::PiezoSpeakerV2Bricklet::set_response_expected) for the list of function ID constants available for this function.
241    pub fn get_response_expected(&mut self, fun: PiezoSpeakerV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
242        self.device.get_response_expected(u8::from(fun))
243    }
244
245    /// Changes the response expected flag of the function specified by the function ID parameter.
246    /// This flag can only be changed for setter (default value: false) and callback configuration
247    /// functions (default value: true). For getter functions it is always enabled.
248    ///
249    /// Enabling the response expected flag for a setter function allows to detect timeouts and
250    /// other error conditions calls of this setter as well. The device will then send a response
251    /// for this purpose. If this flag is disabled for a setter function then no response is sent
252    /// and errors are silently ignored, because they cannot be detected.
253    pub fn set_response_expected(
254        &mut self,
255        fun: PiezoSpeakerV2BrickletFunction,
256        response_expected: bool,
257    ) -> Result<(), SetResponseExpectedError> {
258        self.device.set_response_expected(u8::from(fun), response_expected)
259    }
260
261    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
262    pub fn set_response_expected_all(&mut self, response_expected: bool) {
263        self.device.set_response_expected_all(response_expected)
264    }
265
266    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
267    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
268    pub fn get_api_version(&self) -> [u8; 3] {
269        self.device.api_version
270    }
271
272    /// This receiver is triggered if a beep set by [`set_beep`] is finished
273    ///
274    /// [`set_beep`]: #method.set_beep
275    pub async fn get_beep_finished_callback_receiver(&mut self) -> impl Stream<Item = ()> {
276        self.device.get_callback_receiver(u8::from(PiezoSpeakerV2BrickletFunction::CallbackBeepFinished)).await.map(|_p| ())
277    }
278
279    /// This receiver is triggered if a alarm set by [`set_alarm`] is finished
280    pub async fn get_alarm_finished_callback_receiver(&mut self) -> impl Stream<Item = ()> {
281        self.device.get_callback_receiver(u8::from(PiezoSpeakerV2BrickletFunction::CallbackAlarmFinished)).await.map(|_p| ())
282    }
283
284    /// Beeps with the given frequency and volume for the duration.
285    ///
286    /// A duration of 0 stops the current beep if any is ongoing.
287    /// A duration of 4294967295 results in an infinite beep.
288    ///
289    /// Associated constants:
290    /// * PIEZO_SPEAKER_V2_BRICKLET_BEEP_DURATION_OFF
291    ///	* PIEZO_SPEAKER_V2_BRICKLET_BEEP_DURATION_INFINITE
292    pub async fn set_beep(&mut self, frequency: u16, volume: u8, duration: u32) -> Result<(), TinkerforgeError> {
293        let mut payload = [0; 7];
294        frequency.write_to_slice(&mut payload[0..2]);
295        volume.write_to_slice(&mut payload[2..3]);
296        duration.write_to_slice(&mut payload[3..7]);
297
298        #[allow(unused_variables)]
299        let result = self.device.set(u8::from(PiezoSpeakerV2BrickletFunction::SetBeep), &payload).await?;
300        Ok(())
301    }
302
303    /// Returns the last beep settings as set by [`set_beep`]. If a beep is currently
304    /// running it also returns the remaining duration of the beep.
305    ///
306    /// If the frequency or volume is updated during a beep (with [`update_frequency`]
307    /// or [`update_volume`]) this function returns the updated value.
308    ///
309    /// Associated constants:
310    /// * PIEZO_SPEAKER_V2_BRICKLET_BEEP_DURATION_OFF
311    ///	* PIEZO_SPEAKER_V2_BRICKLET_BEEP_DURATION_INFINITE
312    pub async fn get_beep(&mut self) -> Result<Beep, TinkerforgeError> {
313        let payload = [0; 0];
314
315        #[allow(unused_variables)]
316        let result = self.device.get(u8::from(PiezoSpeakerV2BrickletFunction::GetBeep), &payload).await?;
317        Ok(Beep::from_le_byte_slice(result.body()))
318    }
319
320    /// Creates an alarm (a tone that goes back and force between two specified frequencies).
321    ///
322    /// The following parameters can be set:
323    ///
324    /// * Start Frequency: Start frequency of the alarm.
325    /// * End Frequency: End frequency of the alarm.
326    /// * Step Size: Size of one step of the sweep between the start/end frequencies.
327    /// * Step Delay: Delay between two steps (duration of time that one tone is used in a sweep).
328    /// * Duration: Duration of the alarm.
329    ///
330    /// A duration of 0 stops the current alarm if any is ongoing.
331    /// A duration of 4294967295 results in an infinite alarm.
332    ///
333    /// Below you can find two sets of example settings that you can try out. You can use
334    /// these as a starting point to find an alarm signal that suits your application.
335    ///
336    /// Example 1: 10 seconds of loud annoying fast alarm
337    ///
338    /// * Start Frequency = 800
339    /// * End Frequency = 2000
340    /// * Step Size = 10
341    /// * Step Delay = 1
342    /// * Volume = 10
343    /// * Duration = 10000
344    ///
345    /// Example 2: 10 seconds of soft siren sound with slow build-up
346    ///
347    /// * Start Frequency = 250
348    /// * End Frequency = 750
349    /// * Step Size = 1
350    /// * Step Delay = 5
351    /// * Volume = 0
352    /// * Duration = 10000
353    ///
354    /// The following conditions must be met:
355    ///
356    /// * Start Frequency: has to be smaller than end frequency
357    /// * End Frequency: has to be bigger than start frequency
358    /// * Step Size: has to be small enough to fit into the frequency range
359    /// * Step Delay: has to be small enough to fit into the duration
360    ///
361    /// Associated constants:
362    /// * PIEZO_SPEAKER_V2_BRICKLET_ALARM_DURATION_OFF
363    ///	* PIEZO_SPEAKER_V2_BRICKLET_ALARM_DURATION_INFINITE
364    pub async fn set_alarm(
365        &mut self,
366        start_frequency: u16,
367        end_frequency: u16,
368        step_size: u16,
369        step_delay: u16,
370        volume: u8,
371        duration: u32,
372    ) -> Result<(), TinkerforgeError> {
373        let mut payload = [0; 13];
374        start_frequency.write_to_slice(&mut payload[0..2]);
375        end_frequency.write_to_slice(&mut payload[2..4]);
376        step_size.write_to_slice(&mut payload[4..6]);
377        step_delay.write_to_slice(&mut payload[6..8]);
378        volume.write_to_slice(&mut payload[8..9]);
379        duration.write_to_slice(&mut payload[9..13]);
380
381        #[allow(unused_variables)]
382        let result = self.device.set(u8::from(PiezoSpeakerV2BrickletFunction::SetAlarm), &payload).await?;
383        Ok(())
384    }
385
386    /// Returns the last alarm settings as set by [`set_alarm`]. If an alarm is currently
387    /// running it also returns the remaining duration of the alarm as well as the
388    /// current frequency of the alarm.
389    ///
390    /// If the volume is updated during an alarm (with [`update_volume`])
391    /// this function returns the updated value.
392    ///
393    /// Associated constants:
394    /// * PIEZO_SPEAKER_V2_BRICKLET_ALARM_DURATION_OFF
395    ///	* PIEZO_SPEAKER_V2_BRICKLET_ALARM_DURATION_INFINITE
396    pub async fn get_alarm(&mut self) -> Result<Alarm, TinkerforgeError> {
397        let payload = [0; 0];
398
399        #[allow(unused_variables)]
400        let result = self.device.get(u8::from(PiezoSpeakerV2BrickletFunction::GetAlarm), &payload).await?;
401        Ok(Alarm::from_le_byte_slice(result.body()))
402    }
403
404    /// Updates the volume of an ongoing beep or alarm.
405    pub async fn update_volume(&mut self, volume: u8) -> Result<(), TinkerforgeError> {
406        let mut payload = [0; 1];
407        volume.write_to_slice(&mut payload[0..1]);
408
409        #[allow(unused_variables)]
410        let result = self.device.set(u8::from(PiezoSpeakerV2BrickletFunction::UpdateVolume), &payload).await?;
411        Ok(())
412    }
413
414    /// Updates the frequency of an ongoing beep.
415    pub async fn update_frequency(&mut self, frequency: u16) -> Result<(), TinkerforgeError> {
416        let mut payload = [0; 2];
417        frequency.write_to_slice(&mut payload[0..2]);
418
419        #[allow(unused_variables)]
420        let result = self.device.set(u8::from(PiezoSpeakerV2BrickletFunction::UpdateFrequency), &payload).await?;
421        Ok(())
422    }
423
424    /// Returns the error count for the communication between Brick and Bricklet.
425    ///
426    /// The errors are divided into
427    ///
428    /// * ACK checksum errors,
429    /// * message checksum errors,
430    /// * framing errors and
431    /// * overflow errors.
432    ///
433    /// The errors counts are for errors that occur on the Bricklet side. All
434    /// Bricks have a similar function that returns the errors on the Brick side.
435    pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
436        let payload = [0; 0];
437
438        #[allow(unused_variables)]
439        let result = self.device.get(u8::from(PiezoSpeakerV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
440        Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
441    }
442
443    /// Sets the bootloader mode and returns the status after the requested
444    /// mode change was instigated.
445    ///
446    /// You can change from bootloader mode to firmware mode and vice versa. A change
447    /// from bootloader mode to firmware mode will only take place if the entry function,
448    /// device identifier and CRC are present and correct.
449    ///
450    /// This function is used by Brick Viewer during flashing. It should not be
451    /// necessary to call it in a normal user program.
452    ///
453    /// Associated constants:
454    /// * PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
455    ///	* PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
456    ///	* PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
457    ///	* PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
458    ///	* PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
459    ///	* PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_STATUS_OK
460    ///	* PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
461    ///	* PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
462    ///	* PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
463    ///	* PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
464    ///	* PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
465    pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
466        let mut payload = [0; 1];
467        mode.write_to_slice(&mut payload[0..1]);
468
469        #[allow(unused_variables)]
470        let result = self.device.get(u8::from(PiezoSpeakerV2BrickletFunction::SetBootloaderMode), &payload).await?;
471        Ok(u8::from_le_byte_slice(result.body()))
472    }
473
474    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
475    ///
476    /// Associated constants:
477    /// * PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
478    ///	* PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
479    ///	* PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
480    ///	* PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
481    ///	* PIEZO_SPEAKER_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
482    pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
483        let payload = [0; 0];
484
485        #[allow(unused_variables)]
486        let result = self.device.get(u8::from(PiezoSpeakerV2BrickletFunction::GetBootloaderMode), &payload).await?;
487        Ok(u8::from_le_byte_slice(result.body()))
488    }
489
490    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
491    /// to be increased by chunks of size 64. The data is written to flash
492    /// every 4 chunks (which equals to one page of size 256).
493    ///
494    /// This function is used by Brick Viewer during flashing. It should not be
495    /// necessary to call it in a normal user program.
496    pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
497        let mut payload = [0; 4];
498        pointer.write_to_slice(&mut payload[0..4]);
499
500        #[allow(unused_variables)]
501        let result = self.device.set(u8::from(PiezoSpeakerV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
502        Ok(())
503    }
504
505    /// Writes 64 Bytes of firmware at the position as written by
506    /// [`set_write_firmware_pointer`] before. The firmware is written
507    /// to flash every 4 chunks.
508    ///
509    /// You can only write firmware in bootloader mode.
510    ///
511    /// This function is used by Brick Viewer during flashing. It should not be
512    /// necessary to call it in a normal user program.
513    pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
514        let mut payload = [0; 64];
515        data.write_to_slice(&mut payload[0..64]);
516
517        #[allow(unused_variables)]
518        let result = self.device.get(u8::from(PiezoSpeakerV2BrickletFunction::WriteFirmware), &payload).await?;
519        Ok(u8::from_le_byte_slice(result.body()))
520    }
521
522    /// Sets the status LED configuration. By default the LED shows
523    /// communication traffic between Brick and Bricklet, it flickers once
524    /// for every 10 received data packets.
525    ///
526    /// You can also turn the LED permanently on/off or show a heartbeat.
527    ///
528    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
529    ///
530    /// Associated constants:
531    /// * PIEZO_SPEAKER_V2_BRICKLET_STATUS_LED_CONFIG_OFF
532    ///	* PIEZO_SPEAKER_V2_BRICKLET_STATUS_LED_CONFIG_ON
533    ///	* PIEZO_SPEAKER_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
534    ///	* PIEZO_SPEAKER_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
535    pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
536        let mut payload = [0; 1];
537        config.write_to_slice(&mut payload[0..1]);
538
539        #[allow(unused_variables)]
540        let result = self.device.set(u8::from(PiezoSpeakerV2BrickletFunction::SetStatusLedConfig), &payload).await?;
541        Ok(())
542    }
543
544    /// Returns the configuration as set by [`set_status_led_config`]
545    ///
546    /// Associated constants:
547    /// * PIEZO_SPEAKER_V2_BRICKLET_STATUS_LED_CONFIG_OFF
548    ///	* PIEZO_SPEAKER_V2_BRICKLET_STATUS_LED_CONFIG_ON
549    ///	* PIEZO_SPEAKER_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
550    ///	* PIEZO_SPEAKER_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
551    pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
552        let payload = [0; 0];
553
554        #[allow(unused_variables)]
555        let result = self.device.get(u8::from(PiezoSpeakerV2BrickletFunction::GetStatusLedConfig), &payload).await?;
556        Ok(u8::from_le_byte_slice(result.body()))
557    }
558
559    /// Returns the temperature as measured inside the microcontroller. The
560    /// value returned is not the ambient temperature!
561    ///
562    /// The temperature is only proportional to the real temperature and it has bad
563    /// accuracy. Practically it is only useful as an indicator for
564    /// temperature changes.
565    pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
566        let payload = [0; 0];
567
568        #[allow(unused_variables)]
569        let result = self.device.get(u8::from(PiezoSpeakerV2BrickletFunction::GetChipTemperature), &payload).await?;
570        Ok(i16::from_le_byte_slice(result.body()))
571    }
572
573    /// Calling this function will reset the Bricklet. All configurations
574    /// will be lost.
575    ///
576    /// After a reset you have to create new device objects,
577    /// calling functions on the existing ones will result in
578    /// undefined behavior!
579    pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
580        let payload = [0; 0];
581
582        #[allow(unused_variables)]
583        let result = self.device.set(u8::from(PiezoSpeakerV2BrickletFunction::Reset), &payload).await?;
584        Ok(())
585    }
586
587    /// Writes a new UID into flash. If you want to set a new UID
588    /// you have to decode the Base58 encoded UID string into an
589    /// integer first.
590    ///
591    /// We recommend that you use Brick Viewer to change the UID.
592    pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
593        let mut payload = [0; 4];
594        uid.write_to_slice(&mut payload[0..4]);
595
596        #[allow(unused_variables)]
597        let result = self.device.set(u8::from(PiezoSpeakerV2BrickletFunction::WriteUid), &payload).await?;
598        Ok(())
599    }
600
601    /// Returns the current UID as an integer. Encode as
602    /// Base58 to get the usual string version.
603    pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
604        let payload = [0; 0];
605
606        #[allow(unused_variables)]
607        let result = self.device.get(u8::from(PiezoSpeakerV2BrickletFunction::ReadUid), &payload).await?;
608        Ok(u32::from_le_byte_slice(result.body()))
609    }
610
611    /// Returns the UID, the UID where the Bricklet is connected to,
612    /// the position, the hardware and firmware version as well as the
613    /// device identifier.
614    ///
615    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
616    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
617    /// position 'z'.
618    ///
619    /// The device identifier numbers can be found [here](device_identifier).
620    /// |device_identifier_constant|
621    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
622        let payload = [0; 0];
623
624        #[allow(unused_variables)]
625        let result = self.device.get(u8::from(PiezoSpeakerV2BrickletFunction::GetIdentity), &payload).await?;
626        Ok(Identity::from_le_byte_slice(result.body()))
627    }
628}