1#[allow(unused_imports)]
15use crate::{
16 base58::Uid, byte_converter::*, device::*, error::TinkerforgeError, ip_connection::async_io::AsyncIpConnection,
17 low_level_traits::LowLevelRead,
18};
19#[allow(unused_imports)]
20use futures_core::Stream;
21#[allow(unused_imports)]
22use tokio_stream::StreamExt;
23pub enum HallEffectV2BrickletFunction {
24 GetMagneticFluxDensity,
25 SetMagneticFluxDensityCallbackConfiguration,
26 GetMagneticFluxDensityCallbackConfiguration,
27 GetCounter,
28 SetCounterConfig,
29 GetCounterConfig,
30 SetCounterCallbackConfiguration,
31 GetCounterCallbackConfiguration,
32 GetSpitfpErrorCount,
33 SetBootloaderMode,
34 GetBootloaderMode,
35 SetWriteFirmwarePointer,
36 WriteFirmware,
37 SetStatusLedConfig,
38 GetStatusLedConfig,
39 GetChipTemperature,
40 Reset,
41 WriteUid,
42 ReadUid,
43 GetIdentity,
44 CallbackMagneticFluxDensity,
45 CallbackCounter,
46}
47impl From<HallEffectV2BrickletFunction> for u8 {
48 fn from(fun: HallEffectV2BrickletFunction) -> Self {
49 match fun {
50 HallEffectV2BrickletFunction::GetMagneticFluxDensity => 1,
51 HallEffectV2BrickletFunction::SetMagneticFluxDensityCallbackConfiguration => 2,
52 HallEffectV2BrickletFunction::GetMagneticFluxDensityCallbackConfiguration => 3,
53 HallEffectV2BrickletFunction::GetCounter => 5,
54 HallEffectV2BrickletFunction::SetCounterConfig => 6,
55 HallEffectV2BrickletFunction::GetCounterConfig => 7,
56 HallEffectV2BrickletFunction::SetCounterCallbackConfiguration => 8,
57 HallEffectV2BrickletFunction::GetCounterCallbackConfiguration => 9,
58 HallEffectV2BrickletFunction::GetSpitfpErrorCount => 234,
59 HallEffectV2BrickletFunction::SetBootloaderMode => 235,
60 HallEffectV2BrickletFunction::GetBootloaderMode => 236,
61 HallEffectV2BrickletFunction::SetWriteFirmwarePointer => 237,
62 HallEffectV2BrickletFunction::WriteFirmware => 238,
63 HallEffectV2BrickletFunction::SetStatusLedConfig => 239,
64 HallEffectV2BrickletFunction::GetStatusLedConfig => 240,
65 HallEffectV2BrickletFunction::GetChipTemperature => 242,
66 HallEffectV2BrickletFunction::Reset => 243,
67 HallEffectV2BrickletFunction::WriteUid => 248,
68 HallEffectV2BrickletFunction::ReadUid => 249,
69 HallEffectV2BrickletFunction::GetIdentity => 255,
70 HallEffectV2BrickletFunction::CallbackMagneticFluxDensity => 4,
71 HallEffectV2BrickletFunction::CallbackCounter => 10,
72 }
73 }
74}
75pub const HALL_EFFECT_V2_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
76pub const HALL_EFFECT_V2_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
77pub const HALL_EFFECT_V2_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
78pub const HALL_EFFECT_V2_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
79pub const HALL_EFFECT_V2_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
80pub const HALL_EFFECT_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
81pub const HALL_EFFECT_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
82pub const HALL_EFFECT_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
83pub const HALL_EFFECT_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
84pub const HALL_EFFECT_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
85pub const HALL_EFFECT_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
86pub const HALL_EFFECT_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
87pub const HALL_EFFECT_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
88pub const HALL_EFFECT_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
89pub const HALL_EFFECT_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
90pub const HALL_EFFECT_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
91pub const HALL_EFFECT_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
92pub const HALL_EFFECT_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
93pub const HALL_EFFECT_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
94pub const HALL_EFFECT_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
95
96#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
97pub struct MagneticFluxDensityCallbackConfiguration {
98 pub period: u32,
99 pub value_has_to_change: bool,
100 pub option: char,
101 pub min: i16,
102 pub max: i16,
103}
104impl FromByteSlice for MagneticFluxDensityCallbackConfiguration {
105 fn bytes_expected() -> usize {
106 10
107 }
108 fn from_le_byte_slice(bytes: &[u8]) -> MagneticFluxDensityCallbackConfiguration {
109 MagneticFluxDensityCallbackConfiguration {
110 period: <u32>::from_le_byte_slice(&bytes[0..4]),
111 value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
112 option: <char>::from_le_byte_slice(&bytes[5..6]),
113 min: <i16>::from_le_byte_slice(&bytes[6..8]),
114 max: <i16>::from_le_byte_slice(&bytes[8..10]),
115 }
116 }
117}
118
119#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
120pub struct CounterConfig {
121 pub high_threshold: i16,
122 pub low_threshold: i16,
123 pub debounce: u32,
124}
125impl FromByteSlice for CounterConfig {
126 fn bytes_expected() -> usize {
127 8
128 }
129 fn from_le_byte_slice(bytes: &[u8]) -> CounterConfig {
130 CounterConfig {
131 high_threshold: <i16>::from_le_byte_slice(&bytes[0..2]),
132 low_threshold: <i16>::from_le_byte_slice(&bytes[2..4]),
133 debounce: <u32>::from_le_byte_slice(&bytes[4..8]),
134 }
135 }
136}
137
138#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
139pub struct CounterCallbackConfiguration {
140 pub period: u32,
141 pub value_has_to_change: bool,
142}
143impl FromByteSlice for CounterCallbackConfiguration {
144 fn bytes_expected() -> usize {
145 5
146 }
147 fn from_le_byte_slice(bytes: &[u8]) -> CounterCallbackConfiguration {
148 CounterCallbackConfiguration {
149 period: <u32>::from_le_byte_slice(&bytes[0..4]),
150 value_has_to_change: <bool>::from_le_byte_slice(&bytes[4..5]),
151 }
152 }
153}
154
155#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
156pub struct SpitfpErrorCount {
157 pub error_count_ack_checksum: u32,
158 pub error_count_message_checksum: u32,
159 pub error_count_frame: u32,
160 pub error_count_overflow: u32,
161}
162impl FromByteSlice for SpitfpErrorCount {
163 fn bytes_expected() -> usize {
164 16
165 }
166 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
167 SpitfpErrorCount {
168 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
169 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
170 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
171 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
172 }
173 }
174}
175
176#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
177pub struct Identity {
178 pub uid: String,
179 pub connected_uid: String,
180 pub position: char,
181 pub hardware_version: [u8; 3],
182 pub firmware_version: [u8; 3],
183 pub device_identifier: u16,
184}
185impl FromByteSlice for Identity {
186 fn bytes_expected() -> usize {
187 25
188 }
189 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
190 Identity {
191 uid: <String>::from_le_byte_slice(&bytes[0..8]),
192 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
193 position: <char>::from_le_byte_slice(&bytes[16..17]),
194 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
195 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
196 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
197 }
198 }
199}
200
201#[derive(Clone)]
203pub struct HallEffectV2Bricklet {
204 device: Device,
205}
206impl HallEffectV2Bricklet {
207 pub const DEVICE_IDENTIFIER: u16 = 2132;
208 pub const DEVICE_DISPLAY_NAME: &'static str = "Hall Effect Bricklet 2.0";
209 pub fn new(uid: Uid, connection: AsyncIpConnection) -> HallEffectV2Bricklet {
211 let mut result = HallEffectV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
212 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::GetMagneticFluxDensity) as usize] =
213 ResponseExpectedFlag::AlwaysTrue;
214 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::SetMagneticFluxDensityCallbackConfiguration) as usize] =
215 ResponseExpectedFlag::True;
216 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::GetMagneticFluxDensityCallbackConfiguration) as usize] =
217 ResponseExpectedFlag::AlwaysTrue;
218 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::GetCounter) as usize] = ResponseExpectedFlag::AlwaysTrue;
219 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::SetCounterConfig) as usize] = ResponseExpectedFlag::False;
220 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::GetCounterConfig) as usize] =
221 ResponseExpectedFlag::AlwaysTrue;
222 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::SetCounterCallbackConfiguration) as usize] =
223 ResponseExpectedFlag::True;
224 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::GetCounterCallbackConfiguration) as usize] =
225 ResponseExpectedFlag::AlwaysTrue;
226 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::GetSpitfpErrorCount) as usize] =
227 ResponseExpectedFlag::AlwaysTrue;
228 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::SetBootloaderMode) as usize] =
229 ResponseExpectedFlag::AlwaysTrue;
230 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::GetBootloaderMode) as usize] =
231 ResponseExpectedFlag::AlwaysTrue;
232 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
233 ResponseExpectedFlag::False;
234 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
235 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
236 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::GetStatusLedConfig) as usize] =
237 ResponseExpectedFlag::AlwaysTrue;
238 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::GetChipTemperature) as usize] =
239 ResponseExpectedFlag::AlwaysTrue;
240 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
241 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
242 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
243 result.device.response_expected[u8::from(HallEffectV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
244 result
245 }
246
247 pub fn get_response_expected(&mut self, fun: HallEffectV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
262 self.device.get_response_expected(u8::from(fun))
263 }
264
265 pub fn set_response_expected(
274 &mut self,
275 fun: HallEffectV2BrickletFunction,
276 response_expected: bool,
277 ) -> Result<(), SetResponseExpectedError> {
278 self.device.set_response_expected(u8::from(fun), response_expected)
279 }
280
281 pub fn set_response_expected_all(&mut self, response_expected: bool) {
283 self.device.set_response_expected_all(response_expected)
284 }
285
286 pub fn get_api_version(&self) -> [u8; 3] {
289 self.device.api_version
290 }
291
292 pub async fn get_magnetic_flux_density_callback_receiver(&mut self) -> impl Stream<Item = i16> {
300 self.device
301 .get_callback_receiver(u8::from(HallEffectV2BrickletFunction::CallbackMagneticFluxDensity))
302 .await
303 .map(|p| i16::from_le_byte_slice(p.body()))
304 }
305
306 pub async fn get_counter_callback_receiver(&mut self) -> impl Stream<Item = u32> {
311 self.device
312 .get_callback_receiver(u8::from(HallEffectV2BrickletFunction::CallbackCounter))
313 .await
314 .map(|p| u32::from_le_byte_slice(p.body()))
315 }
316
317 pub async fn get_magnetic_flux_density(&mut self) -> Result<i16, TinkerforgeError> {
324 let payload = [0; 0];
325
326 #[allow(unused_variables)]
327 let result = self.device.get(u8::from(HallEffectV2BrickletFunction::GetMagneticFluxDensity), &payload).await?;
328 Ok(i16::from_le_byte_slice(result.body()))
329 }
330
331 pub async fn set_magnetic_flux_density_callback_configuration(
364 &mut self,
365 period: u32,
366 value_has_to_change: bool,
367 option: char,
368 min: i16,
369 max: i16,
370 ) -> Result<(), TinkerforgeError> {
371 let mut payload = [0; 10];
372 period.write_to_slice(&mut payload[0..4]);
373 value_has_to_change.write_to_slice(&mut payload[4..5]);
374 option.write_to_slice(&mut payload[5..6]);
375 min.write_to_slice(&mut payload[6..8]);
376 max.write_to_slice(&mut payload[8..10]);
377
378 #[allow(unused_variables)]
379 let result = self.device.set(u8::from(HallEffectV2BrickletFunction::SetMagneticFluxDensityCallbackConfiguration), &payload).await?;
380 Ok(())
381 }
382
383 pub async fn get_magnetic_flux_density_callback_configuration(
392 &mut self,
393 ) -> Result<MagneticFluxDensityCallbackConfiguration, TinkerforgeError> {
394 let payload = [0; 0];
395
396 #[allow(unused_variables)]
397 let result = self.device.get(u8::from(HallEffectV2BrickletFunction::GetMagneticFluxDensityCallbackConfiguration), &payload).await?;
398 Ok(MagneticFluxDensityCallbackConfiguration::from_le_byte_slice(result.body()))
399 }
400
401 pub async fn get_counter(&mut self, reset_counter: bool) -> Result<u32, TinkerforgeError> {
413 let mut payload = [0; 1];
414 reset_counter.write_to_slice(&mut payload[0..1]);
415
416 #[allow(unused_variables)]
417 let result = self.device.get(u8::from(HallEffectV2BrickletFunction::GetCounter), &payload).await?;
418 Ok(u32::from_le_byte_slice(result.body()))
419 }
420
421 pub async fn set_counter_config(&mut self, high_threshold: i16, low_threshold: i16, debounce: u32) -> Result<(), TinkerforgeError> {
428 let mut payload = [0; 8];
429 high_threshold.write_to_slice(&mut payload[0..2]);
430 low_threshold.write_to_slice(&mut payload[2..4]);
431 debounce.write_to_slice(&mut payload[4..8]);
432
433 #[allow(unused_variables)]
434 let result = self.device.set(u8::from(HallEffectV2BrickletFunction::SetCounterConfig), &payload).await?;
435 Ok(())
436 }
437
438 pub async fn get_counter_config(&mut self) -> Result<CounterConfig, TinkerforgeError> {
440 let payload = [0; 0];
441
442 #[allow(unused_variables)]
443 let result = self.device.get(u8::from(HallEffectV2BrickletFunction::GetCounterConfig), &payload).await?;
444 Ok(CounterConfig::from_le_byte_slice(result.body()))
445 }
446
447 pub async fn set_counter_callback_configuration(&mut self, period: u32, value_has_to_change: bool) -> Result<(), TinkerforgeError> {
457 let mut payload = [0; 5];
458 period.write_to_slice(&mut payload[0..4]);
459 value_has_to_change.write_to_slice(&mut payload[4..5]);
460
461 #[allow(unused_variables)]
462 let result = self.device.set(u8::from(HallEffectV2BrickletFunction::SetCounterCallbackConfiguration), &payload).await?;
463 Ok(())
464 }
465
466 pub async fn get_counter_callback_configuration(&mut self) -> Result<CounterCallbackConfiguration, TinkerforgeError> {
469 let payload = [0; 0];
470
471 #[allow(unused_variables)]
472 let result = self.device.get(u8::from(HallEffectV2BrickletFunction::GetCounterCallbackConfiguration), &payload).await?;
473 Ok(CounterCallbackConfiguration::from_le_byte_slice(result.body()))
474 }
475
476 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
488 let payload = [0; 0];
489
490 #[allow(unused_variables)]
491 let result = self.device.get(u8::from(HallEffectV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
492 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
493 }
494
495 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
518 let mut payload = [0; 1];
519 mode.write_to_slice(&mut payload[0..1]);
520
521 #[allow(unused_variables)]
522 let result = self.device.get(u8::from(HallEffectV2BrickletFunction::SetBootloaderMode), &payload).await?;
523 Ok(u8::from_le_byte_slice(result.body()))
524 }
525
526 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
535 let payload = [0; 0];
536
537 #[allow(unused_variables)]
538 let result = self.device.get(u8::from(HallEffectV2BrickletFunction::GetBootloaderMode), &payload).await?;
539 Ok(u8::from_le_byte_slice(result.body()))
540 }
541
542 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
549 let mut payload = [0; 4];
550 pointer.write_to_slice(&mut payload[0..4]);
551
552 #[allow(unused_variables)]
553 let result = self.device.set(u8::from(HallEffectV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
554 Ok(())
555 }
556
557 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
566 let mut payload = [0; 64];
567 data.write_to_slice(&mut payload[0..64]);
568
569 #[allow(unused_variables)]
570 let result = self.device.get(u8::from(HallEffectV2BrickletFunction::WriteFirmware), &payload).await?;
571 Ok(u8::from_le_byte_slice(result.body()))
572 }
573
574 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
588 let mut payload = [0; 1];
589 config.write_to_slice(&mut payload[0..1]);
590
591 #[allow(unused_variables)]
592 let result = self.device.set(u8::from(HallEffectV2BrickletFunction::SetStatusLedConfig), &payload).await?;
593 Ok(())
594 }
595
596 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
604 let payload = [0; 0];
605
606 #[allow(unused_variables)]
607 let result = self.device.get(u8::from(HallEffectV2BrickletFunction::GetStatusLedConfig), &payload).await?;
608 Ok(u8::from_le_byte_slice(result.body()))
609 }
610
611 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
618 let payload = [0; 0];
619
620 #[allow(unused_variables)]
621 let result = self.device.get(u8::from(HallEffectV2BrickletFunction::GetChipTemperature), &payload).await?;
622 Ok(i16::from_le_byte_slice(result.body()))
623 }
624
625 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
632 let payload = [0; 0];
633
634 #[allow(unused_variables)]
635 let result = self.device.set(u8::from(HallEffectV2BrickletFunction::Reset), &payload).await?;
636 Ok(())
637 }
638
639 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
645 let mut payload = [0; 4];
646 uid.write_to_slice(&mut payload[0..4]);
647
648 #[allow(unused_variables)]
649 let result = self.device.set(u8::from(HallEffectV2BrickletFunction::WriteUid), &payload).await?;
650 Ok(())
651 }
652
653 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
656 let payload = [0; 0];
657
658 #[allow(unused_variables)]
659 let result = self.device.get(u8::from(HallEffectV2BrickletFunction::ReadUid), &payload).await?;
660 Ok(u32::from_le_byte_slice(result.body()))
661 }
662
663 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
674 let payload = [0; 0];
675
676 #[allow(unused_variables)]
677 let result = self.device.get(u8::from(HallEffectV2BrickletFunction::GetIdentity), &payload).await?;
678 Ok(Identity::from_le_byte_slice(result.body()))
679 }
680}