Skip to content

Commit e273252

Browse files
Remove unnecessary runtime state from pin types
Also makes required fields private, replacing them with getters. Fixes #46
1 parent 96981c8 commit e273252

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

examples/button_irq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn main() -> ! {
4646

4747
// Configure the external interrupt on the falling edge for the pin 0.
4848
let exti = dp.EXTI;
49-
exti.listen(&mut syscfg, button.port, button.i, TriggerEdge::Falling);
49+
exti.listen(&mut syscfg, button.port(), button.pin_number(), TriggerEdge::Falling);
5050

5151
// Store the external interrupt and LED in mutex reffcells to make them
5252
// available from the interrupt.

examples/button_irq_rtfm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const APP: () = {
3535

3636
// Configure the external interrupt on the falling edge for the pin 0.
3737
let exti = device.EXTI;
38-
exti.listen(&mut syscfg, button.port, button.i, TriggerEdge::Falling);
38+
exti.listen(&mut syscfg, button.port(), button.pin_number(), TriggerEdge::Falling);
3939

4040
// Return the initialised resources.
4141
init::LateResources {

examples/exti_wakeup.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ fn main() -> ! {
3636

3737
exti.listen(
3838
&mut syscfg,
39-
button.port,
40-
button.i,
39+
button.port(),
40+
button.pin_number(),
4141
exti::TriggerEdge::Falling,
4242
);
4343

4444
loop {
4545
exti.wait_for_irq(
46-
button.i,
46+
button.pin_number(),
4747
pwr.stop_mode(
4848
&mut scb,
4949
&mut rcc,

src/gpio.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ macro_rules! gpio {
118118
Parts {
119119
$(
120120
$pxi: $PXi {
121-
i: $i,
122-
port: Port::$PXx,
123-
_mode: PhantomData
121+
_mode: PhantomData,
124122
},
125123
)+
126124
}
@@ -129,11 +127,22 @@ macro_rules! gpio {
129127

130128
/// Partially erased pin
131129
pub struct $PXx<MODE> {
132-
pub i: u8,
133-
pub port: Port,
130+
i: u8,
134131
_mode: PhantomData<MODE>,
135132
}
136133

134+
impl<MODE> $PXx<MODE> {
135+
/// Returns the port this pin is part of.
136+
pub fn port(&self) -> Port {
137+
Port::$PXx
138+
}
139+
140+
/// Returns this pin's number inside its port.
141+
pub fn pin_number(&self) -> u8 {
142+
self.i
143+
}
144+
}
145+
137146
impl<MODE> OutputPin for $PXx<Output<MODE>> {
138147
type Error = super::Error;
139148

@@ -198,11 +207,21 @@ macro_rules! gpio {
198207
$(
199208
/// Pin
200209
pub struct $PXi<MODE> {
201-
pub i: u8,
202-
pub port: Port,
203210
_mode: PhantomData<MODE>,
204211
}
205212

213+
impl<MODE> $PXi<MODE> {
214+
/// Returns the port this pin is part of.
215+
pub fn port(&self) -> Port {
216+
Port::$PXx
217+
}
218+
219+
/// Returns this pin's number inside its port.
220+
pub fn pin_number(&self) -> u8 {
221+
$i
222+
}
223+
}
224+
206225
impl<MODE> $PXi<MODE> {
207226
/// Configures the pin to operate as a floating input pin
208227
pub fn into_floating_input(
@@ -218,8 +237,6 @@ macro_rules! gpio {
218237
})
219238
};
220239
$PXi {
221-
i: $i,
222-
port: Port::$PXx,
223240
_mode: PhantomData
224241
}
225242
}
@@ -238,8 +255,6 @@ macro_rules! gpio {
238255
})
239256
};
240257
$PXi {
241-
i: $i,
242-
port: Port::$PXx,
243258
_mode: PhantomData
244259
}
245260
}
@@ -258,8 +273,6 @@ macro_rules! gpio {
258273
})
259274
};
260275
$PXi {
261-
i: $i,
262-
port: Port::$PXx,
263276
_mode: PhantomData
264277
}
265278
}
@@ -278,8 +291,6 @@ macro_rules! gpio {
278291
});
279292
}
280293
$PXi {
281-
i: $i,
282-
port: Port::$PXx,
283294
_mode: PhantomData
284295
}
285296
}
@@ -301,8 +312,6 @@ macro_rules! gpio {
301312
})
302313
};
303314
$PXi {
304-
i: $i,
305-
port: Port::$PXx,
306315
_mode: PhantomData
307316
}
308317
}
@@ -324,8 +333,6 @@ macro_rules! gpio {
324333
})
325334
};
326335
$PXi {
327-
i: $i,
328-
port: Port::$PXx,
329336
_mode: PhantomData
330337
}
331338
}
@@ -372,7 +379,6 @@ macro_rules! gpio {
372379
pub fn downgrade(self) -> $PXx<Output<MODE>> {
373380
$PXx {
374381
i: $i,
375-
port: Port::$PXx,
376382
_mode: self._mode,
377383
}
378384
}
@@ -433,7 +439,6 @@ macro_rules! gpio {
433439
pub fn downgrade(self) -> $PXx<Input<MODE>> {
434440
$PXx {
435441
i: $i,
436-
port: Port::$PXx,
437442
_mode: self._mode,
438443
}
439444
}

0 commit comments

Comments
 (0)