Skip to content

Commit f955816

Browse files
authored
Merge pull request #11 from fplust/led_new
add new method to each rgb led
2 parents 6eade74 + 068d799 commit f955816

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

src/led.rs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,53 @@ use gd32vf103xx_hal::gpio::gpioa::{PA1, PA2};
99
use gd32vf103xx_hal::gpio::{Output, PushPull, Active};
1010

1111
/// Red LED
12-
pub type RED = PC13<Output<PushPull>>;
12+
pub struct RED {
13+
port: PC13<Output<PushPull>>
14+
}
15+
16+
impl RED {
17+
pub fn new<T: Active>(port: PC13<T>) -> Self {
18+
Self {
19+
port: port.into_push_pull_output()
20+
}
21+
}
22+
}
1323

1424
/// Green LED
15-
pub type GREEN = PA1<Output<PushPull>>;
25+
pub struct GREEN {
26+
port: PA1<Output<PushPull>>
27+
}
28+
29+
impl GREEN {
30+
pub fn new<T: Active>(port: PA1<T>) -> Self {
31+
Self {
32+
port: port.into_push_pull_output()
33+
}
34+
}
35+
}
1636

1737
/// Blue LED
18-
pub type BLUE = PA2<Output<PushPull>>;
38+
pub struct BLUE {
39+
port: PA2<Output<PushPull>>
40+
}
41+
42+
impl BLUE {
43+
pub fn new<T: Active>(port: PA2<T>) -> Self {
44+
Self {
45+
port: port.into_push_pull_output()
46+
}
47+
}
48+
}
1949

2050
/// Returns RED, GREEN and BLUE LEDs.
2151
pub fn rgb<X, Y, Z>(
2252
red: PC13<X>, green: PA1<Y>, blue: PA2<Z>
2353
) -> (RED, GREEN, BLUE)
2454
where X: Active, Y: Active, Z: Active
2555
{
26-
let red: RED = red.into_push_pull_output();
27-
let green: GREEN = green.into_push_pull_output();
28-
let blue: BLUE = blue.into_push_pull_output();
56+
let red: RED = RED::new(red);
57+
let green: GREEN = GREEN::new(green);
58+
let blue: BLUE = BLUE::new(blue);
2959
(red, green, blue)
3060
}
3161

@@ -40,30 +70,30 @@ pub trait Led {
4070

4171
impl Led for RED {
4272
fn off(&mut self) {
43-
self.set_high().unwrap();
73+
self.port.set_high().unwrap();
4474
}
4575

4676
fn on(&mut self) {
47-
self.set_low().unwrap();
77+
self.port.set_low().unwrap();
4878
}
4979
}
5080

5181
impl Led for GREEN {
5282
fn off(&mut self) {
53-
self.set_high().unwrap();
83+
self.port.set_high().unwrap();
5484
}
5585

5686
fn on(&mut self) {
57-
self.set_low().unwrap();
87+
self.port.set_low().unwrap();
5888
}
5989
}
6090

6191
impl Led for BLUE {
6292
fn off(&mut self) {
63-
self.set_high().unwrap();
93+
self.port.set_high().unwrap();
6494
}
6595

6696
fn on(&mut self) {
67-
self.set_low().unwrap();
97+
self.port.set_low().unwrap();
6898
}
6999
}

0 commit comments

Comments
 (0)