Skip to content

Commit

Permalink
add new method to each rgb led
Browse files Browse the repository at this point in the history
  • Loading branch information
fplust committed Apr 15, 2020
1 parent f595db0 commit 068d799
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions src/led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,53 @@ use gd32vf103xx_hal::gpio::gpioa::{PA1, PA2};
use gd32vf103xx_hal::gpio::{Output, PushPull, Active};

/// Red LED
pub type RED = PC13<Output<PushPull>>;
pub struct RED {
port: PC13<Output<PushPull>>
}

impl RED {
pub fn new<T: Active>(port: PC13<T>) -> Self {
Self {
port: port.into_push_pull_output()
}
}
}

/// Green LED
pub type GREEN = PA1<Output<PushPull>>;
pub struct GREEN {
port: PA1<Output<PushPull>>
}

impl GREEN {
pub fn new<T: Active>(port: PA1<T>) -> Self {
Self {
port: port.into_push_pull_output()
}
}
}

/// Blue LED
pub type BLUE = PA2<Output<PushPull>>;
pub struct BLUE {
port: PA2<Output<PushPull>>
}

impl BLUE {
pub fn new<T: Active>(port: PA2<T>) -> Self {
Self {
port: port.into_push_pull_output()
}
}
}

/// Returns RED, GREEN and BLUE LEDs.
pub fn rgb<X, Y, Z>(
red: PC13<X>, green: PA1<Y>, blue: PA2<Z>
) -> (RED, GREEN, BLUE)
where X: Active, Y: Active, Z: Active
{
let red: RED = red.into_push_pull_output();
let green: GREEN = green.into_push_pull_output();
let blue: BLUE = blue.into_push_pull_output();
let red: RED = RED::new(red);
let green: GREEN = GREEN::new(green);
let blue: BLUE = BLUE::new(blue);
(red, green, blue)
}

Expand All @@ -40,30 +70,30 @@ pub trait Led {

impl Led for RED {
fn off(&mut self) {
self.set_high().unwrap();
self.port.set_high().unwrap();
}

fn on(&mut self) {
self.set_low().unwrap();
self.port.set_low().unwrap();
}
}

impl Led for GREEN {
fn off(&mut self) {
self.set_high().unwrap();
self.port.set_high().unwrap();
}

fn on(&mut self) {
self.set_low().unwrap();
self.port.set_low().unwrap();
}
}

impl Led for BLUE {
fn off(&mut self) {
self.set_high().unwrap();
self.port.set_high().unwrap();
}

fn on(&mut self) {
self.set_low().unwrap();
self.port.set_low().unwrap();
}
}

0 comments on commit 068d799

Please sign in to comment.