Skip to content

Commit

Permalink
Remove gpio dispatch proc macro
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Sep 3, 2024
1 parent 93a862b commit 0b02233
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 129 deletions.
77 changes: 0 additions & 77 deletions esp-hal-procmacros/src/enum_dispatch.rs

This file was deleted.

33 changes: 0 additions & 33 deletions esp-hal-procmacros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ use proc_macro::TokenStream;

#[cfg(feature = "embassy")]
mod embassy;
#[cfg(feature = "enum-dispatch")]
mod enum_dispatch;
#[cfg(feature = "interrupt")]
mod interrupt;
#[cfg(any(
Expand Down Expand Up @@ -339,37 +337,6 @@ pub fn handler(args: TokenStream, input: TokenStream) -> TokenStream {
.into()
}

/// Create an enum for erased GPIO pins, using the enum-dispatch pattern
///
/// Only used internally
#[cfg(feature = "enum-dispatch")]
#[proc_macro]
pub fn make_gpio_enum_dispatch_macro(input: TokenStream) -> TokenStream {
use quote::{format_ident, quote};

use self::enum_dispatch::{build_match_arms, MakeGpioEnumDispatchMacro};

let input = syn::parse_macro_input!(input as MakeGpioEnumDispatchMacro);

let macro_name = format_ident!("{}", input.name);
let arms = build_match_arms(input);

quote! {
#[doc(hidden)]
#[macro_export]
macro_rules! #macro_name {
($m:ident, $target:ident, $body:block) => {
match $m {
#(#arms)*
}
}
}

pub(crate) use #macro_name;
}
.into()
}

/// Load code to be run on the LP/ULP core.
///
/// ## Example
Expand Down
64 changes: 45 additions & 19 deletions esp-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,6 @@ pub fn connect_high_to_peripheral(signal: InputSignal) {
});
}



#[doc(hidden)]
pub trait BooleanType {
const VALUE: bool;
Expand All @@ -670,6 +668,10 @@ pub trait PinType {
type IsOutput: BooleanType;
type IsAnalog: BooleanType;
type IsTouch: BooleanType;

fn is_output() -> bool {
<Self::IsOutput as BooleanType>::VALUE
}
}

#[doc(hidden)]
Expand Down Expand Up @@ -1328,6 +1330,16 @@ pub trait GpioProperties {
type PinType: PinType;
}

#[doc(hidden)]
#[macro_export]
macro_rules! if_output_pin {
(InputOnlyAnalog, { $($then:tt)* } else { $($else:tt)* } ) => { $($else)* };
(InputOutputAnalog, { $($then:tt)* } else { $($else:tt)* } ) => { $($then)* };
(InputOutputAnalogTouch, { $($then:tt)* } else { $($else:tt)* } ) => { $($then)* };
(InputOutput, { $($then:tt)* } else { $($else:tt)* } ) => { $($then)* };
}
pub use if_output_pin;

#[doc(hidden)]
#[macro_export]
macro_rules! gpio {
Expand Down Expand Up @@ -1431,25 +1443,39 @@ macro_rules! gpio {
}
}

procmacros::make_gpio_enum_dispatch_macro!(
handle_gpio_output
{ InputOutputAnalogTouch, InputOutputAnalog, InputOutput, }
{
$(
$type,$gpionum
)+
// These macros call the code block on the actually contained GPIO pin.

#[doc(hidden)]
#[macro_export]
macro_rules! handle_gpio_output {
($this:ident, $inner:ident, $code:tt) => {
match $this {
$(
ErasedPin::[<Gpio $gpionum >]($inner) => if_output_pin!($type, {
$code
} else {{
let _ = $inner;
panic!("Unsupported")
}}),
)+
}
}
);

procmacros::make_gpio_enum_dispatch_macro!(
handle_gpio_input
{ InputOutputAnalogTouch, InputOutputAnalog, InputOutput, InputOnlyAnalog }
{
$(
$type,$gpionum
)+
}

#[doc(hidden)]
#[macro_export]
macro_rules! handle_gpio_input {
($this:ident, $inner:ident, $code:tt) => {
match $this {
$(
ErasedPin::[<Gpio $gpionum >]($inner) => $code
)+
}
}
);
}

pub(crate) use handle_gpio_output;
pub(crate) use handle_gpio_input;
}
};
}
Expand Down

0 comments on commit 0b02233

Please sign in to comment.