Skip to content

Commit e7c4bf1

Browse files
committed
Replaced board-specific LED impls with a macro
1 parent 288ef28 commit e7c4bf1

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

src/led.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use e310x_hal::gpio::gpio0::Pin5;
1313
#[cfg(any(feature = "board-hifive1", feature = "board-hifive1-revb"))]
1414
use e310x_hal::gpio::gpio0::{Pin19, Pin21, Pin22};
1515
use e310x_hal::gpio::{Invert, Output, Regular};
16-
use embedded_hal::digital::v2::OutputPin;
16+
use embedded_hal::digital::v2::{OutputPin, ToggleableOutputPin};
1717

1818
#[cfg(any(feature = "board-hifive1", feature = "board-hifive1-revb"))]
1919
/// Red LED
@@ -47,36 +47,34 @@ pub trait Led {
4747

4848
/// Turns the LED on
4949
fn on(&mut self);
50-
}
51-
52-
#[cfg(any(feature = "board-hifive1", feature = "board-hifive1-revb"))]
53-
impl Led for RED {
54-
fn off(&mut self) {
55-
self.set_low().unwrap();
56-
}
5750

58-
fn on(&mut self) {
59-
self.set_high().unwrap();
60-
}
51+
/// Toggles the LED state
52+
fn toggle(&mut self);
6153
}
6254

63-
#[cfg(any(feature = "board-hifive1", feature = "board-hifive1-revb"))]
64-
impl Led for GREEN {
65-
fn off(&mut self) {
66-
self.set_low().unwrap();
67-
}
55+
/// Macro to implement the Led trait for each of the board LEDs
56+
macro_rules! led_impl {
57+
($($LEDTYPE:ident),+) => {
58+
$(
59+
impl Led for $LEDTYPE {
60+
fn off(&mut self) {
61+
self.set_low().unwrap();
62+
}
63+
64+
fn on(&mut self) {
65+
self.set_high().unwrap();
66+
}
6867

69-
fn on(&mut self) {
70-
self.set_high().unwrap();
68+
fn toggle(&mut self) {
69+
ToggleableOutputPin::toggle(self).unwrap();
70+
}
71+
}
72+
)+
7173
}
7274
}
7375

74-
impl Led for BLUE {
75-
fn off(&mut self) {
76-
self.set_low().unwrap();
77-
}
76+
/// Call the macro for each LED
77+
#[cfg(any(feature = "board-hifive1", feature = "board-hifive1-revb"))]
78+
led_impl!(RED, GREEN);
7879

79-
fn on(&mut self) {
80-
self.set_high().unwrap();
81-
}
82-
}
80+
led_impl!(BLUE);

0 commit comments

Comments
 (0)