Skip to content

Commit 2c279c4

Browse files
authored
Merge pull request #26 from greenlsi/master
Substitute multiple LED impl calls with a generic macro & add the toggle() method
2 parents 288ef28 + a3c0965 commit 2c279c4

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
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);

src/stdout.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ struct SerialWrapper(Tx<UART0>);
1919
impl core::fmt::Write for SerialWrapper {
2020
fn write_str(&mut self, s: &str) -> fmt::Result {
2121
for byte in s.as_bytes() {
22-
if *byte == '\n' as u8 {
23-
let res = block!(self.0.write('\r' as u8));
22+
if *byte == b'\n' {
23+
let res = block!(self.0.write(b'\r'));
2424

2525
if res.is_err() {
2626
return Err(::core::fmt::Error);
@@ -53,7 +53,7 @@ pub fn configure<X, Y>(
5353
interrupt::free(|_| unsafe {
5454
STDOUT.replace(SerialWrapper(tx));
5555
});
56-
return rx;
56+
rx
5757
}
5858

5959
/// Writes string to stdout

0 commit comments

Comments
 (0)