Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rework initialization #79

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 76 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions examples/application/Cargo.toml

This file was deleted.

5 changes: 0 additions & 5 deletions examples/application/README.md

This file was deleted.

4 changes: 0 additions & 4 deletions examples/application/laze.yml

This file was deleted.

29 changes: 0 additions & 29 deletions examples/application/src/main.rs

This file was deleted.

93 changes: 36 additions & 57 deletions examples/embassy-http-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ mod routes;

use riot_rs as _;

use riot_rs::embassy::{
arch::OptionalPeripherals, Application, ApplicationInitError, Drivers, InitializationArgs,
NetworkStack,
};
use riot_rs::embassy::network_stack;
use riot_rs::rt::debug::println;

use embassy_net::tcp::TcpSocket;
Expand Down Expand Up @@ -54,11 +51,12 @@ const WEB_TASK_POOL_SIZE: usize = 2;
#[embassy_executor::task(pool_size = WEB_TASK_POOL_SIZE)]
async fn web_task(
id: usize,
stack: &'static NetworkStack,
app: &'static picoserve::Router<AppRouter, AppState>,
config: &'static picoserve::Config<Duration>,
state: AppState,
) -> ! {
let stack = network_stack().await.unwrap();

let mut rx_buffer = [0; 1024];
let mut tx_buffer = [0; 1024];

Expand Down Expand Up @@ -87,68 +85,49 @@ async fn web_task(
}
}

struct WebServer {
// TODO: macro up this up
use riot_rs::embassy::{arch::OptionalPeripherals, Spawner};
#[riot_rs::embassy::distributed_slice(riot_rs::embassy::EMBASSY_TASKS)]
#[linkme(crate = riot_rs::embassy::linkme)]
fn web_server_init(spawner: &Spawner, peripherals: &mut OptionalPeripherals) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here, b/c the Buttons peripherals are optional, I found it easier to just write a custom non-async init function.

#[cfg(feature = "button-readings")]
button_inputs: ButtonInputs,
}

impl Application for WebServer {
fn initialize(
peripherals: &mut OptionalPeripherals,
_init_args: InitializationArgs,
) -> Result<&dyn Application, ApplicationInitError> {
#[cfg(feature = "button-readings")]
let button_inputs = {
let buttons = pins::Buttons::take_from(peripherals)?;

let buttons = Buttons {
button1: Input::new(buttons.btn1.degrade(), Pull::Up),
button2: Input::new(buttons.btn2.degrade(), Pull::Up),
button3: Input::new(buttons.btn3.degrade(), Pull::Up),
button4: Input::new(buttons.btn4.degrade(), Pull::Up),
};

ButtonInputs(make_static!(Mutex::new(buttons)))
let button_inputs = {
let buttons = pins::Buttons::take_from(peripherals).unwrap();

let buttons = Buttons {
button1: Input::new(buttons.btn1.degrade(), Pull::Up),
button2: Input::new(buttons.btn2.degrade(), Pull::Up),
button3: Input::new(buttons.btn3.degrade(), Pull::Up),
button4: Input::new(buttons.btn4.degrade(), Pull::Up),
};

Ok(make_static!(Self {
#[cfg(feature = "button-readings")]
button_inputs,
}))
ButtonInputs(make_static!(Mutex::new(buttons)))
};

fn make_app() -> picoserve::Router<AppRouter, AppState> {
let router = picoserve::Router::new().route("/", get(routes::index));
#[cfg(feature = "button-readings")]
let router = router.route("/buttons", get(routes::buttons));
router
}

fn start(&self, spawner: embassy_executor::Spawner, drivers: Drivers) {
let stack = drivers.stack.get().unwrap();
let app = make_static!(make_app());

fn make_app() -> picoserve::Router<AppRouter, AppState> {
let router = picoserve::Router::new().route("/", get(routes::index));
#[cfg(feature = "button-readings")]
let router = router.route("/buttons", get(routes::buttons));
router
}
let config = make_static!(picoserve::Config::new(picoserve::Timeouts {
start_read_request: Some(Duration::from_secs(5)),
read_request: Some(Duration::from_secs(1)),
write: Some(Duration::from_secs(1)),
}));

let app = make_static!(make_app());

let config = make_static!(picoserve::Config::new(picoserve::Timeouts {
start_read_request: Some(Duration::from_secs(5)),
read_request: Some(Duration::from_secs(1)),
write: Some(Duration::from_secs(1)),
}));

for id in 0..WEB_TASK_POOL_SIZE {
let app_state = AppState {
#[cfg(feature = "button-readings")]
buttons: self.button_inputs,
};
spawner
.spawn(web_task(id, stack, app, config, app_state))
.unwrap();
}
for id in 0..WEB_TASK_POOL_SIZE {
let app_state = AppState {
#[cfg(feature = "button-readings")]
buttons: button_inputs,
};
spawner.spawn(web_task(id, app, config, app_state)).unwrap();
}
}

riot_rs::embassy::riot_initialize!(WebServer);

#[no_mangle]
fn riot_rs_network_config() -> embassy_net::Config {
use embassy_net::Ipv4Address;
Expand Down
27 changes: 9 additions & 18 deletions examples/embassy-net-tcp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
#![feature(type_alias_impl_trait)]
#![feature(used_with_arg)]

use riot_rs::embassy::{arch, Application, ApplicationInitError, Drivers, InitializationArgs};
use riot_rs::embassy::network_stack;

use riot_rs::rt::debug::println;

use embedded_io_async::Write;

#[embassy_executor::task]
async fn tcp_echo(drivers: Drivers) {
async fn tcp_echo() {
use embassy_net::tcp::TcpSocket;
let stack = drivers.stack.get().unwrap();
let stack = network_stack().await.unwrap();

let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
Expand Down Expand Up @@ -56,23 +56,14 @@ async fn tcp_echo(drivers: Drivers) {
}
}

struct TcpEcho {}

impl Application for TcpEcho {
fn initialize(
_peripherals: &mut arch::OptionalPeripherals,
_init_args: InitializationArgs,
) -> Result<&dyn Application, ApplicationInitError> {
Ok(&Self {})
}

fn start(&self, spawner: embassy_executor::Spawner, drivers: Drivers) {
spawner.spawn(tcp_echo(drivers)).unwrap();
}
// TODO: macro up this
use riot_rs::embassy::{arch::OptionalPeripherals, Spawner};
#[riot_rs::embassy::distributed_slice(riot_rs::embassy::EMBASSY_TASKS)]
#[linkme(crate = riot_rs::embassy::linkme)]
fn __init_tcp_echo(spawner: &Spawner, _peripherals: &mut OptionalPeripherals) {
spawner.spawn(tcp_echo()).unwrap();
}

riot_rs::embassy::riot_initialize!(TcpEcho);

#[no_mangle]
fn riot_rs_network_config() -> embassy_net::Config {
use embassy_net::Ipv4Address;
Expand Down
Loading