Skip to content

Commit

Permalink
Merge pull request #26 from greenlsi/master
Browse files Browse the repository at this point in the history
Substitute multiple LED impl calls with a generic macro & add the toggle() method
  • Loading branch information
almindor authored Oct 19, 2022
2 parents 288ef28 + a3c0965 commit 2c279c4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
50 changes: 24 additions & 26 deletions src/led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
6 changes: 3 additions & 3 deletions src/stdout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ struct SerialWrapper(Tx<UART0>);
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);
Expand Down Expand Up @@ -53,7 +53,7 @@ pub fn configure<X, Y>(
interrupt::free(|_| unsafe {
STDOUT.replace(SerialWrapper(tx));
});
return rx;
rx
}

/// Writes string to stdout
Expand Down

0 comments on commit 2c279c4

Please sign in to comment.