Basic RGB library for non-PWM LEDs

Dependents:   MenuExample exosite_http_example exosite_http_example FastPWM

Warning : Works with non-PWM LED (0 or 1).

A quick example on how to use it.

#include "mbed.h"
#include "RGBLed.h"

RGBLed led(LED_RED, LED_GREEN, LED_BLUE);

int main() {
    RGBLed::Color list[8] = {RGBLed::BLACK, RGBLed::RED, RGBLed::GREEN, RGBLed::BLUE, RGBLed::MAGENTA, RGBLed::CYAN, RGBLed::YELLOW, RGBLed::WHITE};
    int i = 0;

    while (true) {
        i = (i+1)%8;
        led.setColor(list[i]);
        wait_ms(100);
    }
}
Committer:
rominos2
Date:
Fri Jun 12 19:02:04 2015 +0000
Revision:
6:9b7ee7ba4109
Parent:
5:b125e5a28295
Fixed the documentation in RGBLed.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rominos2 4:176363412797 1 /*
rominos2 4:176363412797 2 Copyright (c) 2014 Romain Berrada
rominos2 4:176363412797 3
rominos2 4:176363412797 4 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
rominos2 4:176363412797 5 and associated documentation files (the "Software"), to deal in the Software without restriction,
rominos2 4:176363412797 6 including without limitation the rights to use, copy, modify, merge, publish, distribute,
rominos2 4:176363412797 7 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
rominos2 4:176363412797 8 furnished to do so, subject to the following conditions:
rominos2 4:176363412797 9
rominos2 4:176363412797 10 The above copyright notice and this permission notice shall be included in all copies or
rominos2 4:176363412797 11 substantial portions of the Software.
rominos2 4:176363412797 12
rominos2 4:176363412797 13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
rominos2 4:176363412797 14 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
rominos2 4:176363412797 15 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
rominos2 4:176363412797 16 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
rominos2 4:176363412797 17 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
rominos2 4:176363412797 18 */
rominos2 4:176363412797 19
rominos2 5:b125e5a28295 20 #ifndef INCLUDE_RGBLED_H
rominos2 5:b125e5a28295 21 #define INCLUDE_RGBLED_H
rominos2 5:b125e5a28295 22
rominos2 3:be0a3c2ec426 23 #include "mbed.h"
rominos2 3:be0a3c2ec426 24
rominos2 3:be0a3c2ec426 25 /** A light RGB LED Class \n
rominos2 3:be0a3c2ec426 26 Warning : This library is for non-PWN LED \n
rominos2 3:be0a3c2ec426 27 Here is an quick hello-world class that makes the LED blink with all colors. \n
rominos2 3:be0a3c2ec426 28 @code
rominos2 3:be0a3c2ec426 29 #include "mbed.h"
rominos2 6:9b7ee7ba4109 30 #include "RGBLed.h"
rominos2 3:be0a3c2ec426 31
rominos2 3:be0a3c2ec426 32 RGBLed led(LED_RED, LED_GREEN, LED_BLUE);
rominos2 3:be0a3c2ec426 33
rominos2 3:be0a3c2ec426 34 int main() {
rominos2 3:be0a3c2ec426 35 RGBLed::Color list[8] = {RGBLed::BLACK, RGBLed::RED, RGBLed::GREEN, RGBLed::BLUE, RGBLed::MAGENTA, RGBLed::CYAN, RGBLed::YELLOW, RGBLed::WHITE};
rominos2 3:be0a3c2ec426 36 int i = 0;
rominos2 3:be0a3c2ec426 37
rominos2 3:be0a3c2ec426 38 while (true) {
rominos2 3:be0a3c2ec426 39 i = (i+1)%8;
rominos2 3:be0a3c2ec426 40 led.setColor(list[i]);
rominos2 3:be0a3c2ec426 41 wait_ms(100);
rominos2 3:be0a3c2ec426 42 }
rominos2 3:be0a3c2ec426 43 }
rominos2 3:be0a3c2ec426 44 @endcode
rominos2 3:be0a3c2ec426 45 */
rominos2 3:be0a3c2ec426 46 class RGBLed {
rominos2 3:be0a3c2ec426 47 private:
rominos2 3:be0a3c2ec426 48 DigitalOut _red;
rominos2 3:be0a3c2ec426 49 DigitalOut _green;
rominos2 3:be0a3c2ec426 50 DigitalOut _blue;
rominos2 3:be0a3c2ec426 51
rominos2 3:be0a3c2ec426 52 public:
rominos2 3:be0a3c2ec426 53 /** RGB Color class \n
rominos2 3:be0a3c2ec426 54 Colors have been defined and are ready to use in RGBLed class
rominos2 3:be0a3c2ec426 55 */
rominos2 3:be0a3c2ec426 56 class Color {
rominos2 3:be0a3c2ec426 57 private:
rominos2 3:be0a3c2ec426 58 bool _r; /**< Red component of the Color */
rominos2 3:be0a3c2ec426 59 bool _g; /**< Green component of the Color */
rominos2 3:be0a3c2ec426 60 bool _b; /**< Blue component of the Color */
rominos2 3:be0a3c2ec426 61 Color(bool r, bool g, bool b); /**< Constructor */
rominos2 3:be0a3c2ec426 62 friend class RGBLed;
rominos2 3:be0a3c2ec426 63 };
rominos2 3:be0a3c2ec426 64
rominos2 3:be0a3c2ec426 65 /** Create a RGBLed, containing the informations about the LED pinout.
rominos2 3:be0a3c2ec426 66 @param redPin the pin linked to the Red LED
rominos2 3:be0a3c2ec426 67 @param greenPin the pin linked to the green LED
rominos2 3:be0a3c2ec426 68 @param blue the pin linked to the blue LED
rominos2 3:be0a3c2ec426 69 */
rominos2 3:be0a3c2ec426 70 RGBLed(PinName redPin, PinName greenPin, PinName bluePin);
rominos2 3:be0a3c2ec426 71
rominos2 3:be0a3c2ec426 72 /** Change the color of the LED.
rominos2 3:be0a3c2ec426 73 @param color the color to display
rominos2 3:be0a3c2ec426 74 @see RGBLed::Color
rominos2 3:be0a3c2ec426 75 */
rominos2 3:be0a3c2ec426 76 void setColor(RGBLed::Color& color);
rominos2 3:be0a3c2ec426 77
rominos2 3:be0a3c2ec426 78 static Color BLACK; /**< Black Color (no color) */
rominos2 3:be0a3c2ec426 79 static Color RED; /**< Red Color */
rominos2 3:be0a3c2ec426 80 static Color GREEN; /**< Green Color */
rominos2 3:be0a3c2ec426 81 static Color BLUE; /**< Blue Color */
rominos2 3:be0a3c2ec426 82 static Color MAGENTA; /**< Magenta Color (Red + Blue) */
rominos2 3:be0a3c2ec426 83 static Color CYAN; /**< Cyan Color (Green + Blue) */
rominos2 3:be0a3c2ec426 84 static Color YELLOW; /**< Yellow Color (Red + Green) */
rominos2 3:be0a3c2ec426 85 static Color WHITE; /**< White Color (Red + Green + Blue) */
rominos2 5:b125e5a28295 86 };
rominos2 5:b125e5a28295 87
rominos2 5:b125e5a28295 88 #endif