tinkerforge_async/bindings/
co2_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 Co2BrickletFunction {
24 GetCo2Concentration,
25 SetCo2ConcentrationCallbackPeriod,
26 GetCo2ConcentrationCallbackPeriod,
27 SetCo2ConcentrationCallbackThreshold,
28 GetCo2ConcentrationCallbackThreshold,
29 SetDebouncePeriod,
30 GetDebouncePeriod,
31 GetIdentity,
32 CallbackCo2Concentration,
33 CallbackCo2ConcentrationReached,
34}
35impl From<Co2BrickletFunction> for u8 {
36 fn from(fun: Co2BrickletFunction) -> Self {
37 match fun {
38 Co2BrickletFunction::GetCo2Concentration => 1,
39 Co2BrickletFunction::SetCo2ConcentrationCallbackPeriod => 2,
40 Co2BrickletFunction::GetCo2ConcentrationCallbackPeriod => 3,
41 Co2BrickletFunction::SetCo2ConcentrationCallbackThreshold => 4,
42 Co2BrickletFunction::GetCo2ConcentrationCallbackThreshold => 5,
43 Co2BrickletFunction::SetDebouncePeriod => 6,
44 Co2BrickletFunction::GetDebouncePeriod => 7,
45 Co2BrickletFunction::GetIdentity => 255,
46 Co2BrickletFunction::CallbackCo2Concentration => 8,
47 Co2BrickletFunction::CallbackCo2ConcentrationReached => 9,
48 }
49 }
50}
51pub const CO2_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
52pub const CO2_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
53pub const CO2_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
54pub const CO2_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
55pub const CO2_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
56
57#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
58pub struct Co2ConcentrationCallbackThreshold {
59 pub option: char,
60 pub min: u16,
61 pub max: u16,
62}
63impl FromByteSlice for Co2ConcentrationCallbackThreshold {
64 fn bytes_expected() -> usize {
65 5
66 }
67 fn from_le_byte_slice(bytes: &[u8]) -> Co2ConcentrationCallbackThreshold {
68 Co2ConcentrationCallbackThreshold {
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 Co2Bricklet {
104 device: Device,
105}
106impl Co2Bricklet {
107 pub const DEVICE_IDENTIFIER: u16 = 262;
108 pub const DEVICE_DISPLAY_NAME: &'static str = "CO2 Bricklet";
109 pub fn new(uid: Uid, connection: AsyncIpConnection) -> Co2Bricklet {
111 let mut result = Co2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
112 result.device.response_expected[u8::from(Co2BrickletFunction::GetCo2Concentration) as usize] = ResponseExpectedFlag::AlwaysTrue;
113 result.device.response_expected[u8::from(Co2BrickletFunction::SetCo2ConcentrationCallbackPeriod) as usize] =
114 ResponseExpectedFlag::True;
115 result.device.response_expected[u8::from(Co2BrickletFunction::GetCo2ConcentrationCallbackPeriod) as usize] =
116 ResponseExpectedFlag::AlwaysTrue;
117 result.device.response_expected[u8::from(Co2BrickletFunction::SetCo2ConcentrationCallbackThreshold) as usize] =
118 ResponseExpectedFlag::True;
119 result.device.response_expected[u8::from(Co2BrickletFunction::GetCo2ConcentrationCallbackThreshold) as usize] =
120 ResponseExpectedFlag::AlwaysTrue;
121 result.device.response_expected[u8::from(Co2BrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
122 result.device.response_expected[u8::from(Co2BrickletFunction::GetDebouncePeriod) as usize] = ResponseExpectedFlag::AlwaysTrue;
123 result.device.response_expected[u8::from(Co2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
124 result
125 }
126
127 pub fn get_response_expected(&mut self, fun: Co2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
142 self.device.get_response_expected(u8::from(fun))
143 }
144
145 pub fn set_response_expected(&mut self, fun: Co2BrickletFunction, response_expected: bool) -> Result<(), SetResponseExpectedError> {
154 self.device.set_response_expected(u8::from(fun), response_expected)
155 }
156
157 pub fn set_response_expected_all(&mut self, response_expected: bool) {
159 self.device.set_response_expected_all(response_expected)
160 }
161
162 pub fn get_api_version(&self) -> [u8; 3] {
165 self.device.api_version
166 }
167
168 pub async fn get_co2_concentration_callback_receiver(&mut self) -> impl Stream<Item = u16> {
178 self.device
179 .get_callback_receiver(u8::from(Co2BrickletFunction::CallbackCo2Concentration))
180 .await
181 .map(|p| u16::from_le_byte_slice(p.body()))
182 }
183
184 pub async fn get_co2_concentration_reached_callback_receiver(&mut self) -> impl Stream<Item = u16> {
191 self.device
192 .get_callback_receiver(u8::from(Co2BrickletFunction::CallbackCo2ConcentrationReached))
193 .await
194 .map(|p| u16::from_le_byte_slice(p.body()))
195 }
196
197 pub async fn get_co2_concentration(&mut self) -> Result<u16, TinkerforgeError> {
203 let payload = [0; 0];
204
205 #[allow(unused_variables)]
206 let result = self.device.get(u8::from(Co2BrickletFunction::GetCo2Concentration), &payload).await?;
207 Ok(u16::from_le_byte_slice(result.body()))
208 }
209
210 pub async fn set_co2_concentration_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
216 let mut payload = [0; 4];
217 period.write_to_slice(&mut payload[0..4]);
218
219 #[allow(unused_variables)]
220 let result = self.device.set(u8::from(Co2BrickletFunction::SetCo2ConcentrationCallbackPeriod), &payload).await?;
221 Ok(())
222 }
223
224 pub async fn get_co2_concentration_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
226 let payload = [0; 0];
227
228 #[allow(unused_variables)]
229 let result = self.device.get(u8::from(Co2BrickletFunction::GetCo2ConcentrationCallbackPeriod), &payload).await?;
230 Ok(u32::from_le_byte_slice(result.body()))
231 }
232
233 pub async fn set_co2_concentration_callback_threshold(&mut self, option: char, min: u16, max: u16) -> Result<(), TinkerforgeError> {
252 let mut payload = [0; 5];
253 option.write_to_slice(&mut payload[0..1]);
254 min.write_to_slice(&mut payload[1..3]);
255 max.write_to_slice(&mut payload[3..5]);
256
257 #[allow(unused_variables)]
258 let result = self.device.set(u8::from(Co2BrickletFunction::SetCo2ConcentrationCallbackThreshold), &payload).await?;
259 Ok(())
260 }
261
262 pub async fn get_co2_concentration_callback_threshold(&mut self) -> Result<Co2ConcentrationCallbackThreshold, TinkerforgeError> {
271 let payload = [0; 0];
272
273 #[allow(unused_variables)]
274 let result = self.device.get(u8::from(Co2BrickletFunction::GetCo2ConcentrationCallbackThreshold), &payload).await?;
275 Ok(Co2ConcentrationCallbackThreshold::from_le_byte_slice(result.body()))
276 }
277
278 pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
288 let mut payload = [0; 4];
289 debounce.write_to_slice(&mut payload[0..4]);
290
291 #[allow(unused_variables)]
292 let result = self.device.set(u8::from(Co2BrickletFunction::SetDebouncePeriod), &payload).await?;
293 Ok(())
294 }
295
296 pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
298 let payload = [0; 0];
299
300 #[allow(unused_variables)]
301 let result = self.device.get(u8::from(Co2BrickletFunction::GetDebouncePeriod), &payload).await?;
302 Ok(u32::from_le_byte_slice(result.body()))
303 }
304
305 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
316 let payload = [0; 0];
317
318 #[allow(unused_variables)]
319 let result = self.device.get(u8::from(Co2BrickletFunction::GetIdentity), &payload).await?;
320 Ok(Identity::from_le_byte_slice(result.body()))
321 }
322}