1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::sensors::Sensor;
use crate::{Attribute, Device, Driver, Ev3Result, Findable};

/// Continuous measurement - sets LEDs on, steady. Units in centimeters. Distance (0-2550)
pub const MODE_US_DIST_CM: &str = "US-DIST-CM";

/// Continuous measurement - sets LEDs on, steady. Units in inches. Distance (0-1003)
pub const MODE_US_DIST_IN: &str = "US-DIST-IN";

/// Listen - sets LEDs on, blinking. Presence (0-1)
pub const MODE_US_LISTEN: &str = "US-LISTEN";

/// Single measurement - LEDs on momentarily when mode is set, then off. Units in centimeters. Distance (0-2550)
pub const MODE_US_SI_CM: &str = "US-SI-CM";

/// Single measurement - LEDs on momentarily when mode is set, then off. Units in inches. Distance (0-1003)
pub const MODE_US_SI_IN: &str = "US-SI-IN";

/// ??? - sets LEDs on, steady . Units in centimeters. Distance (0-2550)
pub const MODE_US_DC_CM: &str = "US-DC-CM";

/// ??? - sets LEDs on, steady . Units in inches. Distance (0-1003)
pub const MODE_US_DC_IN: &str = "US-DC-IN";

/// LEGO EV3 ultrasonic sensor.
#[derive(Debug, Clone, Device, Sensor, Findable)]
#[class_name = "lego-sensor"]
#[driver_name = "lego-ev3-us"]
#[port = "crate::sensors::SensorPort"]
pub struct UltrasonicSensor {
    driver: Driver,
}

impl UltrasonicSensor {
    /// Continuous measurement - sets LEDs on, steady. Units in centimeters. Distance (0-2550)
    pub fn set_mode_us_dist_cm(&self) -> Ev3Result<()> {
        self.set_mode(MODE_US_DIST_CM)
    }

    /// Continuous measurement - sets LEDs on, steady. Units in inches. Distance (0-1003)
    pub fn set_mode_us_dist_in(&self) -> Ev3Result<()> {
        self.set_mode(MODE_US_DIST_IN)
    }

    /// Listen - sets LEDs on, blinking. Presence (0-1)
    pub fn set_mode_us_listen(&self) -> Ev3Result<()> {
        self.set_mode(MODE_US_LISTEN)
    }

    /// Single measurement - LEDs on momentarily when mode is set, then off. Units in centimeters. Distance (0-2550)
    pub fn set_mode_us_si_cm(&self) -> Ev3Result<()> {
        self.set_mode(MODE_US_SI_CM)
    }

    /// Single measurement - LEDs on momentarily when mode is set, then off. Units in inches. Distance (0-1003)
    pub fn set_mode_us_si_in(&self) -> Ev3Result<()> {
        self.set_mode(MODE_US_SI_IN)
    }

    /// ??? - sets LEDs on, steady . Units in centimeters. Distance (0-2550)
    pub fn set_mode_us_dc_cm(&self) -> Ev3Result<()> {
        self.set_mode(MODE_US_DC_CM)
    }

    /// ??? - sets LEDs on, steady . Units in inches. Distance (0-1003)
    pub fn set_mode_us_dc_in(&self) -> Ev3Result<()> {
        self.set_mode(MODE_US_DC_IN)
    }

    /// Measurement of the distance detected by the sensor
    pub fn get_distance(&self) -> Ev3Result<i32> {
        self.get_value0()
    }
}