Skip to content

Instantly share code, notes, and snippets.

@TheZoq2
Created October 10, 2019 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheZoq2/a17dc0019da2f29fd4c9480e1aa5ef48 to your computer and use it in GitHub Desktop.
Save TheZoq2/a17dc0019da2f29fd4c9480e1aa5ef48 to your computer and use it in GitHub Desktop.
pub trait Pins {
const C1: bool;
const C2: bool;
const C3: bool;
type Channels;
}
macro_rules! impl_pins {
// Expands to a struct that implements the Pins trait
// The syntax is a bit strange, but it should be generated by another
// macro anyway, so it's not a huge issue.
//
// 4 channels have to be specified as a comma separated list of $channel(...)
// Each active channel should have $pin, $channel, true; inside the parens
// Each inactive channel should have ;$unused_type, false inside
(
$( $channel:ident(
$($channel_:ty, $pin:ty, $true:expr)?
; $($unused_type:ty, $false:expr)?
)
),*
) => {
// Expand to a struct containing every active channel
impl Pins for ( $( $( $pin )? $( $unused_type )?),* ) {
// Set the channel active booleans to true or false depending on activity
$(
$(const $channel: bool = $true;)?
$(const $channel: bool = $false;)?
)*
// Create the Channels type
type Channels = ( $( $($channel_,)? )* );
}
}
}
macro_rules! expand_pwm_map {
() => {};
( ($channel:ty,$pin:ty), $( ($rest_channel:ty, $rest_pin:ty) ),* ) => {
impl_pins!(
$channel($pin, $channel, true;),
expand_pwm_map!( $( ($rest_channel, $rest_ty) ),* )
);
impl_pins!(
$channel($pin, $channel, true;),
expand_pwm_map!( $( ($rest_channel, $rest_ty) ),* )
);
}
}
macro_rules! pwm_map {
($pin1:ty, $pin2:ty, $pin3:ty, $pin4:ty) => {
}
}
pub struct C1;
pub struct C2;
pub struct C3;
pub struct C4;
pub struct P1;
pub struct P2;
pub struct P3;
pub struct P4;
// pwm_map!(S1, S2, S3, S4);
impl_pins!{C1(P1, C1, true;), C2(;(), false), C3(;(), false)}
impl_pins!{C1(P1, C1, true;), C2(;(), false), C3(P3, C3, true;)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment