ev3dev_lang_rust/sensors/
sensor.rs

1//! Common utility functions for sensors.
2
3use crate::{Device, Ev3Result};
4
5/// Common utility functions for sensors.
6pub trait Sensor: Device {
7    /// Reading the file will give the unscaled raw values in the `value<N>` attributes.
8    /// Use `bin_data_format`, `num_values` and the individual sensor documentation to determine how to interpret the data.
9    fn get_bin_data(&self) -> Ev3Result<String> {
10        self.get_attribute("bin_data").get()
11    }
12
13    /// Returns the format of the values in `bin_data` for the current mode. Possible values are:
14    // * u8: Unsigned 8-bit integer (byte)
15    // * s8: Signed 8-bit integer (sbyte)
16    // * u16: Unsigned 16-bit integer (ushort)
17    // * s16: Signed 16-bit integer (short)
18    // * s16_be: Signed 16-bit integer, big endian
19    // * s32: Signed 32-bit integer (int)
20    // * s32_be: Signed 32-bit integer, big endian
21    // * float: IEEE 754 32-bit floating point (float)
22    fn get_bin_data_format(&self) -> Ev3Result<String> {
23        self.get_attribute("bin_data_format").get()
24    }
25
26    /// Returns the number of decimal places for the values in the `value<N>` attributes of the current mode.
27    fn get_decimals(&self) -> Ev3Result<i32> {
28        self.get_attribute("decimals").get()
29    }
30
31    /// Returns the firmware version of the sensor if available.
32    /// Currently only NXT/I2C sensors support this.
33    fn get_fw_version(&self) -> Ev3Result<String> {
34        self.get_attribute("fw_version").get()
35    }
36
37    /// Returns the current mode.
38    /// See the individual sensor documentation for a description of the modes available for each type of sensor.
39    fn get_mode(&self) -> Ev3Result<String> {
40        self.get_attribute("mode").get()
41    }
42
43    /// Sets the sensor to that mode.
44    /// See the individual sensor documentation for a description of the modes available for each type of sensor.
45    fn set_mode(&self, mode: &str) -> Ev3Result<()> {
46        self.get_attribute("mode").set_str_slice(mode)
47    }
48
49    /// Returns a list of the valid modes for the sensor.
50    fn get_modes(&self) -> Ev3Result<Vec<String>> {
51        self.get_attribute("modes").get_vec()
52    }
53
54    /// Returns the number of `value<N>` attributes that will return a valid value for the current mode.
55    fn get_num_values(&self) -> Ev3Result<i32> {
56        self.get_attribute("num_values").get()
57    }
58
59    /// Returns the polling period of the sensor in milliseconds.
60    /// Returns `-EOPNOTSUPP` if changing polling is not supported.
61    /// Note: Setting poll_ms too high can cause the input port auto detection to fail.
62    /// If this happens, use the mode attribute of the port to force the port to `nxt-i2c mode`. Values must not be negative.
63    fn get_poll_ms(&self) -> Ev3Result<i32> {
64        self.get_attribute("poll_ms").get()
65    }
66
67    /// Sets the polling period of the sensor in milliseconds.
68    /// Setting to 0 disables polling.
69    /// Note: Setting poll_ms too high can cause the input port auto detection to fail.
70    /// If this happens, use the mode attribute of the port to force the port to `nxt-i2c mode`. Values must not be negative.
71    fn set_poll_ms(&self, poll_ms: i32) -> Ev3Result<()> {
72        self.get_attribute("poll_ms").set(poll_ms)
73    }
74
75    /// Returns the units of the measured value for the current mode. May return empty string if units are unknown.
76    fn get_units(&self) -> Ev3Result<String> {
77        self.get_attribute("units").get()
78    }
79
80    /// Returns the current `value{index}` value if available.
81    fn get_value(&self, index: u8) -> Ev3Result<i32> {
82        use crate::Ev3Error;
83        match index {
84            0 => self.get_value0(),
85            1 => self.get_value1(),
86            2 => self.get_value2(),
87            3 => self.get_value3(),
88            4 => self.get_value4(),
89            5 => self.get_value5(),
90            6 => self.get_value6(),
91            7 => self.get_value7(),
92            _ => Ev3Result::Err(Ev3Error::InternalError {
93                msg: format!("Sensor value index {index} is out of bounds [0, 7]"),
94            }),
95        }
96    }
97
98    /// Returns the current `value0` value if available.
99    fn get_value0(&self) -> Ev3Result<i32> {
100        self.get_attribute("value0").get()
101    }
102
103    /// Returns the current `value1` value if available.
104    fn get_value1(&self) -> Ev3Result<i32> {
105        self.get_attribute("value1").get()
106    }
107
108    /// Returns the current `value2` value if available.
109    fn get_value2(&self) -> Ev3Result<i32> {
110        self.get_attribute("value2").get()
111    }
112
113    /// Returns the current `value3` value if available.
114    fn get_value3(&self) -> Ev3Result<i32> {
115        self.get_attribute("value3").get()
116    }
117
118    /// Returns the current `value4` value if available.
119    fn get_value4(&self) -> Ev3Result<i32> {
120        self.get_attribute("value4").get()
121    }
122
123    /// Returns the current `value5` value if available.
124    fn get_value5(&self) -> Ev3Result<i32> {
125        self.get_attribute("value5").get()
126    }
127
128    /// Returns the current `value6` value if available.
129    fn get_value6(&self) -> Ev3Result<i32> {
130        self.get_attribute("value6").get()
131    }
132
133    /// Returns the current `value7` value if available.
134    fn get_value7(&self) -> Ev3Result<i32> {
135        self.get_attribute("value7").get()
136    }
137
138    /// Returns a space delimited string representing sensor-specific text values. Returns `-EOPNOTSUPP` if a sensor does not support text values.
139    fn get_text_value(&self) -> Ev3Result<String> {
140        self.get_attribute("text_value").get()
141    }
142}