pub struct Board { /* private fields */ }
Expand description
Represents a physical board (Arduino most-likely) where your crate::devices::Device
can be attached and controlled through this API.
The board gives access to IoData
through a communication IoProtocol
.
Implementations§
Source§impl Board
impl Board
Sourcepub fn run() -> Self
pub fn run() -> Self
Creates and open a default board (using default protocol).
This method creates a board using the default RemoteIo
protocol with Serial
transport layer.
The port will be auto-detected as the first available serial port matching a board.
§Example
use hermes_five::hardware::Board;
use hermes_five::io::RemoteIo;
#[hermes_five::runtime]
async fn main() {
// Following lines are all equivalent:
let board = Board::run();
let board = Board::default().open();
let board = Board::new(RemoteIo::default()).open();
}
Sourcepub fn new<P: IoProtocol + 'static>(protocol: P) -> Self
pub fn new<P: IoProtocol + 'static>(protocol: P) -> Self
Creates a board using a given protocol.
§Example
use hermes_five::hardware::Board;
use hermes_five::io::RemoteIo;
#[hermes_five::runtime]
async fn main() {
let board = Board::new(RemoteIo::new("COM4")).open();
}
Sourcepub fn open(self) -> Self
pub fn open(self) -> Self
Starts a board connexion procedure (using the appropriate configured protocol) in an asynchronous way.
Note 1: you probably might not want to call this method yourself and use Self::run()
instead.
Note 2: after this method, you cannot consider the board to be connected until you receive the “ready” event.
§Example
Have a look at the examples/board folder more detailed examples.
use hermes_five::hardware::{Board, BoardEvent};
use hermes_five::io::IO;
#[hermes_five::runtime]
async fn main() {
let board = Board::run();
// Is equivalent to:
let mut board = Board::default().open();
// Register something to do when the board is connected.
board.on(BoardEvent::OnReady, |_: Board| async move {
// Something to do when connected.
Ok(())
});
// code here will be executed right away, before the board is actually connected.
}
Sourcepub fn close(self) -> Self
pub fn close(self) -> Self
Close a board connexion (using the appropriate configured protocol) in an asynchronous way. Note: after this method, you cannot consider the board to be connected until you receive the “close” event.
§Example
Have a look at the examples/board folder more detailed examples.
use hermes_five::pause;
use hermes_five::hardware::{Board, BoardEvent};
use hermes_five::io::IO;
#[hermes_five::runtime]
async fn main() {
let board = Board::run();
board.on(BoardEvent::OnReady, |mut board: Board| async move {
// Something to do when connected.
pause!(3000);
board.close();
Ok(())
});
board.on(BoardEvent::OnClose, |_: Board| async move {
// Something to do when connection closes.
Ok(())
});
}
Sourcepub fn blocking_open(self) -> Result<Self, Error>
pub fn blocking_open(self) -> Result<Self, Error>
Blocking version of Self::open()
method.
Sourcepub fn blocking_close(self) -> Result<Self, Error>
pub fn blocking_close(self) -> Result<Self, Error>
Blocking version of Self::close()
method.
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 a board are defined by the enum: BoardEvent
:
OnRead
|ready
: Triggered when the board is connected and ready to run.
The callback must receive the following parameter:|_: Board| { ... }
OnClose
|close
: Triggered when the board is disconnected.
The callback must receive the following parameter:|_: Board| { ... }
§Example
use hermes_five::hardware::{Board, BoardEvent};
#[hermes_five::runtime]
async fn main() {
let board = Board::run();
board.on(BoardEvent::OnReady, |_: Board| async move {
// Here, you know the board to be connected and ready to receive data.
Ok(())
});
}
Trait Implementations§
Source§impl Default for Board
impl Default for Board
Source§fn default() -> Self
fn default() -> Self
Default implementation for a board.
This method creates a board using the default RemoteIo
protocol with Serial
transport layer.
The port will be auto-detected as the first available serial port matching a board.
/!\ The board will NOT be connected until the Board::open
method is called.
§Example
use hermes_five::hardware::Board;
use hermes_five::io::RemoteIo;
#[hermes_five::runtime]
async fn main() {
// Following lines are all equivalent:
let board = Board::run();
let board = Board::default().open();
let board = Board::new(RemoteIo::default()).open();
}
Source§impl<T: IoTransport> From<T> for Board
Creates a board using the given transport layer with the RemoteIo protocol.
impl<T: IoTransport> From<T> for Board
Creates a board using the given transport layer with the RemoteIo protocol.
§Example
use hermes_five::hardware::Board;
use hermes_five::io::RemoteIo;
use hermes_five::io::Serial;
#[hermes_five::runtime]
async fn main() {
let board = Board::from(Serial::new("/dev/ttyUSB0")).open();
}
Source§impl Hardware for Board
impl Hardware for Board
Source§fn get_protocol(&self) -> Box<dyn IoProtocol>
fn get_protocol(&self) -> Box<dyn IoProtocol>
Source§fn set_protocol(&mut self, protocol: Box<dyn IoProtocol>)
fn set_protocol(&mut self, protocol: Box<dyn IoProtocol>)
Source§fn get_protocol_name(&self) -> &str
fn get_protocol_name(&self) -> &str
Source§impl IO for Board
impl IO for Board
Source§fn get_io(&self) -> &Arc<RwLock<IoData>>
fn get_io(&self) -> &Arc<RwLock<IoData>>
Easy access to hardware through the board.
§Example
use hermes_five::hardware::Board;
use hermes_five::hardware::BoardEvent;
use hermes_five::io::IO;
#[hermes_five::runtime]
async fn main() {
let board = Board::run();
board.on(BoardEvent::OnReady, |mut board: Board| async move {
println!("Board connected: {}", board);
println!("Pins {:#?}", board.get_io().read().pins);
Ok(())
});
}
Source§fn is_connected(&self) -> bool
fn is_connected(&self) -> bool
Source§fn servo_config(&mut self, pin: u8, pwm_range: Range<u16>) -> Result<(), Error>
fn servo_config(&mut self, pin: u8, pwm_range: Range<u16>) -> Result<(), Error>
Source§fn i2c_config(&mut self, delay: u16) -> Result<(), Error>
fn i2c_config(&mut self, delay: u16) -> Result<(), Error>
delay
in microseconds for I2C devices that require a delay between when the
register is written to and the data in that register can be read.