Skip to content

Commit

Permalink
feat(macros): add an attribute macro for init functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ROMemories committed Feb 22, 2024
1 parent ff59493 commit 574ca7b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 30 deletions.
13 changes: 5 additions & 8 deletions examples/embassy-http-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod routes;

use riot_rs as _;

use riot_rs::embassy::network;
use riot_rs::embassy::{network, Spawner};
use riot_rs::rt::debug::println;

use embassy_net::tcp::TcpSocket;
Expand Down Expand Up @@ -85,15 +85,10 @@ async fn web_task(
}
}

// 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) {
#[riot_rs::main]
async fn main(#[cfg(feature = "button-readings")] buttons: pins::Buttons) {
#[cfg(feature = "button-readings")]
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),
Expand All @@ -119,6 +114,8 @@ fn web_server_init(spawner: &Spawner, peripherals: &mut OptionalPeripherals) {
write: Some(Duration::from_secs(1)),
}));

let spawner = Spawner::for_current_executor().await;

for id in 0..WEB_TASK_POOL_SIZE {
let app_state = AppState {
#[cfg(feature = "button-readings")]
Expand Down
14 changes: 3 additions & 11 deletions examples/embassy-net-tcp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#![feature(type_alias_impl_trait)]
#![feature(used_with_arg)]

use riot_rs::embassy::network;
use riot_rs::embassy::{network, Spawner};
use riot_rs::rt::debug::println;

use embedded_io_async::Write;

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

Expand Down Expand Up @@ -55,14 +55,6 @@ async fn tcp_echo() {
}
}

// 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();
}

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

use riot_rs::embassy::network;
use riot_rs::embassy::{arch, network, Spawner};
use riot_rs::rt::debug::println;

#[embassy_executor::task]
async fn udp_echo() {
#[riot_rs::main]
async fn main() {
use embassy_net::udp::{PacketMetadata, UdpSocket};
let stack = network::network_stack().await.unwrap();

Expand Down Expand Up @@ -60,14 +60,6 @@ async fn udp_echo() {
}
}

// 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_udp_echo(spawner: &Spawner, _peripherals: &mut OptionalPeripherals) {
spawner.spawn(udp_echo()).unwrap();
}

#[no_mangle]
fn riot_rs_network_config() -> embassy_net::Config {
use embassy_net::Ipv4Address;
Expand Down
17 changes: 17 additions & 0 deletions src/riot-rs-embassy/src/define_peripherals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,26 @@ macro_rules! define_peripherals {
})
}
}

impl $crate::define_peripherals::IntoPeripherals<$peripherals> for &mut $crate::arch::OptionalPeripherals {
fn into_peripherals(&mut self) -> $peripherals {
$peripherals {
$($peripheral_name: self.$peripheral_field
.take()
.ok_or($crate::define_peripherals::DefinePeripheralsError::TakingPeripheral)
.unwrap()
),*
}
}
}
}
}

#[doc(hidden)]
pub trait IntoPeripherals<T> {
fn into_peripherals(&mut self) -> T;
}

#[derive(Debug)]
pub enum DefinePeripheralsError {
TakingPeripheral,
Expand Down
1 change: 1 addition & 0 deletions src/riot-rs-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ mod utils;

use proc_macro::TokenStream;

include!("main_macro.rs");
include!("thread.rs");
43 changes: 43 additions & 0 deletions src/riot-rs-macros/src/main_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/// Registers a function acting as an entrypoint for an application.
///
/// Peripheral groups defined with the `define_peripherals!` macro can be obtained in that function
/// by taking them as parameters.
///
/// # Parameters
///
/// None.
///
/// # Panics
///
/// This macro panics when the `riot-rs` crate cannot be found as a dependency of the crate where
/// this macro is used.
#[proc_macro_attribute]
pub fn main(_args: TokenStream, item: TokenStream) -> TokenStream {
use std::iter;

use quote::quote;

let task_function = syn::parse_macro_input!(item as syn::ItemFn);
let function_param_count = task_function.sig.inputs.len();

// Add an extractor for each function parameter, so that the function receives the requested
// peripherals
let extractors =
iter::repeat(quote! { peripherals.into_peripherals() }).take(function_param_count);

let riot_rs_crate = utils::riot_rs_crate();

let expanded = quote! {
#[#riot_rs_crate::embassy::distributed_slice(#riot_rs_crate::embassy::EMBASSY_TASKS)]
#[linkme(crate = #riot_rs_crate::embassy::linkme)]
fn __main(spawner: &Spawner, mut peripherals: &mut #riot_rs_crate::embassy::arch::OptionalPeripherals) {
use #riot_rs_crate::define_peripherals::IntoPeripherals;
spawner.spawn(main(#(#extractors),*)).unwrap();
}

#[embassy_executor::task]
#task_function
};

TokenStream::from(expanded)
}
3 changes: 3 additions & 0 deletions src/riot-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ pub use riot_rs_buildinfo as buildinfo;
pub use riot_rs_embassy::{self as embassy, define_peripherals};
pub use riot_rs_rt as rt;

// Attribute macros
pub use riot_rs_macros::main;
#[cfg(feature = "threading")]
pub use riot_rs_macros::thread;

#[cfg(feature = "threading")]
pub use riot_rs_threads as thread;

Expand Down

0 comments on commit 574ca7b

Please sign in to comment.