tinkerforge/bindings/thermocouple_v2_bricklet.rs
1/* ***********************************************************
2 * This file was automatically generated on 2024-02-27. *
3 * *
4 * Rust Bindings Version 2.0.21 *
5 * *
6 * If you have a bugfix for this file and want to commit it, *
7 * please fix the bug in the generator. You can find a link *
8 * to the generators git repository on tinkerforge.com *
9 *************************************************************/
10
11//! Measures temperature with thermocouples.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/ThermocoupleV2_Bricklet_Rust.html).
14use crate::{
15 byte_converter::*, converting_callback_receiver::ConvertingCallbackReceiver, converting_receiver::ConvertingReceiver, device::*,
16 ip_connection::GetRequestSender,
17};
18pub enum ThermocoupleV2BrickletFunction {
19 GetTemperature,
20 SetTemperatureCallbackConfiguration,
21 GetTemperatureCallbackConfiguration,
22 SetConfiguration,
23 GetConfiguration,
24 GetErrorState,
25 GetSpitfpErrorCount,
26 SetBootloaderMode,
27 GetBootloaderMode,
28 SetWriteFirmwarePointer,
29 WriteFirmware,
30 SetStatusLedConfig,
31 GetStatusLedConfig,
32 GetChipTemperature,
33 Reset,
34 WriteUid,
35 ReadUid,
36 GetIdentity,
37 CallbackTemperature,
38 CallbackErrorState,
39}
40impl From<ThermocoupleV2BrickletFunction> for u8 {
41 fn from(fun: ThermocoupleV2BrickletFunction) -> Self {
42 match fun {
43 ThermocoupleV2BrickletFunction::GetTemperature => 1,
44 ThermocoupleV2BrickletFunction::SetTemperatureCallbackConfiguration => 2,
45 ThermocoupleV2BrickletFunction::GetTemperatureCallbackConfiguration => 3,
46 ThermocoupleV2BrickletFunction::SetConfiguration => 5,
47 ThermocoupleV2BrickletFunction::GetConfiguration => 6,
48 ThermocoupleV2BrickletFunction::GetErrorState => 7,
49 ThermocoupleV2BrickletFunction::GetSpitfpErrorCount => 234,
50 ThermocoupleV2BrickletFunction::SetBootloaderMode => 235,
51 ThermocoupleV2BrickletFunction::GetBootloaderMode => 236,
52 ThermocoupleV2BrickletFunction::SetWriteFirmwarePointer => 237,
53 ThermocoupleV2BrickletFunction::WriteFirmware => 238,
54 ThermocoupleV2BrickletFunction::SetStatusLedConfig => 239,
55 ThermocoupleV2BrickletFunction::GetStatusLedConfig => 240,
56 ThermocoupleV2BrickletFunction::GetChipTemperature => 242,
57 ThermocoupleV2BrickletFunction::Reset => 243,
58 ThermocoupleV2BrickletFunction::WriteUid => 248,
59 ThermocoupleV2BrickletFunction::ReadUid => 249,
60 ThermocoupleV2BrickletFunction::GetIdentity => 255,
61 ThermocoupleV2BrickletFunction::CallbackTemperature => 4,
62 ThermocoupleV2BrickletFunction::CallbackErrorState => 8,
63 }
64 }
65}
66pub const THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
67pub const THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
68pub const THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
69pub const THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
70pub const THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
71pub const THERMOCOUPLE_V2_BRICKLET_AVERAGING_1: u8 = 1;
72pub const THERMOCOUPLE_V2_BRICKLET_AVERAGING_2: u8 = 2;
73pub const THERMOCOUPLE_V2_BRICKLET_AVERAGING_4: u8 = 4;
74pub const THERMOCOUPLE_V2_BRICKLET_AVERAGING_8: u8 = 8;
75pub const THERMOCOUPLE_V2_BRICKLET_AVERAGING_16: u8 = 16;
76pub const THERMOCOUPLE_V2_BRICKLET_TYPE_B: u8 = 0;
77pub const THERMOCOUPLE_V2_BRICKLET_TYPE_E: u8 = 1;
78pub const THERMOCOUPLE_V2_BRICKLET_TYPE_J: u8 = 2;
79pub const THERMOCOUPLE_V2_BRICKLET_TYPE_K: u8 = 3;
80pub const THERMOCOUPLE_V2_BRICKLET_TYPE_N: u8 = 4;
81pub const THERMOCOUPLE_V2_BRICKLET_TYPE_R: u8 = 5;
82pub const THERMOCOUPLE_V2_BRICKLET_TYPE_S: u8 = 6;
83pub const THERMOCOUPLE_V2_BRICKLET_TYPE_T: u8 = 7;
84pub const THERMOCOUPLE_V2_BRICKLET_TYPE_G8: u8 = 8;
85pub const THERMOCOUPLE_V2_BRICKLET_TYPE_G32: u8 = 9;
86pub const THERMOCOUPLE_V2_BRICKLET_FILTER_OPTION_50HZ: u8 = 0;
87pub const THERMOCOUPLE_V2_BRICKLET_FILTER_OPTION_60HZ: u8 = 1;
88pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
89pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
90pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
91pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
92pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
93pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
94pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
95pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
96pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
97pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
98pub const THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
99pub const THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
100pub const THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
101pub const THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
102pub const THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
103
104#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
105pub struct TemperatureCallbackConfiguration {
106 pub period: u32,
107 pub value_has_to_change: bool,
108 pub option: char,
109 pub min: i32,
110 pub max: i32,
111}
112impl FromByteSlice for TemperatureCallbackConfiguration {
113 fn bytes_expected() -> usize { 14 }
114 fn from_le_byte_slice(bytes: &[u8]) -> TemperatureCallbackConfiguration {
115 TemperatureCallbackConfiguration {
116 period: <u32>::from_le_byte_slice(&bytes[0..4]),
117 value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
118 option: <char>::from_le_byte_slice(&bytes[5..6]),
119 min: <i32>::from_le_byte_slice(&bytes[6..10]),
120 max: <i32>::from_le_byte_slice(&bytes[10..14]),
121 }
122 }
123}
124
125#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
126pub struct Configuration {
127 pub averaging: u8,
128 pub thermocouple_type: u8,
129 pub filter: u8,
130}
131impl FromByteSlice for Configuration {
132 fn bytes_expected() -> usize { 3 }
133 fn from_le_byte_slice(bytes: &[u8]) -> Configuration {
134 Configuration {
135 averaging: <u8>::from_le_byte_slice(&bytes[0..1]),
136 thermocouple_type: <u8>::from_le_byte_slice(&bytes[1..2]),
137 filter: <u8>::from_le_byte_slice(&bytes[2..3]),
138 }
139 }
140}
141
142#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
143pub struct ErrorState {
144 pub over_under: bool,
145 pub open_circuit: bool,
146}
147impl FromByteSlice for ErrorState {
148 fn bytes_expected() -> usize { 2 }
149 fn from_le_byte_slice(bytes: &[u8]) -> ErrorState {
150 ErrorState { over_under: <bool>::from_le_byte_slice(&bytes[0..1]), open_circuit: <bool>::from_le_byte_slice(&bytes[1..2]) }
151 }
152}
153
154#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
155pub struct ErrorStateEvent {
156 pub over_under: bool,
157 pub open_circuit: bool,
158}
159impl FromByteSlice for ErrorStateEvent {
160 fn bytes_expected() -> usize { 2 }
161 fn from_le_byte_slice(bytes: &[u8]) -> ErrorStateEvent {
162 ErrorStateEvent { over_under: <bool>::from_le_byte_slice(&bytes[0..1]), open_circuit: <bool>::from_le_byte_slice(&bytes[1..2]) }
163 }
164}
165
166#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
167pub struct SpitfpErrorCount {
168 pub error_count_ack_checksum: u32,
169 pub error_count_message_checksum: u32,
170 pub error_count_frame: u32,
171 pub error_count_overflow: u32,
172}
173impl FromByteSlice for SpitfpErrorCount {
174 fn bytes_expected() -> usize { 16 }
175 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
176 SpitfpErrorCount {
177 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
178 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
179 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
180 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
181 }
182 }
183}
184
185#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
186pub struct Identity {
187 pub uid: String,
188 pub connected_uid: String,
189 pub position: char,
190 pub hardware_version: [u8; 3],
191 pub firmware_version: [u8; 3],
192 pub device_identifier: u16,
193}
194impl FromByteSlice for Identity {
195 fn bytes_expected() -> usize { 25 }
196 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
197 Identity {
198 uid: <String>::from_le_byte_slice(&bytes[0..8]),
199 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
200 position: <char>::from_le_byte_slice(&bytes[16..17]),
201 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
202 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
203 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
204 }
205 }
206}
207
208/// Measures temperature with thermocouples
209#[derive(Clone)]
210pub struct ThermocoupleV2Bricklet {
211 device: Device,
212}
213impl ThermocoupleV2Bricklet {
214 pub const DEVICE_IDENTIFIER: u16 = 2109;
215 pub const DEVICE_DISPLAY_NAME: &'static str = "Thermocouple Bricklet 2.0";
216 /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
217 pub fn new<T: GetRequestSender>(uid: &str, req_sender: T) -> ThermocoupleV2Bricklet {
218 let mut result = ThermocoupleV2Bricklet { device: Device::new([2, 0, 0], uid, req_sender, 0) };
219 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetTemperature) as usize] =
220 ResponseExpectedFlag::AlwaysTrue;
221 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::SetTemperatureCallbackConfiguration) as usize] =
222 ResponseExpectedFlag::True;
223 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetTemperatureCallbackConfiguration) as usize] =
224 ResponseExpectedFlag::AlwaysTrue;
225 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::SetConfiguration) as usize] = ResponseExpectedFlag::False;
226 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetConfiguration) as usize] =
227 ResponseExpectedFlag::AlwaysTrue;
228 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetErrorState) as usize] =
229 ResponseExpectedFlag::AlwaysTrue;
230 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetSpitfpErrorCount) as usize] =
231 ResponseExpectedFlag::AlwaysTrue;
232 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::SetBootloaderMode) as usize] =
233 ResponseExpectedFlag::AlwaysTrue;
234 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetBootloaderMode) as usize] =
235 ResponseExpectedFlag::AlwaysTrue;
236 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
237 ResponseExpectedFlag::False;
238 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::WriteFirmware) as usize] =
239 ResponseExpectedFlag::AlwaysTrue;
240 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::SetStatusLedConfig) as usize] =
241 ResponseExpectedFlag::False;
242 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetStatusLedConfig) as usize] =
243 ResponseExpectedFlag::AlwaysTrue;
244 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetChipTemperature) as usize] =
245 ResponseExpectedFlag::AlwaysTrue;
246 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
247 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
248 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
249 result.device.response_expected[u8::from(ThermocoupleV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
250 result
251 }
252
253 /// Returns the response expected flag for the function specified by the function ID parameter.
254 /// It is true if the function is expected to send a response, false otherwise.
255 ///
256 /// For getter functions this is enabled by default and cannot be disabled, because those
257 /// functions will always send a response. For callback configuration functions it is enabled
258 /// by default too, but can be disabled by [`set_response_expected`](crate::thermocouple_v2_bricklet::ThermocoupleV2Bricklet::set_response_expected).
259 /// For setter functions it is disabled by default and can be enabled.
260 ///
261 /// Enabling the response expected flag for a setter function allows to detect timeouts
262 /// and other error conditions calls of this setter as well. The device will then send a response
263 /// for this purpose. If this flag is disabled for a setter function then no response is sent
264 /// and errors are silently ignored, because they cannot be detected.
265 ///
266 /// See [`set_response_expected`](crate::thermocouple_v2_bricklet::ThermocoupleV2Bricklet::set_response_expected) for the list of function ID constants available for this function.
267 pub fn get_response_expected(&mut self, fun: ThermocoupleV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
268 self.device.get_response_expected(u8::from(fun))
269 }
270
271 /// Changes the response expected flag of the function specified by the function ID parameter.
272 /// This flag can only be changed for setter (default value: false) and callback configuration
273 /// functions (default value: true). For getter functions it is always enabled.
274 ///
275 /// Enabling the response expected flag for a setter function allows to detect timeouts and
276 /// other error conditions calls of this setter as well. The device will then send a response
277 /// for this purpose. If this flag is disabled for a setter function then no response is sent
278 /// and errors are silently ignored, because they cannot be detected.
279 pub fn set_response_expected(
280 &mut self,
281 fun: ThermocoupleV2BrickletFunction,
282 response_expected: bool,
283 ) -> Result<(), SetResponseExpectedError> {
284 self.device.set_response_expected(u8::from(fun), response_expected)
285 }
286
287 /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
288 pub fn set_response_expected_all(&mut self, response_expected: bool) { self.device.set_response_expected_all(response_expected) }
289
290 /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
291 /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
292 pub fn get_api_version(&self) -> [u8; 3] { self.device.api_version }
293
294 /// This receiver is triggered periodically according to the configuration set by
295 /// [`set_temperature_callback_configuration`].
296 ///
297 /// The parameter is the same as [`get_temperature`].
298 ///
299 /// [`get_temperature`]: #method.get_temperature
300 /// [`set_temperature_callback_configuration`]: #method.set_temperature_callback_configuration
301 pub fn get_temperature_callback_receiver(&self) -> ConvertingCallbackReceiver<i32> {
302 self.device.get_callback_receiver(u8::from(ThermocoupleV2BrickletFunction::CallbackTemperature))
303 }
304
305 /// This Receiver is triggered every time the error state changes
306 /// (see [`get_error_state`]).
307 pub fn get_error_state_callback_receiver(&self) -> ConvertingCallbackReceiver<ErrorStateEvent> {
308 self.device.get_callback_receiver(u8::from(ThermocoupleV2BrickletFunction::CallbackErrorState))
309 }
310
311 /// Returns the temperature of the thermocouple. The value is given in °C/100,
312 /// e.g. a value of 4223 means that a temperature of 42.23 °C is measured.
313 ///
314 /// If you want to get the temperature periodically, it is recommended
315 /// to use the [`get_temperature_callback_receiver`] receiver and set the period with
316 /// [`set_temperature_callback_configuration`].
317 ///
318 ///
319 /// If you want to get the value periodically, it is recommended to use the
320 /// [`get_temperature_callback_receiver`] receiver. You can set the receiver configuration
321 /// with [`set_temperature_callback_configuration`].
322 pub fn get_temperature(&self) -> ConvertingReceiver<i32> {
323 let payload = vec![0; 0];
324
325 self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetTemperature), payload)
326 }
327
328 /// The period is the period with which the [`get_temperature_callback_receiver`] receiver is triggered
329 /// periodically. A value of 0 turns the receiver off.
330 ///
331 /// If the `value has to change`-parameter is set to true, the receiver is only
332 /// triggered after the value has changed. If the value didn't change
333 /// within the period, the receiver is triggered immediately on change.
334 ///
335 /// If it is set to false, the receiver is continuously triggered with the period,
336 /// independent of the value.
337 ///
338 /// It is furthermore possible to constrain the receiver with thresholds.
339 ///
340 /// The `option`-parameter together with min/max sets a threshold for the [`get_temperature_callback_receiver`] receiver.
341 ///
342 /// The following options are possible:
343 ///
344 /// Option| Description
345 /// --- | ---
346 /// 'x'| Threshold is turned off
347 /// 'o'| Threshold is triggered when the value is *outside* the min and max values
348 /// 'i'| Threshold is triggered when the value is *inside* or equal to the min and max values
349 /// '<'| Threshold is triggered when the value is smaller than the min value (max is ignored)
350 /// '>'| Threshold is triggered when the value is greater than the min value (max is ignored)
351 ///
352 /// If the option is set to 'x' (threshold turned off) the receiver is triggered with the fixed period.
353 ///
354 /// Associated constants:
355 /// * THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_OFF
356 /// * THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
357 /// * THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
358 /// * THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
359 /// * THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_GREATER
360 pub fn set_temperature_callback_configuration(
361 &self,
362 period: u32,
363 value_has_to_change: bool,
364 option: char,
365 min: i32,
366 max: i32,
367 ) -> ConvertingReceiver<()> {
368 let mut payload = vec![0; 14];
369 payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
370 payload[4..5].copy_from_slice(&<bool>::to_le_byte_vec(value_has_to_change));
371 payload[5..6].copy_from_slice(&<char>::to_le_byte_vec(option));
372 payload[6..10].copy_from_slice(&<i32>::to_le_byte_vec(min));
373 payload[10..14].copy_from_slice(&<i32>::to_le_byte_vec(max));
374
375 self.device.set(u8::from(ThermocoupleV2BrickletFunction::SetTemperatureCallbackConfiguration), payload)
376 }
377
378 /// Returns the receiver configuration as set by [`set_temperature_callback_configuration`].
379 ///
380 /// Associated constants:
381 /// * THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_OFF
382 /// * THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE
383 /// * THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_INSIDE
384 /// * THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_SMALLER
385 /// * THERMOCOUPLE_V2_BRICKLET_THRESHOLD_OPTION_GREATER
386 pub fn get_temperature_callback_configuration(&self) -> ConvertingReceiver<TemperatureCallbackConfiguration> {
387 let payload = vec![0; 0];
388
389 self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetTemperatureCallbackConfiguration), payload)
390 }
391
392 /// You can configure averaging size, thermocouple type and frequency
393 /// filtering.
394 ///
395 /// Available averaging sizes are 1, 2, 4, 8 and 16 samples.
396 ///
397 /// As thermocouple type you can use B, E, J, K, N, R, S and T. If you have a
398 /// different thermocouple or a custom thermocouple you can also use
399 /// G8 and G32. With these types the returned value will not be in °C/100,
400 /// it will be calculated by the following formulas:
401 ///
402 /// * G8: ``value = 8 * 1.6 * 2^17 * Vin``
403 /// * G32: ``value = 32 * 1.6 * 2^17 * Vin``
404 ///
405 /// where Vin is the thermocouple input voltage.
406 ///
407 /// The frequency filter can be either configured to 50Hz or to 60Hz. You should
408 /// configure it according to your utility frequency.
409 ///
410 /// The conversion time depends on the averaging and filter configuration, it can
411 /// be calculated as follows:
412 ///
413 /// * 60Hz: ``time = 82 + (samples - 1) * 16.67``
414 /// * 50Hz: ``time = 98 + (samples - 1) * 20``
415 ///
416 /// Associated constants:
417 /// * THERMOCOUPLE_V2_BRICKLET_AVERAGING_1
418 /// * THERMOCOUPLE_V2_BRICKLET_AVERAGING_2
419 /// * THERMOCOUPLE_V2_BRICKLET_AVERAGING_4
420 /// * THERMOCOUPLE_V2_BRICKLET_AVERAGING_8
421 /// * THERMOCOUPLE_V2_BRICKLET_AVERAGING_16
422 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_B
423 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_E
424 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_J
425 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_K
426 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_N
427 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_R
428 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_S
429 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_T
430 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_G8
431 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_G32
432 /// * THERMOCOUPLE_V2_BRICKLET_FILTER_OPTION_50HZ
433 /// * THERMOCOUPLE_V2_BRICKLET_FILTER_OPTION_60HZ
434 pub fn set_configuration(&self, averaging: u8, thermocouple_type: u8, filter: u8) -> ConvertingReceiver<()> {
435 let mut payload = vec![0; 3];
436 payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(averaging));
437 payload[1..2].copy_from_slice(&<u8>::to_le_byte_vec(thermocouple_type));
438 payload[2..3].copy_from_slice(&<u8>::to_le_byte_vec(filter));
439
440 self.device.set(u8::from(ThermocoupleV2BrickletFunction::SetConfiguration), payload)
441 }
442
443 /// Returns the configuration as set by [`set_configuration`].
444 ///
445 /// Associated constants:
446 /// * THERMOCOUPLE_V2_BRICKLET_AVERAGING_1
447 /// * THERMOCOUPLE_V2_BRICKLET_AVERAGING_2
448 /// * THERMOCOUPLE_V2_BRICKLET_AVERAGING_4
449 /// * THERMOCOUPLE_V2_BRICKLET_AVERAGING_8
450 /// * THERMOCOUPLE_V2_BRICKLET_AVERAGING_16
451 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_B
452 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_E
453 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_J
454 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_K
455 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_N
456 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_R
457 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_S
458 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_T
459 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_G8
460 /// * THERMOCOUPLE_V2_BRICKLET_TYPE_G32
461 /// * THERMOCOUPLE_V2_BRICKLET_FILTER_OPTION_50HZ
462 /// * THERMOCOUPLE_V2_BRICKLET_FILTER_OPTION_60HZ
463 pub fn get_configuration(&self) -> ConvertingReceiver<Configuration> {
464 let payload = vec![0; 0];
465
466 self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetConfiguration), payload)
467 }
468
469 /// Returns the current error state. There are two possible errors:
470 ///
471 /// * Over/Under Voltage and
472 /// * Open Circuit.
473 ///
474 /// Over/Under Voltage happens for voltages below 0V or above 3.3V. In this case
475 /// it is very likely that your thermocouple is defective. An Open Circuit error
476 /// indicates that there is no thermocouple connected.
477 ///
478 /// You can use the [`get_error_state_callback_receiver`] receiver to automatically get triggered
479 /// when the error state changes.
480 pub fn get_error_state(&self) -> ConvertingReceiver<ErrorState> {
481 let payload = vec![0; 0];
482
483 self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetErrorState), payload)
484 }
485
486 /// Returns the error count for the communication between Brick and Bricklet.
487 ///
488 /// The errors are divided into
489 ///
490 /// * ACK checksum errors,
491 /// * message checksum errors,
492 /// * framing errors and
493 /// * overflow errors.
494 ///
495 /// The errors counts are for errors that occur on the Bricklet side. All
496 /// Bricks have a similar function that returns the errors on the Brick side.
497 pub fn get_spitfp_error_count(&self) -> ConvertingReceiver<SpitfpErrorCount> {
498 let payload = vec![0; 0];
499
500 self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetSpitfpErrorCount), payload)
501 }
502
503 /// Sets the bootloader mode and returns the status after the requested
504 /// mode change was instigated.
505 ///
506 /// You can change from bootloader mode to firmware mode and vice versa. A change
507 /// from bootloader mode to firmware mode will only take place if the entry function,
508 /// device identifier and CRC are present and correct.
509 ///
510 /// This function is used by Brick Viewer during flashing. It should not be
511 /// necessary to call it in a normal user program.
512 ///
513 /// Associated constants:
514 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
515 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
516 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
517 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
518 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
519 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_OK
520 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
521 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
522 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
523 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
524 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
525 pub fn set_bootloader_mode(&self, mode: u8) -> ConvertingReceiver<u8> {
526 let mut payload = vec![0; 1];
527 payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(mode));
528
529 self.device.get(u8::from(ThermocoupleV2BrickletFunction::SetBootloaderMode), payload)
530 }
531
532 /// Returns the current bootloader mode, see [`set_bootloader_mode`].
533 ///
534 /// Associated constants:
535 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
536 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
537 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
538 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
539 /// * THERMOCOUPLE_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
540 pub fn get_bootloader_mode(&self) -> ConvertingReceiver<u8> {
541 let payload = vec![0; 0];
542
543 self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetBootloaderMode), payload)
544 }
545
546 /// Sets the firmware pointer for [`write_firmware`]. The pointer has
547 /// to be increased by chunks of size 64. The data is written to flash
548 /// every 4 chunks (which equals to one page of size 256).
549 ///
550 /// This function is used by Brick Viewer during flashing. It should not be
551 /// necessary to call it in a normal user program.
552 pub fn set_write_firmware_pointer(&self, pointer: u32) -> ConvertingReceiver<()> {
553 let mut payload = vec![0; 4];
554 payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(pointer));
555
556 self.device.set(u8::from(ThermocoupleV2BrickletFunction::SetWriteFirmwarePointer), payload)
557 }
558
559 /// Writes 64 Bytes of firmware at the position as written by
560 /// [`set_write_firmware_pointer`] before. The firmware is written
561 /// to flash every 4 chunks.
562 ///
563 /// You can only write firmware in bootloader mode.
564 ///
565 /// This function is used by Brick Viewer during flashing. It should not be
566 /// necessary to call it in a normal user program.
567 pub fn write_firmware(&self, data: [u8; 64]) -> ConvertingReceiver<u8> {
568 let mut payload = vec![0; 64];
569 payload[0..64].copy_from_slice(&<[u8; 64]>::to_le_byte_vec(data));
570
571 self.device.get(u8::from(ThermocoupleV2BrickletFunction::WriteFirmware), payload)
572 }
573
574 /// Sets the status LED configuration. By default the LED shows
575 /// communication traffic between Brick and Bricklet, it flickers once
576 /// for every 10 received data packets.
577 ///
578 /// You can also turn the LED permanently on/off or show a heartbeat.
579 ///
580 /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
581 ///
582 /// Associated constants:
583 /// * THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_OFF
584 /// * THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_ON
585 /// * THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
586 /// * THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
587 pub fn set_status_led_config(&self, config: u8) -> ConvertingReceiver<()> {
588 let mut payload = vec![0; 1];
589 payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(config));
590
591 self.device.set(u8::from(ThermocoupleV2BrickletFunction::SetStatusLedConfig), payload)
592 }
593
594 /// Returns the configuration as set by [`set_status_led_config`]
595 ///
596 /// Associated constants:
597 /// * THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_OFF
598 /// * THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_ON
599 /// * THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
600 /// * THERMOCOUPLE_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
601 pub fn get_status_led_config(&self) -> ConvertingReceiver<u8> {
602 let payload = vec![0; 0];
603
604 self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetStatusLedConfig), payload)
605 }
606
607 /// Returns the temperature as measured inside the microcontroller. The
608 /// value returned is not the ambient temperature!
609 ///
610 /// The temperature is only proportional to the real temperature and it has bad
611 /// accuracy. Practically it is only useful as an indicator for
612 /// temperature changes.
613 pub fn get_chip_temperature(&self) -> ConvertingReceiver<i16> {
614 let payload = vec![0; 0];
615
616 self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetChipTemperature), payload)
617 }
618
619 /// Calling this function will reset the Bricklet. All configurations
620 /// will be lost.
621 ///
622 /// After a reset you have to create new device objects,
623 /// calling functions on the existing ones will result in
624 /// undefined behavior!
625 pub fn reset(&self) -> ConvertingReceiver<()> {
626 let payload = vec![0; 0];
627
628 self.device.set(u8::from(ThermocoupleV2BrickletFunction::Reset), payload)
629 }
630
631 /// Writes a new UID into flash. If you want to set a new UID
632 /// you have to decode the Base58 encoded UID string into an
633 /// integer first.
634 ///
635 /// We recommend that you use Brick Viewer to change the UID.
636 pub fn write_uid(&self, uid: u32) -> ConvertingReceiver<()> {
637 let mut payload = vec![0; 4];
638 payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(uid));
639
640 self.device.set(u8::from(ThermocoupleV2BrickletFunction::WriteUid), payload)
641 }
642
643 /// Returns the current UID as an integer. Encode as
644 /// Base58 to get the usual string version.
645 pub fn read_uid(&self) -> ConvertingReceiver<u32> {
646 let payload = vec![0; 0];
647
648 self.device.get(u8::from(ThermocoupleV2BrickletFunction::ReadUid), payload)
649 }
650
651 /// Returns the UID, the UID where the Bricklet is connected to,
652 /// the position, the hardware and firmware version as well as the
653 /// device identifier.
654 ///
655 /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
656 /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
657 /// position 'z'.
658 ///
659 /// The device identifier numbers can be found [here](device_identifier).
660 /// |device_identifier_constant|
661 pub fn get_identity(&self) -> ConvertingReceiver<Identity> {
662 let payload = vec![0; 0];
663
664 self.device.get(u8::from(ThermocoupleV2BrickletFunction::GetIdentity), payload)
665 }
666}