Struct Attribute

Source
pub struct Attribute { /* private fields */ }
Expand description

A wrapper to a attribute file in the /sys/class/ directory.

Implementations§

Source§

impl Attribute

Source

pub fn from_path(path: &Path) -> Ev3Result<Attribute>

Create a new Attribute instance for the given path.

Source

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}.

Source

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?
examples/custom-attributes.rs (lines 8-13)
6fn main() -> Ev3Result<()> {
7    // Get value0 of first connected color sensor.
8    let color_sensor_value = Attribute::from_path_with_discriminator(
9        "/sys/class/lego-sensor",
10        "value0",
11        "driver_name",
12        "lego-ev3-color",
13    )?;
14
15    // Get raw rotation count of motor in port `A`.
16    // See https://github.com/ev3dev/ev3dev/wiki/Internals:-ev3dev-stretch for more infomation.
17    let rotation_count = Attribute::from_path_with_discriminator(
18        "/sys/bus/iio/devices",
19        "in_count0_raw",
20        "name",
21        "ev3-tacho",
22    )?;
23
24    loop {
25        println!(
26            "value0 of color sensor: {}",
27            color_sensor_value.get::<i32>()?
28        );
29        println!("Raw rotation count: {}", rotation_count.get::<i32>()?);
30
31        std::thread::sleep(std::time::Duration::from_secs(1));
32    }
33}
Source

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?
examples/custom-attributes.rs (line 27)
6fn main() -> Ev3Result<()> {
7    // Get value0 of first connected color sensor.
8    let color_sensor_value = Attribute::from_path_with_discriminator(
9        "/sys/class/lego-sensor",
10        "value0",
11        "driver_name",
12        "lego-ev3-color",
13    )?;
14
15    // Get raw rotation count of motor in port `A`.
16    // See https://github.com/ev3dev/ev3dev/wiki/Internals:-ev3dev-stretch for more infomation.
17    let rotation_count = Attribute::from_path_with_discriminator(
18        "/sys/bus/iio/devices",
19        "in_count0_raw",
20        "name",
21        "ev3-tacho",
22    )?;
23
24    loop {
25        println!(
26            "value0 of color sensor: {}",
27            color_sensor_value.get::<i32>()?
28        );
29        println!("Raw rotation count: {}", rotation_count.get::<i32>()?);
30
31        std::thread::sleep(std::time::Duration::from_secs(1));
32    }
33}
Source

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.

Source

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.

Source

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.

Source

pub fn get_raw_fd(&self) -> RawFd

Returns a C pointer to the wrapped file.

Source

pub fn get_file_path(&self) -> PathBuf

Returns the path to the wrapped file.

Trait Implementations§

Source§

impl Clone for Attribute

Source§

fn clone(&self) -> Attribute

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Attribute

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.