tinkerforge_async/bindings/
sound_intensity_bricklet.rs1#[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 SoundIntensityBrickletFunction {
24 GetIntensity,
25 SetIntensityCallbackPeriod,
26 GetIntensityCallbackPeriod,
27 SetIntensityCallbackThreshold,
28 GetIntensityCallbackThreshold,
29 SetDebouncePeriod,
30 GetDebouncePeriod,
31 GetIdentity,
32 CallbackIntensity,
33 CallbackIntensityReached,
34}
35impl From<SoundIntensityBrickletFunction> for u8 {
36 fn from(fun: SoundIntensityBrickletFunction) -> Self {
37 match fun {
38 SoundIntensityBrickletFunction::GetIntensity => 1,
39 SoundIntensityBrickletFunction::SetIntensityCallbackPeriod => 2,
40 SoundIntensityBrickletFunction::GetIntensityCallbackPeriod => 3,
41 SoundIntensityBrickletFunction::SetIntensityCallbackThreshold => 4,
42 SoundIntensityBrickletFunction::GetIntensityCallbackThreshold => 5,
43 SoundIntensityBrickletFunction::SetDebouncePeriod => 6,
44 SoundIntensityBrickletFunction::GetDebouncePeriod => 7,
45 SoundIntensityBrickletFunction::GetIdentity => 255,
46 SoundIntensityBrickletFunction::CallbackIntensity => 8,
47 SoundIntensityBrickletFunction::CallbackIntensityReached => 9,
48 }
49 }
50}
51pub const SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
52pub const SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
53pub const SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
54pub const SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
55pub const SOUND_INTENSITY_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
56
57#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
58pub struct IntensityCallbackThreshold {
59 pub option: char,
60 pub min: u16,
61 pub max: u16,
62}
63impl FromByteSlice for IntensityCallbackThreshold {
64 fn bytes_expected() -> usize {
65 5
66 }
67 fn from_le_byte_slice(bytes: &[u8]) -> IntensityCallbackThreshold {
68 IntensityCallbackThreshold {
69 option: <char>::from_le_byte_slice(&bytes[0..1]),
70 min: <u16>::from_le_byte_slice(&bytes[1..3]),
71 max: <u16>::from_le_byte_slice(&bytes[3..5]),
72 }
73 }
74}
75
76#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
77pub struct Identity {
78 pub uid: String,
79 pub connected_uid: String,
80 pub position: char,
81 pub hardware_version: [u8; 3],
82 pub firmware_version: [u8; 3],
83 pub device_identifier: u16,
84}
85impl FromByteSlice for Identity {
86 fn bytes_expected() -> usize {
87 25
88 }
89 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
90 Identity {
91 uid: <String>::from_le_byte_slice(&bytes[0..8]),
92 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
93 position: <char>::from_le_byte_slice(&bytes[16..17]),
94 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
95 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
96 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
97 }
98 }
99}
100
101#[derive(Clone)]
103pub struct SoundIntensityBricklet {
104 device: Device,
105}
106impl SoundIntensityBricklet {
107 pub const DEVICE_IDENTIFIER: u16 = 238;
108 pub const DEVICE_DISPLAY_NAME: &'static str = "Sound Intensity Bricklet";
109 pub fn new(uid: Uid, connection: AsyncIpConnection) -> SoundIntensityBricklet {
111 let mut result = SoundIntensityBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
112 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::GetIntensity) as usize] = ResponseExpectedFlag::AlwaysTrue;
113 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::SetIntensityCallbackPeriod) as usize] =
114 ResponseExpectedFlag::True;
115 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::GetIntensityCallbackPeriod) as usize] =
116 ResponseExpectedFlag::AlwaysTrue;
117 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::SetIntensityCallbackThreshold) as usize] =
118 ResponseExpectedFlag::True;
119 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::GetIntensityCallbackThreshold) as usize] =
120 ResponseExpectedFlag::AlwaysTrue;
121 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
122 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::GetDebouncePeriod) as usize] =
123 ResponseExpectedFlag::AlwaysTrue;
124 result.device.response_expected[u8::from(SoundIntensityBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
125 result
126 }
127
128 pub fn get_response_expected(&mut self, fun: SoundIntensityBrickletFunction) -> Result<bool, GetResponseExpectedError> {
143 self.device.get_response_expected(u8::from(fun))
144 }
145
146 pub fn set_response_expected(
155 &mut self,
156 fun: SoundIntensityBrickletFunction,
157 response_expected: bool,
158 ) -> Result<(), SetResponseExpectedError> {
159 self.device.set_response_expected(u8::from(fun), response_expected)
160 }
161
162 pub fn set_response_expected_all(&mut self, response_expected: bool) {
164 self.device.set_response_expected_all(response_expected)
165 }
166
167 pub fn get_api_version(&self) -> [u8; 3] {
170 self.device.api_version
171 }
172
173 pub async fn get_intensity_callback_receiver(&mut self) -> impl Stream<Item = u16> {
183 self.device
184 .get_callback_receiver(u8::from(SoundIntensityBrickletFunction::CallbackIntensity))
185 .await
186 .map(|p| u16::from_le_byte_slice(p.body()))
187 }
188
189 pub async fn get_intensity_reached_callback_receiver(&mut self) -> impl Stream<Item = u16> {
196 self.device
197 .get_callback_receiver(u8::from(SoundIntensityBrickletFunction::CallbackIntensityReached))
198 .await
199 .map(|p| u16::from_le_byte_slice(p.body()))
200 }
201
202 pub async fn get_intensity(&mut self) -> Result<u16, TinkerforgeError> {
212 let payload = [0; 0];
213
214 #[allow(unused_variables)]
215 let result = self.device.get(u8::from(SoundIntensityBrickletFunction::GetIntensity), &payload).await?;
216 Ok(u16::from_le_byte_slice(result.body()))
217 }
218
219 pub async fn set_intensity_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
225 let mut payload = [0; 4];
226 period.write_to_slice(&mut payload[0..4]);
227
228 #[allow(unused_variables)]
229 let result = self.device.set(u8::from(SoundIntensityBrickletFunction::SetIntensityCallbackPeriod), &payload).await?;
230 Ok(())
231 }
232
233 pub async fn get_intensity_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
235 let payload = [0; 0];
236
237 #[allow(unused_variables)]
238 let result = self.device.get(u8::from(SoundIntensityBrickletFunction::GetIntensityCallbackPeriod), &payload).await?;
239 Ok(u32::from_le_byte_slice(result.body()))
240 }
241
242 pub async fn set_intensity_callback_threshold(&mut self, option: char, min: u16, max: u16) -> Result<(), TinkerforgeError> {
261 let mut payload = [0; 5];
262 option.write_to_slice(&mut payload[0..1]);
263 min.write_to_slice(&mut payload[1..3]);
264 max.write_to_slice(&mut payload[3..5]);
265
266 #[allow(unused_variables)]
267 let result = self.device.set(u8::from(SoundIntensityBrickletFunction::SetIntensityCallbackThreshold), &payload).await?;
268 Ok(())
269 }
270
271 pub async fn get_intensity_callback_threshold(&mut self) -> Result<IntensityCallbackThreshold, TinkerforgeError> {
280 let payload = [0; 0];
281
282 #[allow(unused_variables)]
283 let result = self.device.get(u8::from(SoundIntensityBrickletFunction::GetIntensityCallbackThreshold), &payload).await?;
284 Ok(IntensityCallbackThreshold::from_le_byte_slice(result.body()))
285 }
286
287 pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
297 let mut payload = [0; 4];
298 debounce.write_to_slice(&mut payload[0..4]);
299
300 #[allow(unused_variables)]
301 let result = self.device.set(u8::from(SoundIntensityBrickletFunction::SetDebouncePeriod), &payload).await?;
302 Ok(())
303 }
304
305 pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
307 let payload = [0; 0];
308
309 #[allow(unused_variables)]
310 let result = self.device.get(u8::from(SoundIntensityBrickletFunction::GetDebouncePeriod), &payload).await?;
311 Ok(u32::from_le_byte_slice(result.body()))
312 }
313
314 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
325 let payload = [0; 0];
326
327 #[allow(unused_variables)]
328 let result = self.device.get(u8::from(SoundIntensityBrickletFunction::GetIdentity), &payload).await?;
329 Ok(Identity::from_le_byte_slice(result.body()))
330 }
331}