tinkerforge_async/bindings/
thermocouple_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 ThermocoupleBrickletFunction {
24 GetTemperature,
25 SetTemperatureCallbackPeriod,
26 GetTemperatureCallbackPeriod,
27 SetTemperatureCallbackThreshold,
28 GetTemperatureCallbackThreshold,
29 SetDebouncePeriod,
30 GetDebouncePeriod,
31 SetConfiguration,
32 GetConfiguration,
33 GetErrorState,
34 GetIdentity,
35 CallbackTemperature,
36 CallbackTemperatureReached,
37 CallbackErrorState,
38}
39impl From<ThermocoupleBrickletFunction> for u8 {
40 fn from(fun: ThermocoupleBrickletFunction) -> Self {
41 match fun {
42 ThermocoupleBrickletFunction::GetTemperature => 1,
43 ThermocoupleBrickletFunction::SetTemperatureCallbackPeriod => 2,
44 ThermocoupleBrickletFunction::GetTemperatureCallbackPeriod => 3,
45 ThermocoupleBrickletFunction::SetTemperatureCallbackThreshold => 4,
46 ThermocoupleBrickletFunction::GetTemperatureCallbackThreshold => 5,
47 ThermocoupleBrickletFunction::SetDebouncePeriod => 6,
48 ThermocoupleBrickletFunction::GetDebouncePeriod => 7,
49 ThermocoupleBrickletFunction::SetConfiguration => 10,
50 ThermocoupleBrickletFunction::GetConfiguration => 11,
51 ThermocoupleBrickletFunction::GetErrorState => 12,
52 ThermocoupleBrickletFunction::GetIdentity => 255,
53 ThermocoupleBrickletFunction::CallbackTemperature => 8,
54 ThermocoupleBrickletFunction::CallbackTemperatureReached => 9,
55 ThermocoupleBrickletFunction::CallbackErrorState => 13,
56 }
57 }
58}
59pub const THERMOCOUPLE_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
60pub const THERMOCOUPLE_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
61pub const THERMOCOUPLE_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
62pub const THERMOCOUPLE_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
63pub const THERMOCOUPLE_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
64pub const THERMOCOUPLE_BRICKLET_AVERAGING_1: u8 = 1;
65pub const THERMOCOUPLE_BRICKLET_AVERAGING_2: u8 = 2;
66pub const THERMOCOUPLE_BRICKLET_AVERAGING_4: u8 = 4;
67pub const THERMOCOUPLE_BRICKLET_AVERAGING_8: u8 = 8;
68pub const THERMOCOUPLE_BRICKLET_AVERAGING_16: u8 = 16;
69pub const THERMOCOUPLE_BRICKLET_TYPE_B: u8 = 0;
70pub const THERMOCOUPLE_BRICKLET_TYPE_E: u8 = 1;
71pub const THERMOCOUPLE_BRICKLET_TYPE_J: u8 = 2;
72pub const THERMOCOUPLE_BRICKLET_TYPE_K: u8 = 3;
73pub const THERMOCOUPLE_BRICKLET_TYPE_N: u8 = 4;
74pub const THERMOCOUPLE_BRICKLET_TYPE_R: u8 = 5;
75pub const THERMOCOUPLE_BRICKLET_TYPE_S: u8 = 6;
76pub const THERMOCOUPLE_BRICKLET_TYPE_T: u8 = 7;
77pub const THERMOCOUPLE_BRICKLET_TYPE_G8: u8 = 8;
78pub const THERMOCOUPLE_BRICKLET_TYPE_G32: u8 = 9;
79pub const THERMOCOUPLE_BRICKLET_FILTER_OPTION_50HZ: u8 = 0;
80pub const THERMOCOUPLE_BRICKLET_FILTER_OPTION_60HZ: u8 = 1;
81
82#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
83pub struct TemperatureCallbackThreshold {
84 pub option: char,
85 pub min: i32,
86 pub max: i32,
87}
88impl FromByteSlice for TemperatureCallbackThreshold {
89 fn bytes_expected() -> usize {
90 9
91 }
92 fn from_le_byte_slice(bytes: &[u8]) -> TemperatureCallbackThreshold {
93 TemperatureCallbackThreshold {
94 option: <char>::from_le_byte_slice(&bytes[0..1]),
95 min: <i32>::from_le_byte_slice(&bytes[1..5]),
96 max: <i32>::from_le_byte_slice(&bytes[5..9]),
97 }
98 }
99}
100
101#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
102pub struct Configuration {
103 pub averaging: u8,
104 pub thermocouple_type: u8,
105 pub filter: u8,
106}
107impl FromByteSlice for Configuration {
108 fn bytes_expected() -> usize {
109 3
110 }
111 fn from_le_byte_slice(bytes: &[u8]) -> Configuration {
112 Configuration {
113 averaging: <u8>::from_le_byte_slice(&bytes[0..1]),
114 thermocouple_type: <u8>::from_le_byte_slice(&bytes[1..2]),
115 filter: <u8>::from_le_byte_slice(&bytes[2..3]),
116 }
117 }
118}
119
120#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
121pub struct ErrorState {
122 pub over_under: bool,
123 pub open_circuit: bool,
124}
125impl FromByteSlice for ErrorState {
126 fn bytes_expected() -> usize {
127 2
128 }
129 fn from_le_byte_slice(bytes: &[u8]) -> ErrorState {
130 ErrorState { over_under: <bool>::from_le_byte_slice(&bytes[0..1]), open_circuit: <bool>::from_le_byte_slice(&bytes[1..2]) }
131 }
132}
133
134#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
135pub struct ErrorStateEvent {
136 pub over_under: bool,
137 pub open_circuit: bool,
138}
139impl FromByteSlice for ErrorStateEvent {
140 fn bytes_expected() -> usize {
141 2
142 }
143 fn from_le_byte_slice(bytes: &[u8]) -> ErrorStateEvent {
144 ErrorStateEvent { over_under: <bool>::from_le_byte_slice(&bytes[0..1]), open_circuit: <bool>::from_le_byte_slice(&bytes[1..2]) }
145 }
146}
147
148#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
149pub struct Identity {
150 pub uid: String,
151 pub connected_uid: String,
152 pub position: char,
153 pub hardware_version: [u8; 3],
154 pub firmware_version: [u8; 3],
155 pub device_identifier: u16,
156}
157impl FromByteSlice for Identity {
158 fn bytes_expected() -> usize {
159 25
160 }
161 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
162 Identity {
163 uid: <String>::from_le_byte_slice(&bytes[0..8]),
164 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
165 position: <char>::from_le_byte_slice(&bytes[16..17]),
166 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
167 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
168 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
169 }
170 }
171}
172
173#[derive(Clone)]
175pub struct ThermocoupleBricklet {
176 device: Device,
177}
178impl ThermocoupleBricklet {
179 pub const DEVICE_IDENTIFIER: u16 = 266;
180 pub const DEVICE_DISPLAY_NAME: &'static str = "Thermocouple Bricklet";
181 pub fn new(uid: Uid, connection: AsyncIpConnection) -> ThermocoupleBricklet {
183 let mut result = ThermocoupleBricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
184 result.device.response_expected[u8::from(ThermocoupleBrickletFunction::GetTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
185 result.device.response_expected[u8::from(ThermocoupleBrickletFunction::SetTemperatureCallbackPeriod) as usize] =
186 ResponseExpectedFlag::True;
187 result.device.response_expected[u8::from(ThermocoupleBrickletFunction::GetTemperatureCallbackPeriod) as usize] =
188 ResponseExpectedFlag::AlwaysTrue;
189 result.device.response_expected[u8::from(ThermocoupleBrickletFunction::SetTemperatureCallbackThreshold) as usize] =
190 ResponseExpectedFlag::True;
191 result.device.response_expected[u8::from(ThermocoupleBrickletFunction::GetTemperatureCallbackThreshold) as usize] =
192 ResponseExpectedFlag::AlwaysTrue;
193 result.device.response_expected[u8::from(ThermocoupleBrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
194 result.device.response_expected[u8::from(ThermocoupleBrickletFunction::GetDebouncePeriod) as usize] =
195 ResponseExpectedFlag::AlwaysTrue;
196 result.device.response_expected[u8::from(ThermocoupleBrickletFunction::SetConfiguration) as usize] = ResponseExpectedFlag::False;
197 result.device.response_expected[u8::from(ThermocoupleBrickletFunction::GetConfiguration) as usize] =
198 ResponseExpectedFlag::AlwaysTrue;
199 result.device.response_expected[u8::from(ThermocoupleBrickletFunction::GetErrorState) as usize] = ResponseExpectedFlag::AlwaysTrue;
200 result.device.response_expected[u8::from(ThermocoupleBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
201 result
202 }
203
204 pub fn get_response_expected(&mut self, fun: ThermocoupleBrickletFunction) -> Result<bool, GetResponseExpectedError> {
219 self.device.get_response_expected(u8::from(fun))
220 }
221
222 pub fn set_response_expected(
231 &mut self,
232 fun: ThermocoupleBrickletFunction,
233 response_expected: bool,
234 ) -> Result<(), SetResponseExpectedError> {
235 self.device.set_response_expected(u8::from(fun), response_expected)
236 }
237
238 pub fn set_response_expected_all(&mut self, response_expected: bool) {
240 self.device.set_response_expected_all(response_expected)
241 }
242
243 pub fn get_api_version(&self) -> [u8; 3] {
246 self.device.api_version
247 }
248
249 pub async fn get_temperature_callback_receiver(&mut self) -> impl Stream<Item = i32> {
259 self.device
260 .get_callback_receiver(u8::from(ThermocoupleBrickletFunction::CallbackTemperature))
261 .await
262 .map(|p| i32::from_le_byte_slice(p.body()))
263 }
264
265 pub async fn get_temperature_reached_callback_receiver(&mut self) -> impl Stream<Item = i32> {
272 self.device
273 .get_callback_receiver(u8::from(ThermocoupleBrickletFunction::CallbackTemperatureReached))
274 .await
275 .map(|p| i32::from_le_byte_slice(p.body()))
276 }
277
278 pub async fn get_error_state_callback_receiver(&mut self) -> impl Stream<Item = ErrorStateEvent> {
281 self.device
282 .get_callback_receiver(u8::from(ThermocoupleBrickletFunction::CallbackErrorState))
283 .await
284 .map(|p| ErrorStateEvent::from_le_byte_slice(p.body()))
285 }
286
287 pub async fn get_temperature(&mut self) -> Result<i32, TinkerforgeError> {
293 let payload = [0; 0];
294
295 #[allow(unused_variables)]
296 let result = self.device.get(u8::from(ThermocoupleBrickletFunction::GetTemperature), &payload).await?;
297 Ok(i32::from_le_byte_slice(result.body()))
298 }
299
300 pub async fn set_temperature_callback_period(&mut self, period: u32) -> Result<(), TinkerforgeError> {
306 let mut payload = [0; 4];
307 period.write_to_slice(&mut payload[0..4]);
308
309 #[allow(unused_variables)]
310 let result = self.device.set(u8::from(ThermocoupleBrickletFunction::SetTemperatureCallbackPeriod), &payload).await?;
311 Ok(())
312 }
313
314 pub async fn get_temperature_callback_period(&mut self) -> Result<u32, TinkerforgeError> {
316 let payload = [0; 0];
317
318 #[allow(unused_variables)]
319 let result = self.device.get(u8::from(ThermocoupleBrickletFunction::GetTemperatureCallbackPeriod), &payload).await?;
320 Ok(u32::from_le_byte_slice(result.body()))
321 }
322
323 pub async fn set_temperature_callback_threshold(&mut self, option: char, min: i32, max: i32) -> Result<(), TinkerforgeError> {
342 let mut payload = [0; 9];
343 option.write_to_slice(&mut payload[0..1]);
344 min.write_to_slice(&mut payload[1..5]);
345 max.write_to_slice(&mut payload[5..9]);
346
347 #[allow(unused_variables)]
348 let result = self.device.set(u8::from(ThermocoupleBrickletFunction::SetTemperatureCallbackThreshold), &payload).await?;
349 Ok(())
350 }
351
352 pub async fn get_temperature_callback_threshold(&mut self) -> Result<TemperatureCallbackThreshold, TinkerforgeError> {
361 let payload = [0; 0];
362
363 #[allow(unused_variables)]
364 let result = self.device.get(u8::from(ThermocoupleBrickletFunction::GetTemperatureCallbackThreshold), &payload).await?;
365 Ok(TemperatureCallbackThreshold::from_le_byte_slice(result.body()))
366 }
367
368 pub async fn set_debounce_period(&mut self, debounce: u32) -> Result<(), TinkerforgeError> {
378 let mut payload = [0; 4];
379 debounce.write_to_slice(&mut payload[0..4]);
380
381 #[allow(unused_variables)]
382 let result = self.device.set(u8::from(ThermocoupleBrickletFunction::SetDebouncePeriod), &payload).await?;
383 Ok(())
384 }
385
386 pub async fn get_debounce_period(&mut self) -> Result<u32, TinkerforgeError> {
388 let payload = [0; 0];
389
390 #[allow(unused_variables)]
391 let result = self.device.get(u8::from(ThermocoupleBrickletFunction::GetDebouncePeriod), &payload).await?;
392 Ok(u32::from_le_byte_slice(result.body()))
393 }
394
395 pub async fn set_configuration(&mut self, averaging: u8, thermocouple_type: u8, filter: u8) -> Result<(), TinkerforgeError> {
438 let mut payload = [0; 3];
439 averaging.write_to_slice(&mut payload[0..1]);
440 thermocouple_type.write_to_slice(&mut payload[1..2]);
441 filter.write_to_slice(&mut payload[2..3]);
442
443 #[allow(unused_variables)]
444 let result = self.device.set(u8::from(ThermocoupleBrickletFunction::SetConfiguration), &payload).await?;
445 Ok(())
446 }
447
448 pub async fn get_configuration(&mut self) -> Result<Configuration, TinkerforgeError> {
469 let payload = [0; 0];
470
471 #[allow(unused_variables)]
472 let result = self.device.get(u8::from(ThermocoupleBrickletFunction::GetConfiguration), &payload).await?;
473 Ok(Configuration::from_le_byte_slice(result.body()))
474 }
475
476 pub async fn get_error_state(&mut self) -> Result<ErrorState, TinkerforgeError> {
488 let payload = [0; 0];
489
490 #[allow(unused_variables)]
491 let result = self.device.get(u8::from(ThermocoupleBrickletFunction::GetErrorState), &payload).await?;
492 Ok(ErrorState::from_le_byte_slice(result.body()))
493 }
494
495 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
506 let payload = [0; 0];
507
508 #[allow(unused_variables)]
509 let result = self.device.get(u8::from(ThermocoupleBrickletFunction::GetIdentity), &payload).await?;
510 Ok(Identity::from_le_byte_slice(result.body()))
511 }
512}