tinkerforge/bindings/sound_intensity_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//! Measures sound intensity.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/SoundIntensity_Bricklet_Rust.html).
14use crate::{
15 byte_converter::*, converting_callback_receiver::ConvertingCallbackReceiver, converting_receiver::ConvertingReceiver, device::*,
16 ip_connection::GetRequestSender,
17};
18pub enum SoundIntensityBrickletFunction {
19 GetIntensity,
20 SetIntensityCallbackPeriod,
21 GetIntensityCallbackPeriod,
22 SetIntensityCallbackThreshold,
23 GetIntensityCallbackThreshold,
24 SetDebouncePeriod,
25 GetDebouncePeriod,
26 GetIdentity,
27 CallbackIntensity,
28 CallbackIntensityReached,
29}
30impl From<SoundIntensityBrickletFunction> for u8 {
31 fn from(fun: SoundIntensityBrickletFunction) -> Self {
32 match fun {
33 SoundIntensityBrickletFunction::GetIntensity => 1,
34 SoundIntensityBrickletFunction::SetIntensityCallbackPeriod => 2,
35 SoundIntensityBrickletFunction::GetIntensityCallbackPeriod => 3,
36 SoundIntensityBrickletFunction::SetIntensityCallbackThreshold => 4,
37 SoundIntensityBrickletFunction::GetIntensityCallbackThreshold => 5,
38 SoundIntensityBrickletFunction::SetDebouncePeriod => 6,
39 SoundIntensityBrickletFunction::GetDebouncePeriod => 7,
40 SoundIntensityBrickletFunction::GetIdentity => 255,
41 SoundIntensityBrickletFunction::CallbackIntensity => 8,
42 SoundIntensityBrickletFunction::CallbackIntensityReached => 9,
43 }
44 }
45}
46pub const SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
47pub const SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
48pub const SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
49pub const SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
50pub const SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
51
52#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
53pub struct IntensityCallbackThreshold {
54 pub option: char,
55 pub min: u16,
56 pub max: u16,
57}
58impl FromByteSlice for IntensityCallbackThreshold {
59 fn bytes_expected() -> usize { 5 }
60 fn from_le_byte_slice(bytes: &[u8]) -> IntensityCallbackThreshold {
61 IntensityCallbackThreshold {
62 option: <char>::from_le_byte_slice(&bytes[0..1]),
63 min: <u16>::from_le_byte_slice(&bytes[1..3]),
64 max: <u16>::from_le_byte_slice(&bytes[3..5]),
65 }
66 }
67}
68
69#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
70pub struct Identity {
71 pub uid: String,
72 pub connected_uid: String,
73 pub position: char,
74 pub hardware_version: [u8; 3],
75 pub firmware_version: [u8; 3],
76 pub device_identifier: u16,
77}
78impl FromByteSlice for Identity {
79 fn bytes_expected() -> usize { 25 }
80 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
81 Identity {
82 uid: <String>::from_le_byte_slice(&bytes[0..8]),
83 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
84 position: <char>::from_le_byte_slice(&bytes[16..17]),
85 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
86 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
87 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
88 }
89 }
90}
91
92/// Measures sound intensity
93#[derive(Clone)]
94pub struct SoundIntensityBricklet {
95 device: Device,
96}
97impl SoundIntensityBricklet {
98 pub const DEVICE_IDENTIFIER: u16 = 238;
99 pub const DEVICE_DISPLAY_NAME: &'static str = "Sound Intensity Bricklet";
100 /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
101 pub fn new<T: GetRequestSender>(uid: &str, req_sender: T) -> SoundIntensityBricklet {
102 let mut result = SoundIntensityBricklet { device: Device::new([2, 0, 0], uid, req_sender, 0) };
103 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::GetIntensity) as usize] = ResponseExpectedFlag::AlwaysTrue;
104 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::SetIntensityCallbackPeriod) as usize] =
105 ResponseExpectedFlag::True;
106 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::GetIntensityCallbackPeriod) as usize] =
107 ResponseExpectedFlag::AlwaysTrue;
108 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::SetIntensityCallbackThreshold) as usize] =
109 ResponseExpectedFlag::True;
110 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::GetIntensityCallbackThreshold) as usize] =
111 ResponseExpectedFlag::AlwaysTrue;
112 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
113 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::GetDebouncePeriod) as usize] =
114 ResponseExpectedFlag::AlwaysTrue;
115 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
116 result
117 }
118
119 /// Returns the response expected flag for the function specified by the function ID parameter.
120 /// It is true if the function is expected to send a response, false otherwise.
121 ///
122 /// For getter functions this is enabled by default and cannot be disabled, because those
123 /// functions will always send a response. For callback configuration functions it is enabled
124 /// by default too, but can be disabled by [`set_response_expected`](crate::sound_intensity_bricklet::SoundIntensityBricklet::set_response_expected).
125 /// For setter functions it is disabled by default and can be enabled.
126 ///
127 /// Enabling the response expected flag for a setter function allows to detect timeouts
128 /// and other error conditions calls of this setter as well. The device will then send a response
129 /// for this purpose. If this flag is disabled for a setter function then no response is sent
130 /// and errors are silently ignored, because they cannot be detected.
131 ///
132 /// See [`set_response_expected`](crate::sound_intensity_bricklet::SoundIntensityBricklet::set_response_expected) for the list of function ID constants available for this function.
133 pub fn get_response_expected(&mut self, fun: SoundIntensityBrickletFunction) -> Result<bool, GetResponseExpectedError> {
134 self.device.get_response_expected(u8::from(fun))
135 }
136
137 /// Changes the response expected flag of the function specified by the function ID parameter.
138 /// This flag can only be changed for setter (default value: false) and callback configuration
139 /// functions (default value: true). For getter functions it is always enabled.
140 ///
141 /// Enabling the response expected flag for a setter function allows to detect timeouts and
142 /// other error conditions calls of this setter as well. The device will then send a response
143 /// for this purpose. If this flag is disabled for a setter function then no response is sent
144 /// and errors are silently ignored, because they cannot be detected.
145 pub fn set_response_expected(
146 &mut self,
147 fun: SoundIntensityBrickletFunction,
148 response_expected: bool,
149 ) -> Result<(), SetResponseExpectedError> {
150 self.device.set_response_expected(u8::from(fun), response_expected)
151 }
152
153 /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
154 pub fn set_response_expected_all(&mut self, response_expected: bool) { self.device.set_response_expected_all(response_expected) }
155
156 /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
157 /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
158 pub fn get_api_version(&self) -> [u8; 3] { self.device.api_version }
159
160 /// This receiver is triggered periodically with the period that is set by
161 /// [`set_intensity_callback_period`]. The parameter is the intensity
162 /// of the sensor.
163 ///
164 /// The [`get_intensity_callback_receiver`] receiver is only triggered if the intensity has changed
165 /// since the last triggering.
166 ///
167 /// [`set_intensity_callback_period`]: #method.set_intensity_callback_period
168 /// [`get_intensity_callback_receiver`]: #method.get_intensity_callback_receiver
169 pub fn get_intensity_callback_receiver(&self) -> ConvertingCallbackReceiver<u16> {
170 self.device.get_callback_receiver(u8::from(SoundIntensityBrickletFunction::CallbackIntensity))
171 }
172
173 /// This receiver is triggered when the threshold as set by
174 /// [`set_intensity_callback_threshold`] is reached.
175 /// The parameter is the intensity of the encoder.
176 ///
177 /// If the threshold keeps being reached, the receiver is triggered periodically
178 /// with the period as set by [`set_debounce_period`].
179 pub fn get_intensity_reached_callback_receiver(&self) -> ConvertingCallbackReceiver<u16> {
180 self.device.get_callback_receiver(u8::from(SoundIntensityBrickletFunction::CallbackIntensityReached))
181 }
182
183 /// Returns the current sound intensity.
184 ///
185 /// The value corresponds to the
186 /// [upper envelop](https://en.wikipedia.org/wiki/Envelope_(waves))__
187 /// of the signal of the microphone capsule.
188 ///
189 /// If you want to get the intensity periodically, it is recommended to use the
190 /// [`get_intensity_callback_receiver`] receiver and set the period with
191 /// [`set_intensity_callback_period`].
192 pub fn get_intensity(&self) -> ConvertingReceiver<u16> {
193 let payload = vec![0; 0];
194
195 self.device.get(u8::from(SoundIntensityBrickletFunction::GetIntensity), payload)
196 }
197
198 /// Sets the period with which the [`get_intensity_callback_receiver`] receiver is triggered
199 /// periodically. A value of 0 turns the receiver off.
200 ///
201 /// The [`get_intensity_callback_receiver`] receiver is only triggered if the intensity has changed
202 /// since the last triggering.
203 pub fn set_intensity_callback_period(&self, period: u32) -> ConvertingReceiver<()> {
204 let mut payload = vec![0; 4];
205 payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
206
207 self.device.set(u8::from(SoundIntensityBrickletFunction::SetIntensityCallbackPeriod), payload)
208 }
209
210 /// Returns the period as set by [`set_intensity_callback_period`].
211 pub fn get_intensity_callback_period(&self) -> ConvertingReceiver<u32> {
212 let payload = vec![0; 0];
213
214 self.device.get(u8::from(SoundIntensityBrickletFunction::GetIntensityCallbackPeriod), payload)
215 }
216
217 /// Sets the thresholds for the [`get_intensity_reached_callback_receiver`] receiver.
218 ///
219 /// The following options are possible:
220 ///
221 /// Option| Description
222 /// --- | ---
223 /// 'x'| Receiver is turned off
224 /// 'o'| Receiver is triggered when the intensity is *outside* the min and max values
225 /// 'i'| Receiver is triggered when the intensity is *inside* the min and max values
226 /// '<'| Receiver is triggered when the intensity is smaller than the min value (max is ignored)
227 /// '>'| Receiver is triggered when the intensity is greater than the min value (max is ignored)
228 ///
229 /// Associated constants:
230 /// * SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_OFF
231 /// * SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_OUTSIDE
232 /// * SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_INSIDE
233 /// * SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_SMALLER
234 /// * SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_GREATER
235 pub fn set_intensity_callback_threshold(&self, option: char, min: u16, max: u16) -> ConvertingReceiver<()> {
236 let mut payload = vec![0; 5];
237 payload[0..1].copy_from_slice(&<char>::to_le_byte_vec(option));
238 payload[1..3].copy_from_slice(&<u16>::to_le_byte_vec(min));
239 payload[3..5].copy_from_slice(&<u16>::to_le_byte_vec(max));
240
241 self.device.set(u8::from(SoundIntensityBrickletFunction::SetIntensityCallbackThreshold), payload)
242 }
243
244 /// Returns the threshold as set by [`set_intensity_callback_threshold`].
245 ///
246 /// Associated constants:
247 /// * SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_OFF
248 /// * SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_OUTSIDE
249 /// * SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_INSIDE
250 /// * SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_SMALLER
251 /// * SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_GREATER
252 pub fn get_intensity_callback_threshold(&self) -> ConvertingReceiver<IntensityCallbackThreshold> {
253 let payload = vec![0; 0];
254
255 self.device.get(u8::from(SoundIntensityBrickletFunction::GetIntensityCallbackThreshold), payload)
256 }
257
258 /// Sets the period with which the threshold receiver
259 ///
260 /// * [`get_intensity_reached_callback_receiver`]
261 ///
262 /// is triggered, if the thresholds
263 ///
264 /// * [`set_intensity_callback_threshold`]
265 ///
266 /// keeps being reached.
267 pub fn set_debounce_period(&self, debounce: u32) -> ConvertingReceiver<()> {
268 let mut payload = vec![0; 4];
269 payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(debounce));
270
271 self.device.set(u8::from(SoundIntensityBrickletFunction::SetDebouncePeriod), payload)
272 }
273
274 /// Returns the debounce period as set by [`set_debounce_period`].
275 pub fn get_debounce_period(&self) -> ConvertingReceiver<u32> {
276 let payload = vec![0; 0];
277
278 self.device.get(u8::from(SoundIntensityBrickletFunction::GetDebouncePeriod), payload)
279 }
280
281 /// Returns the UID, the UID where the Bricklet is connected to,
282 /// the position, the hardware and firmware version as well as the
283 /// device identifier.
284 ///
285 /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
286 /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
287 /// position 'z'.
288 ///
289 /// The device identifier numbers can be found [here](device_identifier).
290 /// |device_identifier_constant|
291 pub fn get_identity(&self) -> ConvertingReceiver<Identity> {
292 let payload = vec![0; 0];
293
294 self.device.get(u8::from(SoundIntensityBrickletFunction::GetIdentity), payload)
295 }
296}