tinkerforge/bindings/current25_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 AC/DC current between -25A and +25A.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/Current25_Bricklet_Rust.html).
14use crate::{
15 byte_converter::*, converting_callback_receiver::ConvertingCallbackReceiver, converting_receiver::ConvertingReceiver, device::*,
16 ip_connection::GetRequestSender,
17};
18pub enum Current25BrickletFunction {
19 GetCurrent,
20 Calibrate,
21 IsOverCurrent,
22 GetAnalogValue,
23 SetCurrentCallbackPeriod,
24 GetCurrentCallbackPeriod,
25 SetAnalogValueCallbackPeriod,
26 GetAnalogValueCallbackPeriod,
27 SetCurrentCallbackThreshold,
28 GetCurrentCallbackThreshold,
29 SetAnalogValueCallbackThreshold,
30 GetAnalogValueCallbackThreshold,
31 SetDebouncePeriod,
32 GetDebouncePeriod,
33 GetIdentity,
34 CallbackCurrent,
35 CallbackAnalogValue,
36 CallbackCurrentReached,
37 CallbackAnalogValueReached,
38 CallbackOverCurrent,
39}
40impl From<Current25BrickletFunction> for u8 {
41 fn from(fun: Current25BrickletFunction) -> Self {
42 match fun {
43 Current25BrickletFunction::GetCurrent => 1,
44 Current25BrickletFunction::Calibrate => 2,
45 Current25BrickletFunction::IsOverCurrent => 3,
46 Current25BrickletFunction::GetAnalogValue => 4,
47 Current25BrickletFunction::SetCurrentCallbackPeriod => 5,
48 Current25BrickletFunction::GetCurrentCallbackPeriod => 6,
49 Current25BrickletFunction::SetAnalogValueCallbackPeriod => 7,
50 Current25BrickletFunction::GetAnalogValueCallbackPeriod => 8,
51 Current25BrickletFunction::SetCurrentCallbackThreshold => 9,
52 Current25BrickletFunction::GetCurrentCallbackThreshold => 10,
53 Current25BrickletFunction::SetAnalogValueCallbackThreshold => 11,
54 Current25BrickletFunction::GetAnalogValueCallbackThreshold => 12,
55 Current25BrickletFunction::SetDebouncePeriod => 13,
56 Current25BrickletFunction::GetDebouncePeriod => 14,
57 Current25BrickletFunction::GetIdentity => 255,
58 Current25BrickletFunction::CallbackCurrent => 15,
59 Current25BrickletFunction::CallbackAnalogValue => 16,
60 Current25BrickletFunction::CallbackCurrentReached => 17,
61 Current25BrickletFunction::CallbackAnalogValueReached => 18,
62 Current25BrickletFunction::CallbackOverCurrent => 19,
63 }
64 }
65}
66pub const CURRENT25_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
67pub const CURRENT25_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
68pub const CURRENT25_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
69pub const CURRENT25_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
70pub const CURRENT25_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
71
72#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
73pub struct CurrentCallbackThreshold {
74 pub option: char,
75 pub min: i16,
76 pub max: i16,
77}
78impl FromByteSlice for CurrentCallbackThreshold {
79 fn bytes_expected() -> usize { 5 }
80 fn from_le_byte_slice(bytes: &[u8]) -> CurrentCallbackThreshold {
81 CurrentCallbackThreshold {
82 option: <char>::from_le_byte_slice(&bytes[0..1]),
83 min: <i16>::from_le_byte_slice(&bytes[1..3]),
84 max: <i16>::from_le_byte_slice(&bytes[3..5]),
85 }
86 }
87}
88
89#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
90pub struct AnalogValueCallbackThreshold {
91 pub option: char,
92 pub min: u16,
93 pub max: u16,
94}
95impl FromByteSlice for AnalogValueCallbackThreshold {
96 fn bytes_expected() -> usize { 5 }
97 fn from_le_byte_slice(bytes: &[u8]) -> AnalogValueCallbackThreshold {
98 AnalogValueCallbackThreshold {
99 option: <char>::from_le_byte_slice(&bytes[0..1]),
100 min: <u16>::from_le_byte_slice(&bytes[1..3]),
101 max: <u16>::from_le_byte_slice(&bytes[3..5]),
102 }
103 }
104}
105
106#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
107pub struct Identity {
108 pub uid: String,
109 pub connected_uid: String,
110 pub position: char,
111 pub hardware_version: [u8; 3],
112 pub firmware_version: [u8; 3],
113 pub device_identifier: u16,
114}
115impl FromByteSlice for Identity {
116 fn bytes_expected() -> usize { 25 }
117 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
118 Identity {
119 uid: <String>::from_le_byte_slice(&bytes[0..8]),
120 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
121 position: <char>::from_le_byte_slice(&bytes[16..17]),
122 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
123 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
124 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
125 }
126 }
127}
128
129/// Measures AC/DC current between -25A and +25A
130#[derive(Clone)]
131pub struct Current25Bricklet {
132 device: Device,
133}
134impl Current25Bricklet {
135 pub const DEVICE_IDENTIFIER: u16 = 24;
136 pub const DEVICE_DISPLAY_NAME: &'static str = "Current25 Bricklet";
137 /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
138 pub fn new<T: GetRequestSender>(uid: &str, req_sender: T) -> Current25Bricklet {
139 let mut result = Current25Bricklet { device: Device::new([2, 0, 0], uid, req_sender, 0) };
140 result.device.response_expected[u8::from(Current25BrickletFunction::GetCurrent) as usize] = ResponseExpectedFlag::AlwaysTrue;
141 result.device.response_expected[u8::from(Current25BrickletFunction::Calibrate) as usize] = ResponseExpectedFlag::False;
142 result.device.response_expected[u8::from(Current25BrickletFunction::IsOverCurrent) as usize] = ResponseExpectedFlag::AlwaysTrue;
143 result.device.response_expected[u8::from(Current25BrickletFunction::GetAnalogValue) as usize] = ResponseExpectedFlag::AlwaysTrue;
144 result.device.response_expected[u8::from(Current25BrickletFunction::SetCurrentCallbackPeriod) as usize] =
145 ResponseExpectedFlag::True;
146 result.device.response_expected[u8::from(Current25BrickletFunction::GetCurrentCallbackPeriod) as usize] =
147 ResponseExpectedFlag::AlwaysTrue;
148 result.device.response_expected[u8::from(Current25BrickletFunction::SetAnalogValueCallbackPeriod) as usize] =
149 ResponseExpectedFlag::True;
150 result.device.response_expected[u8::from(Current25BrickletFunction::GetAnalogValueCallbackPeriod) as usize] =
151 ResponseExpectedFlag::AlwaysTrue;
152 result.device.response_expected[u8::from(Current25BrickletFunction::SetCurrentCallbackThreshold) as usize] =
153 ResponseExpectedFlag::True;
154 result.device.response_expected[u8::from(Current25BrickletFunction::GetCurrentCallbackThreshold) as usize] =
155 ResponseExpectedFlag::AlwaysTrue;
156 result.device.response_expected[u8::from(Current25BrickletFunction::SetAnalogValueCallbackThreshold) as usize] =
157 ResponseExpectedFlag::True;
158 result.device.response_expected[u8::from(Current25BrickletFunction::GetAnalogValueCallbackThreshold) as usize] =
159 ResponseExpectedFlag::AlwaysTrue;
160 result.device.response_expected[u8::from(Current25BrickletFunction::SetDebouncePeriod) as usize] = ResponseExpectedFlag::True;
161 result.device.response_expected[u8::from(Current25BrickletFunction::GetDebouncePeriod) as usize] = ResponseExpectedFlag::AlwaysTrue;
162 result.device.response_expected[u8::from(Current25BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
163 result
164 }
165
166 /// Returns the response expected flag for the function specified by the function ID parameter.
167 /// It is true if the function is expected to send a response, false otherwise.
168 ///
169 /// For getter functions this is enabled by default and cannot be disabled, because those
170 /// functions will always send a response. For callback configuration functions it is enabled
171 /// by default too, but can be disabled by [`set_response_expected`](crate::current25_bricklet::Current25Bricklet::set_response_expected).
172 /// For setter functions it is disabled by default and can be enabled.
173 ///
174 /// Enabling the response expected flag for a setter function allows to detect timeouts
175 /// and other error conditions calls of this setter as well. The device will then send a response
176 /// for this purpose. If this flag is disabled for a setter function then no response is sent
177 /// and errors are silently ignored, because they cannot be detected.
178 ///
179 /// See [`set_response_expected`](crate::current25_bricklet::Current25Bricklet::set_response_expected) for the list of function ID constants available for this function.
180 pub fn get_response_expected(&mut self, fun: Current25BrickletFunction) -> Result<bool, GetResponseExpectedError> {
181 self.device.get_response_expected(u8::from(fun))
182 }
183
184 /// Changes the response expected flag of the function specified by the function ID parameter.
185 /// This flag can only be changed for setter (default value: false) and callback configuration
186 /// functions (default value: true). For getter functions it is always enabled.
187 ///
188 /// Enabling the response expected flag for a setter function allows to detect timeouts and
189 /// other error conditions calls of this setter as well. The device will then send a response
190 /// for this purpose. If this flag is disabled for a setter function then no response is sent
191 /// and errors are silently ignored, because they cannot be detected.
192 pub fn set_response_expected(
193 &mut self,
194 fun: Current25BrickletFunction,
195 response_expected: bool,
196 ) -> Result<(), SetResponseExpectedError> {
197 self.device.set_response_expected(u8::from(fun), response_expected)
198 }
199
200 /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
201 pub fn set_response_expected_all(&mut self, response_expected: bool) { self.device.set_response_expected_all(response_expected) }
202
203 /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
204 /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
205 pub fn get_api_version(&self) -> [u8; 3] { self.device.api_version }
206
207 /// This receiver is triggered periodically with the period that is set by
208 /// [`set_current_callback_period`]. The parameter is the current of the
209 /// sensor.
210 ///
211 /// The [`get_current_callback_receiver`] receiver is only triggered if the current has changed since the
212 /// last triggering.
213 ///
214 /// [`set_current_callback_period`]: #method.set_current_callback_period
215 /// [`get_current_callback_receiver`]: #method.get_current_callback_receiver
216 pub fn get_current_callback_receiver(&self) -> ConvertingCallbackReceiver<i16> {
217 self.device.get_callback_receiver(u8::from(Current25BrickletFunction::CallbackCurrent))
218 }
219
220 /// This receiver is triggered periodically with the period that is set by
221 /// [`set_analog_value_callback_period`]. The parameter is the analog value of the
222 /// sensor.
223 ///
224 /// The [`get_analog_value_callback_receiver`] receiver is only triggered if the current has changed since the
225 /// last triggering.
226 pub fn get_analog_value_callback_receiver(&self) -> ConvertingCallbackReceiver<u16> {
227 self.device.get_callback_receiver(u8::from(Current25BrickletFunction::CallbackAnalogValue))
228 }
229
230 /// This receiver is triggered when the threshold as set by
231 /// [`set_current_callback_threshold`] is reached.
232 /// The parameter is the current of the sensor.
233 ///
234 /// If the threshold keeps being reached, the receiver is triggered periodically
235 /// with the period as set by [`set_debounce_period`].
236 pub fn get_current_reached_callback_receiver(&self) -> ConvertingCallbackReceiver<i16> {
237 self.device.get_callback_receiver(u8::from(Current25BrickletFunction::CallbackCurrentReached))
238 }
239
240 /// This receiver is triggered when the threshold as set by
241 /// [`set_analog_value_callback_threshold`] is reached.
242 /// The parameter is the analog value of the sensor.
243 ///
244 /// If the threshold keeps being reached, the receiver is triggered periodically
245 /// with the period as set by [`set_debounce_period`].
246 pub fn get_analog_value_reached_callback_receiver(&self) -> ConvertingCallbackReceiver<u16> {
247 self.device.get_callback_receiver(u8::from(Current25BrickletFunction::CallbackAnalogValueReached))
248 }
249
250 /// This receiver is triggered when an over current is measured
251 /// (see [`is_over_current`]).
252 pub fn get_over_current_callback_receiver(&self) -> ConvertingCallbackReceiver<()> {
253 self.device.get_callback_receiver(u8::from(Current25BrickletFunction::CallbackOverCurrent))
254 }
255
256 /// Returns the current of the sensor.
257 ///
258 /// If you want to get the current periodically, it is recommended to use the
259 /// [`get_current_callback_receiver`] receiver and set the period with
260 /// [`set_current_callback_period`].
261 pub fn get_current(&self) -> ConvertingReceiver<i16> {
262 let payload = vec![0; 0];
263
264 self.device.get(u8::from(Current25BrickletFunction::GetCurrent), payload)
265 }
266
267 /// Calibrates the 0 value of the sensor. You have to call this function
268 /// when there is no current present.
269 ///
270 /// The zero point of the current sensor
271 /// is depending on the exact properties of the analog-to-digital converter,
272 /// the length of the Bricklet cable and the temperature. Thus, if you change
273 /// the Brick or the environment in which the Bricklet is used, you might
274 /// have to recalibrate.
275 ///
276 /// The resulting calibration will be saved on the EEPROM of the Current
277 /// Bricklet.
278 pub fn calibrate(&self) -> ConvertingReceiver<()> {
279 let payload = vec![0; 0];
280
281 self.device.set(u8::from(Current25BrickletFunction::Calibrate), payload)
282 }
283
284 /// Returns *true* if more than 25A were measured.
285 ///
286 /// # Note
287 /// To reset this value you have to power cycle the Bricklet.
288 pub fn is_over_current(&self) -> ConvertingReceiver<bool> {
289 let payload = vec![0; 0];
290
291 self.device.get(u8::from(Current25BrickletFunction::IsOverCurrent), payload)
292 }
293
294 /// Returns the value as read by a 12-bit analog-to-digital converter.
295 ///
296 /// # Note
297 /// The value returned by [`get_current`] is averaged over several samples
298 /// to yield less noise, while [`get_analog_value`] gives back raw
299 /// unfiltered analog values. The only reason to use [`get_analog_value`] is,
300 /// if you need the full resolution of the analog-to-digital converter.
301 ///
302 /// If you want the analog value periodically, it is recommended to use the
303 /// [`get_analog_value_callback_receiver`] receiver and set the period with
304 /// [`set_analog_value_callback_period`].
305 pub fn get_analog_value(&self) -> ConvertingReceiver<u16> {
306 let payload = vec![0; 0];
307
308 self.device.get(u8::from(Current25BrickletFunction::GetAnalogValue), payload)
309 }
310
311 /// Sets the period with which the [`get_current_callback_receiver`] receiver is triggered
312 /// periodically. A value of 0 turns the receiver off.
313 ///
314 /// The [`get_current_callback_receiver`] receiver is only triggered if the current has changed since
315 /// the last triggering.
316 pub fn set_current_callback_period(&self, period: u32) -> ConvertingReceiver<()> {
317 let mut payload = vec![0; 4];
318 payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
319
320 self.device.set(u8::from(Current25BrickletFunction::SetCurrentCallbackPeriod), payload)
321 }
322
323 /// Returns the period as set by [`set_current_callback_period`].
324 pub fn get_current_callback_period(&self) -> ConvertingReceiver<u32> {
325 let payload = vec![0; 0];
326
327 self.device.get(u8::from(Current25BrickletFunction::GetCurrentCallbackPeriod), payload)
328 }
329
330 /// Sets the period with which the [`get_analog_value_callback_receiver`] receiver is triggered
331 /// periodically. A value of 0 turns the receiver off.
332 ///
333 /// The [`get_analog_value_callback_receiver`] receiver is only triggered if the analog value has
334 /// changed since the last triggering.
335 pub fn set_analog_value_callback_period(&self, period: u32) -> ConvertingReceiver<()> {
336 let mut payload = vec![0; 4];
337 payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
338
339 self.device.set(u8::from(Current25BrickletFunction::SetAnalogValueCallbackPeriod), payload)
340 }
341
342 /// Returns the period as set by [`set_analog_value_callback_period`].
343 pub fn get_analog_value_callback_period(&self) -> ConvertingReceiver<u32> {
344 let payload = vec![0; 0];
345
346 self.device.get(u8::from(Current25BrickletFunction::GetAnalogValueCallbackPeriod), payload)
347 }
348
349 /// Sets the thresholds for the [`get_current_reached_callback_receiver`] receiver.
350 ///
351 /// The following options are possible:
352 ///
353 /// Option| Description
354 /// --- | ---
355 /// 'x'| Receiver is turned off
356 /// 'o'| Receiver is triggered when the current is *outside* the min and max values
357 /// 'i'| Receiver is triggered when the current is *inside* the min and max values
358 /// '<'| Receiver is triggered when the current is smaller than the min value (max is ignored)
359 /// '>'| Receiver is triggered when the current is greater than the min value (max is ignored)
360 ///
361 /// Associated constants:
362 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_OFF
363 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_OUTSIDE
364 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_INSIDE
365 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_SMALLER
366 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_GREATER
367 pub fn set_current_callback_threshold(&self, option: char, min: i16, max: i16) -> ConvertingReceiver<()> {
368 let mut payload = vec![0; 5];
369 payload[0..1].copy_from_slice(&<char>::to_le_byte_vec(option));
370 payload[1..3].copy_from_slice(&<i16>::to_le_byte_vec(min));
371 payload[3..5].copy_from_slice(&<i16>::to_le_byte_vec(max));
372
373 self.device.set(u8::from(Current25BrickletFunction::SetCurrentCallbackThreshold), payload)
374 }
375
376 /// Returns the threshold as set by [`set_current_callback_threshold`].
377 ///
378 /// Associated constants:
379 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_OFF
380 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_OUTSIDE
381 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_INSIDE
382 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_SMALLER
383 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_GREATER
384 pub fn get_current_callback_threshold(&self) -> ConvertingReceiver<CurrentCallbackThreshold> {
385 let payload = vec![0; 0];
386
387 self.device.get(u8::from(Current25BrickletFunction::GetCurrentCallbackThreshold), payload)
388 }
389
390 /// Sets the thresholds for the [`get_analog_value_reached_callback_receiver`] receiver.
391 ///
392 /// The following options are possible:
393 ///
394 /// Option| Description
395 /// --- | ---
396 /// 'x'| Receiver is turned off
397 /// 'o'| Receiver is triggered when the analog value is *outside* the min and max values
398 /// 'i'| Receiver is triggered when the analog value is *inside* the min and max values
399 /// '<'| Receiver is triggered when the analog value is smaller than the min value (max is ignored)
400 /// '>'| Receiver is triggered when the analog value is greater than the min value (max is ignored)
401 ///
402 /// Associated constants:
403 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_OFF
404 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_OUTSIDE
405 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_INSIDE
406 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_SMALLER
407 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_GREATER
408 pub fn set_analog_value_callback_threshold(&self, option: char, min: u16, max: u16) -> ConvertingReceiver<()> {
409 let mut payload = vec![0; 5];
410 payload[0..1].copy_from_slice(&<char>::to_le_byte_vec(option));
411 payload[1..3].copy_from_slice(&<u16>::to_le_byte_vec(min));
412 payload[3..5].copy_from_slice(&<u16>::to_le_byte_vec(max));
413
414 self.device.set(u8::from(Current25BrickletFunction::SetAnalogValueCallbackThreshold), payload)
415 }
416
417 /// Returns the threshold as set by [`set_analog_value_callback_threshold`].
418 ///
419 /// Associated constants:
420 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_OFF
421 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_OUTSIDE
422 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_INSIDE
423 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_SMALLER
424 /// * CURRENT25_BRICKLET_THRESHOLD_OPTION_GREATER
425 pub fn get_analog_value_callback_threshold(&self) -> ConvertingReceiver<AnalogValueCallbackThreshold> {
426 let payload = vec![0; 0];
427
428 self.device.get(u8::from(Current25BrickletFunction::GetAnalogValueCallbackThreshold), payload)
429 }
430
431 /// Sets the period with which the threshold receivers
432 ///
433 /// * [`get_current_reached_callback_receiver`],
434 /// * [`get_analog_value_reached_callback_receiver`]
435 ///
436 /// are triggered, if the thresholds
437 ///
438 /// * [`set_current_callback_threshold`],
439 /// * [`set_analog_value_callback_threshold`]
440 ///
441 /// keep being reached.
442 pub fn set_debounce_period(&self, debounce: u32) -> ConvertingReceiver<()> {
443 let mut payload = vec![0; 4];
444 payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(debounce));
445
446 self.device.set(u8::from(Current25BrickletFunction::SetDebouncePeriod), payload)
447 }
448
449 /// Returns the debounce period as set by [`set_debounce_period`].
450 pub fn get_debounce_period(&self) -> ConvertingReceiver<u32> {
451 let payload = vec![0; 0];
452
453 self.device.get(u8::from(Current25BrickletFunction::GetDebouncePeriod), payload)
454 }
455
456 /// Returns the UID, the UID where the Bricklet is connected to,
457 /// the position, the hardware and firmware version as well as the
458 /// device identifier.
459 ///
460 /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
461 /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
462 /// position 'z'.
463 ///
464 /// The device identifier numbers can be found [here](device_identifier).
465 /// |device_identifier_constant|
466 pub fn get_identity(&self) -> ConvertingReceiver<Identity> {
467 let payload = vec![0; 0];
468
469 self.device.get(u8::from(Current25BrickletFunction::GetIdentity), payload)
470 }
471}