tinkerforge_async/bindings/
dust_detector_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 DustDetectorBrickletFunction {
24 GetDustDensity,
25 SetDustDensityCallbackPeriod,
26 GetDustDensityCallbackPeriod,
27 SetDustDensityCallbackThreshold,
28 GetDustDensityCallbackThreshold,
29 SetDebouncePeriod,
30 GetDebouncePeriod,
31 SetMovingAverage,
32 GetMovingAverage,
33 GetIdentity,
34 CallbackDustDensity,
35 CallbackDustDensityReached,
36}
37impl From<DustDetectorBrickletFunction> for u8 {
38 fn from(fun: DustDetectorBrickletFunction) -> Self {
39 match fun {
40 DustDetectorBrickletFunction::GetDustDensity => 1,
41 DustDetectorBrickletFunction::SetDustDensityCallbackPeriod => 2,
42 DustDetectorBrickletFunction::GetDustDensityCallbackPeriod => 3,
43 DustDetectorBrickletFunction::SetDustDensityCallbackThreshold => 4,
44 DustDetectorBrickletFunction::GetDustDensityCallbackThreshold => 5,
45 DustDetectorBrickletFunction::SetDebouncePeriod => 6,
46 DustDetectorBrickletFunction::GetDebouncePeriod => 7,
47 DustDetectorBrickletFunction::SetMovingAverage => 10,
48 DustDetectorBrickletFunction::GetMovingAverage => 11,
49 DustDetectorBrickletFunction::GetIdentity => 255,
50 DustDetectorBrickletFunction::CallbackDustDensity => 8,
51 DustDetectorBrickletFunction::CallbackDustDensityReached => 9,
52 }
53 }
54}
55pub const DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
56pub const DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
57pub const DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
58pub const DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
59pub const DUST_DETECTOR_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
60
61#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
62pub struct DustDensityCallbackThreshold {
63 pub option: char,
64 pub min: u16,
65 pub max: u16,
66}
67impl FromByteSlice for DustDensityCallbackThreshold {
68 fn bytes_expected() -> usize {
69 5
70 }
71 fn from_le_byte_slice(bytes: &[u8]) -> DustDensityCallbackThreshold {
72 DustDensityCallbackThreshold {
73 option: <char>::from_le_byte_slice(&bytes[0..1]),
74 min: <u16>::from_le_byte_slice(&bytes[1..3]),
75 max: <u16>::from_le_byte_slice(&bytes[3..5]),
76 }
77 }
78}
79
80#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
81pub struct Identity {
82 pub uid: String,
83 pub connected_uid: String,
84 pub position: char,
85 pub hardware_version: [u8; 3],
86 pub firmware_version: [u8; 3],
87 pub device_identifier: u16,
88}
89impl FromByteSlice for Identity {
90 fn bytes_expected() -> usize {
91 25
92 }
93 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
94 Identity {
95 uid: <String>::from_le_byte_slice(&bytes[0..8]),
96 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
97 position: <char>::from_le_byte_slice(&bytes[16..17]),
98 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
99 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
100 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
101 }
102 }
103}
104
105#[derive(Clone)]
107pub struct DustDetectorBricklet {
108 device: Device,
109}
110impl DustDetectorBricklet {
111 pub const DEVICE_IDENTIFIER: u16 = 260;
112 pub const DEVICE_DISPLAY_NAME: &'static str = "Dust Detector Bricklet";
113 pub fn new(uid: Uid, connection: AsyncIpConnection) -> DustDetectorBricklet {
115 let mut result = DustDetectorBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
116 result.device.response_expected[u8::from(DustDetectorBrickletFunction::GetDustDensity) as usize] = ResponseExpectedFlag::AlwaysTrue;
117 result.device.response_expected[u8::from(DustDetectorBrickletFunction::SetDustDensityCallbackPeriod) as usize] =
118 ResponseExpectedFlag::True;
119 result.device.response_expected[u8::from(DustDetectorBrickletFunction::GetDustDensityCallbackPeriod) as usize] =
120 ResponseExpectedFlag::AlwaysTrue;
121 result.device.response_expected[u8::from(DustDetectorBrickletFunction::SetDustDensityCallbackThreshold) as usize] =
122 ResponseExpectedFlag::True;
123 result.device.response_expected[u8::from(DustDetectorBrickletFunction::GetDustDensityCallbackThreshold) as usize] =
124 ResponseExpectedFlag::AlwaysTrue;
125 result.device.response_expected[u8::from(DustDetectorBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
126 result.device.response_expected[u8::from(DustDetectorBrickletFunction::GetDebouncePeriod) as usize] =
127 ResponseExpectedFlag::AlwaysTrue;
128 result.device.response_expected[u8::from(DustDetectorBrickletFunction::SetMovingAverage) as usize] = ResponseExpectedFlag::False;
129 result.device.response_expected[u8::from(DustDetectorBrickletFunction::GetMovingAverage) as usize] =
130 ResponseExpectedFlag::AlwaysTrue;
131 result.device.response_expected[u8::from(DustDetectorBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
132 result
133 }
134
135 pub fn get_response_expected(&mut self, fun: DustDetectorBrickletFunction) -> Result<bool, GetResponseExpectedError> {
150 self.device.get_response_expected(u8::from(fun))
151 }
152
153 pub fn set_response_expected(
162 &mut self,
163 fun: DustDetectorBrickletFunction,
164 response_expected: bool,
165 ) -> Result<(), SetResponseExpectedError> {
166 self.device.set_response_expected(u8::from(fun), response_expected)
167 }
168
169 pub fn set_response_expected_all(&mut self, response_expected: bool) {
171 self.device.set_response_expected_all(response_expected)
172 }
173
174 pub fn get_api_version(&self) -> [u8; 3] {
177 self.device.api_version
178 }
179
180 pub async fn get_dust_density_callback_receiver(&mut self) -> impl Stream<Item = u16> {
190 self.device
191 .get_callback_receiver(u8::from(DustDetectorBrickletFunction::CallbackDustDensity))
192 .await
193 .map(|p| u16::from_le_byte_slice(p.body()))
194 }
195
196 pub async fn get_dust_density_reached_callback_receiver(&mut self) -> impl Stream<Item = u16> {
203 self.device
204 .get_callback_receiver(u8::from(DustDetectorBrickletFunction::CallbackDustDensityReached))
205 .await
206 .map(|p| u16::from_le_byte_slice(p.body()))
207 }
208
209 pub async fn get_dust_density(&mut self) -> Result<u16, TinkerforgeError> {
215 let payload = [0; 0];
216
217 #[allow(unused_variables)]
218 let result = self.device.get(u8::from(DustDetectorBrickletFunction::GetDustDensity), &payload).await?;
219 Ok(u16::from_le_byte_slice(result.body()))
220 }
221
222 pub async fn set_dust_density_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
228 let mut payload = [0; 4];
229 period.write_to_slice(&mut payload[0..4]);
230
231 #[allow(unused_variables)]
232 let result = self.device.set(u8::from(DustDetectorBrickletFunction::SetDustDensityCallbackPeriod), &payload).await?;
233 Ok(())
234 }
235
236 pub async fn get_dust_density_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
238 let payload = [0; 0];
239
240 #[allow(unused_variables)]
241 let result = self.device.get(u8::from(DustDetectorBrickletFunction::GetDustDensityCallbackPeriod), &payload).await?;
242 Ok(u32::from_le_byte_slice(result.body()))
243 }
244
245 pub async fn set_dust_density_callback_threshold(&mut self, option: char, min: u16, max: u16) -> Result<(), TinkerforgeError> {
264 let mut payload = [0; 5];
265 option.write_to_slice(&mut payload[0..1]);
266 min.write_to_slice(&mut payload[1..3]);
267 max.write_to_slice(&mut payload[3..5]);
268
269 #[allow(unused_variables)]
270 let result = self.device.set(u8::from(DustDetectorBrickletFunction::SetDustDensityCallbackThreshold), &payload).await?;
271 Ok(())
272 }
273
274 pub async fn get_dust_density_callback_threshold(&mut self) -> Result<DustDensityCallbackThreshold, TinkerforgeError> {
283 let payload = [0; 0];
284
285 #[allow(unused_variables)]
286 let result = self.device.get(u8::from(DustDetectorBrickletFunction::GetDustDensityCallbackThreshold), &payload).await?;
287 Ok(DustDensityCallbackThreshold::from_le_byte_slice(result.body()))
288 }
289
290 pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
300 let mut payload = [0; 4];
301 debounce.write_to_slice(&mut payload[0..4]);
302
303 #[allow(unused_variables)]
304 let result = self.device.set(u8::from(DustDetectorBrickletFunction::SetDebouncePeriod), &payload).await?;
305 Ok(())
306 }
307
308 pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
310 let payload = [0; 0];
311
312 #[allow(unused_variables)]
313 let result = self.device.get(u8::from(DustDetectorBrickletFunction::GetDebouncePeriod), &payload).await?;
314 Ok(u32::from_le_byte_slice(result.body()))
315 }
316
317 pub async fn set_moving_average(&mut self, average: u8) -> Result<(), TinkerforgeError> {
323 let mut payload = [0; 1];
324 average.write_to_slice(&mut payload[0..1]);
325
326 #[allow(unused_variables)]
327 let result = self.device.set(u8::from(DustDetectorBrickletFunction::SetMovingAverage), &payload).await?;
328 Ok(())
329 }
330
331 pub async fn get_moving_average(&mut self) -> Result<u8, TinkerforgeError> {
333 let payload = [0; 0];
334
335 #[allow(unused_variables)]
336 let result = self.device.get(u8::from(DustDetectorBrickletFunction::GetMovingAverage), &payload).await?;
337 Ok(u8::from_le_byte_slice(result.body()))
338 }
339
340 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
351 let payload = [0; 0];
352
353 #[allow(unused_variables)]
354 let result = self.device.get(u8::from(DustDetectorBrickletFunction::GetIdentity), &payload).await?;
355 Ok(Identity::from_le_byte_slice(result.body()))
356 }
357}