Move examples to a subdirectory

This commit is contained in:
Dario Nieuwenhuis
2021-06-02 01:30:07 +02:00
parent a0c5f7137f
commit dff03ecfc7
51 changed files with 374 additions and 36 deletions

103
examples/std/src/bin/net.rs Normal file
View File

@ -0,0 +1,103 @@
#![feature(type_alias_impl_trait)]
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![allow(incomplete_features)]
use clap::{AppSettings, Clap};
use embassy::executor::Spawner;
use embassy::io::AsyncWriteExt;
use embassy::util::Forever;
use embassy_net::*;
use embassy_std::Executor;
use heapless::Vec;
use log::*;
#[path = "../tuntap.rs"]
mod tuntap;
use crate::tuntap::TunTapDevice;
static DEVICE: Forever<TunTapDevice> = Forever::new();
static CONFIG: Forever<DhcpConfigurator> = Forever::new();
#[derive(Clap)]
#[clap(version = "1.0")]
#[clap(setting = AppSettings::ColoredHelp)]
struct Opts {
/// TAP device name
#[clap(long, default_value = "tap0")]
tap: String,
}
#[embassy::task]
async fn net_task() {
embassy_net::run().await
}
#[embassy::task]
async fn main_task(spawner: Spawner) {
let opts: Opts = Opts::parse();
// Init network device
let device = TunTapDevice::new(&opts.tap).unwrap();
// Static IP configuration
let config = StaticConfigurator::new(Config {
address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24),
dns_servers: Vec::new(),
gateway: Some(Ipv4Address::new(192, 168, 69, 1)),
});
// DHCP configruation
let config = DhcpConfigurator::new();
// Init network stack
embassy_net::init(DEVICE.put(device), CONFIG.put(config));
// Launch network task
spawner.spawn(net_task()).unwrap();
// Then we can use it!
let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
let mut socket = TcpSocket::new(&mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10)));
let remote_endpoint = (Ipv4Address::new(192, 168, 69, 74), 8000);
info!("connecting to {:?}...", remote_endpoint);
let r = socket.connect(remote_endpoint).await;
if let Err(e) = r {
warn!("connect error: {:?}", e);
return;
}
info!("connected!");
loop {
let r = socket.write_all(b"Hello!\n").await;
if let Err(e) = r {
warn!("write error: {:?}", e);
return;
}
}
}
#[no_mangle]
fn _embassy_rand(buf: &mut [u8]) {
use rand_core::{OsRng, RngCore};
OsRng.fill_bytes(buf);
}
static EXECUTOR: Forever<Executor> = Forever::new();
fn main() {
env_logger::builder()
.filter_level(log::LevelFilter::Debug)
.filter_module("async_io", log::LevelFilter::Info)
.format_timestamp_nanos()
.init();
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
spawner.spawn(main_task(spawner)).unwrap();
});
}

View File

@ -0,0 +1,59 @@
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(type_alias_impl_trait)]
#![allow(incomplete_features)]
#[path = "../serial_port.rs"]
mod serial_port;
use async_io::Async;
use embassy::io::AsyncBufReadExt;
use embassy::util::Forever;
use embassy_std::Executor;
use log::*;
use nix::sys::termios;
use self::serial_port::SerialPort;
#[embassy::task]
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();
// This implements futures's AsyncBufRead based on futures's AsyncRead
let port = futures::io::BufReader::new(port);
// We can then use FromStdIo to convert from futures's AsyncBufRead+AsyncWrite
// to embassy's AsyncBufRead+AsyncWrite
let mut port = embassy::io::FromStdIo::new(port);
info!("Serial opened!");
loop {
let mut buf = [0u8; 256];
let n = port.read(&mut buf).await.unwrap();
info!("read {:?}", &buf[..n]);
}
}
static EXECUTOR: Forever<Executor> = Forever::new();
fn main() {
env_logger::builder()
.filter_level(log::LevelFilter::Debug)
.filter_module("async_io", log::LevelFilter::Info)
.format_timestamp_nanos()
.init();
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
spawner.spawn(run()).unwrap();
});
}

View File

@ -0,0 +1,31 @@
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(type_alias_impl_trait)]
#![allow(incomplete_features)]
use embassy::time::{Duration, Timer};
use embassy::util::Forever;
use embassy_std::Executor;
use log::*;
#[embassy::task]
async fn run() {
loop {
info!("tick");
Timer::after(Duration::from_secs(1)).await;
}
}
static EXECUTOR: Forever<Executor> = Forever::new();
fn main() {
env_logger::builder()
.filter_level(log::LevelFilter::Debug)
.format_timestamp_nanos()
.init();
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
spawner.spawn(run()).unwrap();
});
}