Skip to content

Commit 5a22740

Browse files
committed
wrap tuple
1 parent 545318b commit 5a22740

File tree

1 file changed

+23
-29
lines changed

1 file changed

+23
-29
lines changed

src/gpio/outport.rs

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
use super::*;
22

3+
/// Convert tuple or array of pins to output port
34
pub trait OutPort {
45
type Target;
56
fn outport(self) -> Self::Target;
67
}
78

89
macro_rules! out_port {
9-
( $name:ident => $n:literal, ( $($i:literal),+ ), ( $($N:ident),+ ), ( $($d:ident),* )) => {
10-
pub struct $name<const P: char $(, const $N: u8)+> {
11-
$(pub $d: Pin<P, $N, Output<PushPull>>,)+
12-
}
10+
( $name:ident => $n:literal, ( $($i:literal),+ ), ( $($N:ident),+ )) => {
11+
pub struct $name<const P: char $(, const $N: u8)+> (
12+
pub ($(Pin<P, $N, Output<PushPull>>,)+)
13+
);
1314

1415
impl<const P: char $(, const $N: u8)+> OutPort for ($(Pin<P, $N, Output<PushPull>>),+) {
1516
type Target = $name<P $(, $N)+>;
1617
fn outport(self) -> Self::Target {
17-
let ($($d),+) = self;
18-
Self::Target { $($d),+ }
18+
$name(self)
1919
}
2020
}
2121

22-
#[allow(clippy::too_many_arguments)]
22+
/// Wrapper for tuple of `Pin`s
2323
impl<const P: char $(, const $N: u8)+> $name<P $(, $N)+> {
24-
pub const fn new(
25-
$($d: Pin<P, $N, Output<PushPull>>,)+
26-
) -> Self {
27-
Self { $($d),+ }
28-
}
2924
const fn mask() -> u32 {
3025
0 $( | (1 << { $N }))+
3126
}
@@ -43,7 +38,7 @@ macro_rules! out_port {
4338
}
4439
}
4540

46-
/// Sets all pins to `PinState::High`
41+
/// Set all pins to `PinState::High`
4742
pub fn all_high(&mut self) {
4843
unsafe {
4944
(*Gpio::<P>::ptr())
@@ -52,7 +47,7 @@ macro_rules! out_port {
5247
}
5348
}
5449

55-
/// Sets all pins to `PinState::Low`
50+
/// Reset all pins to `PinState::Low`
5651
pub fn all_low(&mut self) {
5752
unsafe {
5853
(*Gpio::<P>::ptr())
@@ -64,14 +59,15 @@ macro_rules! out_port {
6459
}
6560
}
6661

67-
out_port!(OutPort2 => 2, (0, 1), (N0, N1), (d0, d1));
68-
out_port!(OutPort3 => 3, (0, 1, 2), (N0, N1, N2), (d0, d1, d2));
69-
out_port!(OutPort4 => 4, (0, 1, 2, 3), (N0, N1, N2, N3), (d0, d1, d2, d3));
70-
out_port!(OutPort5 => 5, (0, 1, 2, 3, 4), (N0, N1, N2, N3, N4), (d0, d1, d2, d3, d4));
71-
out_port!(OutPort6 => 6, (0, 1, 2, 3, 4, 5), (N0, N1, N2, N3, N4, N5), (d0, d1, d2, d3, d4, d5));
72-
out_port!(OutPort7 => 7, (0, 1, 2, 3, 4, 5, 6), (N0, N1, N2, N3, N4, N5, N6), (d0, d1, d2, d3, d4, d5, d6));
73-
out_port!(OutPort8 => 8, (0, 1, 2, 3, 4, 5, 6, 7), (N0, N1, N2, N3, N4, N5, N6, N7), (d0, d1, d2, d3, d4, d5, d6, d7));
62+
out_port!(OutPort2 => 2, (0, 1), (N0, N1));
63+
out_port!(OutPort3 => 3, (0, 1, 2), (N0, N1, N2));
64+
out_port!(OutPort4 => 4, (0, 1, 2, 3), (N0, N1, N2, N3));
65+
out_port!(OutPort5 => 5, (0, 1, 2, 3, 4), (N0, N1, N2, N3, N4));
66+
out_port!(OutPort6 => 6, (0, 1, 2, 3, 4, 5), (N0, N1, N2, N3, N4, N5));
67+
out_port!(OutPort7 => 7, (0, 1, 2, 3, 4, 5, 6), (N0, N1, N2, N3, N4, N5, N6));
68+
out_port!(OutPort8 => 8, (0, 1, 2, 3, 4, 5, 6, 7), (N0, N1, N2, N3, N4, N5, N6, N7));
7469

70+
/// Wrapper for array of `PartiallyErasedPin`s
7571
pub struct OutPortArray<const P: char, const SIZE: usize>(pub [PEPin<P, Output<PushPull>>; SIZE]);
7672

7773
impl<const P: char, const SIZE: usize> OutPort for [PEPin<P, Output<PushPull>>; SIZE] {
@@ -84,18 +80,16 @@ impl<const P: char, const SIZE: usize> OutPort for [PEPin<P, Output<PushPull>>;
8480
impl<const P: char, const SIZE: usize> OutPortArray<P, SIZE> {
8581
fn mask(&self) -> u32 {
8682
let mut msk = 0;
87-
let mut iter = self.0.iter();
88-
while let Some(pin) = iter.next() {
83+
for pin in self.0.iter() {
8984
msk |= 1 << pin.i;
9085
}
9186
msk
9287
}
9388
fn value_for_write_bsrr(&self, val: u32) -> u32 {
9489
let mut msk = 0;
95-
let mut iter = 0..SIZE;
96-
while let Some(pin) = iter.next() {
97-
let n = self.0[pin].i;
98-
msk |= 1 << (1 << (if val & (1 << pin) != 0 { n } else { n + 16 }));
90+
for (idx, pin) in self.0.iter().enumerate() {
91+
let n = pin.i;
92+
msk |= 1 << (if val & (1 << idx) != 0 { n } else { n + 16 });
9993
}
10094
msk
10195
}
@@ -110,12 +104,12 @@ impl<const P: char, const SIZE: usize> OutPortArray<P, SIZE> {
110104
}
111105
}
112106

113-
/// Sets all pins to `PinState::High`
107+
/// Set all pins to `PinState::High`
114108
pub fn all_high(&mut self) {
115109
unsafe { (*Gpio::<P>::ptr()).bsrr.write(|w| w.bits(self.mask())) }
116110
}
117111

118-
/// Sets all pins to `PinState::Low`
112+
/// Reset all pins to `PinState::Low`
119113
pub fn all_low(&mut self) {
120114
unsafe {
121115
(*Gpio::<P>::ptr())

0 commit comments

Comments
 (0)