diff --git a/src/led.rs b/src/led.rs index d2c1b27..5530d19 100644 --- a/src/led.rs +++ b/src/led.rs @@ -13,7 +13,7 @@ use e310x_hal::gpio::gpio0::Pin5; #[cfg(any(feature = "board-hifive1", feature = "board-hifive1-revb"))] use e310x_hal::gpio::gpio0::{Pin19, Pin21, Pin22}; use e310x_hal::gpio::{Invert, Output, Regular}; -use embedded_hal::digital::v2::OutputPin; +use embedded_hal::digital::v2::{OutputPin, ToggleableOutputPin}; #[cfg(any(feature = "board-hifive1", feature = "board-hifive1-revb"))] /// Red LED @@ -47,36 +47,34 @@ pub trait Led { /// Turns the LED on fn on(&mut self); -} - -#[cfg(any(feature = "board-hifive1", feature = "board-hifive1-revb"))] -impl Led for RED { - fn off(&mut self) { - self.set_low().unwrap(); - } - fn on(&mut self) { - self.set_high().unwrap(); - } + /// Toggles the LED state + fn toggle(&mut self); } -#[cfg(any(feature = "board-hifive1", feature = "board-hifive1-revb"))] -impl Led for GREEN { - fn off(&mut self) { - self.set_low().unwrap(); - } +/// Macro to implement the Led trait for each of the board LEDs +macro_rules! led_impl { + ($($LEDTYPE:ident),+) => { + $( + impl Led for $LEDTYPE { + fn off(&mut self) { + self.set_low().unwrap(); + } + + fn on(&mut self) { + self.set_high().unwrap(); + } - fn on(&mut self) { - self.set_high().unwrap(); + fn toggle(&mut self) { + ToggleableOutputPin::toggle(self).unwrap(); + } + } + )+ } } -impl Led for BLUE { - fn off(&mut self) { - self.set_low().unwrap(); - } +/// Call the macro for each LED +#[cfg(any(feature = "board-hifive1", feature = "board-hifive1-revb"))] +led_impl!(RED, GREEN); - fn on(&mut self) { - self.set_high().unwrap(); - } -} +led_impl!(BLUE); diff --git a/src/stdout.rs b/src/stdout.rs index e683f36..68779d4 100644 --- a/src/stdout.rs +++ b/src/stdout.rs @@ -19,8 +19,8 @@ struct SerialWrapper(Tx); impl core::fmt::Write for SerialWrapper { fn write_str(&mut self, s: &str) -> fmt::Result { for byte in s.as_bytes() { - if *byte == '\n' as u8 { - let res = block!(self.0.write('\r' as u8)); + if *byte == b'\n' { + let res = block!(self.0.write(b'\r')); if res.is_err() { return Err(::core::fmt::Error); @@ -53,7 +53,7 @@ pub fn configure( interrupt::free(|_| unsafe { STDOUT.replace(SerialWrapper(tx)); }); - return rx; + rx } /// Writes string to stdout