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 ThermocoupleV2BrickletFunction {
24 GetTemperature,
25 SetTemperatureCallbackConfiguration,
26 GetTemperatureCallbackConfiguration,
27 SetConfiguration,
28 GetConfiguration,
29 GetErrorState,
30 GetSpitfpErrorCount,
31 SetBootloaderMode,
32 GetBootloaderMode,
33 SetWriteFirmwarePointer,
34 WriteFirmware,
35 SetStatusLedConfig,
36 GetStatusLedConfig,
37 GetChipTemperature,
38 Reset,
39 WriteUid,
40 ReadUid,
41 GetIdentity,
42 CallbackTemperature,
43 CallbackErrorState,
44}
45impl From<ThermocoupleV2BrickletFunction> for u8 {
46 fn from(fun: ThermocoupleV2BrickletFunction) -> Self {
47 match fun {
48 ThermocoupleV2BrickletFunction::GetTemperature => 1,
49 ThermocoupleV2BrickletFunction::SetTemperatureCallbackConfiguration => 2,
50 ThermocoupleV2BrickletFunction::GetTemperatureCallbackConfiguration => 3,
51 ThermocoupleV2BrickletFunction::SetConfiguration => 5,
52 ThermocoupleV2BrickletFunction::GetConfiguration => 6,
53 ThermocoupleV2BrickletFunction::GetErrorState => 7,
54 ThermocoupleV2BrickletFunction::GetSpitfpErrorCount => 234,
55 ThermocoupleV2BrickletFunction::SetBootloaderMode => 235,
56 ThermocoupleV2BrickletFunction::GetBootloaderMode => 236,
57 ThermocoupleV2BrickletFunction::SetWriteFirmwarePointer => 237,
58 ThermocoupleV2BrickletFunction::WriteFirmware => 238,
59 ThermocoupleV2BrickletFunction::SetStatusLedConfig => 239,
60 ThermocoupleV2BrickletFunction::GetStatusLedConfig => 240,
61 ThermocoupleV2BrickletFunction::GetChipTemperature => 242,
62 ThermocoupleV2BrickletFunction::Reset => 243,
63 ThermocoupleV2BrickletFunction::WriteUid => 248,
64 ThermocoupleV2BrickletFunction::ReadUid => 249,
65 ThermocoupleV2BrickletFunction::GetIdentity => 255,
66 ThermocoupleV2BrickletFunction::CallbackTemperature => 4,
67 ThermocoupleV2BrickletFunction::CallbackErrorState => 8,
68 }
69 }
70}
71pub const THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
72pub const THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
73pub const THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
74pub const THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
75pub const THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
76pub const THERMOCOUPLE_V2_BRICKLET_AVERAGING_1: u8 = 1;
77pub const THERMOCOUPLE_V2_BRICKLET_AVERAGING_2: u8 = 2;
78pub const THERMOCOUPLE_V2_BRICKLET_AVERAGING_4: u8 = 4;
79pub const THERMOCOUPLE_V2_BRICKLET_AVERAGING_8: u8 = 8;
80pub const THERMOCOUPLE_V2_BRICKLET_AVERAGING_16: u8 = 16;
81pub const THERMOCOUPLE_V2_BRICKLET_TYPE_B: u8 = 0;
82pub const THERMOCOUPLE_V2_BRICKLET_TYPE_E: u8 = 1;
83pub const THERMOCOUPLE_V2_BRICKLET_TYPE_J: u8 = 2;
84pub const THERMOCOUPLE_V2_BRICKLET_TYPE_K: u8 = 3;
85pub const THERMOCOUPLE_V2_BRICKLET_TYPE_N: u8 = 4;
86pub const THERMOCOUPLE_V2_BRICKLET_TYPE_R: u8 = 5;
87pub const THERMOCOUPLE_V2_BRICKLET_TYPE_S: u8 = 6;
88pub const THERMOCOUPLE_V2_BRICKLET_TYPE_T: u8 = 7;
89pub const THERMOCOUPLE_V2_BRICKLET_TYPE_G8: u8 = 8;
90pub const THERMOCOUPLE_V2_BRICKLET_TYPE_G32: u8 = 9;
91pub const THERMOCOUPLE_V2_BRICKLET_FILTER_OPTION_50HZ: u8 = 0;
92pub const THERMOCOUPLE_V2_BRICKLET_FILTER_OPTION_60HZ: u8 = 1;
93pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
94pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
95pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
96pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
97pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
98pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
99pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
100pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
101pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
102pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
103pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
104pub const THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
105pub const THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
106pub const THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
107pub const THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
108
109#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
110pub struct TemperatureCallbackConfiguration {
111 pub period: u32,
112 pub value_has_to_change: bool,
113 pub option: char,
114 pub min: i32,
115 pub max: i32,
116}
117impl FromByteSlice for TemperatureCallbackConfiguration {
118 fn bytes_expected() -> usize {
119 14
120 }
121 fn from_le_byte_slice(bytes: &[u8]) -> TemperatureCallbackConfiguration {
122 TemperatureCallbackConfiguration {
123 period: <u32>::from_le_byte_slice(&bytes[0..4]),
124 value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
125 option: <char>::from_le_byte_slice(&bytes[5..6]),
126 min: <i32>::from_le_byte_slice(&bytes[6..10]),
127 max: <i32>::from_le_byte_slice(&bytes[10..14]),
128 }
129 }
130}
131
132#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
133pub struct Configuration {
134 pub averaging: u8,
135 pub thermocouple_type: u8,
136 pub filter: u8,
137}
138impl FromByteSlice for Configuration {
139 fn bytes_expected() -> usize {
140 3
141 }
142 fn from_le_byte_slice(bytes: &[u8]) -> Configuration {
143 Configuration {
144 averaging: <u8>::from_le_byte_slice(&bytes[0..1]),
145 thermocouple_type: <u8>::from_le_byte_slice(&bytes[1..2]),
146 filter: <u8>::from_le_byte_slice(&bytes[2..3]),
147 }
148 }
149}
150
151#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
152pub struct ErrorState {
153 pub over_under: bool,
154 pub open_circuit: bool,
155}
156impl FromByteSlice for ErrorState {
157 fn bytes_expected() -> usize {
158 2
159 }
160 fn from_le_byte_slice(bytes: &[u8]) -> ErrorState {
161 ErrorState { over_under: <bool>::from_le_byte_slice(&bytes[0..1]), open_circuit: <bool>::from_le_byte_slice(&bytes[1..2]) }
162 }
163}
164
165#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
166pub struct ErrorStateEvent {
167 pub over_under: bool,
168 pub open_circuit: bool,
169}
170impl FromByteSlice for ErrorStateEvent {
171 fn bytes_expected() -> usize {
172 2
173 }
174 fn from_le_byte_slice(bytes: &[u8]) -> ErrorStateEvent {
175 ErrorStateEvent { over_under: <bool>::from_le_byte_slice(&bytes[0..1]), open_circuit: <bool>::from_le_byte_slice(&bytes[1..2]) }
176 }
177}
178
179#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
180pub struct SpitfpErrorCount {
181 pub error_count_ack_checksum: u32,
182 pub error_count_message_checksum: u32,
183 pub error_count_frame: u32,
184 pub error_count_overflow: u32,
185}
186impl FromByteSlice for SpitfpErrorCount {
187 fn bytes_expected() -> usize {
188 16
189 }
190 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
191 SpitfpErrorCount {
192 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
193 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
194 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
195 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
196 }
197 }
198}
199
200#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
201pub struct Identity {
202 pub uid: String,
203 pub connected_uid: String,
204 pub position: char,
205 pub hardware_version: [u8; 3],
206 pub firmware_version: [u8; 3],
207 pub device_identifier: u16,
208}
209impl FromByteSlice for Identity {
210 fn bytes_expected() -> usize {
211 25
212 }
213 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
214 Identity {
215 uid: <String>::from_le_byte_slice(&bytes[0..8]),
216 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
217 position: <char>::from_le_byte_slice(&bytes[16..17]),
218 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
219 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
220 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
221 }
222 }
223}
224
225#[derive(Clone)]
227pub struct ThermocoupleV2Bricklet {
228 device: Device,
229}
230impl ThermocoupleV2Bricklet {
231 pub const DEVICE_IDENTIFIER: u16 = 2109;
232 pub const DEVICE_DISPLAY_NAME: &'static str = "Thermocouple Bricklet 2.0";
233 pub fn new(uid: Uid, connection: AsyncIpConnection) -> ThermocoupleV2Bricklet {
235 let mut result = ThermocoupleV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
236 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetTemperature) as usize] =
237 ResponseExpectedFlag::AlwaysTrue;
238 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::SetTemperatureCallbackConfiguration) as usize] =
239 ResponseExpectedFlag::True;
240 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetTemperatureCallbackConfiguration) as usize] =
241 ResponseExpectedFlag::AlwaysTrue;
242 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::SetConfiguration) as usize] = ResponseExpectedFlag::False;
243 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetConfiguration) as usize] =
244 ResponseExpectedFlag::AlwaysTrue;
245 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetErrorState) as usize] =
246 ResponseExpectedFlag::AlwaysTrue;
247 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetSpitfpErrorCount) as usize] =
248 ResponseExpectedFlag::AlwaysTrue;
249 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::SetBootloaderMode) as usize] =
250 ResponseExpectedFlag::AlwaysTrue;
251 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetBootloaderMode) as usize] =
252 ResponseExpectedFlag::AlwaysTrue;
253 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
254 ResponseExpectedFlag::False;
255 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::WriteFirmware) as usize] =
256 ResponseExpectedFlag::AlwaysTrue;
257 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::SetStatusLedConfig) as usize] =
258 ResponseExpectedFlag::False;
259 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetStatusLedConfig) as usize] =
260 ResponseExpectedFlag::AlwaysTrue;
261 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetChipTemperature) as usize] =
262 ResponseExpectedFlag::AlwaysTrue;
263 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
264 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
265 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
266 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
267 result
268 }
269
270 pub fn get_response_expected(&mut self, fun: ThermocoupleV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
285 self.device.get_response_expected(u8::from(fun))
286 }
287
288 pub fn set_response_expected(
297 &mut self,
298 fun: ThermocoupleV2BrickletFunction,
299 response_expected: bool,
300 ) -> Result<(), SetResponseExpectedError> {
301 self.device.set_response_expected(u8::from(fun), response_expected)
302 }
303
304 pub fn set_response_expected_all(&mut self, response_expected: bool) {
306 self.device.set_response_expected_all(response_expected)
307 }
308
309 pub fn get_api_version(&self) -> [u8; 3] {
312 self.device.api_version
313 }
314
315 pub async fn get_temperature_callback_receiver(&mut self) -> impl Stream<Item = i32> {
323 self.device
324 .get_callback_receiver(u8::from(ThermocoupleV2BrickletFunction::CallbackTemperature))
325 .await
326 .map(|p| i32::from_le_byte_slice(p.body()))
327 }
328
329 pub async fn get_error_state_callback_receiver(&mut self) -> impl Stream<Item = ErrorStateEvent> {
332 self.device
333 .get_callback_receiver(u8::from(ThermocoupleV2BrickletFunction::CallbackErrorState))
334 .await
335 .map(|p| ErrorStateEvent::from_le_byte_slice(p.body()))
336 }
337
338 pub async fn get_temperature(&mut self) -> Result<i32, TinkerforgeError> {
350 let payload = [0; 0];
351
352 #[allow(unused_variables)]
353 let result = self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetTemperature), &payload).await?;
354 Ok(i32::from_le_byte_slice(result.body()))
355 }
356
357 pub async fn set_temperature_callback_configuration(
390 &mut self,
391 period: u32,
392 value_has_to_change: bool,
393 option: char,
394 min: i32,
395 max: i32,
396 ) -> Result<(), TinkerforgeError> {
397 let mut payload = [0; 14];
398 period.write_to_slice(&mut payload[0..4]);
399 value_has_to_change.write_to_slice(&mut payload[4..5]);
400 option.write_to_slice(&mut payload[5..6]);
401 min.write_to_slice(&mut payload[6..10]);
402 max.write_to_slice(&mut payload[10..14]);
403
404 #[allow(unused_variables)]
405 let result = self.device.set(u8::from(ThermocoupleV2BrickletFunction::SetTemperatureCallbackConfiguration), &payload).await?;
406 Ok(())
407 }
408
409 pub async fn get_temperature_callback_configuration(&mut self) -> Result<TemperatureCallbackConfiguration, TinkerforgeError> {
418 let payload = [0; 0];
419
420 #[allow(unused_variables)]
421 let result = self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetTemperatureCallbackConfiguration), &payload).await?;
422 Ok(TemperatureCallbackConfiguration::from_le_byte_slice(result.body()))
423 }
424
425 pub async fn set_configuration(&mut self, averaging: u8, thermocouple_type: u8, filter: u8) -> Result<(), TinkerforgeError> {
468 let mut payload = [0; 3];
469 averaging.write_to_slice(&mut payload[0..1]);
470 thermocouple_type.write_to_slice(&mut payload[1..2]);
471 filter.write_to_slice(&mut payload[2..3]);
472
473 #[allow(unused_variables)]
474 let result = self.device.set(u8::from(ThermocoupleV2BrickletFunction::SetConfiguration), &payload).await?;
475 Ok(())
476 }
477
478 pub async fn get_configuration(&mut self) -> Result<Configuration, TinkerforgeError> {
499 let payload = [0; 0];
500
501 #[allow(unused_variables)]
502 let result = self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetConfiguration), &payload).await?;
503 Ok(Configuration::from_le_byte_slice(result.body()))
504 }
505
506 pub async fn get_error_state(&mut self) -> Result<ErrorState, TinkerforgeError> {
518 let payload = [0; 0];
519
520 #[allow(unused_variables)]
521 let result = self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetErrorState), &payload).await?;
522 Ok(ErrorState::from_le_byte_slice(result.body()))
523 }
524
525 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
537 let payload = [0; 0];
538
539 #[allow(unused_variables)]
540 let result = self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
541 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
542 }
543
544 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
567 let mut payload = [0; 1];
568 mode.write_to_slice(&mut payload[0..1]);
569
570 #[allow(unused_variables)]
571 let result = self.device.get(u8::from(ThermocoupleV2BrickletFunction::SetBootloaderMode), &payload).await?;
572 Ok(u8::from_le_byte_slice(result.body()))
573 }
574
575 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
584 let payload = [0; 0];
585
586 #[allow(unused_variables)]
587 let result = self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetBootloaderMode), &payload).await?;
588 Ok(u8::from_le_byte_slice(result.body()))
589 }
590
591 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
598 let mut payload = [0; 4];
599 pointer.write_to_slice(&mut payload[0..4]);
600
601 #[allow(unused_variables)]
602 let result = self.device.set(u8::from(ThermocoupleV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
603 Ok(())
604 }
605
606 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
615 let mut payload = [0; 64];
616 data.write_to_slice(&mut payload[0..64]);
617
618 #[allow(unused_variables)]
619 let result = self.device.get(u8::from(ThermocoupleV2BrickletFunction::WriteFirmware), &payload).await?;
620 Ok(u8::from_le_byte_slice(result.body()))
621 }
622
623 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
637 let mut payload = [0; 1];
638 config.write_to_slice(&mut payload[0..1]);
639
640 #[allow(unused_variables)]
641 let result = self.device.set(u8::from(ThermocoupleV2BrickletFunction::SetStatusLedConfig), &payload).await?;
642 Ok(())
643 }
644
645 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
653 let payload = [0; 0];
654
655 #[allow(unused_variables)]
656 let result = self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetStatusLedConfig), &payload).await?;
657 Ok(u8::from_le_byte_slice(result.body()))
658 }
659
660 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
667 let payload = [0; 0];
668
669 #[allow(unused_variables)]
670 let result = self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetChipTemperature), &payload).await?;
671 Ok(i16::from_le_byte_slice(result.body()))
672 }
673
674 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
681 let payload = [0; 0];
682
683 #[allow(unused_variables)]
684 let result = self.device.set(u8::from(ThermocoupleV2BrickletFunction::Reset), &payload).await?;
685 Ok(())
686 }
687
688 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
694 let mut payload = [0; 4];
695 uid.write_to_slice(&mut payload[0..4]);
696
697 #[allow(unused_variables)]
698 let result = self.device.set(u8::from(ThermocoupleV2BrickletFunction::WriteUid), &payload).await?;
699 Ok(())
700 }
701
702 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
705 let payload = [0; 0];
706
707 #[allow(unused_variables)]
708 let result = self.device.get(u8::from(ThermocoupleV2BrickletFunction::ReadUid), &payload).await?;
709 Ok(u32::from_le_byte_slice(result.body()))
710 }
711
712 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
723 let payload = [0; 0];
724
725 #[allow(unused_variables)]
726 let result = self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetIdentity), &payload).await?;
727 Ok(Identity::from_le_byte_slice(result.body()))
728 }
729}