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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2019 Alex Crawford
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! RGB LEDs
//!
//! Various types of RGB LEDs.
//!
//! # Examples
//!
//! ```
//! # use embedded_hal::digital::OutputPin;
//! #
//! # struct Pin {}
//! #
//! # impl OutputPin for Pin {
//! #     fn set_low(&mut self){}
//! #     fn set_high(&mut self){}
//! # }
//! #
//! # let r = Pin{};
//! # let g = Pin{};
//! # let b = Pin{};
//! #
//! use led::LED;
//! use led::rgb::{Color, CommonAnodeLED};
//!
//! let mut led = CommonAnodeLED::new(r, g, b);
//!
//! led.set(Color::Green);
//! ```
//!
//! ```
//! # use embedded_hal::digital::OutputPin;
//! #
//! # struct Pin {}
//! #
//! # impl OutputPin for Pin {
//! #     fn set_low(&mut self){}
//! #     fn set_high(&mut self){}
//! # }
//! #
//! # let r1 = Pin{};
//! # let g1 = Pin{};
//! # let b1 = Pin{};
//! # let r2 = Pin{};
//! # let g2 = Pin{};
//! # let b2 = Pin{};
//! #
//! use led::LED;
//! use led::rgb::{Color, CommonAnodeLED, CommonCathodeLED, RGB};
//!
//! let mut leds: [&mut RGB; 2] = [
//!     &mut CommonAnodeLED::new(r1, g1, b1),
//!     &mut CommonCathodeLED::new(r2, g2, b2),
//! ];
//!
//! for led in leds.iter_mut() {
//!     led.set(Color::Red)
//! }
//! ```

use core::marker::PhantomData;
use embedded_hal::digital::OutputPin;

/// The set of primary colors and secondary colors that can be created by an RGB LED along with
/// black and white.
pub enum Color {
    Red,
    Green,
    Blue,
    Yellow,
    Cyan,
    Magenta,
    White,
    Black,
}

/// A common anode LED.
pub type CommonAnodeLED<R, G, B> = LED<CommonAnode, R, G, B>;

/// A common cathode LED.
pub type CommonCathodeLED<R, G, B> = LED<CommonCathode, R, G, B>;

/// An RGB LED; either common anode or common cathode.
///
/// # Examples
///
/// ```
/// # use embedded_hal::digital::OutputPin;
/// #
/// # struct Pin {}
/// #
/// # impl OutputPin for Pin {
/// #     fn set_low(&mut self){}
/// #     fn set_high(&mut self){}
/// # }
/// #
/// # let r1 = Pin{};
/// # let g1 = Pin{};
/// # let b1 = Pin{};
/// # let r2 = Pin{};
/// # let g2 = Pin{};
/// # let b2 = Pin{};
/// #
/// use led::rgb::{Color, CommonAnodeLED, CommonCathodeLED, RGB};
///
/// let mut leds: [&mut RGB; 2] = [
///     &mut CommonAnodeLED::new(r1, g1, b1),
///     &mut CommonCathodeLED::new(r2, g2, b2),
/// ];
///
/// for led in leds.iter_mut() {
///     led.set(Color::Red)
/// }
/// ```
pub trait RGB: crate::LED<Input = Color> {}
impl<L> RGB for L where L: crate::LED<Input = Color> {}

/// An RGB LED
///
/// The RGB LED is represented by three owned instances of `embedded_hal::digital::OutputPin` and a
/// polarity (common anode or common cathode). Because the outputs are binary, only eight colors
/// can be presented: primary colors, secondary colors, white, and black.
pub struct LED<C, R, G, B> {
    common: PhantomData<C>,
    red: R,
    green: G,
    blue: B,
}

impl<C, R, G, B> LED<C, R, G, B>
where
    C: Common,
    R: OutputPin,
    G: OutputPin,
    B: OutputPin,
{
    /// Creates a new RGB LED given three GPIOs.
    pub fn new(red: R, green: G, blue: B) -> LED<C, R, G, B> {
        LED {
            common: PhantomData,
            red,
            green,
            blue,
        }
    }
}

impl<C, R, G, B> crate::LED for LED<C, R, G, B>
where
    C: Common,
    R: OutputPin,
    G: OutputPin,
    B: OutputPin,
{
    type Input = Color;

    /// Sets the RGB LED to the specified color.
    fn set(&mut self, color: Color) {
        match color {
            Color::Red => {
                C::enable(&mut self.red);
                C::disable(&mut self.green);
                C::disable(&mut self.blue);
            }
            Color::Green => {
                C::disable(&mut self.red);
                C::enable(&mut self.green);
                C::disable(&mut self.blue);
            }
            Color::Blue => {
                C::disable(&mut self.red);
                C::disable(&mut self.green);
                C::enable(&mut self.blue);
            }
            Color::Yellow => {
                C::enable(&mut self.red);
                C::enable(&mut self.green);
                C::disable(&mut self.blue);
            }
            Color::Cyan => {
                C::disable(&mut self.red);
                C::enable(&mut self.green);
                C::enable(&mut self.blue);
            }
            Color::Magenta => {
                C::enable(&mut self.red);
                C::disable(&mut self.green);
                C::enable(&mut self.blue);
            }
            Color::White => {
                C::enable(&mut self.red);
                C::enable(&mut self.green);
                C::enable(&mut self.blue);
            }
            Color::Black => {
                C::disable(&mut self.red);
                C::disable(&mut self.green);
                C::disable(&mut self.blue);
            }
        }
    }
}

/// The polarity of the LED; either anode or cathode.
pub trait Common {
    /// Enables the pin output.
    fn enable<P: OutputPin>(pin: &mut P);

    /// Disables the pin output.
    fn disable<P: OutputPin>(pin: &mut P);
}

/// A marker type that represents a common anode connection.
///
/// # Examples
///
/// ```
/// # use embedded_hal::digital::OutputPin;
/// #
/// # struct Pin {}
/// #
/// # impl OutputPin for Pin {
/// #     fn set_low(&mut self) {}
/// #     fn set_high(&mut self) {}
/// # }
/// #
/// # let r = Pin{};
/// # let g = Pin{};
/// # let b = Pin{};
/// #
/// use led::rgb::{CommonAnode, LED};
///
/// let led: LED<CommonAnode, _, _, _> = LED::new(r, g, b);
/// ```
pub struct CommonAnode {
    private: PhantomData<()>,
}

/// A marker type that represents a common cathode connection.
///
/// # Examples
///
/// ```
/// # use embedded_hal::digital::OutputPin;
/// #
/// # struct Pin {}
/// #
/// # impl OutputPin for Pin {
/// #     fn set_low(&mut self) {}
/// #     fn set_high(&mut self) {}
/// # }
/// #
/// # let r = Pin{};
/// # let g = Pin{};
/// # let b = Pin{};
/// #
/// use led::rgb::{CommonCathode, LED};
///
/// let led: LED<CommonCathode, _, _, _> = LED::new(r, g, b);
/// ```
pub struct CommonCathode {
    private: PhantomData<()>,
}

impl Common for CommonAnode {
    fn enable<P: OutputPin>(pin: &mut P) {
        pin.set_low();
    }

    fn disable<P: OutputPin>(pin: &mut P) {
        pin.set_high();
    }
}

impl Common for CommonCathode {
    fn enable<P: OutputPin>(pin: &mut P) {
        pin.set_high();
    }

    fn disable<P: OutputPin>(pin: &mut P) {
        pin.set_low();
    }
}