embassy/examples/std/src/bin/serial.rs

57 lines
1.7 KiB
Rust
Raw Normal View History

2021-02-03 05:05:05 +01:00
#![feature(type_alias_impl_trait)]
#[path = "../serial_port.rs"]
mod serial_port;
use async_io::Async;
use embassy_executor::Executor;
2022-05-04 20:48:37 +02:00
use embedded_io::asynch::Read;
2021-02-03 05:05:05 +01:00
use log::*;
use nix::sys::termios;
2022-08-22 15:51:44 +02:00
use static_cell::StaticCell;
2021-02-03 05:05:05 +01:00
use self::serial_port::SerialPort;
#[embassy_executor::task]
2021-02-03 05:05:05 +01:00
async fn run() {
// Open the serial port.
let baudrate = termios::BaudRate::B115200;
let port = SerialPort::new("/dev/ttyACM0", baudrate).unwrap();
//let port = Spy::new(port);
// Use async_io's reactor for async IO.
// This demonstrates how embassy's executor can drive futures from another IO library.
// Essentially, async_io::Async converts from AsRawFd+Read+Write to futures's AsyncRead+AsyncWrite
let port = Async::new(port).unwrap();
2022-05-04 20:48:37 +02:00
// We can then use FromStdIo to convert from futures's AsyncRead+AsyncWrite
// to embedded_io's async Read+Write.
//
// This is not really needed, you could write the code below using futures::io directly.
// It's useful if you want to have portable code across embedded and std.
let mut port = embedded_io::adapters::FromFutures::new(port);
2021-02-03 05:05:05 +01:00
info!("Serial opened!");
loop {
let mut buf = [0u8; 256];
let n = port.read(&mut buf).await.unwrap();
info!("read {:?}", &buf[..n]);
}
}
2022-08-22 15:51:44 +02:00
static EXECUTOR: StaticCell<Executor> = StaticCell::new();
2021-02-03 05:05:05 +01:00
fn main() {
env_logger::builder()
.filter_level(log::LevelFilter::Debug)
.filter_module("async_io", log::LevelFilter::Info)
.format_timestamp_nanos()
.init();
2022-08-22 15:51:44 +02:00
let executor = EXECUTOR.init(Executor::new());
2021-02-03 05:05:05 +01:00
executor.run(|spawner| {
spawner.spawn(run()).unwrap();
});
}