1use crate::{
15 byte_converter::*, converting_callback_receiver::ConvertingCallbackReceiver, converting_receiver::ConvertingReceiver, device::*,
16 ip_connection::GetRequestSender,
17};
18pub enum LaserRangeFinderBrickletFunction {
19 GetDistance,
20 GetVelocity,
21 SetDistanceCallbackPeriod,
22 GetDistanceCallbackPeriod,
23 SetVelocityCallbackPeriod,
24 GetVelocityCallbackPeriod,
25 SetDistanceCallbackThreshold,
26 GetDistanceCallbackThreshold,
27 SetVelocityCallbackThreshold,
28 GetVelocityCallbackThreshold,
29 SetDebouncePeriod,
30 GetDebouncePeriod,
31 SetMovingAverage,
32 GetMovingAverage,
33 SetMode,
34 GetMode,
35 EnableLaser,
36 DisableLaser,
37 IsLaserEnabled,
38 GetSensorHardwareVersion,
39 SetConfiguration,
40 GetConfiguration,
41 GetIdentity,
42 CallbackDistance,
43 CallbackVelocity,
44 CallbackDistanceReached,
45 CallbackVelocityReached,
46}
47impl From<LaserRangeFinderBrickletFunction> for u8 {
48 fn from(fun: LaserRangeFinderBrickletFunction) -> Self {
49 match fun {
50 LaserRangeFinderBrickletFunction::GetDistance => 1,
51 LaserRangeFinderBrickletFunction::GetVelocity => 2,
52 LaserRangeFinderBrickletFunction::SetDistanceCallbackPeriod => 3,
53 LaserRangeFinderBrickletFunction::GetDistanceCallbackPeriod => 4,
54 LaserRangeFinderBrickletFunction::SetVelocityCallbackPeriod => 5,
55 LaserRangeFinderBrickletFunction::GetVelocityCallbackPeriod => 6,
56 LaserRangeFinderBrickletFunction::SetDistanceCallbackThreshold => 7,
57 LaserRangeFinderBrickletFunction::GetDistanceCallbackThreshold => 8,
58 LaserRangeFinderBrickletFunction::SetVelocityCallbackThreshold => 9,
59 LaserRangeFinderBrickletFunction::GetVelocityCallbackThreshold => 10,
60 LaserRangeFinderBrickletFunction::SetDebouncePeriod => 11,
61 LaserRangeFinderBrickletFunction::GetDebouncePeriod => 12,
62 LaserRangeFinderBrickletFunction::SetMovingAverage => 13,
63 LaserRangeFinderBrickletFunction::GetMovingAverage => 14,
64 LaserRangeFinderBrickletFunction::SetMode => 15,
65 LaserRangeFinderBrickletFunction::GetMode => 16,
66 LaserRangeFinderBrickletFunction::EnableLaser => 17,
67 LaserRangeFinderBrickletFunction::DisableLaser => 18,
68 LaserRangeFinderBrickletFunction::IsLaserEnabled => 19,
69 LaserRangeFinderBrickletFunction::GetSensorHardwareVersion => 24,
70 LaserRangeFinderBrickletFunction::SetConfiguration => 25,
71 LaserRangeFinderBrickletFunction::GetConfiguration => 26,
72 LaserRangeFinderBrickletFunction::GetIdentity => 255,
73 LaserRangeFinderBrickletFunction::CallbackDistance => 20,
74 LaserRangeFinderBrickletFunction::CallbackVelocity => 21,
75 LaserRangeFinderBrickletFunction::CallbackDistanceReached => 22,
76 LaserRangeFinderBrickletFunction::CallbackVelocityReached => 23,
77 }
78 }
79}
80pub const LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_OFF: char = 'x';
81pub const LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_OUTSIDE: char = 'o';
82pub const LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_INSIDE: char = 'i';
83pub const LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_SMALLER: char = '<';
84pub const LASER_RANGE_FINDER_BRICKLET_THRESHOLD_OPTION_GREATER: char = '>';
85pub const LASER_RANGE_FINDER_BRICKLET_MODE_DISTANCE: u8 = 0;
86pub const LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_13MS: u8 = 1;
87pub const LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_32MS: u8 = 2;
88pub const LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_64MS: u8 = 3;
89pub const LASER_RANGE_FINDER_BRICKLET_MODE_VELOCITY_MAX_127MS: u8 = 4;
90pub const LASER_RANGE_FINDER_BRICKLET_VERSION_1: u8 = 1;
91pub const LASER_RANGE_FINDER_BRICKLET_VERSION_3: u8 = 3;
92
93#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
94pub struct DistanceCallbackThreshold {
95 pub option: char,
96 pub min: u16,
97 pub max: u16,
98}
99impl FromByteSlice for DistanceCallbackThreshold {
100 fn bytes_expected() -> usize { 5 }
101 fn from_le_byte_slice(bytes: &[u8]) -> DistanceCallbackThreshold {
102 DistanceCallbackThreshold {
103 option: <char>::from_le_byte_slice(&bytes[0..1]),
104 min: <u16>::from_le_byte_slice(&bytes[1..3]),
105 max: <u16>::from_le_byte_slice(&bytes[3..5]),
106 }
107 }
108}
109
110#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
111pub struct VelocityCallbackThreshold {
112 pub option: char,
113 pub min: i16,
114 pub max: i16,
115}
116impl FromByteSlice for VelocityCallbackThreshold {
117 fn bytes_expected() -> usize { 5 }
118 fn from_le_byte_slice(bytes: &[u8]) -> VelocityCallbackThreshold {
119 VelocityCallbackThreshold {
120 option: <char>::from_le_byte_slice(&bytes[0..1]),
121 min: <i16>::from_le_byte_slice(&bytes[1..3]),
122 max: <i16>::from_le_byte_slice(&bytes[3..5]),
123 }
124 }
125}
126
127#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
128pub struct MovingAverage {
129 pub distance_average_length: u8,
130 pub velocity_average_length: u8,
131}
132impl FromByteSlice for MovingAverage {
133 fn bytes_expected() -> usize { 2 }
134 fn from_le_byte_slice(bytes: &[u8]) -> MovingAverage {
135 MovingAverage {
136 distance_average_length: <u8>::from_le_byte_slice(&bytes[0..1]),
137 velocity_average_length: <u8>::from_le_byte_slice(&bytes[1..2]),
138 }
139 }
140}
141
142#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
143pub struct Configuration {
144 pub acquisition_count: u8,
145 pub enable_quick_termination: bool,
146 pub threshold_value: u8,
147 pub measurement_frequency: u16,
148}
149impl FromByteSlice for Configuration {
150 fn bytes_expected() -> usize { 5 }
151 fn from_le_byte_slice(bytes: &[u8]) -> Configuration {
152 Configuration {
153 acquisition_count: <u8>::from_le_byte_slice(&bytes[0..1]),
154 enable_quick_termination: <bool>::from_le_byte_slice(&bytes[1..2]),
155 threshold_value: <u8>::from_le_byte_slice(&bytes[2..3]),
156 measurement_frequency: <u16>::from_le_byte_slice(&bytes[3..5]),
157 }
158 }
159}
160
161#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
162pub struct Identity {
163 pub uid: String,
164 pub connected_uid: String,
165 pub position: char,
166 pub hardware_version: [u8; 3],
167 pub firmware_version: [u8; 3],
168 pub device_identifier: u16,
169}
170impl FromByteSlice for Identity {
171 fn bytes_expected() -> usize { 25 }
172 fn from_le_byte_slice(bytes: &[u8]) -> Identity {
173 Identity {
174 uid: <String>::from_le_byte_slice(&bytes[0..8]),
175 connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
176 position: <char>::from_le_byte_slice(&bytes[16..17]),
177 hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
178 firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
179 device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
180 }
181 }
182}
183
184#[derive(Clone)]
186pub struct LaserRangeFinderBricklet {
187 device: Device,
188}
189impl LaserRangeFinderBricklet {
190 pub const DEVICE_IDENTIFIER: u16 = 255;
191 pub const DEVICE_DISPLAY_NAME: &'static str = "Laser Range Finder Bricklet";
192 pub fn new<T: GetRequestSender>(uid: &str, req_sender: T) -> LaserRangeFinderBricklet {
194 let mut result = LaserRangeFinderBricklet { device: Device::new([2, 0, 1], uid, req_sender, 0) };
195 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetDistance) as usize] =
196 ResponseExpectedFlag::AlwaysTrue;
197 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetVelocity) as usize] =
198 ResponseExpectedFlag::AlwaysTrue;
199 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetDistanceCallbackPeriod) as usize] =
200 ResponseExpectedFlag::True;
201 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetDistanceCallbackPeriod) as usize] =
202 ResponseExpectedFlag::AlwaysTrue;
203 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetVelocityCallbackPeriod) as usize] =
204 ResponseExpectedFlag::True;
205 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetVelocityCallbackPeriod) as usize] =
206 ResponseExpectedFlag::AlwaysTrue;
207 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetDistanceCallbackThreshold) as usize] =
208 ResponseExpectedFlag::True;
209 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetDistanceCallbackThreshold) as usize] =
210 ResponseExpectedFlag::AlwaysTrue;
211 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetVelocityCallbackThreshold) as usize] =
212 ResponseExpectedFlag::True;
213 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetVelocityCallbackThreshold) as usize] =
214 ResponseExpectedFlag::AlwaysTrue;
215 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetDebouncePeriod) as usize] =
216 ResponseExpectedFlag::True;
217 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetDebouncePeriod) as usize] =
218 ResponseExpectedFlag::AlwaysTrue;
219 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetMovingAverage) as usize] =
220 ResponseExpectedFlag::False;
221 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetMovingAverage) as usize] =
222 ResponseExpectedFlag::AlwaysTrue;
223 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetMode) as usize] = ResponseExpectedFlag::False;
224 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
225 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::EnableLaser) as usize] = ResponseExpectedFlag::False;
226 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::DisableLaser) as usize] = ResponseExpectedFlag::False;
227 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::IsLaserEnabled) as usize] =
228 ResponseExpectedFlag::AlwaysTrue;
229 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetSensorHardwareVersion) as usize] =
230 ResponseExpectedFlag::AlwaysTrue;
231 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::SetConfiguration) as usize] =
232 ResponseExpectedFlag::False;
233 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetConfiguration) as usize] =
234 ResponseExpectedFlag::AlwaysTrue;
235 result.device.response_expected[u8::from(LaserRangeFinderBrickletFunction::GetIdentity) as usize] =
236 ResponseExpectedFlag::AlwaysTrue;
237 result
238 }
239
240 pub fn get_response_expected(&mut self, fun: LaserRangeFinderBrickletFunction) -> Result<bool, GetResponseExpectedError> {
255 self.device.get_response_expected(u8::from(fun))
256 }
257
258 pub fn set_response_expected(
267 &mut self,
268 fun: LaserRangeFinderBrickletFunction,
269 response_expected: bool,
270 ) -> Result<(), SetResponseExpectedError> {
271 self.device.set_response_expected(u8::from(fun), response_expected)
272 }
273
274 pub fn set_response_expected_all(&mut self, response_expected: bool) { self.device.set_response_expected_all(response_expected) }
276
277 pub fn get_api_version(&self) -> [u8; 3] { self.device.api_version }
280
281 pub fn get_distance_callback_receiver(&self) -> ConvertingCallbackReceiver<u16> {
291 self.device.get_callback_receiver(u8::from(LaserRangeFinderBrickletFunction::CallbackDistance))
292 }
293
294 pub fn get_velocity_callback_receiver(&self) -> ConvertingCallbackReceiver<i16> {
301 self.device.get_callback_receiver(u8::from(LaserRangeFinderBrickletFunction::CallbackVelocity))
302 }
303
304 pub fn get_distance_reached_callback_receiver(&self) -> ConvertingCallbackReceiver<u16> {
311 self.device.get_callback_receiver(u8::from(LaserRangeFinderBrickletFunction::CallbackDistanceReached))
312 }
313
314 pub fn get_velocity_reached_callback_receiver(&self) -> ConvertingCallbackReceiver<i16> {
321 self.device.get_callback_receiver(u8::from(LaserRangeFinderBrickletFunction::CallbackVelocityReached))
322 }
323
324 pub fn get_distance(&self) -> ConvertingReceiver<u16> {
336 let payload = vec![0; 0];
337
338 self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetDistance), payload)
339 }
340
341 pub fn get_velocity(&self) -> ConvertingReceiver<i16> {
355 let payload = vec![0; 0];
356
357 self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetVelocity), payload)
358 }
359
360 pub fn set_distance_callback_period(&self, period: u32) -> ConvertingReceiver<()> {
366 let mut payload = vec![0; 4];
367 payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
368
369 self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetDistanceCallbackPeriod), payload)
370 }
371
372 pub fn get_distance_callback_period(&self) -> ConvertingReceiver<u32> {
374 let payload = vec![0; 0];
375
376 self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetDistanceCallbackPeriod), payload)
377 }
378
379 pub fn set_velocity_callback_period(&self, period: u32) -> ConvertingReceiver<()> {
385 let mut payload = vec![0; 4];
386 payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(period));
387
388 self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetVelocityCallbackPeriod), payload)
389 }
390
391 pub fn get_velocity_callback_period(&self) -> ConvertingReceiver<u32> {
393 let payload = vec![0; 0];
394
395 self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetVelocityCallbackPeriod), payload)
396 }
397
398 pub fn set_distance_callback_threshold(&self, option: char, min: u16, max: u16) -> ConvertingReceiver<()> {
417 let mut payload = vec![0; 5];
418 payload[0..1].copy_from_slice(&<char>::to_le_byte_vec(option));
419 payload[1..3].copy_from_slice(&<u16>::to_le_byte_vec(min));
420 payload[3..5].copy_from_slice(&<u16>::to_le_byte_vec(max));
421
422 self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetDistanceCallbackThreshold), payload)
423 }
424
425 pub fn get_distance_callback_threshold(&self) -> ConvertingReceiver<DistanceCallbackThreshold> {
434 let payload = vec![0; 0];
435
436 self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetDistanceCallbackThreshold), payload)
437 }
438
439 pub fn set_velocity_callback_threshold(&self, option: char, min: i16, max: i16) -> ConvertingReceiver<()> {
458 let mut payload = vec![0; 5];
459 payload[0..1].copy_from_slice(&<char>::to_le_byte_vec(option));
460 payload[1..3].copy_from_slice(&<i16>::to_le_byte_vec(min));
461 payload[3..5].copy_from_slice(&<i16>::to_le_byte_vec(max));
462
463 self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetVelocityCallbackThreshold), payload)
464 }
465
466 pub fn get_velocity_callback_threshold(&self) -> ConvertingReceiver<VelocityCallbackThreshold> {
475 let payload = vec![0; 0];
476
477 self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetVelocityCallbackThreshold), payload)
478 }
479
480 pub fn set_debounce_period(&self, debounce: u32) -> ConvertingReceiver<()> {
492 let mut payload = vec![0; 4];
493 payload[0..4].copy_from_slice(&<u32>::to_le_byte_vec(debounce));
494
495 self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetDebouncePeriod), payload)
496 }
497
498 pub fn get_debounce_period(&self) -> ConvertingReceiver<u32> {
500 let payload = vec![0; 0];
501
502 self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetDebouncePeriod), payload)
503 }
504
505 pub fn set_moving_average(&self, distance_average_length: u8, velocity_average_length: u8) -> ConvertingReceiver<()> {
511 let mut payload = vec![0; 2];
512 payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(distance_average_length));
513 payload[1..2].copy_from_slice(&<u8>::to_le_byte_vec(velocity_average_length));
514
515 self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetMovingAverage), payload)
516 }
517
518 pub fn get_moving_average(&self) -> ConvertingReceiver<MovingAverage> {
520 let payload = vec![0; 0];
521
522 self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetMovingAverage), payload)
523 }
524
525 pub fn set_mode(&self, mode: u8) -> ConvertingReceiver<()> {
549 let mut payload = vec![0; 1];
550 payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(mode));
551
552 self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetMode), payload)
553 }
554
555 pub fn get_mode(&self) -> ConvertingReceiver<u8> {
564 let payload = vec![0; 0];
565
566 self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetMode), payload)
567 }
568
569 pub fn enable_laser(&self) -> ConvertingReceiver<()> {
574 let payload = vec![0; 0];
575
576 self.device.set(u8::from(LaserRangeFinderBrickletFunction::EnableLaser), payload)
577 }
578
579 pub fn disable_laser(&self) -> ConvertingReceiver<()> {
581 let payload = vec![0; 0];
582
583 self.device.set(u8::from(LaserRangeFinderBrickletFunction::DisableLaser), payload)
584 }
585
586 pub fn is_laser_enabled(&self) -> ConvertingReceiver<bool> {
588 let payload = vec![0; 0];
589
590 self.device.get(u8::from(LaserRangeFinderBrickletFunction::IsLaserEnabled), payload)
591 }
592
593 pub fn get_sensor_hardware_version(&self) -> ConvertingReceiver<u8> {
602 let payload = vec![0; 0];
603
604 self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetSensorHardwareVersion), payload)
605 }
606
607 pub fn set_configuration(
640 &self,
641 acquisition_count: u8,
642 enable_quick_termination: bool,
643 threshold_value: u8,
644 measurement_frequency: u16,
645 ) -> ConvertingReceiver<()> {
646 let mut payload = vec![0; 5];
647 payload[0..1].copy_from_slice(&<u8>::to_le_byte_vec(acquisition_count));
648 payload[1..2].copy_from_slice(&<bool>::to_le_byte_vec(enable_quick_termination));
649 payload[2..3].copy_from_slice(&<u8>::to_le_byte_vec(threshold_value));
650 payload[3..5].copy_from_slice(&<u16>::to_le_byte_vec(measurement_frequency));
651
652 self.device.set(u8::from(LaserRangeFinderBrickletFunction::SetConfiguration), payload)
653 }
654
655 pub fn get_configuration(&self) -> ConvertingReceiver<Configuration> {
660 let payload = vec![0; 0];
661
662 self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetConfiguration), payload)
663 }
664
665 pub fn get_identity(&self) -> ConvertingReceiver<Identity> {
676 let payload = vec![0; 0];
677
678 self.device.get(u8::from(LaserRangeFinderBrickletFunction::GetIdentity), payload)
679 }
680}