1#[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 BarometerBrickletFunction {
24 GetAirPressure,
25 GetAltitude,
26 SetAirPressureCallbackPeriod,
27 GetAirPressureCallbackPeriod,
28 SetAltitudeCallbackPeriod,
29 GetAltitudeCallbackPeriod,
30 SetAirPressureCallbackThreshold,
31 GetAirPressureCallbackThreshold,
32 SetAltitudeCallbackThreshold,
33 GetAltitudeCallbackThreshold,
34 SetDebouncePeriod,
35 GetDebouncePeriod,
36 SetReferenceAirPressure,
37 GetChipTemperature,
38 GetReferenceAirPressure,
39 SetAveraging,
40 GetAveraging,
41 SetI2cMode,
42 GetI2cMode,
43 GetIdentity,
44 CallbackAirPressure,
45 CallbackAltitude,
46 CallbackAirPressureReached,
47 CallbackAltitudeReached,
48}
49impl From<BarometerBrickletFunction> for u8 {
50 fn from(fun: BarometerBrickletFunction) -> Self {
51 match fun {
52 BarometerBrickletFunction::GetAirPressure => 1,
53 BarometerBrickletFunction::GetAltitude => 2,
54 BarometerBrickletFunction::SetAirPressureCallbackPeriod => 3,
55 BarometerBrickletFunction::GetAirPressureCallbackPeriod => 4,
56 BarometerBrickletFunction::SetAltitudeCallbackPeriod => 5,
57 BarometerBrickletFunction::GetAltitudeCallbackPeriod => 6,
58 BarometerBrickletFunction::SetAirPressureCallbackThreshold => 7,
59 BarometerBrickletFunction::GetAirPressureCallbackThreshold => 8,
60 BarometerBrickletFunction::SetAltitudeCallbackThreshold => 9,
61 BarometerBrickletFunction::GetAltitudeCallbackThreshold => 10,
62 BarometerBrickletFunction::SetDebouncePeriod => 11,
63 BarometerBrickletFunction::GetDebouncePeriod => 12,
64 BarometerBrickletFunction::SetReferenceAirPressure => 13,
65 BarometerBrickletFunction::GetChipTemperature => 14,
66 BarometerBrickletFunction::GetReferenceAirPressure => 19,
67 BarometerBrickletFunction::SetAveraging => 20,
68 BarometerBrickletFunction::GetAveraging => 21,
69 BarometerBrickletFunction::SetI2cMode => 22,
70 BarometerBrickletFunction::GetI2cMode => 23,
71 BarometerBrickletFunction::GetIdentity => 255,
72 BarometerBrickletFunction::CallbackAirPressure => 15,
73 BarometerBrickletFunction::CallbackAltitude => 16,
74 BarometerBrickletFunction::CallbackAirPressureReached => 17,
75 BarometerBrickletFunction::CallbackAltitudeReached => 18,
76 }
77 }
78}
79pub const BAROMETER_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
80pub const BAROMETER_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
81pub const BAROMETER_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
82pub const BAROMETER_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
83pub const BAROMETER_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
84pub const BAROMETER_BRICKLET_I2C_MODE_FAST: u8 = 0;
85pub const BAROMETER_BRICKLET_I2C_MODE_SLOW: u8 = 1;
86
87#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
88pub struct AirPressureCallbackThreshold {
89 pub option: char,
90 pub min: i32,
91 pub max: i32,
92}
93impl FromByteSlice for AirPressureCallbackThreshold {
94 fn bytes_expected() -> usize {
95 9
96 }
97 fn from_le_byte_slice(bytes: &[u8]) -> AirPressureCallbackThreshold {
98 AirPressureCallbackThreshold {
99 option: <char>::from_le_byte_slice(&bytes[0..1]),
100 min: <i32>::from_le_byte_slice(&bytes[1..5]),
101 max: <i32>::from_le_byte_slice(&bytes[5..9]),
102 }
103 }
104}
105
106#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
107pub struct AltitudeCallbackThreshold {
108 pub option: char,
109 pub min: i32,
110 pub max: i32,
111}
112impl FromByteSlice for AltitudeCallbackThreshold {
113 fn bytes_expected() -> usize {
114 9
115 }
116 fn from_le_byte_slice(bytes: &[u8]) -> AltitudeCallbackThreshold {
117 AltitudeCallbackThreshold {
118 option: <char>::from_le_byte_slice(&bytes[0..1]),
119 min: <i32>::from_le_byte_slice(&bytes[1..5]),
120 max: <i32>::from_le_byte_slice(&bytes[5..9]),
121 }
122 }
123}
124
125#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
126pub struct Averaging {
127 pub moving_average_pressure: u8,
128 pub average_pressure: u8,
129 pub average_temperature: u8,
130}
131impl FromByteSlice for Averaging {
132 fn bytes_expected() -> usize {
133 3
134 }
135 fn from_le_byte_slice(bytes: &[u8]) -> Averaging {
136 Averaging {
137 moving_average_pressure: <u8>::from_le_byte_slice(&bytes[0..1]),
138 average_pressure: <u8>::from_le_byte_slice(&bytes[1..2]),
139 average_temperature: <u8>::from_le_byte_slice(&bytes[2..3]),
140 }
141 }
142}
143
144#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
145pub struct Identity {
146 pub uid: String,
147 pub connected_uid: String,
148 pub position: char,
149 pub hardware_version: [u8; 3],
150 pub firmware_version: [u8; 3],
151 pub device_identifier: u16,
152}
153impl FromByteSlice for Identity {
154 fn bytes_expected() -> usize {
155 25
156 }
157 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
158 Identity {
159 uid: <String>::from_le_byte_slice(&bytes[0..8]),
160 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
161 position: <char>::from_le_byte_slice(&bytes[16..17]),
162 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
163 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
164 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
165 }
166 }
167}
168
169#[derive(Clone)]
171pub struct BarometerBricklet {
172 device: Device,
173}
174impl BarometerBricklet {
175 pub const DEVICE_IDENTIFIER: u16 = 221;
176 pub const DEVICE_DISPLAY_NAME: &'static str = "Barometer Bricklet";
177 pub fn new(uid: Uid, connection: AsyncIpConnection) -> BarometerBricklet {
179 let mut result = BarometerBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
180 result.device.response_expected[u8::from(BarometerBrickletFunction::GetAirPressure) as usize] = ResponseExpectedFlag::AlwaysTrue;
181 result.device.response_expected[u8::from(BarometerBrickletFunction::GetAltitude) as usize] = ResponseExpectedFlag::AlwaysTrue;
182 result.device.response_expected[u8::from(BarometerBrickletFunction::SetAirPressureCallbackPeriod) as usize] =
183 ResponseExpectedFlag::True;
184 result.device.response_expected[u8::from(BarometerBrickletFunction::GetAirPressureCallbackPeriod) as usize] =
185 ResponseExpectedFlag::AlwaysTrue;
186 result.device.response_expected[u8::from(BarometerBrickletFunction::SetAltitudeCallbackPeriod) as usize] =
187 ResponseExpectedFlag::True;
188 result.device.response_expected[u8::from(BarometerBrickletFunction::GetAltitudeCallbackPeriod) as usize] =
189 ResponseExpectedFlag::AlwaysTrue;
190 result.device.response_expected[u8::from(BarometerBrickletFunction::SetAirPressureCallbackThreshold) as usize] =
191 ResponseExpectedFlag::True;
192 result.device.response_expected[u8::from(BarometerBrickletFunction::GetAirPressureCallbackThreshold) as usize] =
193 ResponseExpectedFlag::AlwaysTrue;
194 result.device.response_expected[u8::from(BarometerBrickletFunction::SetAltitudeCallbackThreshold) as usize] =
195 ResponseExpectedFlag::True;
196 result.device.response_expected[u8::from(BarometerBrickletFunction::GetAltitudeCallbackThreshold) as usize] =
197 ResponseExpectedFlag::AlwaysTrue;
198 result.device.response_expected[u8::from(BarometerBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
199 result.device.response_expected[u8::from(BarometerBrickletFunction::GetDebouncePeriod) as usize] = ResponseExpectedFlag::AlwaysTrue;
200 result.device.response_expected[u8::from(BarometerBrickletFunction::SetReferenceAirPressure) as usize] =
201 ResponseExpectedFlag::False;
202 result.device.response_expected[u8::from(BarometerBrickletFunction::GetChipTemperature) as usize] =
203 ResponseExpectedFlag::AlwaysTrue;
204 result.device.response_expected[u8::from(BarometerBrickletFunction::GetReferenceAirPressure) as usize] =
205 ResponseExpectedFlag::AlwaysTrue;
206 result.device.response_expected[u8::from(BarometerBrickletFunction::SetAveraging) as usize] = ResponseExpectedFlag::False;
207 result.device.response_expected[u8::from(BarometerBrickletFunction::GetAveraging) as usize] = ResponseExpectedFlag::AlwaysTrue;
208 result.device.response_expected[u8::from(BarometerBrickletFunction::SetI2cMode) as usize] = ResponseExpectedFlag::False;
209 result.device.response_expected[u8::from(BarometerBrickletFunction::GetI2cMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
210 result.device.response_expected[u8::from(BarometerBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
211 result
212 }
213
214 pub fn get_response_expected(&mut self, fun: BarometerBrickletFunction) -> Result<bool, GetResponseExpectedError> {
229 self.device.get_response_expected(u8::from(fun))
230 }
231
232 pub fn set_response_expected(
241 &mut self,
242 fun: BarometerBrickletFunction,
243 response_expected: bool,
244 ) -> Result<(), SetResponseExpectedError> {
245 self.device.set_response_expected(u8::from(fun), response_expected)
246 }
247
248 pub fn set_response_expected_all(&mut self, response_expected: bool) {
250 self.device.set_response_expected_all(response_expected)
251 }
252
253 pub fn get_api_version(&self) -> [u8; 3] {
256 self.device.api_version
257 }
258
259 pub async fn get_air_pressure_callback_receiver(&mut self) -> impl Stream<Item = i32> {
269 self.device
270 .get_callback_receiver(u8::from(BarometerBrickletFunction::CallbackAirPressure))
271 .await
272 .map(|p| i32::from_le_byte_slice(p.body()))
273 }
274
275 pub async fn get_altitude_callback_receiver(&mut self) -> impl Stream<Item = i32> {
282 self.device
283 .get_callback_receiver(u8::from(BarometerBrickletFunction::CallbackAltitude))
284 .await
285 .map(|p| i32::from_le_byte_slice(p.body()))
286 }
287
288 pub async fn get_air_pressure_reached_callback_receiver(&mut self) -> impl Stream<Item = i32> {
295 self.device
296 .get_callback_receiver(u8::from(BarometerBrickletFunction::CallbackAirPressureReached))
297 .await
298 .map(|p| i32::from_le_byte_slice(p.body()))
299 }
300
301 pub async fn get_altitude_reached_callback_receiver(&mut self) -> impl Stream<Item = i32> {
308 self.device
309 .get_callback_receiver(u8::from(BarometerBrickletFunction::CallbackAltitudeReached))
310 .await
311 .map(|p| i32::from_le_byte_slice(p.body()))
312 }
313
314 pub async fn get_air_pressure(&mut self) -> Result<i32, TinkerforgeError> {
320 let payload = [0; 0];
321
322 #[allow(unused_variables)]
323 let result = self.device.get(u8::from(BarometerBrickletFunction::GetAirPressure), &payload).await?;
324 Ok(i32::from_le_byte_slice(result.body()))
325 }
326
327 pub async fn get_altitude(&mut self) -> Result<i32, TinkerforgeError> {
335 let payload = [0; 0];
336
337 #[allow(unused_variables)]
338 let result = self.device.get(u8::from(BarometerBrickletFunction::GetAltitude), &payload).await?;
339 Ok(i32::from_le_byte_slice(result.body()))
340 }
341
342 pub async fn set_air_pressure_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
348 let mut payload = [0; 4];
349 period.write_to_slice(&mut payload[0..4]);
350
351 #[allow(unused_variables)]
352 let result = self.device.set(u8::from(BarometerBrickletFunction::SetAirPressureCallbackPeriod), &payload).await?;
353 Ok(())
354 }
355
356 pub async fn get_air_pressure_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
358 let payload = [0; 0];
359
360 #[allow(unused_variables)]
361 let result = self.device.get(u8::from(BarometerBrickletFunction::GetAirPressureCallbackPeriod), &payload).await?;
362 Ok(u32::from_le_byte_slice(result.body()))
363 }
364
365 pub async fn set_altitude_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
371 let mut payload = [0; 4];
372 period.write_to_slice(&mut payload[0..4]);
373
374 #[allow(unused_variables)]
375 let result = self.device.set(u8::from(BarometerBrickletFunction::SetAltitudeCallbackPeriod), &payload).await?;
376 Ok(())
377 }
378
379 pub async fn get_altitude_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
381 let payload = [0; 0];
382
383 #[allow(unused_variables)]
384 let result = self.device.get(u8::from(BarometerBrickletFunction::GetAltitudeCallbackPeriod), &payload).await?;
385 Ok(u32::from_le_byte_slice(result.body()))
386 }
387
388 pub async fn set_air_pressure_callback_threshold(&mut self, option: char, min: i32, max: i32) -> Result<(), TinkerforgeError> {
407 let mut payload = [0; 9];
408 option.write_to_slice(&mut payload[0..1]);
409 min.write_to_slice(&mut payload[1..5]);
410 max.write_to_slice(&mut payload[5..9]);
411
412 #[allow(unused_variables)]
413 let result = self.device.set(u8::from(BarometerBrickletFunction::SetAirPressureCallbackThreshold), &payload).await?;
414 Ok(())
415 }
416
417 pub async fn get_air_pressure_callback_threshold(&mut self) -> Result<AirPressureCallbackThreshold, TinkerforgeError> {
426 let payload = [0; 0];
427
428 #[allow(unused_variables)]
429 let result = self.device.get(u8::from(BarometerBrickletFunction::GetAirPressureCallbackThreshold), &payload).await?;
430 Ok(AirPressureCallbackThreshold::from_le_byte_slice(result.body()))
431 }
432
433 pub async fn set_altitude_callback_threshold(&mut self, option: char, min: i32, max: i32) -> Result<(), TinkerforgeError> {
452 let mut payload = [0; 9];
453 option.write_to_slice(&mut payload[0..1]);
454 min.write_to_slice(&mut payload[1..5]);
455 max.write_to_slice(&mut payload[5..9]);
456
457 #[allow(unused_variables)]
458 let result = self.device.set(u8::from(BarometerBrickletFunction::SetAltitudeCallbackThreshold), &payload).await?;
459 Ok(())
460 }
461
462 pub async fn get_altitude_callback_threshold(&mut self) -> Result<AltitudeCallbackThreshold, TinkerforgeError> {
471 let payload = [0; 0];
472
473 #[allow(unused_variables)]
474 let result = self.device.get(u8::from(BarometerBrickletFunction::GetAltitudeCallbackThreshold), &payload).await?;
475 Ok(AltitudeCallbackThreshold::from_le_byte_slice(result.body()))
476 }
477
478 pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
490 let mut payload = [0; 4];
491 debounce.write_to_slice(&mut payload[0..4]);
492
493 #[allow(unused_variables)]
494 let result = self.device.set(u8::from(BarometerBrickletFunction::SetDebouncePeriod), &payload).await?;
495 Ok(())
496 }
497
498 pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
500 let payload = [0; 0];
501
502 #[allow(unused_variables)]
503 let result = self.device.get(u8::from(BarometerBrickletFunction::GetDebouncePeriod), &payload).await?;
504 Ok(u32::from_le_byte_slice(result.body()))
505 }
506
507 pub async fn set_reference_air_pressure(&mut self, air_pressure: i32) -> Result<(), TinkerforgeError> {
517 let mut payload = [0; 4];
518 air_pressure.write_to_slice(&mut payload[0..4]);
519
520 #[allow(unused_variables)]
521 let result = self.device.set(u8::from(BarometerBrickletFunction::SetReferenceAirPressure), &payload).await?;
522 Ok(())
523 }
524
525 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
531 let payload = [0; 0];
532
533 #[allow(unused_variables)]
534 let result = self.device.get(u8::from(BarometerBrickletFunction::GetChipTemperature), &payload).await?;
535 Ok(i16::from_le_byte_slice(result.body()))
536 }
537
538 pub async fn get_reference_air_pressure(&mut self) -> Result<i32, TinkerforgeError> {
540 let payload = [0; 0];
541
542 #[allow(unused_variables)]
543 let result = self.device.get(u8::from(BarometerBrickletFunction::GetReferenceAirPressure), &payload).await?;
544 Ok(i32::from_le_byte_slice(result.body()))
545 }
546
547 pub async fn set_averaging(
563 &mut self,
564 moving_average_pressure: u8,
565 average_pressure: u8,
566 average_temperature: u8,
567 ) -> Result<(), TinkerforgeError> {
568 let mut payload = [0; 3];
569 moving_average_pressure.write_to_slice(&mut payload[0..1]);
570 average_pressure.write_to_slice(&mut payload[1..2]);
571 average_temperature.write_to_slice(&mut payload[2..3]);
572
573 #[allow(unused_variables)]
574 let result = self.device.set(u8::from(BarometerBrickletFunction::SetAveraging), &payload).await?;
575 Ok(())
576 }
577
578 pub async fn get_averaging(&mut self) -> Result<Averaging, TinkerforgeError> {
583 let payload = [0; 0];
584
585 #[allow(unused_variables)]
586 let result = self.device.get(u8::from(BarometerBrickletFunction::GetAveraging), &payload).await?;
587 Ok(Averaging::from_le_byte_slice(result.body()))
588 }
589
590 pub async fn set_i2c_mode(&mut self, mode: u8) -> Result<(), TinkerforgeError> {
609 let mut payload = [0; 1];
610 mode.write_to_slice(&mut payload[0..1]);
611
612 #[allow(unused_variables)]
613 let result = self.device.set(u8::from(BarometerBrickletFunction::SetI2cMode), &payload).await?;
614 Ok(())
615 }
616
617 pub async fn get_i2c_mode(&mut self) -> Result<u8, TinkerforgeError> {
626 let payload = [0; 0];
627
628 #[allow(unused_variables)]
629 let result = self.device.get(u8::from(BarometerBrickletFunction::GetI2cMode), &payload).await?;
630 Ok(u8::from_le_byte_slice(result.body()))
631 }
632
633 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
644 let payload = [0; 0];
645
646 #[allow(unused_variables)]
647 let result = self.device.get(u8::from(BarometerBrickletFunction::GetIdentity), &payload).await?;
648 Ok(Identity::from_le_byte_slice(result.body()))
649 }
650}