tinkerforge_async/bindings/
sound_pressure_level_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//! Measures Sound Pressure Level in dB(A/B/C/D/Z).
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/SoundPressureLevel_Bricklet_Rust.html).
14#[allow(unused_imports)]
15use crate::{
16    base58::Uid, byte_converter::*, converting_high_level_callback_receiver::ConvertingHighLevelCallbackReceiver,
17    converting_receiver::BrickletRecvTimeoutError, device::*, error::TinkerforgeError, ip_connection::async_io::AsyncIpConnection,
18    low_level_traits::LowLevelRead,
19};
20#[allow(unused_imports)]
21use futures_core::Stream;
22#[allow(unused_imports)]
23use tokio_stream::StreamExt;
24pub enum SoundPressureLevelBrickletFunction {
25    GetDecibel,
26    SetDecibelCallbackConfiguration,
27    GetDecibelCallbackConfiguration,
28    GetSpectrumLowLevel,
29    SetSpectrumCallbackConfiguration,
30    GetSpectrumCallbackConfiguration,
31    SetConfiguration,
32    GetConfiguration,
33    GetSpitfpErrorCount,
34    SetBootloaderMode,
35    GetBootloaderMode,
36    SetWriteFirmwarePointer,
37    WriteFirmware,
38    SetStatusLedConfig,
39    GetStatusLedConfig,
40    GetChipTemperature,
41    Reset,
42    WriteUid,
43    ReadUid,
44    GetIdentity,
45    CallbackDecibel,
46    CallbackSpectrumLowLevel,
47}
48impl From<SoundPressureLevelBrickletFunction> for u8 {
49    fn from(fun: SoundPressureLevelBrickletFunction) -> Self {
50        match fun {
51            SoundPressureLevelBrickletFunction::GetDecibel => 1,
52            SoundPressureLevelBrickletFunction::SetDecibelCallbackConfiguration => 2,
53            SoundPressureLevelBrickletFunction::GetDecibelCallbackConfiguration => 3,
54            SoundPressureLevelBrickletFunction::GetSpectrumLowLevel => 5,
55            SoundPressureLevelBrickletFunction::SetSpectrumCallbackConfiguration => 6,
56            SoundPressureLevelBrickletFunction::GetSpectrumCallbackConfiguration => 7,
57            SoundPressureLevelBrickletFunction::SetConfiguration => 9,
58            SoundPressureLevelBrickletFunction::GetConfiguration => 10,
59            SoundPressureLevelBrickletFunction::GetSpitfpErrorCount => 234,
60            SoundPressureLevelBrickletFunction::SetBootloaderMode => 235,
61            SoundPressureLevelBrickletFunction::GetBootloaderMode => 236,
62            SoundPressureLevelBrickletFunction::SetWriteFirmwarePointer => 237,
63            SoundPressureLevelBrickletFunction::WriteFirmware => 238,
64            SoundPressureLevelBrickletFunction::SetStatusLedConfig => 239,
65            SoundPressureLevelBrickletFunction::GetStatusLedConfig => 240,
66            SoundPressureLevelBrickletFunction::GetChipTemperature => 242,
67            SoundPressureLevelBrickletFunction::Reset => 243,
68            SoundPressureLevelBrickletFunction::WriteUid => 248,
69            SoundPressureLevelBrickletFunction::ReadUid => 249,
70            SoundPressureLevelBrickletFunction::GetIdentity => 255,
71            SoundPressureLevelBrickletFunction::CallbackDecibel => 4,
72            SoundPressureLevelBrickletFunction::CallbackSpectrumLowLevel => 8,
73        }
74    }
75}
76pub const SOUND_PRESSURE_LEVEL_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
77pub const SOUND_PRESSURE_LEVEL_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
78pub const SOUND_PRESSURE_LEVEL_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
79pub const SOUND_PRESSURE_LEVEL_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
80pub const SOUND_PRESSURE_LEVEL_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
81pub const SOUND_PRESSURE_LEVEL_BRICKLET_FFT_SIZE_128: u8 = 0;
82pub const SOUND_PRESSURE_LEVEL_BRICKLET_FFT_SIZE_256: u8 = 1;
83pub const SOUND_PRESSURE_LEVEL_BRICKLET_FFT_SIZE_512: u8 = 2;
84pub const SOUND_PRESSURE_LEVEL_BRICKLET_FFT_SIZE_1024: u8 = 3;
85pub const SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_A: u8 = 0;
86pub const SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_B: u8 = 1;
87pub const SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_C: u8 = 2;
88pub const SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_D: u8 = 3;
89pub const SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_Z: u8 = 4;
90pub const SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_ITU_R_468: u8 = 5;
91pub const SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
92pub const SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
93pub const SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
94pub const SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
95pub const SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
96pub const SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
97pub const SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
98pub const SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
99pub const SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
100pub const SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
101pub const SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
102pub const SOUND_PRESSURE_LEVEL_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
103pub const SOUND_PRESSURE_LEVEL_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
104pub const SOUND_PRESSURE_LEVEL_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
105pub const SOUND_PRESSURE_LEVEL_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
106
107#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
108pub struct DecibelCallbackConfiguration {
109    pub period: u32,
110    pub value_has_to_change: bool,
111    pub option: char,
112    pub min: u16,
113    pub max: u16,
114}
115impl FromByteSlice for DecibelCallbackConfiguration {
116    fn bytes_expected() -> usize {
117        10
118    }
119    fn from_le_byte_slice(bytes: &[u8]) -> DecibelCallbackConfiguration {
120        DecibelCallbackConfiguration {
121            period: <u32>::from_le_byte_slice(&bytes[0..4]),
122            value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
123            option: <char>::from_le_byte_slice(&bytes[5..6]),
124            min: <u16>::from_le_byte_slice(&bytes[6..8]),
125            max: <u16>::from_le_byte_slice(&bytes[8..10]),
126        }
127    }
128}
129
130#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
131pub struct SpectrumLowLevel {
132    pub spectrum_length: u16,
133    pub spectrum_chunk_offset: u16,
134    pub spectrum_chunk_data: [u16; 30],
135}
136impl FromByteSlice for SpectrumLowLevel {
137    fn bytes_expected() -> usize {
138        64
139    }
140    fn from_le_byte_slice(bytes: &[u8]) -> SpectrumLowLevel {
141        SpectrumLowLevel {
142            spectrum_length: <u16>::from_le_byte_slice(&bytes[0..2]),
143            spectrum_chunk_offset: <u16>::from_le_byte_slice(&bytes[2..4]),
144            spectrum_chunk_data: <[u16; 30]>::from_le_byte_slice(&bytes[4..64]),
145        }
146    }
147}
148impl LowLevelRead<u16, SpectrumResult> for SpectrumLowLevel {
149    fn ll_message_length(&self) -> usize {
150        self.spectrum_length as usize
151    }
152
153    fn ll_message_chunk_offset(&self) -> usize {
154        self.spectrum_chunk_offset as usize
155    }
156
157    fn ll_message_chunk_data(&self) -> &[u16] {
158        &self.spectrum_chunk_data
159    }
160
161    fn get_result(&self) -> SpectrumResult {
162        SpectrumResult {}
163    }
164}
165
166#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
167pub struct SpectrumLowLevelEvent {
168    pub spectrum_length: u16,
169    pub spectrum_chunk_offset: u16,
170    pub spectrum_chunk_data: [u16; 30],
171}
172impl FromByteSlice for SpectrumLowLevelEvent {
173    fn bytes_expected() -> usize {
174        64
175    }
176    fn from_le_byte_slice(bytes: &[u8]) -> SpectrumLowLevelEvent {
177        SpectrumLowLevelEvent {
178            spectrum_length: <u16>::from_le_byte_slice(&bytes[0..2]),
179            spectrum_chunk_offset: <u16>::from_le_byte_slice(&bytes[2..4]),
180            spectrum_chunk_data: <[u16; 30]>::from_le_byte_slice(&bytes[4..64]),
181        }
182    }
183}
184impl LowLevelRead<u16, SpectrumResult> for SpectrumLowLevelEvent {
185    fn ll_message_length(&self) -> usize {
186        self.spectrum_length as usize
187    }
188
189    fn ll_message_chunk_offset(&self) -> usize {
190        self.spectrum_chunk_offset as usize
191    }
192
193    fn ll_message_chunk_data(&self) -> &[u16] {
194        &self.spectrum_chunk_data
195    }
196
197    fn get_result(&self) -> SpectrumResult {
198        SpectrumResult {}
199    }
200}
201
202#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
203pub struct Configuration {
204    pub fft_size: u8,
205    pub weighting: u8,
206}
207impl FromByteSlice for Configuration {
208    fn bytes_expected() -> usize {
209        2
210    }
211    fn from_le_byte_slice(bytes: &[u8]) -> Configuration {
212        Configuration { fft_size: <u8>::from_le_byte_slice(&bytes[0..1]), weighting: <u8>::from_le_byte_slice(&bytes[1..2]) }
213    }
214}
215
216#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
217pub struct SpitfpErrorCount {
218    pub error_count_ack_checksum: u32,
219    pub error_count_message_checksum: u32,
220    pub error_count_frame: u32,
221    pub error_count_overflow: u32,
222}
223impl FromByteSlice for SpitfpErrorCount {
224    fn bytes_expected() -> usize {
225        16
226    }
227    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
228        SpitfpErrorCount {
229            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
230            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
231            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
232            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
233        }
234    }
235}
236
237#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
238pub struct Identity {
239    pub uid: String,
240    pub connected_uid: String,
241    pub position: char,
242    pub hardware_version: [u8; 3],
243    pub firmware_version: [u8; 3],
244    pub device_identifier: u16,
245}
246impl FromByteSlice for Identity {
247    fn bytes_expected() -> usize {
248        25
249    }
250    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
251        Identity {
252            uid: <String>::from_le_byte_slice(&bytes[0..8]),
253            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
254            position: <char>::from_le_byte_slice(&bytes[16..17]),
255            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
256            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
257            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
258        }
259    }
260}
261
262#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
263pub struct SpectrumResult {}
264
265/// Measures Sound Pressure Level in dB(A/B/C/D/Z)
266#[derive(Clone)]
267pub struct SoundPressureLevelBricklet {
268    device: Device,
269}
270impl SoundPressureLevelBricklet {
271    pub const DEVICE_IDENTIFIER: u16 = 290;
272    pub const DEVICE_DISPLAY_NAME: &'static str = "Sound Pressure Level Bricklet";
273    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
274    pub fn new(uid: Uid, connection: AsyncIpConnection) -> SoundPressureLevelBricklet {
275        let mut result = SoundPressureLevelBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
276        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::GetDecibel) as usize] =
277            ResponseExpectedFlag::AlwaysTrue;
278        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::SetDecibelCallbackConfiguration) as usize] =
279            ResponseExpectedFlag::True;
280        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::GetDecibelCallbackConfiguration) as usize] =
281            ResponseExpectedFlag::AlwaysTrue;
282        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::GetSpectrumLowLevel) as usize] =
283            ResponseExpectedFlag::AlwaysTrue;
284        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::SetSpectrumCallbackConfiguration) as usize] =
285            ResponseExpectedFlag::True;
286        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::GetSpectrumCallbackConfiguration) as usize] =
287            ResponseExpectedFlag::AlwaysTrue;
288        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::SetConfiguration) as usize] =
289            ResponseExpectedFlag::False;
290        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::GetConfiguration) as usize] =
291            ResponseExpectedFlag::AlwaysTrue;
292        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::GetSpitfpErrorCount) as usize] =
293            ResponseExpectedFlag::AlwaysTrue;
294        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::SetBootloaderMode) as usize] =
295            ResponseExpectedFlag::AlwaysTrue;
296        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::GetBootloaderMode) as usize] =
297            ResponseExpectedFlag::AlwaysTrue;
298        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::SetWriteFirmwarePointer) as usize] =
299            ResponseExpectedFlag::False;
300        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::WriteFirmware) as usize] =
301            ResponseExpectedFlag::AlwaysTrue;
302        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::SetStatusLedConfig) as usize] =
303            ResponseExpectedFlag::False;
304        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::GetStatusLedConfig) as usize] =
305            ResponseExpectedFlag::AlwaysTrue;
306        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::GetChipTemperature) as usize] =
307            ResponseExpectedFlag::AlwaysTrue;
308        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
309        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
310        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
311        result.device.response_expected[u8::from(SoundPressureLevelBrickletFunction::GetIdentity) as usize] =
312            ResponseExpectedFlag::AlwaysTrue;
313        result
314    }
315
316    /// Returns the response expected flag for the function specified by the function ID parameter.
317    /// It is true if the function is expected to send a response, false otherwise.
318    ///
319    /// For getter functions this is enabled by default and cannot be disabled, because those
320    /// functions will always send a response. For callback configuration functions it is enabled
321    /// by default too, but can be disabled by [`set_response_expected`](crate::sound_pressure_level_bricklet::SoundPressureLevelBricklet::set_response_expected).
322    /// For setter functions it is disabled by default and can be enabled.
323    ///
324    /// Enabling the response expected flag for a setter function allows to detect timeouts
325    /// and other error conditions calls of this setter as well. The device will then send a response
326    /// for this purpose. If this flag is disabled for a setter function then no response is sent
327    /// and errors are silently ignored, because they cannot be detected.
328    ///
329    /// See [`set_response_expected`](crate::sound_pressure_level_bricklet::SoundPressureLevelBricklet::set_response_expected) for the list of function ID constants available for this function.
330    pub fn get_response_expected(&mut self, fun: SoundPressureLevelBrickletFunction) -> Result<bool, GetResponseExpectedError> {
331        self.device.get_response_expected(u8::from(fun))
332    }
333
334    /// Changes the response expected flag of the function specified by the function ID parameter.
335    /// This flag can only be changed for setter (default value: false) and callback configuration
336    /// functions (default value: true). For getter functions it is always enabled.
337    ///
338    /// Enabling the response expected flag for a setter function allows to detect timeouts and
339    /// other error conditions calls of this setter as well. The device will then send a response
340    /// for this purpose. If this flag is disabled for a setter function then no response is sent
341    /// and errors are silently ignored, because they cannot be detected.
342    pub fn set_response_expected(
343        &mut self,
344        fun: SoundPressureLevelBrickletFunction,
345        response_expected: bool,
346    ) -> Result<(), SetResponseExpectedError> {
347        self.device.set_response_expected(u8::from(fun), response_expected)
348    }
349
350    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
351    pub fn set_response_expected_all(&mut self, response_expected: bool) {
352        self.device.set_response_expected_all(response_expected)
353    }
354
355    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
356    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
357    pub fn get_api_version(&self) -> [u8; 3] {
358        self.device.api_version
359    }
360
361    /// This receiver is triggered periodically according to the configuration set by
362    /// [`set_decibel_callback_configuration`].
363    ///
364    /// The parameter is the same as [`get_decibel`].
365    ///
366    /// [`get_decibel`]: #method.get_decibel
367    /// [`set_decibel_callback_configuration`]: #method.set_decibel_callback_configuration
368    pub async fn get_decibel_callback_receiver(&mut self) -> impl Stream<Item = u16> {
369        self.device
370            .get_callback_receiver(u8::from(SoundPressureLevelBrickletFunction::CallbackDecibel))
371            .await
372            .map(|p| u16::from_le_byte_slice(p.body()))
373    }
374
375    /// See [`get_spectrum_callback_receiver`](crate::sound_pressure_level::SoundPressureLevel::get_spectrum_callback_receiver)
376    pub async fn get_spectrum_low_level_callback_receiver(&mut self) -> impl Stream<Item = SpectrumLowLevelEvent> {
377        self.device
378            .get_callback_receiver(u8::from(SoundPressureLevelBrickletFunction::CallbackSpectrumLowLevel))
379            .await
380            .map(|p| SpectrumLowLevelEvent::from_le_byte_slice(p.body()))
381    }
382
383    /// Returns the measured sound pressure in decibels.
384    ///
385    /// The Bricklet supports the weighting standards dB(A), dB(B), dB(C), dB(D),
386    /// dB(Z) and ITU-R 468. You can configure the weighting with [`set_configuration`].
387    ///
388    /// By default dB(A) will be used.
389    ///
390    ///
391    /// If you want to get the value periodically, it is recommended to use the
392    /// [`get_decibel_callback_receiver`] receiver. You can set the receiver configuration
393    /// with [`set_decibel_callback_configuration`].
394    pub async fn get_decibel(&mut self) -> Result<u16, TinkerforgeError> {
395        let payload = [0; 0];
396
397        #[allow(unused_variables)]
398        let result = self.device.get(u8::from(SoundPressureLevelBrickletFunction::GetDecibel), &payload).await?;
399        Ok(u16::from_le_byte_slice(result.body()))
400    }
401
402    /// The period is the period with which the [`get_decibel_callback_receiver`] receiver is triggered
403    /// periodically. A value of 0 turns the receiver off.
404    ///
405    /// If the `value has to change`-parameter is set to true, the receiver is only
406    /// triggered after the value has changed. If the value didn't change
407    /// within the period, the receiver is triggered immediately on change.
408    ///
409    /// If it is set to false, the receiver is continuously triggered with the period,
410    /// independent of the value.
411    ///
412    /// It is furthermore possible to constrain the receiver with thresholds.
413    ///
414    /// The `option`-parameter together with min/max sets a threshold for the [`get_decibel_callback_receiver`] receiver.
415    ///
416    /// The following options are possible:
417    ///
418    ///  Option| Description
419    ///  --- | ---
420    ///  'x'|    Threshold is turned off
421    ///  'o'|    Threshold is triggered when the value is *outside* the min and max values
422    ///  'i'|    Threshold is triggered when the value is *inside* or equal to the min and max values
423    ///  '<'|    Threshold is triggered when the value is smaller than the min value (max is ignored)
424    ///  '>'|    Threshold is triggered when the value is greater than the min value (max is ignored)
425    ///
426    /// If the option is set to 'x' (threshold turned off) the receiver is triggered with the fixed period.
427    ///
428    /// Associated constants:
429    /// * SOUND_PRESSURE_LEVEL_BRICKLET_THRESHOLD_OPTION_OFF
430    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_THRESHOLD_OPTION_OUTSIDE
431    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_THRESHOLD_OPTION_INSIDE
432    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_THRESHOLD_OPTION_SMALLER
433    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_THRESHOLD_OPTION_GREATER
434    pub async fn set_decibel_callback_configuration(
435        &mut self,
436        period: u32,
437        value_has_to_change: bool,
438        option: char,
439        min: u16,
440        max: u16,
441    ) -> Result<(), TinkerforgeError> {
442        let mut payload = [0; 10];
443        period.write_to_slice(&mut payload[0..4]);
444        value_has_to_change.write_to_slice(&mut payload[4..5]);
445        option.write_to_slice(&mut payload[5..6]);
446        min.write_to_slice(&mut payload[6..8]);
447        max.write_to_slice(&mut payload[8..10]);
448
449        #[allow(unused_variables)]
450        let result = self.device.set(u8::from(SoundPressureLevelBrickletFunction::SetDecibelCallbackConfiguration), &payload).await?;
451        Ok(())
452    }
453
454    /// Returns the receiver configuration as set by [`set_decibel_callback_configuration`].
455    ///
456    /// Associated constants:
457    /// * SOUND_PRESSURE_LEVEL_BRICKLET_THRESHOLD_OPTION_OFF
458    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_THRESHOLD_OPTION_OUTSIDE
459    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_THRESHOLD_OPTION_INSIDE
460    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_THRESHOLD_OPTION_SMALLER
461    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_THRESHOLD_OPTION_GREATER
462    pub async fn get_decibel_callback_configuration(&mut self) -> Result<DecibelCallbackConfiguration, TinkerforgeError> {
463        let payload = [0; 0];
464
465        #[allow(unused_variables)]
466        let result = self.device.get(u8::from(SoundPressureLevelBrickletFunction::GetDecibelCallbackConfiguration), &payload).await?;
467        Ok(DecibelCallbackConfiguration::from_le_byte_slice(result.body()))
468    }
469
470    /// Returns the frequency spectrum. The length of the spectrum is between
471    /// 512 (FFT size 1024) and 64 (FFT size 128). See [`set_configuration`].
472    ///
473    /// Each array element is one bin of the FFT. The first bin is always the
474    /// DC offset and the other bins have a size between 40Hz (FFT size 1024) and
475    /// 320Hz (FFT size 128).
476    ///
477    /// In sum the frequency of the spectrum always has a range from 0 to
478    /// 20480Hz (the FFT is applied to samples with a frequency of 40960Hz).
479    ///
480    /// The returned data is already equalized, which means that the microphone
481    /// frequency response is compensated and the weighting function is applied
482    /// (see [`set_configuration`] for the available weighting standards). Use
483    /// dB(Z) if you need the unaltered spectrum.
484    ///
485    /// The values are not in dB form yet. If you want a proper dB scale of the
486    /// spectrum you have to apply the formula f(x) = 20*log10(max(1, x/sqrt(2)))
487    /// on each value.
488    pub async fn get_spectrum_low_level(&mut self) -> Result<SpectrumLowLevel, TinkerforgeError> {
489        let payload = [0; 0];
490
491        #[allow(unused_variables)]
492        let result = self.device.get(u8::from(SoundPressureLevelBrickletFunction::GetSpectrumLowLevel), &payload).await?;
493        Ok(SpectrumLowLevel::from_le_byte_slice(result.body()))
494    }
495
496    /// The period is the period with which the [`get_spectrum_callback_receiver`] receiver is
497    /// triggered periodically. A value of 0 turns the receiver off.
498    ///
499    /// Every new measured spectrum will be send at most once. Set the period to 1 to
500    /// make sure that you get every spectrum.
501    pub async fn set_spectrum_callback_configuration(&mut self, period: u32) -> Result<(), TinkerforgeError> {
502        let mut payload = [0; 4];
503        period.write_to_slice(&mut payload[0..4]);
504
505        #[allow(unused_variables)]
506        let result = self.device.set(u8::from(SoundPressureLevelBrickletFunction::SetSpectrumCallbackConfiguration), &payload).await?;
507        Ok(())
508    }
509
510    /// Returns the receiver configuration as set by
511    /// [`get_spectrum_callback_configuration`].
512    pub async fn get_spectrum_callback_configuration(&mut self) -> Result<u32, TinkerforgeError> {
513        let payload = [0; 0];
514
515        #[allow(unused_variables)]
516        let result = self.device.get(u8::from(SoundPressureLevelBrickletFunction::GetSpectrumCallbackConfiguration), &payload).await?;
517        Ok(u32::from_le_byte_slice(result.body()))
518    }
519
520    /// Sets the Sound Pressure Level Bricklet configuration.
521    ///
522    /// With different FFT sizes the Bricklet has a different
523    /// amount of samples per second and the size of the FFT bins
524    /// changes. The higher the FFT size the more precise is the result
525    /// of the dB(X) calculation.
526    ///
527    /// Available FFT sizes are:
528    ///
529    /// * 1024: 512 bins, 10 samples per second, each bin has size 40Hz
530    /// * 512: 256 bins, 20 samples per second, each bin has size 80Hz
531    /// * 256: 128 bins, 40 samples per second, each bin has size 160Hz
532    /// * 128: 64 bins, 80 samples per second, each bin has size 320Hz
533    ///
534    /// The Bricklet supports different weighting functions. You can choose
535    /// between dB(A), dB(B), dB(C), dB(D), dB(Z) and ITU-R 468.
536    ///
537    /// dB(A/B/C/D) are the standard dB weighting curves. dB(A) is
538    /// often used to measure volumes at concerts etc. dB(Z) has a
539    /// flat response, no weighting is applied. ITU-R 468 is an ITU
540    /// weighting standard mostly used in the UK and Europe.
541    ///
542    /// Associated constants:
543    /// * SOUND_PRESSURE_LEVEL_BRICKLET_FFT_SIZE_128
544    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_FFT_SIZE_256
545    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_FFT_SIZE_512
546    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_FFT_SIZE_1024
547    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_A
548    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_B
549    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_C
550    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_D
551    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_Z
552    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_ITU_R_468
553    pub async fn set_configuration(&mut self, fft_size: u8, weighting: u8) -> Result<(), TinkerforgeError> {
554        let mut payload = [0; 2];
555        fft_size.write_to_slice(&mut payload[0..1]);
556        weighting.write_to_slice(&mut payload[1..2]);
557
558        #[allow(unused_variables)]
559        let result = self.device.set(u8::from(SoundPressureLevelBrickletFunction::SetConfiguration), &payload).await?;
560        Ok(())
561    }
562
563    /// Returns the configuration as set by [`set_configuration`].
564    ///
565    /// Associated constants:
566    /// * SOUND_PRESSURE_LEVEL_BRICKLET_FFT_SIZE_128
567    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_FFT_SIZE_256
568    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_FFT_SIZE_512
569    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_FFT_SIZE_1024
570    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_A
571    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_B
572    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_C
573    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_D
574    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_Z
575    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_WEIGHTING_ITU_R_468
576    pub async fn get_configuration(&mut self) -> Result<Configuration, TinkerforgeError> {
577        let payload = [0; 0];
578
579        #[allow(unused_variables)]
580        let result = self.device.get(u8::from(SoundPressureLevelBrickletFunction::GetConfiguration), &payload).await?;
581        Ok(Configuration::from_le_byte_slice(result.body()))
582    }
583
584    /// Returns the error count for the communication between Brick and Bricklet.
585    ///
586    /// The errors are divided into
587    ///
588    /// * ACK checksum errors,
589    /// * message checksum errors,
590    /// * framing errors and
591    /// * overflow errors.
592    ///
593    /// The errors counts are for errors that occur on the Bricklet side. All
594    /// Bricks have a similar function that returns the errors on the Brick side.
595    pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
596        let payload = [0; 0];
597
598        #[allow(unused_variables)]
599        let result = self.device.get(u8::from(SoundPressureLevelBrickletFunction::GetSpitfpErrorCount), &payload).await?;
600        Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
601    }
602
603    /// Sets the bootloader mode and returns the status after the requested
604    /// mode change was instigated.
605    ///
606    /// You can change from bootloader mode to firmware mode and vice versa. A change
607    /// from bootloader mode to firmware mode will only take place if the entry function,
608    /// device identifier and CRC are present and correct.
609    ///
610    /// This function is used by Brick Viewer during flashing. It should not be
611    /// necessary to call it in a normal user program.
612    ///
613    /// Associated constants:
614    /// * SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
615    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_MODE_FIRMWARE
616    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
617    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
618    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
619    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_STATUS_OK
620    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
621    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
622    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
623    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
624    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
625    pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
626        let mut payload = [0; 1];
627        mode.write_to_slice(&mut payload[0..1]);
628
629        #[allow(unused_variables)]
630        let result = self.device.get(u8::from(SoundPressureLevelBrickletFunction::SetBootloaderMode), &payload).await?;
631        Ok(u8::from_le_byte_slice(result.body()))
632    }
633
634    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
635    ///
636    /// Associated constants:
637    /// * SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
638    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_MODE_FIRMWARE
639    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
640    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
641    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
642    pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
643        let payload = [0; 0];
644
645        #[allow(unused_variables)]
646        let result = self.device.get(u8::from(SoundPressureLevelBrickletFunction::GetBootloaderMode), &payload).await?;
647        Ok(u8::from_le_byte_slice(result.body()))
648    }
649
650    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
651    /// to be increased by chunks of size 64. The data is written to flash
652    /// every 4 chunks (which equals to one page of size 256).
653    ///
654    /// This function is used by Brick Viewer during flashing. It should not be
655    /// necessary to call it in a normal user program.
656    pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
657        let mut payload = [0; 4];
658        pointer.write_to_slice(&mut payload[0..4]);
659
660        #[allow(unused_variables)]
661        let result = self.device.set(u8::from(SoundPressureLevelBrickletFunction::SetWriteFirmwarePointer), &payload).await?;
662        Ok(())
663    }
664
665    /// Writes 64 Bytes of firmware at the position as written by
666    /// [`set_write_firmware_pointer`] before. The firmware is written
667    /// to flash every 4 chunks.
668    ///
669    /// You can only write firmware in bootloader mode.
670    ///
671    /// This function is used by Brick Viewer during flashing. It should not be
672    /// necessary to call it in a normal user program.
673    pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
674        let mut payload = [0; 64];
675        data.write_to_slice(&mut payload[0..64]);
676
677        #[allow(unused_variables)]
678        let result = self.device.get(u8::from(SoundPressureLevelBrickletFunction::WriteFirmware), &payload).await?;
679        Ok(u8::from_le_byte_slice(result.body()))
680    }
681
682    /// Sets the status LED configuration. By default the LED shows
683    /// communication traffic between Brick and Bricklet, it flickers once
684    /// for every 10 received data packets.
685    ///
686    /// You can also turn the LED permanently on/off or show a heartbeat.
687    ///
688    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
689    ///
690    /// Associated constants:
691    /// * SOUND_PRESSURE_LEVEL_BRICKLET_STATUS_LED_CONFIG_OFF
692    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_STATUS_LED_CONFIG_ON
693    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
694    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
695    pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
696        let mut payload = [0; 1];
697        config.write_to_slice(&mut payload[0..1]);
698
699        #[allow(unused_variables)]
700        let result = self.device.set(u8::from(SoundPressureLevelBrickletFunction::SetStatusLedConfig), &payload).await?;
701        Ok(())
702    }
703
704    /// Returns the configuration as set by [`set_status_led_config`]
705    ///
706    /// Associated constants:
707    /// * SOUND_PRESSURE_LEVEL_BRICKLET_STATUS_LED_CONFIG_OFF
708    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_STATUS_LED_CONFIG_ON
709    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
710    ///	* SOUND_PRESSURE_LEVEL_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
711    pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
712        let payload = [0; 0];
713
714        #[allow(unused_variables)]
715        let result = self.device.get(u8::from(SoundPressureLevelBrickletFunction::GetStatusLedConfig), &payload).await?;
716        Ok(u8::from_le_byte_slice(result.body()))
717    }
718
719    /// Returns the temperature as measured inside the microcontroller. The
720    /// value returned is not the ambient temperature!
721    ///
722    /// The temperature is only proportional to the real temperature and it has bad
723    /// accuracy. Practically it is only useful as an indicator for
724    /// temperature changes.
725    pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
726        let payload = [0; 0];
727
728        #[allow(unused_variables)]
729        let result = self.device.get(u8::from(SoundPressureLevelBrickletFunction::GetChipTemperature), &payload).await?;
730        Ok(i16::from_le_byte_slice(result.body()))
731    }
732
733    /// Calling this function will reset the Bricklet. All configurations
734    /// will be lost.
735    ///
736    /// After a reset you have to create new device objects,
737    /// calling functions on the existing ones will result in
738    /// undefined behavior!
739    pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
740        let payload = [0; 0];
741
742        #[allow(unused_variables)]
743        let result = self.device.set(u8::from(SoundPressureLevelBrickletFunction::Reset), &payload).await?;
744        Ok(())
745    }
746
747    /// Writes a new UID into flash. If you want to set a new UID
748    /// you have to decode the Base58 encoded UID string into an
749    /// integer first.
750    ///
751    /// We recommend that you use Brick Viewer to change the UID.
752    pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
753        let mut payload = [0; 4];
754        uid.write_to_slice(&mut payload[0..4]);
755
756        #[allow(unused_variables)]
757        let result = self.device.set(u8::from(SoundPressureLevelBrickletFunction::WriteUid), &payload).await?;
758        Ok(())
759    }
760
761    /// Returns the current UID as an integer. Encode as
762    /// Base58 to get the usual string version.
763    pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
764        let payload = [0; 0];
765
766        #[allow(unused_variables)]
767        let result = self.device.get(u8::from(SoundPressureLevelBrickletFunction::ReadUid), &payload).await?;
768        Ok(u32::from_le_byte_slice(result.body()))
769    }
770
771    /// Returns the UID, the UID where the Bricklet is connected to,
772    /// the position, the hardware and firmware version as well as the
773    /// device identifier.
774    ///
775    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
776    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
777    /// position 'z'.
778    ///
779    /// The device identifier numbers can be found [here](device_identifier).
780    /// |device_identifier_constant|
781    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
782        let payload = [0; 0];
783
784        #[allow(unused_variables)]
785        let result = self.device.get(u8::from(SoundPressureLevelBrickletFunction::GetIdentity), &payload).await?;
786        Ok(Identity::from_le_byte_slice(result.body()))
787    }
788}