Skip to content

Commit

Permalink
feat: simplify define_peripherals by removing a level
Browse files Browse the repository at this point in the history
  • Loading branch information
ROMemories committed Jan 22, 2024
1 parent 03b78cf commit 958c715
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 46 deletions.
10 changes: 5 additions & 5 deletions examples/embassy-http-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ impl Application for WebServer {
peripherals: &mut OptionalPeripherals,
_init_args: InitializationArgs,
) -> Result<&dyn Application, ApplicationInitError> {
let our_peripherals = pins::OurPeripherals::take_from(peripherals)?;
let buttons = pins::Buttons::take_from(peripherals)?;

let buttons = Buttons {
button1: Input::new(our_peripherals.buttons.btn1.degrade(), Pull::Up),
button2: Input::new(our_peripherals.buttons.btn2.degrade(), Pull::Up),
button3: Input::new(our_peripherals.buttons.btn3.degrade(), Pull::Up),
button4: Input::new(our_peripherals.buttons.btn4.degrade(), Pull::Up),
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),
};

let button_inputs = ButtonInputs(make_static!(Mutex::new(buttons)));
Expand Down
15 changes: 6 additions & 9 deletions examples/embassy-http-server/src/pins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ use embassy_nrf::peripherals;
use riot_rs::define_peripherals;

#[cfg(builder = "nrf52840dk")]
define_peripherals! {
OurPeripherals,
buttons: Buttons {
btn1: P0_11,
btn2: P0_12,
btn3: P0_24,
btn4: P0_25,
}
}
define_peripherals!(Buttons {
btn1: P0_11,
btn2: P0_12,
btn3: P0_24,
btn4: P0_25,
});
51 changes: 19 additions & 32 deletions src/riot-rs-embassy/src/define_peripherals.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// This macro allows to extract the specified peripherals from `OptionalPeripherals` for use in an
/// application.
///
/// It generates a struct named after the first parameter, which provides a `take_from()` method
/// for extracting the specified peripherals from `OptionalPeripherals`.
/// The generated struct provides a `take_from()` method for extracting the specified peripherals
/// from `OptionalPeripherals`.
///
/// The `define_peripherals!` macro expects a `peripherals` module to be in scope, where the
/// peripheral types should come from.
Expand All @@ -16,34 +16,23 @@
// under MIT license
#[macro_export]
macro_rules! define_peripherals {
{
$peripherals: ident,
$(
$(#[$outer:meta])*
$group_name:ident : $group_struct:ident {
$(
$(#[$inner:meta])*
$peripheral_name:ident : $peripheral_field:ident $(=$peripheral_alias:ident)?),*
$(,)?
}
(
$(#[$outer:meta])*
$peripherals:ident {
$(
$(#[$inner:meta])*
$peripheral_name:ident : $peripheral_field:ident $(=$peripheral_alias:ident)?),*
$(,)?
)+
} => {
}
) => {
#[allow(dead_code,non_snake_case,missing_docs)]
$(#[$outer])*
pub struct $peripherals {
$(pub $group_name : $group_struct),*
$(
$(#[$inner])*
pub $peripheral_name: peripherals::$peripheral_field
),*
}
$(
#[allow(dead_code,non_snake_case)]
$(#[$outer])*
pub struct $group_struct {
$(
$(#[$inner])*
pub $peripheral_name: peripherals::$peripheral_field
),*
}
)+


$($($(
#[allow(missing_docs)]
Expand All @@ -55,12 +44,10 @@ macro_rules! define_peripherals {
opt_peripherals: &mut $crate::arch::OptionalPeripherals
) -> Result<Self, $crate::define_peripherals::DefinePeripheralsError> {
Ok(Self {
$($group_name: $group_struct {
$($peripheral_name: opt_peripherals.$peripheral_field
.take()
.ok_or($crate::define_peripherals::DefinePeripheralsError::TakingPeripheral)?
),*
}),*
$($peripheral_name: opt_peripherals.$peripheral_field
.take()
.ok_or($crate::define_peripherals::DefinePeripheralsError::TakingPeripheral)?
),*
})
}
}
Expand Down

0 comments on commit 958c715

Please sign in to comment.