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
impl AnalogInput
Sourcepub fn new<T: Into<PinIdOrName>>(
board: &dyn Hardware,
analog_pin: T,
) -> Result<Self, Error>
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.
Sourcepub fn attach(&self)
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.
Sourcepub fn detach(&self)
pub fn detach(&self)
Detaches the interval associated with the AnalogInput. This means the AnalogInput won’t react anymore to value changes.
Sourcepub fn on<S, F, T, Fut>(&self, event: S, callback: F) -> EventHandler
pub fn on<S, F, T, Fut>(&self, event: S, callback: F) -> EventHandler
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
impl Clone for AnalogInput
Source§fn clone(&self) -> AnalogInput
fn clone(&self) -> AnalogInput
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for AnalogInput
impl Debug for AnalogInput
Source§impl Display for AnalogInput
impl Display for AnalogInput
Source§impl Input for AnalogInput
impl Input for AnalogInput
impl Device for AnalogInput
Auto Trait Implementations§
impl Freeze for AnalogInput
impl !RefUnwindSafe for AnalogInput
impl Send for AnalogInput
impl Sync for AnalogInput
impl Unpin for AnalogInput
impl !UnwindSafe for AnalogInput
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more