1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! On-board RGB leds
//!
//! - Red = PC13
//! - Green = PA1
//! - Blue = PA2
use embedded_hal::digital::v2::{OutputPin, StatefulOutputPin};
use gd32vf103xx_hal::gpio::gpioc::PC13;
use gd32vf103xx_hal::gpio::gpioa::{PA1, PA2};
use gd32vf103xx_hal::gpio::{Output, PushPull, Active};

/// Red LED
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 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 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::new(red);
    let green: GREEN = GREEN::new(green);
    let blue: BLUE = BLUE::new(blue);
    (red, green, blue)
}

/// Generic LED
pub trait Led {
    /// Turns the LED off
    fn off(&mut self);

    /// Turns the LED on
    fn on(&mut self);

    /// Checks the LED status
    fn is_on(&mut self) -> bool;
}

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

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

    fn is_on(&mut self) -> bool {
        self.port.is_set_low().unwrap()
    }
}

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

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

    fn is_on(&mut self) -> bool {
        self.port.is_set_low().unwrap()
    }
}

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

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

    fn is_on(&mut self) -> bool {
        self.port.is_set_low().unwrap()
    }
}