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 PtcBrickletFunction {
24 GetTemperature,
25 GetResistance,
26 SetTemperatureCallbackPeriod,
27 GetTemperatureCallbackPeriod,
28 SetResistanceCallbackPeriod,
29 GetResistanceCallbackPeriod,
30 SetTemperatureCallbackThreshold,
31 GetTemperatureCallbackThreshold,
32 SetResistanceCallbackThreshold,
33 GetResistanceCallbackThreshold,
34 SetDebouncePeriod,
35 GetDebouncePeriod,
36 SetNoiseRejectionFilter,
37 GetNoiseRejectionFilter,
38 IsSensorConnected,
39 SetWireMode,
40 GetWireMode,
41 SetSensorConnectedCallbackConfiguration,
42 GetSensorConnectedCallbackConfiguration,
43 GetIdentity,
44 CallbackTemperature,
45 CallbackTemperatureReached,
46 CallbackResistance,
47 CallbackResistanceReached,
48 CallbackSensorConnected,
49}
50impl From<PtcBrickletFunction> for u8 {
51 fn from(fun: PtcBrickletFunction) -> Self {
52 match fun {
53 PtcBrickletFunction::GetTemperature => 1,
54 PtcBrickletFunction::GetResistance => 2,
55 PtcBrickletFunction::SetTemperatureCallbackPeriod => 3,
56 PtcBrickletFunction::GetTemperatureCallbackPeriod => 4,
57 PtcBrickletFunction::SetResistanceCallbackPeriod => 5,
58 PtcBrickletFunction::GetResistanceCallbackPeriod => 6,
59 PtcBrickletFunction::SetTemperatureCallbackThreshold => 7,
60 PtcBrickletFunction::GetTemperatureCallbackThreshold => 8,
61 PtcBrickletFunction::SetResistanceCallbackThreshold => 9,
62 PtcBrickletFunction::GetResistanceCallbackThreshold => 10,
63 PtcBrickletFunction::SetDebouncePeriod => 11,
64 PtcBrickletFunction::GetDebouncePeriod => 12,
65 PtcBrickletFunction::SetNoiseRejectionFilter => 17,
66 PtcBrickletFunction::GetNoiseRejectionFilter => 18,
67 PtcBrickletFunction::IsSensorConnected => 19,
68 PtcBrickletFunction::SetWireMode => 20,
69 PtcBrickletFunction::GetWireMode => 21,
70 PtcBrickletFunction::SetSensorConnectedCallbackConfiguration => 22,
71 PtcBrickletFunction::GetSensorConnectedCallbackConfiguration => 23,
72 PtcBrickletFunction::GetIdentity => 255,
73 PtcBrickletFunction::CallbackTemperature => 13,
74 PtcBrickletFunction::CallbackTemperatureReached => 14,
75 PtcBrickletFunction::CallbackResistance => 15,
76 PtcBrickletFunction::CallbackResistanceReached => 16,
77 PtcBrickletFunction::CallbackSensorConnected => 24,
78 }
79 }
80}
81pub const PTC_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
82pub const PTC_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
83pub const PTC_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
84pub const PTC_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
85pub const PTC_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
86pub const PTC_BRICKLET_FILTER_OPTION_50HZ: u8 = 0;
87pub const PTC_BRICKLET_FILTER_OPTION_60HZ: u8 = 1;
88pub const PTC_BRICKLET_WIRE_MODE_2: u8 = 2;
89pub const PTC_BRICKLET_WIRE_MODE_3: u8 = 3;
90pub const PTC_BRICKLET_WIRE_MODE_4: u8 = 4;
91
92#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
93pub struct TemperatureCallbackThreshold {
94 pub option: char,
95 pub min: i32,
96 pub max: i32,
97}
98impl FromByteSlice for TemperatureCallbackThreshold {
99 fn bytes_expected() -> usize {
100 9
101 }
102 fn from_le_byte_slice(bytes: &[u8]) -> TemperatureCallbackThreshold {
103 TemperatureCallbackThreshold {
104 option: <char>::from_le_byte_slice(&bytes[0..1]),
105 min: <i32>::from_le_byte_slice(&bytes[1..5]),
106 max: <i32>::from_le_byte_slice(&bytes[5..9]),
107 }
108 }
109}
110
111#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
112pub struct ResistanceCallbackThreshold {
113 pub option: char,
114 pub min: i32,
115 pub max: i32,
116}
117impl FromByteSlice for ResistanceCallbackThreshold {
118 fn bytes_expected() -> usize {
119 9
120 }
121 fn from_le_byte_slice(bytes: &[u8]) -> ResistanceCallbackThreshold {
122 ResistanceCallbackThreshold {
123 option: <char>::from_le_byte_slice(&bytes[0..1]),
124 min: <i32>::from_le_byte_slice(&bytes[1..5]),
125 max: <i32>::from_le_byte_slice(&bytes[5..9]),
126 }
127 }
128}
129
130#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
131pub struct Identity {
132 pub uid: String,
133 pub connected_uid: String,
134 pub position: char,
135 pub hardware_version: [u8; 3],
136 pub firmware_version: [u8; 3],
137 pub device_identifier: u16,
138}
139impl FromByteSlice for Identity {
140 fn bytes_expected() -> usize {
141 25
142 }
143 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
144 Identity {
145 uid: <String>::from_le_byte_slice(&bytes[0..8]),
146 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
147 position: <char>::from_le_byte_slice(&bytes[16..17]),
148 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
149 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
150 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
151 }
152 }
153}
154
155#[derive(Clone)]
157pub struct PtcBricklet {
158 device: Device,
159}
160impl PtcBricklet {
161 pub const DEVICE_IDENTIFIER: u16 = 226;
162 pub const DEVICE_DISPLAY_NAME: &'static str = "PTC Bricklet";
163 pub fn new(uid: Uid, connection: AsyncIpConnection) -> PtcBricklet {
165 let mut result = PtcBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
166 result.device.response_expected[u8::from(PtcBrickletFunction::GetTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
167 result.device.response_expected[u8::from(PtcBrickletFunction::GetResistance) as usize] = ResponseExpectedFlag::AlwaysTrue;
168 result.device.response_expected[u8::from(PtcBrickletFunction::SetTemperatureCallbackPeriod) as usize] = ResponseExpectedFlag::True;
169 result.device.response_expected[u8::from(PtcBrickletFunction::GetTemperatureCallbackPeriod) as usize] =
170 ResponseExpectedFlag::AlwaysTrue;
171 result.device.response_expected[u8::from(PtcBrickletFunction::SetResistanceCallbackPeriod) as usize] = ResponseExpectedFlag::True;
172 result.device.response_expected[u8::from(PtcBrickletFunction::GetResistanceCallbackPeriod) as usize] =
173 ResponseExpectedFlag::AlwaysTrue;
174 result.device.response_expected[u8::from(PtcBrickletFunction::SetTemperatureCallbackThreshold) as usize] =
175 ResponseExpectedFlag::True;
176 result.device.response_expected[u8::from(PtcBrickletFunction::GetTemperatureCallbackThreshold) as usize] =
177 ResponseExpectedFlag::AlwaysTrue;
178 result.device.response_expected[u8::from(PtcBrickletFunction::SetResistanceCallbackThreshold) as usize] =
179 ResponseExpectedFlag::True;
180 result.device.response_expected[u8::from(PtcBrickletFunction::GetResistanceCallbackThreshold) as usize] =
181 ResponseExpectedFlag::AlwaysTrue;
182 result.device.response_expected[u8::from(PtcBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
183 result.device.response_expected[u8::from(PtcBrickletFunction::GetDebouncePeriod) as usize] = ResponseExpectedFlag::AlwaysTrue;
184 result.device.response_expected[u8::from(PtcBrickletFunction::SetNoiseRejectionFilter) as usize] = ResponseExpectedFlag::False;
185 result.device.response_expected[u8::from(PtcBrickletFunction::GetNoiseRejectionFilter) as usize] = ResponseExpectedFlag::AlwaysTrue;
186 result.device.response_expected[u8::from(PtcBrickletFunction::IsSensorConnected) as usize] = ResponseExpectedFlag::AlwaysTrue;
187 result.device.response_expected[u8::from(PtcBrickletFunction::SetWireMode) as usize] = ResponseExpectedFlag::False;
188 result.device.response_expected[u8::from(PtcBrickletFunction::GetWireMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
189 result.device.response_expected[u8::from(PtcBrickletFunction::SetSensorConnectedCallbackConfiguration) as usize] =
190 ResponseExpectedFlag::True;
191 result.device.response_expected[u8::from(PtcBrickletFunction::GetSensorConnectedCallbackConfiguration) as usize] =
192 ResponseExpectedFlag::AlwaysTrue;
193 result.device.response_expected[u8::from(PtcBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
194 result
195 }
196
197 pub fn get_response_expected(&mut self, fun: PtcBrickletFunction) -> Result<bool, GetResponseExpectedError> {
212 self.device.get_response_expected(u8::from(fun))
213 }
214
215 pub fn set_response_expected(&mut self, fun: PtcBrickletFunction, response_expected: bool) -> Result<(), SetResponseExpectedError> {
224 self.device.set_response_expected(u8::from(fun), response_expected)
225 }
226
227 pub fn set_response_expected_all(&mut self, response_expected: bool) {
229 self.device.set_response_expected_all(response_expected)
230 }
231
232 pub fn get_api_version(&self) -> [u8; 3] {
235 self.device.api_version
236 }
237
238 pub async fn get_temperature_callback_receiver(&mut self) -> impl Stream<Item = i32> {
248 self.device
249 .get_callback_receiver(u8::from(PtcBrickletFunction::CallbackTemperature))
250 .await
251 .map(|p| i32::from_le_byte_slice(p.body()))
252 }
253
254 pub async fn get_temperature_reached_callback_receiver(&mut self) -> impl Stream<Item = i32> {
261 self.device
262 .get_callback_receiver(u8::from(PtcBrickletFunction::CallbackTemperatureReached))
263 .await
264 .map(|p| i32::from_le_byte_slice(p.body()))
265 }
266
267 pub async fn get_resistance_callback_receiver(&mut self) -> impl Stream<Item = i32> {
274 self.device
275 .get_callback_receiver(u8::from(PtcBrickletFunction::CallbackResistance))
276 .await
277 .map(|p| i32::from_le_byte_slice(p.body()))
278 }
279
280 pub async fn get_resistance_reached_callback_receiver(&mut self) -> impl Stream<Item = i32> {
287 self.device
288 .get_callback_receiver(u8::from(PtcBrickletFunction::CallbackResistanceReached))
289 .await
290 .map(|p| i32::from_le_byte_slice(p.body()))
291 }
292
293 pub async fn get_sensor_connected_callback_receiver(&mut self) -> impl Stream<Item = bool> {
301 self.device
302 .get_callback_receiver(u8::from(PtcBrickletFunction::CallbackSensorConnected))
303 .await
304 .map(|p| bool::from_le_byte_slice(p.body()))
305 }
306
307 pub async fn get_temperature(&mut self) -> Result<i32, TinkerforgeError> {
313 let payload = [0; 0];
314
315 #[allow(unused_variables)]
316 let result = self.device.get(u8::from(PtcBrickletFunction::GetTemperature), &payload).await?;
317 Ok(i32::from_le_byte_slice(result.body()))
318 }
319
320 pub async fn get_resistance(&mut self) -> Result<i32, TinkerforgeError> {
331 let payload = [0; 0];
332
333 #[allow(unused_variables)]
334 let result = self.device.get(u8::from(PtcBrickletFunction::GetResistance), &payload).await?;
335 Ok(i32::from_le_byte_slice(result.body()))
336 }
337
338 pub async fn set_temperature_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
344 let mut payload = [0; 4];
345 period.write_to_slice(&mut payload[0..4]);
346
347 #[allow(unused_variables)]
348 let result = self.device.set(u8::from(PtcBrickletFunction::SetTemperatureCallbackPeriod), &payload).await?;
349 Ok(())
350 }
351
352 pub async fn get_temperature_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
354 let payload = [0; 0];
355
356 #[allow(unused_variables)]
357 let result = self.device.get(u8::from(PtcBrickletFunction::GetTemperatureCallbackPeriod), &payload).await?;
358 Ok(u32::from_le_byte_slice(result.body()))
359 }
360
361 pub async fn set_resistance_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
367 let mut payload = [0; 4];
368 period.write_to_slice(&mut payload[0..4]);
369
370 #[allow(unused_variables)]
371 let result = self.device.set(u8::from(PtcBrickletFunction::SetResistanceCallbackPeriod), &payload).await?;
372 Ok(())
373 }
374
375 pub async fn get_resistance_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
377 let payload = [0; 0];
378
379 #[allow(unused_variables)]
380 let result = self.device.get(u8::from(PtcBrickletFunction::GetResistanceCallbackPeriod), &payload).await?;
381 Ok(u32::from_le_byte_slice(result.body()))
382 }
383
384 pub async fn set_temperature_callback_threshold(&mut self, option: char, min: i32, max: i32) -> Result<(), TinkerforgeError> {
403 let mut payload = [0; 9];
404 option.write_to_slice(&mut payload[0..1]);
405 min.write_to_slice(&mut payload[1..5]);
406 max.write_to_slice(&mut payload[5..9]);
407
408 #[allow(unused_variables)]
409 let result = self.device.set(u8::from(PtcBrickletFunction::SetTemperatureCallbackThreshold), &payload).await?;
410 Ok(())
411 }
412
413 pub async fn get_temperature_callback_threshold(&mut self) -> Result<TemperatureCallbackThreshold, TinkerforgeError> {
422 let payload = [0; 0];
423
424 #[allow(unused_variables)]
425 let result = self.device.get(u8::from(PtcBrickletFunction::GetTemperatureCallbackThreshold), &payload).await?;
426 Ok(TemperatureCallbackThreshold::from_le_byte_slice(result.body()))
427 }
428
429 pub async fn set_resistance_callback_threshold(&mut self, option: char, min: i32, max: i32) -> Result<(), TinkerforgeError> {
448 let mut payload = [0; 9];
449 option.write_to_slice(&mut payload[0..1]);
450 min.write_to_slice(&mut payload[1..5]);
451 max.write_to_slice(&mut payload[5..9]);
452
453 #[allow(unused_variables)]
454 let result = self.device.set(u8::from(PtcBrickletFunction::SetResistanceCallbackThreshold), &payload).await?;
455 Ok(())
456 }
457
458 pub async fn get_resistance_callback_threshold(&mut self) -> Result<ResistanceCallbackThreshold, TinkerforgeError> {
467 let payload = [0; 0];
468
469 #[allow(unused_variables)]
470 let result = self.device.get(u8::from(PtcBrickletFunction::GetResistanceCallbackThreshold), &payload).await?;
471 Ok(ResistanceCallbackThreshold::from_le_byte_slice(result.body()))
472 }
473
474 pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
486 let mut payload = [0; 4];
487 debounce.write_to_slice(&mut payload[0..4]);
488
489 #[allow(unused_variables)]
490 let result = self.device.set(u8::from(PtcBrickletFunction::SetDebouncePeriod), &payload).await?;
491 Ok(())
492 }
493
494 pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
496 let payload = [0; 0];
497
498 #[allow(unused_variables)]
499 let result = self.device.get(u8::from(PtcBrickletFunction::GetDebouncePeriod), &payload).await?;
500 Ok(u32::from_le_byte_slice(result.body()))
501 }
502
503 pub async fn set_noise_rejection_filter(&mut self, filter: u8) -> Result<(), TinkerforgeError> {
512 let mut payload = [0; 1];
513 filter.write_to_slice(&mut payload[0..1]);
514
515 #[allow(unused_variables)]
516 let result = self.device.set(u8::from(PtcBrickletFunction::SetNoiseRejectionFilter), &payload).await?;
517 Ok(())
518 }
519
520 pub async fn get_noise_rejection_filter(&mut self) -> Result<u8, TinkerforgeError> {
527 let payload = [0; 0];
528
529 #[allow(unused_variables)]
530 let result = self.device.get(u8::from(PtcBrickletFunction::GetNoiseRejectionFilter), &payload).await?;
531 Ok(u8::from_le_byte_slice(result.body()))
532 }
533
534 pub async fn is_sensor_connected(&mut self) -> Result<bool, TinkerforgeError> {
540 let payload = [0; 0];
541
542 #[allow(unused_variables)]
543 let result = self.device.get(u8::from(PtcBrickletFunction::IsSensorConnected), &payload).await?;
544 Ok(bool::from_le_byte_slice(result.body()))
545 }
546
547 pub async fn set_wire_mode(&mut self, mode: u8) -> Result<(), TinkerforgeError> {
556 let mut payload = [0; 1];
557 mode.write_to_slice(&mut payload[0..1]);
558
559 #[allow(unused_variables)]
560 let result = self.device.set(u8::from(PtcBrickletFunction::SetWireMode), &payload).await?;
561 Ok(())
562 }
563
564 pub async fn get_wire_mode(&mut self) -> Result<u8, TinkerforgeError> {
571 let payload = [0; 0];
572
573 #[allow(unused_variables)]
574 let result = self.device.get(u8::from(PtcBrickletFunction::GetWireMode), &payload).await?;
575 Ok(u8::from_le_byte_slice(result.body()))
576 }
577
578 pub async fn set_sensor_connected_callback_configuration(&mut self, enabled: bool) -> Result<(), TinkerforgeError> {
584 let mut payload = [0; 1];
585 enabled.write_to_slice(&mut payload[0..1]);
586
587 #[allow(unused_variables)]
588 let result = self.device.set(u8::from(PtcBrickletFunction::SetSensorConnectedCallbackConfiguration), &payload).await?;
589 Ok(())
590 }
591
592 pub async fn get_sensor_connected_callback_configuration(&mut self) -> Result<bool, TinkerforgeError> {
597 let payload = [0; 0];
598
599 #[allow(unused_variables)]
600 let result = self.device.get(u8::from(PtcBrickletFunction::GetSensorConnectedCallbackConfiguration), &payload).await?;
601 Ok(bool::from_le_byte_slice(result.body()))
602 }
603
604 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
615 let payload = [0; 0];
616
617 #[allow(unused_variables)]
618 let result = self.device.get(u8::from(PtcBrickletFunction::GetIdentity), &payload).await?;
619 Ok(Identity::from_le_byte_slice(result.body()))
620 }
621}