Struct ev3dev_lang_rust::Attribute
source · pub struct Attribute { /* private fields */ }Expand description
A wrapper to a attribute file in the /sys/class/ directory.
Implementations§
source§impl Attribute
impl Attribute
sourcepub fn from_path(path: &Path) -> Ev3Result<Attribute>
pub fn from_path(path: &Path) -> Ev3Result<Attribute>
Create a new Attribute instance for the given path.
sourcepub fn from_sys_class(
class_name: &str,
name: &str,
attribute_name: &str
) -> Ev3Result<Attribute>
pub fn from_sys_class( class_name: &str, name: &str, attribute_name: &str ) -> Ev3Result<Attribute>
Create a new Attribute instance that wrap’s
the file /sys/class/{class_name}/{name}{attribute_name}.
sourcepub fn from_path_with_discriminator(
driver_path: &str,
attribute_path: &str,
discriminator_path: &str,
discriminator_value: &str
) -> Ev3Result<Attribute>
pub fn from_path_with_discriminator( driver_path: &str, attribute_path: &str, discriminator_path: &str, discriminator_value: &str ) -> Ev3Result<Attribute>
Create a new Attribute instance by a discriminator attribute.
This can be used to manually access driver files or advances features like raw encoder values.
To find the correct file, this function iterates over all directories $d in driver_path and
checks if the content of driver_path/$d/discriminator_path equals discriminator_value. When a
match is found it returns an Attribute for file driver_path/$d/attribute_path.
Example
use ev3dev_lang_rust::Attribute;
// Get value0 of first connected color sensor.
let color_sensor_value = Attribute::from_path_with_discriminator(
"/sys/class/lego-sensor",
"value0",
"driver_name",
"lego-ev3-color"
)?;
println!("value0 of color sensor: {}", color_sensor_value.get::<i32>()?);
// Get raw rotation count of motor in port `A`.
// See https://github.com/ev3dev/ev3dev/wiki/Internals:-ev3dev-stretch for more information.
let rotation_count = Attribute::from_path_with_discriminator(
"/sys/bus/iio/devices",
"in_count0_raw",
"name",
"ev3-tacho"
)?;
println!("Raw rotation count: {}", rotation_count.get::<i32>()?);
Examples found in repository?
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
fn main() -> Ev3Result<()> {
// Get value0 of first connected color sensor.
let color_sensor_value = Attribute::from_path_with_discriminator(
"/sys/class/lego-sensor",
"value0",
"driver_name",
"lego-ev3-color",
)?;
// Get raw rotation count of motor in port `A`.
// See https://github.com/ev3dev/ev3dev/wiki/Internals:-ev3dev-stretch for more infomation.
let rotation_count = Attribute::from_path_with_discriminator(
"/sys/bus/iio/devices",
"in_count0_raw",
"name",
"ev3-tacho",
)?;
loop {
println!(
"value0 of color sensor: {}",
color_sensor_value.get::<i32>()?
);
println!("Raw rotation count: {}", rotation_count.get::<i32>()?);
std::thread::sleep(std::time::Duration::from_secs(1));
}
}sourcepub fn get<T>(&self) -> Ev3Result<T>where
T: FromStr,
<T as FromStr>::Err: Error,
pub fn get<T>(&self) -> Ev3Result<T>where T: FromStr, <T as FromStr>::Err: Error,
Returns the current value of the wrapped file.
The value is parsed to the type T.
Returns a Ev3Result::InternalError if the current value is not parsable to type T.
Examples found in repository?
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
fn main() -> Ev3Result<()> {
// Get value0 of first connected color sensor.
let color_sensor_value = Attribute::from_path_with_discriminator(
"/sys/class/lego-sensor",
"value0",
"driver_name",
"lego-ev3-color",
)?;
// Get raw rotation count of motor in port `A`.
// See https://github.com/ev3dev/ev3dev/wiki/Internals:-ev3dev-stretch for more infomation.
let rotation_count = Attribute::from_path_with_discriminator(
"/sys/bus/iio/devices",
"in_count0_raw",
"name",
"ev3-tacho",
)?;
loop {
println!(
"value0 of color sensor: {}",
color_sensor_value.get::<i32>()?
);
println!("Raw rotation count: {}", rotation_count.get::<i32>()?);
std::thread::sleep(std::time::Duration::from_secs(1));
}
}sourcepub fn set<T>(&self, value: T) -> Ev3Result<()>where
T: ToString,
pub fn set<T>(&self, value: T) -> Ev3Result<()>where T: ToString,
Sets the value of the wrapped file.
The value is parsed from the type T.
Returns a Ev3Result::InternalError if the file is not writable.
sourcepub fn set_str_slice(&self, value: &str) -> Ev3Result<()>
pub fn set_str_slice(&self, value: &str) -> Ev3Result<()>
Sets the value of the wrapped file.
This function skips the string parsing of the self.set<T>() function.
Returns a Ev3Result::InternalError if the file is not writable.
sourcepub fn get_vec(&self) -> Ev3Result<Vec<String>>
pub fn get_vec(&self) -> Ev3Result<Vec<String>>
Returns a string vector representation of the wrapped file. The file value is splitted at whitespace’s.
sourcepub fn get_raw_fd(&self) -> RawFd
pub fn get_raw_fd(&self) -> RawFd
Returns a C pointer to the wrapped file.
sourcepub fn get_file_path(&self) -> PathBuf
pub fn get_file_path(&self) -> PathBuf
Returns the path to the wrapped file.