AnalogInput

Struct AnalogInput 

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

Represents an analog sensor of unspecified type: an Input Device that reads analog values from an ANALOG compatible pin. https://docs.arduino.cc/built-in-examples/analog/AnalogInput

Implementations§

Source§

impl AnalogInput

Source

pub fn new<T: Into<PinIdOrName>>( board: &dyn Hardware, analog_pin: T, ) -> Result<Self, Error>

Creates an instance of an AnalogInput attached to a given board: https://docs.arduino.cc/built-in-examples/analog/AnalogInput/

§Errors
  • UnknownPin: this function will bail an error if the AnalogInput pin does not exist for this board.
  • IncompatiblePin: this function will bail an error if the AnalogInput pin does not support ANALOG mode.
Source

pub fn get_pin(&self) -> u8

Returns the pin (id) used by the device.

Source

pub fn attach(&self)

Manually attaches the AnalogInput with the value change events. This should never be needed unless you manually detach() the AnalogInput first for some reason and want it to start being reactive to events again.

Source

pub fn detach(&self)

Detaches the interval associated with the AnalogInput. This means the AnalogInput won’t react anymore to value changes.

Source

pub fn on<S, F, T, Fut>(&self, event: S, callback: F) -> EventHandler
where S: Into<String>, T: 'static + Send + Sync + Clone, F: FnMut(T) -> Fut + Send + 'static, Fut: Future<Output = Result<(), Error>> + Send + 'static,

Registers a callback to be executed on a given event.

Available events for an analog input are:

  • InputEvent::OnChange | change: Triggered when the AnalogInput value changes.
    The callback must receive the following parameter: |value: u16| { ... }
§Example
use hermes_five::hardware::{Board, BoardEvent};
use hermes_five::devices::{AnalogInput, InputEvent};

#[hermes_five::runtime]
async fn main() {
    let board = Board::run();
    board.on(BoardEvent::OnReady, |board: Board| async move {

        // Register a Sensor on pin 14 (A0).
        let potentiometer = AnalogInput::new(&board, "A0")?;
        // Triggered function when the sensor state changes.
        potentiometer.on(InputEvent::OnChange, |value: u16| async move {
            println!("Sensor value changed: {}", value);
            Ok(())
        });

        // The above code will run forever.
        // <do something useful>

        // The above code will run forever runs a listener on the pin state under-the-hood.
        // It means the program will run forever listening to the InputEvent,
        // until we detach the device and close the board.
        potentiometer.detach();
        board.close();

        Ok(())
    });
}

Trait Implementations§

Source§

impl Clone for AnalogInput

Source§

fn clone(&self) -> AnalogInput

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 AnalogInput

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Display for AnalogInput

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Input for AnalogInput

Source§

fn get_state(&self) -> State

Returns the sensor current state.
Source§

impl Device for AnalogInput

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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.