Skip to content

Commit 39b33a5

Browse files
committed
impl Pins
1 parent 91e6332 commit 39b33a5

File tree

1 file changed

+35
-71
lines changed

1 file changed

+35
-71
lines changed

src/timer/pins.rs

+35-71
Original file line numberDiff line numberDiff line change
@@ -21,53 +21,47 @@ impl<CH1, CH2> From<(CH1, CH2)> for InPins<CH1, CH2> {
2121
}
2222
}
2323

24-
pub struct Pins4<CH1, CH2, CH3, CH4> {
25-
pub c1: CH1,
26-
pub c2: CH2,
27-
pub c3: CH3,
28-
pub c4: CH4,
29-
}
30-
31-
impl<CH1, CH2, CH3, CH4> From<(CH1, CH2, CH3, CH4)> for Pins4<CH1, CH2, CH3, CH4> {
32-
fn from(value: (CH1, CH2, CH3, CH4)) -> Self {
33-
Self {
34-
c1: value.0,
35-
c2: value.1,
36-
c3: value.2,
37-
c4: value.3,
24+
pinsx!(Pins2: (CH1, c1, C1), (CH2, c2, C2));
25+
pinsx!(Pins3: (CH1, c1, C1), (CH2, c2, C2), (CH3, c3, C3));
26+
pinsx!(Pins4: (CH1, c1, C1), (CH2, c2, C2), (CH3, c3, C3), (CH4, c4, C4));
27+
28+
macro_rules! pinsx {
29+
($name:ident: $(($CH:ident, $c:ident, $C:ident)),+) => {
30+
pub struct $name<$($CH),+> {
31+
$(
32+
pub $c: $CH,
33+
)+
3834
}
39-
}
40-
}
4135

42-
pub struct Pins3<CH1, CH2, CH3> {
43-
pub c1: CH1,
44-
pub c2: CH2,
45-
pub c3: CH3,
46-
}
47-
48-
impl<CH1, CH2, CH3> From<(CH1, CH2, CH3)> for Pins3<CH1, CH2, CH3> {
49-
fn from(value: (CH1, CH2, CH3)) -> Self {
50-
Self {
51-
c1: value.0,
52-
c2: value.1,
53-
c3: value.2,
36+
impl<$($CH),+> From<($($CH),+)> for $name<$($CH),+> {
37+
fn from(value: ($($CH),+)) -> Self {
38+
let ($($c),+) = value;
39+
Self {
40+
$(
41+
$c,
42+
)+
43+
}
44+
}
5445
}
55-
}
56-
}
5746

58-
pub struct Pins2<CH1, CH2> {
59-
pub c1: CH1,
60-
pub c2: CH2,
61-
}
62-
63-
impl<CH1, CH2> From<(CH1, CH2)> for Pins2<CH1, CH2> {
64-
fn from(value: (CH1, CH2)) -> Self {
65-
Self {
66-
c1: value.0,
67-
c2: value.1,
47+
impl<TIM, $($CH),+> Pins<TIM> for $name<$($CH),+>
48+
where
49+
$(
50+
$CH: Pins<TIM>,
51+
)+
52+
{
53+
const C1: bool = false $(|| $CH::C1)+;
54+
const C2: bool = false $(|| $CH::C2)+;
55+
const C3: bool = false $(|| $CH::C3)+;
56+
const C4: bool = false $(|| $CH::C4)+;
57+
type Channels = ($($CH::Channels),+);
58+
fn split() -> Self::Channels {
59+
($($CH::split()),+)
60+
}
6861
}
69-
}
62+
};
7063
}
64+
use pinsx;
7165

7266
#[cfg(any(feature = "stm32f100", feature = "stm32f103", feature = "connectivity"))]
7367
pub mod tim1 {
@@ -375,17 +369,6 @@ macro_rules! remap_combo {
375369
($TIM:ty; $C1:ident, $C2:ident, $C3:ident, $C4:ident: [
376370
$($P0:ident, $P1:ident, $P2:ident, $P3:ident $( => $MAPR:ident { $remapex:expr })?;)+
377371
]) => {
378-
impl Pins<$TIM> for Pins4<Ch1, Ch2, Ch3, Ch4> {
379-
const $C1: bool = true;
380-
const $C2: bool = true;
381-
const $C3: bool = true;
382-
const $C4: bool = true;
383-
type Channels = (PwmChannel<$TIM, $C1>, PwmChannel<$TIM, $C2>, PwmChannel<$TIM, $C3>, PwmChannel<$TIM, $C4>);
384-
fn split() -> Self::Channels {
385-
(PwmChannel::new(), PwmChannel::new(), PwmChannel::new(), PwmChannel::new())
386-
}
387-
}
388-
389372
$(
390373
impl From<(gpio::$P0<Alternate>, gpio::$P1<Alternate>, gpio::$P2<Alternate>, gpio::$P3<Alternate> $(, &mut $MAPR)?)> for Pins4<Ch1, Ch2, Ch3, Ch4> {
391374
fn from(p: (gpio::$P0<Alternate>, gpio::$P1<Alternate>, gpio::$P2<Alternate>, gpio::$P3<Alternate> $(, &mut $MAPR)?)) -> Self {
@@ -410,16 +393,6 @@ macro_rules! remap_combo {
410393
($TIM:ty; ($C1:ident, $Ch1:ident), ($C2:ident, $Ch2:ident), ($C3:ident, $Ch3:ident): [
411394
$($P0:ident, $P1:ident, $P2:ident $( => $MAPR:ident { $remapex:expr })?;)+
412395
]) => {
413-
impl Pins<$TIM> for Pins3<$Ch1, $Ch2, $Ch3> {
414-
const $C1: bool = true;
415-
const $C2: bool = true;
416-
const $C3: bool = true;
417-
type Channels = (PwmChannel<$TIM, $C1>, PwmChannel<$TIM, $C2>, PwmChannel<$TIM, $C3>);
418-
fn split() -> Self::Channels {
419-
(PwmChannel::new(), PwmChannel::new(), PwmChannel::new())
420-
}
421-
}
422-
423396
$(
424397
impl From<(gpio::$P0<Alternate>, gpio::$P1<Alternate>, gpio::$P2<Alternate> $(, &mut $MAPR)?)> for Pins3<$Ch1, $Ch2, $Ch3> {
425398
fn from(p: (gpio::$P0<Alternate>, gpio::$P1<Alternate>, gpio::$P2<Alternate> $(, &mut $MAPR)?)) -> Self {
@@ -443,15 +416,6 @@ macro_rules! remap_combo {
443416
($TIM:ty; ($C1:ident, $Ch1:ident), ($C2:ident, $Ch2:ident): [
444417
$($P0:ident, $P1:ident $( => $MAPR:ident { $remapex:expr })?;)+
445418
]) => {
446-
impl Pins<$TIM> for Pins2<$Ch1, $Ch2> {
447-
const $C1: bool = true;
448-
const $C2: bool = true;
449-
type Channels = (PwmChannel<$TIM, $C1>, PwmChannel<$TIM, $C2>);
450-
fn split() -> Self::Channels {
451-
(PwmChannel::new(), PwmChannel::new())
452-
}
453-
}
454-
455419
$(
456420
impl From<(gpio::$P0<Alternate>, gpio::$P1<Alternate> $(, &mut $MAPR)?)> for Pins2<$Ch1, $Ch2> {
457421
fn from(p: (gpio::$P0<Alternate>, gpio::$P1<Alternate> $(, &mut $MAPR)?)) -> Self {

0 commit comments

Comments
 (0)