tinkerforge_async/bindings/
motion_detector_v2_bricklet.rs1#[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 MotionDetectorV2BrickletFunction {
24 GetMotionDetected,
25 SetSensitivity,
26 GetSensitivity,
27 SetIndicator,
28 GetIndicator,
29 GetSpitfpErrorCount,
30 SetBootloaderMode,
31 GetBootloaderMode,
32 SetWriteFirmwarePointer,
33 WriteFirmware,
34 SetStatusLedConfig,
35 GetStatusLedConfig,
36 GetChipTemperature,
37 Reset,
38 WriteUid,
39 ReadUid,
40 GetIdentity,
41 CallbackMotionDetected,
42 CallbackDetectionCycleEnded,
43}
44impl From<MotionDetectorV2BrickletFunction> for u8 {
45 fn from(fun: MotionDetectorV2BrickletFunction) -> Self {
46 match fun {
47 MotionDetectorV2BrickletFunction::GetMotionDetected => 1,
48 MotionDetectorV2BrickletFunction::SetSensitivity => 2,
49 MotionDetectorV2BrickletFunction::GetSensitivity => 3,
50 MotionDetectorV2BrickletFunction::SetIndicator => 4,
51 MotionDetectorV2BrickletFunction::GetIndicator => 5,
52 MotionDetectorV2BrickletFunction::GetSpitfpErrorCount => 234,
53 MotionDetectorV2BrickletFunction::SetBootloaderMode => 235,
54 MotionDetectorV2BrickletFunction::GetBootloaderMode => 236,
55 MotionDetectorV2BrickletFunction::SetWriteFirmwarePointer => 237,
56 MotionDetectorV2BrickletFunction::WriteFirmware => 238,
57 MotionDetectorV2BrickletFunction::SetStatusLedConfig => 239,
58 MotionDetectorV2BrickletFunction::GetStatusLedConfig => 240,
59 MotionDetectorV2BrickletFunction::GetChipTemperature => 242,
60 MotionDetectorV2BrickletFunction::Reset => 243,
61 MotionDetectorV2BrickletFunction::WriteUid => 248,
62 MotionDetectorV2BrickletFunction::ReadUid => 249,
63 MotionDetectorV2BrickletFunction::GetIdentity => 255,
64 MotionDetectorV2BrickletFunction::CallbackMotionDetected => 6,
65 MotionDetectorV2BrickletFunction::CallbackDetectionCycleEnded => 7,
66 }
67 }
68}
69pub const MOTION_DETECTOR_V2_BRICKLET_MOTION_NOT_DETECTED: u8 = 0;
70pub const MOTION_DETECTOR_V2_BRICKLET_MOTION_DETECTED: u8 = 1;
71pub const MOTION_DETECTOR_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
72pub const MOTION_DETECTOR_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
73pub const MOTION_DETECTOR_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
74pub const MOTION_DETECTOR_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
75pub const MOTION_DETECTOR_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
76pub const MOTION_DETECTOR_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
77pub const MOTION_DETECTOR_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
78pub const MOTION_DETECTOR_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
79pub const MOTION_DETECTOR_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
80pub const MOTION_DETECTOR_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
81pub const MOTION_DETECTOR_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
82pub const MOTION_DETECTOR_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
83pub const MOTION_DETECTOR_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
84pub const MOTION_DETECTOR_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
85pub const MOTION_DETECTOR_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
86
87#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
88pub struct Indicator {
89 pub top_left: u8,
90 pub top_right: u8,
91 pub bottom: u8,
92}
93impl FromByteSlice for Indicator {
94 fn bytes_expected() -> usize {
95 3
96 }
97 fn from_le_byte_slice(bytes: &[u8]) -> Indicator {
98 Indicator {
99 top_left: <u8>::from_le_byte_slice(&bytes[0..1]),
100 top_right: <u8>::from_le_byte_slice(&bytes[1..2]),
101 bottom: <u8>::from_le_byte_slice(&bytes[2..3]),
102 }
103 }
104}
105
106#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
107pub struct SpitfpErrorCount {
108 pub error_count_ack_checksum: u32,
109 pub error_count_message_checksum: u32,
110 pub error_count_frame: u32,
111 pub error_count_overflow: u32,
112}
113impl FromByteSlice for SpitfpErrorCount {
114 fn bytes_expected() -> usize {
115 16
116 }
117 fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
118 SpitfpErrorCount {
119 error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
120 error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
121 error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
122 error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
123 }
124 }
125}
126
127#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
128pub struct Identity {
129 pub uid: String,
130 pub connected_uid: String,
131 pub position: char,
132 pub hardware_version: [u8; 3],
133 pub firmware_version: [u8; 3],
134 pub device_identifier: u16,
135}
136impl FromByteSlice for Identity {
137 fn bytes_expected() -> usize {
138 25
139 }
140 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
141 Identity {
142 uid: <String>::from_le_byte_slice(&bytes[0..8]),
143 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
144 position: <char>::from_le_byte_slice(&bytes[16..17]),
145 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
146 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
147 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
148 }
149 }
150}
151
152#[derive(Clone)]
154pub struct MotionDetectorV2Bricklet {
155 device: Device,
156}
157impl MotionDetectorV2Bricklet {
158 pub const DEVICE_IDENTIFIER: u16 = 292;
159 pub const DEVICE_DISPLAY_NAME: &'static str = "Motion Detector Bricklet 2.0";
160 pub fn new(uid: Uid, connection: AsyncIpConnection) -> MotionDetectorV2Bricklet {
162 let mut result = MotionDetectorV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
163 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::GetMotionDetected) as usize] =
164 ResponseExpectedFlag::AlwaysTrue;
165 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::SetSensitivity) as usize] = ResponseExpectedFlag::False;
166 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::GetSensitivity) as usize] =
167 ResponseExpectedFlag::AlwaysTrue;
168 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::SetIndicator) as usize] = ResponseExpectedFlag::False;
169 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::GetIndicator) as usize] =
170 ResponseExpectedFlag::AlwaysTrue;
171 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::GetSpitfpErrorCount) as usize] =
172 ResponseExpectedFlag::AlwaysTrue;
173 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::SetBootloaderMode) as usize] =
174 ResponseExpectedFlag::AlwaysTrue;
175 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::GetBootloaderMode) as usize] =
176 ResponseExpectedFlag::AlwaysTrue;
177 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::SetWriteFirmwarePointer) as usize] =
178 ResponseExpectedFlag::False;
179 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::WriteFirmware) as usize] =
180 ResponseExpectedFlag::AlwaysTrue;
181 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::SetStatusLedConfig) as usize] =
182 ResponseExpectedFlag::False;
183 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::GetStatusLedConfig) as usize] =
184 ResponseExpectedFlag::AlwaysTrue;
185 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::GetChipTemperature) as usize] =
186 ResponseExpectedFlag::AlwaysTrue;
187 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
188 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
189 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
190 result.device.response_expected[u8::from(MotionDetectorV2BrickletFunction::GetIdentity) as usize] =
191 ResponseExpectedFlag::AlwaysTrue;
192 result
193 }
194
195 pub fn get_response_expected(&mut self, fun: MotionDetectorV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
210 self.device.get_response_expected(u8::from(fun))
211 }
212
213 pub fn set_response_expected(
222 &mut self,
223 fun: MotionDetectorV2BrickletFunction,
224 response_expected: bool,
225 ) -> Result<(), SetResponseExpectedError> {
226 self.device.set_response_expected(u8::from(fun), response_expected)
227 }
228
229 pub fn set_response_expected_all(&mut self, response_expected: bool) {
231 self.device.set_response_expected_all(response_expected)
232 }
233
234 pub fn get_api_version(&self) -> [u8; 3] {
237 self.device.api_version
238 }
239
240 pub async fn get_motion_detected_callback_receiver(&mut self) -> impl Stream<Item = ()> {
242 self.device.get_callback_receiver(u8::from(MotionDetectorV2BrickletFunction::CallbackMotionDetected)).await.map(|_p| ())
243 }
244
245 pub async fn get_detection_cycle_ended_callback_receiver(&mut self) -> impl Stream<Item = ()> {
249 self.device.get_callback_receiver(u8::from(MotionDetectorV2BrickletFunction::CallbackDetectionCycleEnded)).await.map(|_p| ())
250 }
251
252 pub async fn get_motion_detected(&mut self) -> Result<u8, TinkerforgeError> {
259 let payload = [0; 0];
260
261 #[allow(unused_variables)]
262 let result = self.device.get(u8::from(MotionDetectorV2BrickletFunction::GetMotionDetected), &payload).await?;
263 Ok(u8::from_le_byte_slice(result.body()))
264 }
265
266 pub async fn set_sensitivity(&mut self, sensitivity: u8) -> Result<(), TinkerforgeError> {
275 let mut payload = [0; 1];
276 sensitivity.write_to_slice(&mut payload[0..1]);
277
278 #[allow(unused_variables)]
279 let result = self.device.set(u8::from(MotionDetectorV2BrickletFunction::SetSensitivity), &payload).await?;
280 Ok(())
281 }
282
283 pub async fn get_sensitivity(&mut self) -> Result<u8, TinkerforgeError> {
285 let payload = [0; 0];
286
287 #[allow(unused_variables)]
288 let result = self.device.get(u8::from(MotionDetectorV2BrickletFunction::GetSensitivity), &payload).await?;
289 Ok(u8::from_le_byte_slice(result.body()))
290 }
291
292 pub async fn set_indicator(&mut self, top_left: u8, top_right: u8, bottom: u8) -> Result<(), TinkerforgeError> {
297 let mut payload = [0; 3];
298 top_left.write_to_slice(&mut payload[0..1]);
299 top_right.write_to_slice(&mut payload[1..2]);
300 bottom.write_to_slice(&mut payload[2..3]);
301
302 #[allow(unused_variables)]
303 let result = self.device.set(u8::from(MotionDetectorV2BrickletFunction::SetIndicator), &payload).await?;
304 Ok(())
305 }
306
307 pub async fn get_indicator(&mut self) -> Result<Indicator, TinkerforgeError> {
309 let payload = [0; 0];
310
311 #[allow(unused_variables)]
312 let result = self.device.get(u8::from(MotionDetectorV2BrickletFunction::GetIndicator), &payload).await?;
313 Ok(Indicator::from_le_byte_slice(result.body()))
314 }
315
316 pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
328 let payload = [0; 0];
329
330 #[allow(unused_variables)]
331 let result = self.device.get(u8::from(MotionDetectorV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
332 Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
333 }
334
335 pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
358 let mut payload = [0; 1];
359 mode.write_to_slice(&mut payload[0..1]);
360
361 #[allow(unused_variables)]
362 let result = self.device.get(u8::from(MotionDetectorV2BrickletFunction::SetBootloaderMode), &payload).await?;
363 Ok(u8::from_le_byte_slice(result.body()))
364 }
365
366 pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
375 let payload = [0; 0];
376
377 #[allow(unused_variables)]
378 let result = self.device.get(u8::from(MotionDetectorV2BrickletFunction::GetBootloaderMode), &payload).await?;
379 Ok(u8::from_le_byte_slice(result.body()))
380 }
381
382 pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
389 let mut payload = [0; 4];
390 pointer.write_to_slice(&mut payload[0..4]);
391
392 #[allow(unused_variables)]
393 let result = self.device.set(u8::from(MotionDetectorV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
394 Ok(())
395 }
396
397 pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
406 let mut payload = [0; 64];
407 data.write_to_slice(&mut payload[0..64]);
408
409 #[allow(unused_variables)]
410 let result = self.device.get(u8::from(MotionDetectorV2BrickletFunction::WriteFirmware), &payload).await?;
411 Ok(u8::from_le_byte_slice(result.body()))
412 }
413
414 pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
428 let mut payload = [0; 1];
429 config.write_to_slice(&mut payload[0..1]);
430
431 #[allow(unused_variables)]
432 let result = self.device.set(u8::from(MotionDetectorV2BrickletFunction::SetStatusLedConfig), &payload).await?;
433 Ok(())
434 }
435
436 pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
444 let payload = [0; 0];
445
446 #[allow(unused_variables)]
447 let result = self.device.get(u8::from(MotionDetectorV2BrickletFunction::GetStatusLedConfig), &payload).await?;
448 Ok(u8::from_le_byte_slice(result.body()))
449 }
450
451 pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
458 let payload = [0; 0];
459
460 #[allow(unused_variables)]
461 let result = self.device.get(u8::from(MotionDetectorV2BrickletFunction::GetChipTemperature), &payload).await?;
462 Ok(i16::from_le_byte_slice(result.body()))
463 }
464
465 pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
472 let payload = [0; 0];
473
474 #[allow(unused_variables)]
475 let result = self.device.set(u8::from(MotionDetectorV2BrickletFunction::Reset), &payload).await?;
476 Ok(())
477 }
478
479 pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
485 let mut payload = [0; 4];
486 uid.write_to_slice(&mut payload[0..4]);
487
488 #[allow(unused_variables)]
489 let result = self.device.set(u8::from(MotionDetectorV2BrickletFunction::WriteUid), &payload).await?;
490 Ok(())
491 }
492
493 pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
496 let payload = [0; 0];
497
498 #[allow(unused_variables)]
499 let result = self.device.get(u8::from(MotionDetectorV2BrickletFunction::ReadUid), &payload).await?;
500 Ok(u32::from_le_byte_slice(result.body()))
501 }
502
503 pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
514 let payload = [0; 0];
515
516 #[allow(unused_variables)]
517 let result = self.device.get(u8::from(MotionDetectorV2BrickletFunction::GetIdentity), &payload).await?;
518 Ok(Identity::from_le_byte_slice(result.body()))
519 }
520}