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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use driver::Attribute;
use driver::AttributeResult;

pub trait Device {
    fn get_attribute(&mut self, name: &str) -> &Attribute;


    /// Returns the name of the port that the motor is connected to.
    fn get_address(&mut self) -> AttributeResult<String> {
        self.get_attribute("address").get_str()
    }

    /// Sends a command to the device controller.
    fn set_command(&mut self, command: String) -> AttributeResult<()> {
        self.get_attribute("command").set_str(command)
    }

    /// Returns a space separated list of commands that are supported by the device controller.
    fn get_commands(&mut self) -> AttributeResult<Vec<String>> {
        self.get_attribute("commands").get_vec()
    }

    /// Returns the name of the driver that provides this device.
    fn get_driver_name(&mut self) -> AttributeResult<String> {
        self.get_attribute("driver_name").get_str()
    }
}

pub trait Motor: Device {}

pub trait Sensor: Device {

    /// Reading the file will give the unscaled raw values in the `value<N>` attributes.
    /// Use `bin_data_format`, `num_values` and the individual sensor documentation to determine how to interpret the data.
    fn get_bin_data(&mut self) -> AttributeResult<String> {
        self.get_attribute("bin_data").get_str()
    }

    /// Returns the format of the values in `bin_data` for the current mode. Possible values are:
    // * u8: Unsigned 8-bit integer (byte)
    // * s8: Signed 8-bit integer (sbyte)
    // * u16: Unsigned 16-bit integer (ushort)
    // * s16: Signed 16-bit integer (short)
    // * s16_be: Signed 16-bit integer, big endian
    // * s32: Signed 32-bit integer (int)
    // * s32_be: Signed 32-bit integer, big endian
    // * float: IEEE 754 32-bit floating point (float)
    fn get_bin_data_format(&mut self) -> AttributeResult<String> {
        self.get_attribute("bin_data_format").get_str()
    }

    /// Returns the number of decimal places for the values in the `value<N>` attributes of the current mode.
    fn get_decimals(&mut self) -> AttributeResult<isize> {
        self.get_attribute("decimals").get_int()
    }

    /// Returns the firmware version of the sensor if available.
    /// Currently only NXT/I2C sensors support this.
    fn get_fw_version(&mut self) -> AttributeResult<String> {
        self.get_attribute("fw_version").get_str()
    }

    /// Returns the current mode.
    /// See the individual sensor documentation for a description of the modes available for each type of sensor.
    fn get_mode(&mut self) -> AttributeResult<String> {
        self.get_attribute("mode").get_str()
    }

    /// Sets the sensor to that mode.
    /// See the individual sensor documentation for a description of the modes available for each type of sensor.
    fn set_mode(&mut self, mode: String) -> AttributeResult<()> {
        self.get_attribute("mode").set_str(mode)
    }

    /// Returns a list of the valid modes for the sensor.
    fn get_modes(&mut self) -> AttributeResult<Vec<String>> {
        self.get_attribute("modes").get_vec()
    }

    /// Returns the number of `value<N>` attributes that will return a valid value for the current mode.
    fn get_num_values(&mut self) -> AttributeResult<isize> {
        self.get_attribute("num_values").get_int()
    }

    /// Returns the polling period of the sensor in milliseconds.
    /// Returns `-EOPNOTSUPP` if changing polling is not supported.
    /// Note: Setting poll_ms too high can cause the input port autodetection to fail.
    /// If this happens, use the mode attribute of the port to force the port to `nxt-i2c mode`. Values must not be negative.
    fn get_poll_ms(&mut self) -> AttributeResult<isize> {
        self.get_attribute("poll_ms").get_int()
    }

    /// Sets the polling period of the sensor in milliseconds.
    /// Setting to 0 disables polling.
    /// Note: Setting poll_ms too high can cause the input port autodetection to fail.
    /// If this happens, use the mode attribute of the port to force the port to `nxt-i2c mode`. Values must not be negative.
    fn set_poll_ms(&mut self, poll_ms: isize) -> AttributeResult<()> {
        self.get_attribute("poll_ms").set_int(poll_ms)
    }

    /// Returns the units of the measured value for the current mode. May return empty string if units are unknown.
    fn get_units(&mut self) -> AttributeResult<String> {
        self.get_attribute("units").get_str()
    }

    fn get_value0(&mut self) -> AttributeResult<isize> {
        self.get_attribute("value0").get_int()
    }
    fn get_value1(&mut self) -> AttributeResult<isize> {
        self.get_attribute("value1").get_int()
    }
    fn get_value2(&mut self) -> AttributeResult<isize> {
        self.get_attribute("value2").get_int()
    }
    fn get_value3(&mut self) -> AttributeResult<isize> {
        self.get_attribute("value3").get_int()
    }
    fn get_value4(&mut self) -> AttributeResult<isize> {
        self.get_attribute("value4").get_int()
    }
    fn get_value5(&mut self) -> AttributeResult<isize> {
        self.get_attribute("value5").get_int()
    }
    fn get_value6(&mut self) -> AttributeResult<isize> {
        self.get_attribute("value6").get_int()
    }
    fn get_value7(&mut self) -> AttributeResult<isize> {
        self.get_attribute("value7").get_int()
    }

    /// Returns a space delimited string representing sensor-specific text values. Returns `-EOPNOTSUPP` if a sensor does not support text values.
    fn get_text_value(&mut self) -> AttributeResult<String> {
        self.get_attribute("text_value").get_str()
    }
}

pub trait Port {
    fn address(&self) -> String;
}

pub enum MotorPort {
    OutA,
    OutB,
    OutC,
    OutD,
}

impl Port for MotorPort {
    fn address(&self) -> String {
        match self {
            MotorPort::OutA => String::from("outA"),
            MotorPort::OutB => String::from("outB"),
            MotorPort::OutC => String::from("outC"),
            MotorPort::OutD => String::from("outD"),
        }
    }
}

pub enum SensorPort {
    In1,
    In2,
    In3,
    In4,
}

impl Port for SensorPort {
    fn address(&self) -> String {
        match self {
            SensorPort::In1 => String::from("in1"),
            SensorPort::In2 => String::from("in2"),
            SensorPort::In3 => String::from("in3"),
            SensorPort::In4 => String::from("in4"),
        }
    }
}