adamgreig / assign-resources

Rust macro to split up fields of a Peripherals struct into new structs
31 stars 3 forks source link

Add Optional Type Aliasing #1

Closed AdinAck closed 11 months ago

AdinAck commented 11 months ago

Motivation

Functions that refer to types inside "resources" are not helped by the assign_resources macro because the types they refer to must still be manually changed.

Solution

Ability to add type aliases if desired like so:

assign_resources! {
    status: StatusResources {
        led: PA6
    }
    feedback: FeedbackResources {
        adc: ADC,
        sense: PA2
    }
    hb: HalfBridgeResources {
        timer: TIM1 = PWMTimer,
        control: PA8,
        sync: PA7,
        enable: PA12 = HBEnablePin
    }
    serial: SerialResources {
        uart: USART1,
        rx: PA10,
        tx: PA9
    }
}

And use them:

fn setup_hb<'a>(hb: HalfBridgeResources) -> (ComplementaryPwm<'a, PWMTimer>, Output<'a, HBEnablePin>) { ... }

@adamgreig would you like me to change the README/docstring in lib.rs to demonstrate this option?

adamgreig commented 11 months ago

Thanks, looks good! Yes, please add an example of this to the README and docstring. I think ideally just add the alias to one or two of the existing pins, but whatever you think looks best.