Skip to content

Commit 1a9aa5d

Browse files
committed
Initial RGBLED with example
1 parent ba9ceaa commit 1a9aa5d

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

examples/blink_rgb.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! Blinks an LED : on_time: 2 seconds and off_time: 3 seconds
2+
3+
use rust_gpiozero::*;
4+
5+
fn main() {
6+
// Create a new LED attached to Pin 17
7+
let mut led = RGBLED::new(23, 24, 25);
8+
9+
// on_time = 2 secs, off_time=3 secs
10+
led.red.blink(0.11, 0.05);
11+
led.green.blink(0.7, 0.17);
12+
led.blue.blink(0.23, 0.03);
13+
14+
// prevent program from exiting immediately
15+
led.red.wait();
16+
led.green.wait();
17+
led.blue.wait();
18+
}

src/output_devices.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,22 @@ impl DigitalOutputDevice {
256256
}
257257
}
258258

259+
pub struct RGBLED {
260+
pub red: LED,
261+
pub green: LED,
262+
pub blue: LED,
263+
}
264+
265+
impl RGBLED {
266+
pub fn new(pin_red: u8, pin_green: u8, pin_blue: u8) -> RGBLED {
267+
RGBLED {
268+
red: LED::new(pin_red),
269+
green: LED::new(pin_green),
270+
blue: LED::new(pin_blue),
271+
}
272+
}
273+
}
274+
259275
/// Represents a light emitting diode (LED)
260276
///
261277
/// # Example
@@ -740,7 +756,8 @@ impl Servo {
740756
if value >= -1.0 && value <= 1.0 {
741757
// Map value form [-1, 1] to [min_pulse_width, max_pulse_width] linearly
742758
let range: f64 = (self.max_pulse_width - self.min_pulse_width) as f64;
743-
let pulse_width: u64 = self.min_pulse_width + (((value + 1.0)/2.0) * range).round() as u64;
759+
let pulse_width: u64 =
760+
self.min_pulse_width + (((value + 1.0) / 2.0) * range).round() as u64;
744761
if self
745762
.pin
746763
.set_pwm(
@@ -751,12 +768,11 @@ impl Servo {
751768
{
752769
println!("Failed to set servo to a new position");
753770
}
754-
}
755-
else {
771+
} else {
756772
println!("set_position value must be between -1 and 1");
757773
}
758774
}
759-
775+
760776
/// Set the servo's minimum pulse width
761777
pub fn set_min_pulse_width(&mut self, value: u64) {
762778
if value >= self.max_pulse_width {
@@ -796,12 +812,8 @@ impl Servo {
796812
}
797813

798814
pub fn detach(&mut self) {
799-
if self
800-
.pin
801-
.clear_pwm()
802-
.is_err()
803-
{
815+
if self.pin.clear_pwm().is_err() {
804816
println!("Failed to detach servo")
805817
}
806-
}
818+
}
807819
}

0 commit comments

Comments
 (0)