ev3dev_lang_rust/sensors/
mod.rs

1//! # Container module for sensor types
2
3mod sensor;
4pub use self::sensor::Sensor;
5
6mod color_sensor;
7pub use self::color_sensor::ColorSensor;
8
9mod hi_technic_color_sensor;
10pub use self::hi_technic_color_sensor::HiTechnicColorSensor;
11
12mod ir_seeker_sensor;
13pub use self::ir_seeker_sensor::IrSeekerSensor;
14
15mod compass_sensor;
16pub use self::compass_sensor::CompassSensor;
17
18mod light_sensor;
19pub use self::light_sensor::LightSensor;
20
21mod gyro_sensor;
22pub use self::gyro_sensor::GyroSensor;
23
24mod infrared_sensor;
25pub use self::infrared_sensor::BeaconSeeker;
26pub use self::infrared_sensor::InfraredSensor;
27pub use self::infrared_sensor::RemoteControl;
28
29mod touch_sensor;
30pub use self::touch_sensor::TouchSensor;
31
32mod ultrasonic_sensor;
33pub use self::ultrasonic_sensor::UltrasonicSensor;
34
35use crate::{port_constants, Port};
36
37/// EV3 ports `in1` to `in4`
38#[derive(Debug, Copy, Clone)]
39pub enum SensorPort {
40    /// EV3 `in1` port
41    In1,
42    /// EV3 `in2` port
43    In2,
44    /// EV3 `in3` port
45    In3,
46    /// EV3 `in4` port
47    In4,
48}
49
50impl SensorPort {
51    /// Try to format a device name path to a  port name.
52    pub fn format_name(name: &str) -> String {
53        match name {
54            "sensor0" => SensorPort::In1.address(),
55            "sensor1" => SensorPort::In2.address(),
56            "sensor2" => SensorPort::In3.address(),
57            "sensor3" => SensorPort::In4.address(),
58            _ => name.to_owned(),
59        }
60    }
61}
62
63impl Port for SensorPort {
64    fn address(&self) -> String {
65        match self {
66            SensorPort::In1 => port_constants::INPUT_1.to_owned(),
67            SensorPort::In2 => port_constants::INPUT_2.to_owned(),
68            SensorPort::In3 => port_constants::INPUT_3.to_owned(),
69            SensorPort::In4 => port_constants::INPUT_4.to_owned(),
70        }
71    }
72}
73
74#[macro_export]
75/// Add a sensor mode constant with getter and setter
76macro_rules! sensor_mode {
77    ($value:expr, $const_name:ident, $docstring:expr, $setter:ident, $getter:ident) => {
78        #[doc = $docstring]
79        pub const $const_name: &'static str = $value;
80
81        #[doc = $docstring]
82        pub fn $setter(&self) -> Ev3Result<()> {
83            self.set_mode(Self::$const_name)
84        }
85
86        #[doc = $docstring]
87        pub fn $getter(&self) -> Ev3Result<bool> {
88            Ok(self.get_mode()? == Self::$const_name)
89        }
90    };
91}