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