tinkerforge/bindings/
hall_effect_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//! Detects presence of magnetic field.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/HallEffect_Bricklet_Rust.html).
14use crate::{
15    byte_converter::*, converting_callback_receiver::ConvertingCallbackReceiver, converting_receiver::ConvertingReceiver, device::*,
16    ip_connection::GetRequestSender,
17};
18pub enum HallEffectBrickletFunction {
19    GetValue,
20    GetEdgeCount,
21    SetEdgeCountConfig,
22    GetEdgeCountConfig,
23    SetEdgeInterrupt,
24    GetEdgeInterrupt,
25    SetEdgeCountCallbackPeriod,
26    GetEdgeCountCallbackPeriod,
27    EdgeInterrupt,
28    GetIdentity,
29    CallbackEdgeCount,
30}
31impl From<HallEffectBrickletFunction> for u8 {
32    fn from(fun: HallEffectBrickletFunction) -> Self {
33        match fun {
34            HallEffectBrickletFunction::GetValue => 1,
35            HallEffectBrickletFunction::GetEdgeCount => 2,
36            HallEffectBrickletFunction::SetEdgeCountConfig => 3,
37            HallEffectBrickletFunction::GetEdgeCountConfig => 4,
38            HallEffectBrickletFunction::SetEdgeInterrupt => 5,
39            HallEffectBrickletFunction::GetEdgeInterrupt => 6,
40            HallEffectBrickletFunction::SetEdgeCountCallbackPeriod => 7,
41            HallEffectBrickletFunction::GetEdgeCountCallbackPeriod => 8,
42            HallEffectBrickletFunction::EdgeInterrupt => 9,
43            HallEffectBrickletFunction::GetIdentity => 255,
44            HallEffectBrickletFunction::CallbackEdgeCount => 10,
45        }
46    }
47}
48pub const HALL_EFFECT_BRICKLET_EDGE_TYPE_RISING: u8 = 0;
49pub const HALL_EFFECT_BRICKLET_EDGE_TYPE_FALLING: u8 = 1;
50pub const HALL_EFFECT_BRICKLET_EDGE_TYPE_BOTH: u8 = 2;
51
52#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
53pub struct EdgeCountConfig {
54    pub edge_type: u8,
55    pub debounce: u8,
56}
57impl FromByteSlice for EdgeCountConfig {
58    fn bytes_expected() -> usize { 2 }
59    fn from_le_byte_slice(bytes: &[u8]) -> EdgeCountConfig {
60        EdgeCountConfig { edge_type: <u8>::from_le_byte_slice(&bytes[0..1]), debounce: <u8>::from_le_byte_slice(&bytes[1..2]) }
61    }
62}
63
64#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
65pub struct EdgeInterrupt {
66    pub count: u32,
67    pub value: bool,
68}
69impl FromByteSlice for EdgeInterrupt {
70    fn bytes_expected() -> usize { 5 }
71    fn from_le_byte_slice(bytes: &[u8]) -> EdgeInterrupt {
72        EdgeInterrupt { count: <u32>::from_le_byte_slice(&bytes[0..4]), value: <bool>::from_le_byte_slice(&bytes[4..5]) }
73    }
74}
75
76#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
77pub struct EdgeCountEvent {
78    pub count: u32,
79    pub value: bool,
80}
81impl FromByteSlice for EdgeCountEvent {
82    fn bytes_expected() -> usize { 5 }
83    fn from_le_byte_slice(bytes: &[u8]) -> EdgeCountEvent {
84        EdgeCountEvent { count: <u32>::from_le_byte_slice(&bytes[0..4]), value: <bool>::from_le_byte_slice(&bytes[4..5]) }
85    }
86}
87
88#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
89pub struct Identity {
90    pub uid: String,
91    pub connected_uid: String,
92    pub position: char,
93    pub hardware_version: [u8; 3],
94    pub firmware_version: [u8; 3],
95    pub device_identifier: u16,
96}
97impl FromByteSlice for Identity {
98    fn bytes_expected() -> usize { 25 }
99    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
100        Identity {
101            uid: <String>::from_le_byte_slice(&bytes[0..8]),
102            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
103            position: <char>::from_le_byte_slice(&bytes[16..17]),
104            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
105            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
106            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
107        }
108    }
109}
110
111/// Detects presence of magnetic field
112#[derive(Clone)]
113pub struct HallEffectBricklet {
114    device: Device,
115}
116impl HallEffectBricklet {
117    pub const DEVICE_IDENTIFIER: u16 = 240;
118    pub const DEVICE_DISPLAY_NAME: &'static str = "Hall Effect Bricklet";
119    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
120    pub fn new<T: GetRequestSender>(uid: &str, req_sender: T) -> HallEffectBricklet {
121        let mut result = HallEffectBricklet { device: Device::new([2, 0, 0], uid, req_sender, 0) };
122        result.device.response_expected[u8::from(HallEffectBrickletFunction::GetValue) as usize] = ResponseExpectedFlag::AlwaysTrue;
123        result.device.response_expected[u8::from(HallEffectBrickletFunction::GetEdgeCount) as usize] = ResponseExpectedFlag::AlwaysTrue;
124        result.device.response_expected[u8::from(HallEffectBrickletFunction::SetEdgeCountConfig) as usize] = ResponseExpectedFlag::False;
125        result.device.response_expected[u8::from(HallEffectBrickletFunction::GetEdgeCountConfig) as usize] =
126            ResponseExpectedFlag::AlwaysTrue;
127        result.device.response_expected[u8::from(HallEffectBrickletFunction::SetEdgeInterrupt) as usize] = ResponseExpectedFlag::True;
128        result.device.response_expected[u8::from(HallEffectBrickletFunction::GetEdgeInterrupt) as usize] = ResponseExpectedFlag::AlwaysTrue;
129        result.device.response_expected[u8::from(HallEffectBrickletFunction::SetEdgeCountCallbackPeriod) as usize] =
130            ResponseExpectedFlag::True;
131        result.device.response_expected[u8::from(HallEffectBrickletFunction::GetEdgeCountCallbackPeriod) as usize] =
132            ResponseExpectedFlag::AlwaysTrue;
133        result.device.response_expected[u8::from(HallEffectBrickletFunction::EdgeInterrupt) as usize] = ResponseExpectedFlag::AlwaysTrue;
134        result.device.response_expected[u8::from(HallEffectBrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
135        result
136    }
137
138    /// Returns the response expected flag for the function specified by the function ID parameter.
139    /// It is true if the function is expected to send a response, false otherwise.
140    ///
141    /// For getter functions this is enabled by default and cannot be disabled, because those
142    /// functions will always send a response. For callback configuration functions it is enabled
143    /// by default too, but can be disabled by [`set_response_expected`](crate::hall_effect_bricklet::HallEffectBricklet::set_response_expected).
144    /// For setter functions it is disabled by default and can be enabled.
145    ///
146    /// Enabling the response expected flag for a setter function allows to detect timeouts
147    /// and other error conditions calls of this setter as well. The device will then send a response
148    /// for this purpose. If this flag is disabled for a setter function then no response is sent
149    /// and errors are silently ignored, because they cannot be detected.
150    ///
151    /// See [`set_response_expected`](crate::hall_effect_bricklet::HallEffectBricklet::set_response_expected) for the list of function ID constants available for this function.
152    pub fn get_response_expected(&mut self, fun: HallEffectBrickletFunction) -> Result<bool, GetResponseExpectedError> {
153        self.device.get_response_expected(u8::from(fun))
154    }
155
156    /// Changes the response expected flag of the function specified by the function ID parameter.
157    /// This flag can only be changed for setter (default value: false) and callback configuration
158    /// functions (default value: true). For getter functions it is always enabled.
159    ///
160    /// Enabling the response expected flag for a setter function allows to detect timeouts and
161    /// other error conditions calls of this setter as well. The device will then send a response
162    /// for this purpose. If this flag is disabled for a setter function then no response is sent
163    /// and errors are silently ignored, because they cannot be detected.
164    pub fn set_response_expected(
165        &mut self,
166        fun: HallEffectBrickletFunction,
167        response_expected: bool,
168    ) -> Result<(), SetResponseExpectedError> {
169        self.device.set_response_expected(u8::from(fun), response_expected)
170    }
171
172    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
173    pub fn set_response_expected_all(&mut self, response_expected: bool) { self.device.set_response_expected_all(response_expected) }
174
175    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
176    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
177    pub fn get_api_version(&self) -> [u8; 3] { self.device.api_version }
178
179    /// This receiver is triggered periodically with the period that is set by
180    /// [`set_edge_count_callback_period`]. The parameters are the
181    /// current count and the current value (see [`get_value`] and
182    /// [`get_edge_count`]).
183    ///
184    /// The [`get_edge_count_callback_receiver`] receiver is only triggered if the count or value changed
185    /// since the last triggering.
186    ///
187    /// [`get_value`]: #method.get_value
188    /// [`get_edge_count`]: #method.get_edge_count
189    /// [`set_edge_count_callback_period`]: #method.set_edge_count_callback_period
190    /// [`get_edge_count_callback_receiver`]: #method.get_edge_count_callback_receiver
191    pub fn get_edge_count_callback_receiver(&self) -> ConvertingCallbackReceiver<EdgeCountEvent> {
192        self.device.get_callback_receiver(u8::from(HallEffectBrickletFunction::CallbackEdgeCount))
193    }
194
195    /// Returns *true* if a magnetic field of 3.5 millitesla or greater is detected.
196    pub fn get_value(&self) -> ConvertingReceiver<bool> {
197        let payload = vec![0; 0];
198
199        self.device.get(u8::from(HallEffectBrickletFunction::GetValue), payload)
200    }
201
202    /// Returns the current value of the edge counter. You can configure
203    /// edge type (rising, falling, both) that is counted with
204    /// [`set_edge_count_config`].
205    ///
206    /// If you set the reset counter to *true*, the count is set back to 0
207    /// directly after it is read.
208    pub fn get_edge_count(&self, reset_counter: bool) -> ConvertingReceiver<u32> {
209        let mut payload = vec![0; 1];
210        payload[0..1].copy_from_slice(&<bool>::to_le_byte_vec(reset_counter));
211
212        self.device.get(u8::from(HallEffectBrickletFunction::GetEdgeCount), payload)
213    }
214
215    /// The edge type parameter configures if rising edges, falling edges or
216    /// both are counted. Possible edge types are:
217    ///
218    /// * 0 = rising
219    /// * 1 = falling
220    /// * 2 = both
221    ///
222    /// A magnetic field of 3.5 millitesla or greater causes a falling edge and a
223    /// magnetic field of 2.5 millitesla or smaller causes a rising edge.
224    ///
225    /// If a magnet comes near the Bricklet the signal goes low (falling edge), if
226    /// a magnet is removed from the vicinity the signal goes high (rising edge).
227    ///
228    /// Configuring an edge counter resets its value to 0.
229    ///
230    /// If you don't know what any of this means, just leave it at default. The
231    /// default configuration is very likely OK for you.
232    ///
233    /// Associated constants:
234    /// * HALL_EFFECT_BRICKLET_EDGE_TYPE_RISING
235    ///	* HALL_EFFECT_BRICKLET_EDGE_TYPE_FALLING
236    ///	* HALL_EFFECT_BRICKLET_EDGE_TYPE_BOTH
237    pub fn set_edge_count_config(&self, edge_type: u8, debounce: u8) -> ConvertingReceiver<()> {
238        let mut payload = vec![0; 2];
239        payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(edge_type));
240        payload[1..2].copy_from_slice(&<u8>::to_le_byte_vec(debounce));
241
242        self.device.set(u8::from(HallEffectBrickletFunction::SetEdgeCountConfig), payload)
243    }
244
245    /// Returns the edge type and debounce time as set by [`set_edge_count_config`].
246    ///
247    /// Associated constants:
248    /// * HALL_EFFECT_BRICKLET_EDGE_TYPE_RISING
249    ///	* HALL_EFFECT_BRICKLET_EDGE_TYPE_FALLING
250    ///	* HALL_EFFECT_BRICKLET_EDGE_TYPE_BOTH
251    pub fn get_edge_count_config(&self) -> ConvertingReceiver<EdgeCountConfig> {
252        let payload = vec![0; 0];
253
254        self.device.get(u8::from(HallEffectBrickletFunction::GetEdgeCountConfig), payload)
255    }
256
257    /// Sets the number of edges until an interrupt is invoked.
258    ///
259    /// If *edges* is set to n, an interrupt is invoked for every n-th detected edge.
260    ///
261    /// If *edges* is set to 0, the interrupt is disabled.
262    pub fn set_edge_interrupt(&self, edges: u32) -> ConvertingReceiver<()> {
263        let mut payload = vec![0; 4];
264        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(edges));
265
266        self.device.set(u8::from(HallEffectBrickletFunction::SetEdgeInterrupt), payload)
267    }
268
269    /// Returns the edges as set by [`set_edge_interrupt`].
270    pub fn get_edge_interrupt(&self) -> ConvertingReceiver<u32> {
271        let payload = vec![0; 0];
272
273        self.device.get(u8::from(HallEffectBrickletFunction::GetEdgeInterrupt), payload)
274    }
275
276    /// Sets the period with which the [`get_edge_count_callback_receiver`] receiver is triggered
277    /// periodically. A value of 0 turns the receiver off.
278    ///
279    /// The [`get_edge_count_callback_receiver`] receiver is only triggered if the edge count has changed
280    /// since the last triggering.
281    pub fn set_edge_count_callback_period(&self, period: u32) -> ConvertingReceiver<()> {
282        let mut payload = vec![0; 4];
283        payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
284
285        self.device.set(u8::from(HallEffectBrickletFunction::SetEdgeCountCallbackPeriod), payload)
286    }
287
288    /// Returns the period as set by [`set_edge_count_callback_period`].
289    pub fn get_edge_count_callback_period(&self) -> ConvertingReceiver<u32> {
290        let payload = vec![0; 0];
291
292        self.device.get(u8::from(HallEffectBrickletFunction::GetEdgeCountCallbackPeriod), payload)
293    }
294
295    /// This receiver is triggered every n-th count, as configured with
296    /// [`set_edge_interrupt`]. The parameters are the
297    /// current count and the current value (see [`get_value`] and
298    /// [`get_edge_count`]).
299    pub fn edge_interrupt(&self) -> ConvertingReceiver<EdgeInterrupt> {
300        let payload = vec![0; 0];
301
302        self.device.get(u8::from(HallEffectBrickletFunction::EdgeInterrupt), payload)
303    }
304
305    /// Returns the UID, the UID where the Bricklet is connected to,
306    /// the position, the hardware and firmware version as well as the
307    /// device identifier.
308    ///
309    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
310    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
311    /// position 'z'.
312    ///
313    /// The device identifier numbers can be found [here](device_identifier).
314    /// |device_identifier_constant|
315    pub fn get_identity(&self) -> ConvertingReceiver<Identity> {
316        let payload = vec![0; 0];
317
318        self.device.get(u8::from(HallEffectBrickletFunction::GetIdentity), payload)
319    }
320}