diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-02-03 05:05:05 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-02-03 05:05:05 +0100 |
| commit | 42c9a603bbe0a98db140e252b6ab2ad7eab56425 (patch) | |
| tree | 2386be4a7b3c9ffb834a73e5be39b8c535832311 /embassy-std-examples | |
| parent | cbea07716b49d026506a61d261a551ff62c1b6c3 (diff) | |
Add embassy-std-examples
Diffstat (limited to 'embassy-std-examples')
| -rw-r--r-- | embassy-std-examples/Cargo.toml | 14 | ||||
| -rw-r--r-- | embassy-std-examples/src/bin/serial.rs | 57 | ||||
| -rw-r--r-- | embassy-std-examples/src/bin/tick.rs | 29 | ||||
| -rw-r--r-- | embassy-std-examples/src/serial_port.rs | 71 |
4 files changed, 171 insertions, 0 deletions
diff --git a/embassy-std-examples/Cargo.toml b/embassy-std-examples/Cargo.toml new file mode 100644 index 000000000..0998899c8 --- /dev/null +++ b/embassy-std-examples/Cargo.toml | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | [package] | ||
| 2 | authors = ["Dario Nieuwenhuis <[email protected]>"] | ||
| 3 | edition = "2018" | ||
| 4 | name = "embassy-std-examples" | ||
| 5 | version = "0.1.0" | ||
| 6 | |||
| 7 | [dependencies] | ||
| 8 | async-io = "1.3.1" | ||
| 9 | embassy = { version = "0.1.0", path = "../embassy", features = ["log"] } | ||
| 10 | embassy-std = { version = "0.1.0", path = "../embassy-std" } | ||
| 11 | env_logger = "0.8.2" | ||
| 12 | futures = { version = "0.3.8", default-features = false, features = ["async-await"] } | ||
| 13 | log = "0.4.11" | ||
| 14 | nix = "0.19.1" | ||
diff --git a/embassy-std-examples/src/bin/serial.rs b/embassy-std-examples/src/bin/serial.rs new file mode 100644 index 000000000..b66b7de20 --- /dev/null +++ b/embassy-std-examples/src/bin/serial.rs | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | #![feature(type_alias_impl_trait)] | ||
| 2 | |||
| 3 | #[path = "../serial_port.rs"] | ||
| 4 | mod serial_port; | ||
| 5 | |||
| 6 | use async_io::Async; | ||
| 7 | use embassy::executor::task; | ||
| 8 | use embassy::io::{AsyncBufRead, AsyncBufReadExt, AsyncWrite, AsyncWriteExt}; | ||
| 9 | use embassy::util::Forever; | ||
| 10 | use embassy_std::Executor; | ||
| 11 | use log::*; | ||
| 12 | use nix::sys::termios; | ||
| 13 | |||
| 14 | use self::serial_port::SerialPort; | ||
| 15 | |||
| 16 | #[task] | ||
| 17 | async fn run() { | ||
| 18 | // Open the serial port. | ||
| 19 | let baudrate = termios::BaudRate::B115200; | ||
| 20 | let port = SerialPort::new("/dev/ttyACM0", baudrate).unwrap(); | ||
| 21 | //let port = Spy::new(port); | ||
| 22 | |||
| 23 | // Use async_io's reactor for async IO. | ||
| 24 | // This demonstrates how embassy's executor can drive futures from another IO library. | ||
| 25 | // Essentially, async_io::Async converts from AsRawFd+Read+Write to futures's AsyncRead+AsyncWrite | ||
| 26 | let port = Async::new(port).unwrap(); | ||
| 27 | |||
| 28 | // This implements futures's AsyncBufRead based on futures's AsyncRead | ||
| 29 | let port = futures::io::BufReader::new(port); | ||
| 30 | |||
| 31 | // We can then use FromStdIo to convert from futures's AsyncBufRead+AsyncWrite | ||
| 32 | // to embassy's AsyncBufRead+AsyncWrite | ||
| 33 | let mut port = embassy::io::FromStdIo::new(port); | ||
| 34 | |||
| 35 | info!("Serial opened!"); | ||
| 36 | |||
| 37 | loop { | ||
| 38 | let mut buf = [0u8; 256]; | ||
| 39 | let n = port.read(&mut buf).await.unwrap(); | ||
| 40 | info!("read {:?}", &buf[..n]); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 45 | |||
| 46 | fn main() { | ||
| 47 | env_logger::builder() | ||
| 48 | .filter_level(log::LevelFilter::Debug) | ||
| 49 | .filter_module("async_io", log::LevelFilter::Info) | ||
| 50 | .format_timestamp_nanos() | ||
| 51 | .init(); | ||
| 52 | |||
| 53 | let executor = EXECUTOR.put(Executor::new()); | ||
| 54 | executor.run(|spawner| { | ||
| 55 | spawner.spawn(run()).unwrap(); | ||
| 56 | }); | ||
| 57 | } | ||
diff --git a/embassy-std-examples/src/bin/tick.rs b/embassy-std-examples/src/bin/tick.rs new file mode 100644 index 000000000..af2305f33 --- /dev/null +++ b/embassy-std-examples/src/bin/tick.rs | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | #![feature(type_alias_impl_trait)] | ||
| 2 | |||
| 3 | use embassy::executor::task; | ||
| 4 | use embassy::time::{Duration, Timer}; | ||
| 5 | use embassy::util::Forever; | ||
| 6 | use embassy_std::Executor; | ||
| 7 | use log::*; | ||
| 8 | |||
| 9 | #[task] | ||
| 10 | async fn run() { | ||
| 11 | loop { | ||
| 12 | info!("tick"); | ||
| 13 | Timer::after(Duration::from_secs(1)).await; | ||
| 14 | } | ||
| 15 | } | ||
| 16 | |||
| 17 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 18 | |||
| 19 | fn main() { | ||
| 20 | env_logger::builder() | ||
| 21 | .filter_level(log::LevelFilter::Debug) | ||
| 22 | .format_timestamp_nanos() | ||
| 23 | .init(); | ||
| 24 | |||
| 25 | let executor = EXECUTOR.put(Executor::new()); | ||
| 26 | executor.run(|spawner| { | ||
| 27 | spawner.spawn(run()).unwrap(); | ||
| 28 | }); | ||
| 29 | } | ||
diff --git a/embassy-std-examples/src/serial_port.rs b/embassy-std-examples/src/serial_port.rs new file mode 100644 index 000000000..7ac1b1edb --- /dev/null +++ b/embassy-std-examples/src/serial_port.rs | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | use nix::fcntl::OFlag; | ||
| 2 | use nix::sys::termios; | ||
| 3 | use nix::Error; | ||
| 4 | use std::io; | ||
| 5 | use std::os::unix::io::{AsRawFd, RawFd}; | ||
| 6 | |||
| 7 | pub struct SerialPort { | ||
| 8 | fd: RawFd, | ||
| 9 | } | ||
| 10 | |||
| 11 | impl SerialPort { | ||
| 12 | pub fn new<'a, P: ?Sized + nix::NixPath>( | ||
| 13 | path: &P, | ||
| 14 | baudrate: termios::BaudRate, | ||
| 15 | ) -> io::Result<Self> { | ||
| 16 | let fd = nix::fcntl::open( | ||
| 17 | path, | ||
| 18 | OFlag::O_RDWR | OFlag::O_NOCTTY | OFlag::O_NONBLOCK, | ||
| 19 | nix::sys::stat::Mode::empty(), | ||
| 20 | ) | ||
| 21 | .map_err(to_io_error)?; | ||
| 22 | |||
| 23 | let mut cfg = termios::tcgetattr(fd).map_err(to_io_error)?; | ||
| 24 | cfg.input_flags = termios::InputFlags::empty(); | ||
| 25 | cfg.output_flags = termios::OutputFlags::empty(); | ||
| 26 | cfg.control_flags = termios::ControlFlags::empty(); | ||
| 27 | cfg.local_flags = termios::LocalFlags::empty(); | ||
| 28 | termios::cfmakeraw(&mut cfg); | ||
| 29 | cfg.input_flags |= termios::InputFlags::IGNBRK; | ||
| 30 | cfg.control_flags |= termios::ControlFlags::CREAD; | ||
| 31 | //cfg.control_flags |= termios::ControlFlags::CRTSCTS; | ||
| 32 | termios::cfsetospeed(&mut cfg, baudrate).map_err(to_io_error)?; | ||
| 33 | termios::cfsetispeed(&mut cfg, baudrate).map_err(to_io_error)?; | ||
| 34 | termios::cfsetspeed(&mut cfg, baudrate).map_err(to_io_error)?; | ||
| 35 | // Set VMIN = 1 to block until at least one character is received. | ||
| 36 | cfg.control_chars[termios::SpecialCharacterIndices::VMIN as usize] = 1; | ||
| 37 | termios::tcsetattr(fd, termios::SetArg::TCSANOW, &cfg).map_err(to_io_error)?; | ||
| 38 | termios::tcflush(fd, termios::FlushArg::TCIOFLUSH).map_err(to_io_error)?; | ||
| 39 | |||
| 40 | Ok(Self { fd }) | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | impl AsRawFd for SerialPort { | ||
| 45 | fn as_raw_fd(&self) -> RawFd { | ||
| 46 | self.fd | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | impl io::Read for SerialPort { | ||
| 51 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||
| 52 | nix::unistd::read(self.fd, buf).map_err(to_io_error) | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | impl io::Write for SerialPort { | ||
| 57 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { | ||
| 58 | nix::unistd::write(self.fd, buf).map_err(to_io_error) | ||
| 59 | } | ||
| 60 | |||
| 61 | fn flush(&mut self) -> io::Result<()> { | ||
| 62 | Ok(()) | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | fn to_io_error(e: Error) -> io::Error { | ||
| 67 | match e { | ||
| 68 | Error::Sys(errno) => errno.into(), | ||
| 69 | e => io::Error::new(io::ErrorKind::InvalidInput, e), | ||
| 70 | } | ||
| 71 | } | ||
