1use 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#[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 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 pub fn get_response_expected(&mut self, fun: HallEffectBrickletFunction) -> Result<bool, GetResponseExpectedError> {
153 self.device.get_response_expected(u8::from(fun))
154 }
155
156 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 pub fn set_response_expected_all(&mut self, response_expected: bool) { self.device.set_response_expected_all(response_expected) }
174
175 pub fn get_api_version(&self) -> [u8; 3] { self.device.api_version }
178
179 pub fn get_edge_count_callback_receiver(&self) -> ConvertingCallbackReceiver<EdgeCountEvent> {
192 self.device.get_callback_receiver(u8::from(HallEffectBrickletFunction::CallbackEdgeCount))
193 }
194
195 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 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 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 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 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 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 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 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 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 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}