avr-hal icon indicating copy to clipboard operation
avr-hal copied to clipboard

Revisit board pins reexport macro

Open Rahix opened this issue 5 years ago • 1 comments

The current incarnation of the board pins reexport macro is already quite successfull at abstracting the actual port pins into something that an application developer can comfortably work with.

However, when the need arises to actually name the pin types, it gets awkward: Right now, the are no reexports in place at all so one needs to reference the type from the hal crate and lookup the real port names. As a real-world usecase, consider a global variable holding a pin:

static mut GLOBAL_PIN: Option<atmega32u4_hal::port::portc::PC7> = None;

Of course, it would be much nicer if the actual pin name, as found in the generated Pins struct could be used:

static mut GLOBAL_PIN: Option<arduino_leonardo::pins::D13> = None;

I think it shouldn't be too difficult to add this: The macro needs to emit type aliases for each pin and reference those instead of the port pin types. For this, it needs to grow more syntax to specify the new pin type identifiers. A rough sketch:

// The macro will change from (L692)
pub $name:ident: $pinport:ident::$pin:ident::$Pin:ident,
// to e.g.
pub $name:ident: $Name:ident = $pinport:ident::$pin:ident::$Pin:ident,

// so call-sites will change from
pub d0: portd::pd2::PD2,
// to
pub d0: D0 = portd::pd2::PD2,

// the macro then generates type aliases like
$(
    $(#[$pin_attr])*
    pub type $Name<MODE>: $pinport::$Pin<MODE>;
)+
// and uses them in the struct (L714)
$(
    $(#[$pin_attr])*
    pub $name: $Name<$crate::port::mode::Input<$crate::port::mode::Floating>>,
)+

Rahix avatar Oct 04 '20 10:10 Rahix

The generic pin type (port::Pin) should also be re-exported I suppose.

Rahix avatar Oct 06 '20 10:10 Rahix