pub struct DigitalInput { /* private fields */ }Expand description
Represents a digital sensor of unspecified type: an Input Device that reads digital values
from an INPUT compatible pin.
https://docs.arduino.cc/built-in-examples/digital/DigitalInput
Implementations§
Source§impl DigitalInput
impl DigitalInput
Sourcepub fn new<T: Into<PinIdOrName>>(
board: &dyn Hardware,
pin: T,
) -> Result<Self, Error>
pub fn new<T: Into<PinIdOrName>>( board: &dyn Hardware, pin: T, ) -> Result<Self, Error>
Creates an instance of a DigitalInput attached to a given board.
§Errors
UnknownPin: this function will bail an error if the DigitalInput pin does not exist for this board.IncompatiblePin: this function will bail an error if the DigitalInput pin does not support ANALOG mode.
Sourcepub fn attach(&self)
pub fn attach(&self)
Manually attaches the DigitalInput with the value change events.
This should never be needed unless you manually detach() the DigitalInput 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 DigitalInput. This means the DigitalInput 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 on the DigitalInput.
Available events for a button are:
InputEvent::OnChange|change: Triggered when the input value changes.
The callback must receive the following parameter:|value: bool| { ... }InputEvent::OnHigh|high: Triggered when the input value changes.
The callback must receive the void parameter:|_:()| { ... }InputEvent::OnLow|low: Triggered when the input value changes.
The callback must receive the void parameter:|_:()| { ... }
§Example
use hermes_five::devices::{DigitalInput, InputEvent};
use hermes_five::hardware::{Board, BoardEvent};
#[hermes_five::runtime]
async fn main() {
let board = Board::run();
board.on(BoardEvent::OnReady, |board: Board| async move {
// Register a sensor on pin 7.
let sensor = DigitalInput::new(&board, 7)?;
// Triggered function when the sensor state changes.
sensor.on(InputEvent::OnChange, |value: bool| 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.
sensor.detach();
board.close();
Ok(())
});
}Trait Implementations§
Source§impl Clone for DigitalInput
impl Clone for DigitalInput
Source§fn clone(&self) -> DigitalInput
fn clone(&self) -> DigitalInput
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 DigitalInput
impl Debug for DigitalInput
Source§impl Display for DigitalInput
impl Display for DigitalInput
Source§impl Input for DigitalInput
impl Input for DigitalInput
impl Device for DigitalInput
Auto Trait Implementations§
impl Freeze for DigitalInput
impl !RefUnwindSafe for DigitalInput
impl Send for DigitalInput
impl Sync for DigitalInput
impl Unpin for DigitalInput
impl !UnwindSafe for DigitalInput
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