diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-06-02 01:30:07 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-06-02 01:32:19 +0200 |
| commit | dff03ecfc74d6af716637888338ebfa99ab7a027 (patch) | |
| tree | c06bf2b0a2e6657c3427c956dbd27a4e45211aaa /examples/std/src/bin/serial.rs | |
| parent | a0c5f7137fe0c45b8db0aad2a116aea91e6a93f7 (diff) | |
Move examples to a subdirectory
Diffstat (limited to 'examples/std/src/bin/serial.rs')
| -rw-r--r-- | examples/std/src/bin/serial.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/examples/std/src/bin/serial.rs b/examples/std/src/bin/serial.rs new file mode 100644 index 000000000..1b22dc0de --- /dev/null +++ b/examples/std/src/bin/serial.rs | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | #![feature(min_type_alias_impl_trait)] | ||
| 2 | #![feature(impl_trait_in_bindings)] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | #![allow(incomplete_features)] | ||
| 5 | |||
| 6 | #[path = "../serial_port.rs"] | ||
| 7 | mod serial_port; | ||
| 8 | |||
| 9 | use async_io::Async; | ||
| 10 | use embassy::io::AsyncBufReadExt; | ||
| 11 | use embassy::util::Forever; | ||
| 12 | use embassy_std::Executor; | ||
| 13 | use log::*; | ||
| 14 | use nix::sys::termios; | ||
| 15 | |||
| 16 | use self::serial_port::SerialPort; | ||
| 17 | |||
| 18 | #[embassy::task] | ||
| 19 | async fn run() { | ||
| 20 | // Open the serial port. | ||
| 21 | let baudrate = termios::BaudRate::B115200; | ||
| 22 | let port = SerialPort::new("/dev/ttyACM0", baudrate).unwrap(); | ||
| 23 | //let port = Spy::new(port); | ||
| 24 | |||
| 25 | // Use async_io's reactor for async IO. | ||
| 26 | // This demonstrates how embassy's executor can drive futures from another IO library. | ||
| 27 | // Essentially, async_io::Async converts from AsRawFd+Read+Write to futures's AsyncRead+AsyncWrite | ||
| 28 | let port = Async::new(port).unwrap(); | ||
| 29 | |||
| 30 | // This implements futures's AsyncBufRead based on futures's AsyncRead | ||
| 31 | let port = futures::io::BufReader::new(port); | ||
| 32 | |||
| 33 | // We can then use FromStdIo to convert from futures's AsyncBufRead+AsyncWrite | ||
| 34 | // to embassy's AsyncBufRead+AsyncWrite | ||
| 35 | let mut port = embassy::io::FromStdIo::new(port); | ||
| 36 | |||
| 37 | info!("Serial opened!"); | ||
| 38 | |||
| 39 | loop { | ||
| 40 | let mut buf = [0u8; 256]; | ||
| 41 | let n = port.read(&mut buf).await.unwrap(); | ||
| 42 | info!("read {:?}", &buf[..n]); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 47 | |||
| 48 | fn main() { | ||
| 49 | env_logger::builder() | ||
| 50 | .filter_level(log::LevelFilter::Debug) | ||
| 51 | .filter_module("async_io", log::LevelFilter::Info) | ||
| 52 | .format_timestamp_nanos() | ||
| 53 | .init(); | ||
| 54 | |||
| 55 | let executor = EXECUTOR.put(Executor::new()); | ||
| 56 | executor.run(|spawner| { | ||
| 57 | spawner.spawn(run()).unwrap(); | ||
| 58 | }); | ||
| 59 | } | ||
