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); } }
RGBLed.h@4:176363412797, 2014-09-03 (annotated)
- Committer:
- rominos2
- Date:
- Wed Sep 03 10:46:40 2014 +0000
- Revision:
- 4:176363412797
- Parent:
- 3:be0a3c2ec426
- Child:
- 5:b125e5a28295
Added License headers.
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:be0a3c2ec426 | 20 | #include "mbed.h" |
rominos2 | 3:be0a3c2ec426 | 21 | |
rominos2 | 3:be0a3c2ec426 | 22 | /** A light RGB LED Class \n |
rominos2 | 3:be0a3c2ec426 | 23 | Warning : This library is for non-PWN LED \n |
rominos2 | 3:be0a3c2ec426 | 24 | Here is an quick hello-world class that makes the LED blink with all colors. \n |
rominos2 | 3:be0a3c2ec426 | 25 | @code |
rominos2 | 3:be0a3c2ec426 | 26 | #include "mbed.h" |
rominos2 | 3:be0a3c2ec426 | 27 | #include "rgb.h" |
rominos2 | 3:be0a3c2ec426 | 28 | |
rominos2 | 3:be0a3c2ec426 | 29 | RGBLed led(LED_RED, LED_GREEN, LED_BLUE); |
rominos2 | 3:be0a3c2ec426 | 30 | |
rominos2 | 3:be0a3c2ec426 | 31 | int main() { |
rominos2 | 3:be0a3c2ec426 | 32 | RGBLed::Color list[8] = {RGBLed::BLACK, RGBLed::RED, RGBLed::GREEN, RGBLed::BLUE, RGBLed::MAGENTA, RGBLed::CYAN, RGBLed::YELLOW, RGBLed::WHITE}; |
rominos2 | 3:be0a3c2ec426 | 33 | int i = 0; |
rominos2 | 3:be0a3c2ec426 | 34 | |
rominos2 | 3:be0a3c2ec426 | 35 | while (true) { |
rominos2 | 3:be0a3c2ec426 | 36 | i = (i+1)%8; |
rominos2 | 3:be0a3c2ec426 | 37 | led.setColor(list[i]); |
rominos2 | 3:be0a3c2ec426 | 38 | wait_ms(100); |
rominos2 | 3:be0a3c2ec426 | 39 | } |
rominos2 | 3:be0a3c2ec426 | 40 | } |
rominos2 | 3:be0a3c2ec426 | 41 | @endcode |
rominos2 | 3:be0a3c2ec426 | 42 | */ |
rominos2 | 3:be0a3c2ec426 | 43 | class RGBLed { |
rominos2 | 3:be0a3c2ec426 | 44 | private: |
rominos2 | 3:be0a3c2ec426 | 45 | DigitalOut _red; |
rominos2 | 3:be0a3c2ec426 | 46 | DigitalOut _green; |
rominos2 | 3:be0a3c2ec426 | 47 | DigitalOut _blue; |
rominos2 | 3:be0a3c2ec426 | 48 | |
rominos2 | 3:be0a3c2ec426 | 49 | public: |
rominos2 | 3:be0a3c2ec426 | 50 | /** RGB Color class \n |
rominos2 | 3:be0a3c2ec426 | 51 | Colors have been defined and are ready to use in RGBLed class |
rominos2 | 3:be0a3c2ec426 | 52 | */ |
rominos2 | 3:be0a3c2ec426 | 53 | class Color { |
rominos2 | 3:be0a3c2ec426 | 54 | private: |
rominos2 | 3:be0a3c2ec426 | 55 | bool _r; /**< Red component of the Color */ |
rominos2 | 3:be0a3c2ec426 | 56 | bool _g; /**< Green component of the Color */ |
rominos2 | 3:be0a3c2ec426 | 57 | bool _b; /**< Blue component of the Color */ |
rominos2 | 3:be0a3c2ec426 | 58 | Color(bool r, bool g, bool b); /**< Constructor */ |
rominos2 | 3:be0a3c2ec426 | 59 | friend class RGBLed; |
rominos2 | 3:be0a3c2ec426 | 60 | }; |
rominos2 | 3:be0a3c2ec426 | 61 | |
rominos2 | 3:be0a3c2ec426 | 62 | /** Create a RGBLed, containing the informations about the LED pinout. |
rominos2 | 3:be0a3c2ec426 | 63 | @param redPin the pin linked to the Red LED |
rominos2 | 3:be0a3c2ec426 | 64 | @param greenPin the pin linked to the green LED |
rominos2 | 3:be0a3c2ec426 | 65 | @param blue the pin linked to the blue LED |
rominos2 | 3:be0a3c2ec426 | 66 | */ |
rominos2 | 3:be0a3c2ec426 | 67 | RGBLed(PinName redPin, PinName greenPin, PinName bluePin); |
rominos2 | 3:be0a3c2ec426 | 68 | |
rominos2 | 3:be0a3c2ec426 | 69 | /** Change the color of the LED. |
rominos2 | 3:be0a3c2ec426 | 70 | @param color the color to display |
rominos2 | 3:be0a3c2ec426 | 71 | @see RGBLed::Color |
rominos2 | 3:be0a3c2ec426 | 72 | */ |
rominos2 | 3:be0a3c2ec426 | 73 | void setColor(RGBLed::Color& color); |
rominos2 | 3:be0a3c2ec426 | 74 | |
rominos2 | 3:be0a3c2ec426 | 75 | static Color BLACK; /**< Black Color (no color) */ |
rominos2 | 3:be0a3c2ec426 | 76 | static Color RED; /**< Red Color */ |
rominos2 | 3:be0a3c2ec426 | 77 | static Color GREEN; /**< Green Color */ |
rominos2 | 3:be0a3c2ec426 | 78 | static Color BLUE; /**< Blue Color */ |
rominos2 | 3:be0a3c2ec426 | 79 | static Color MAGENTA; /**< Magenta Color (Red + Blue) */ |
rominos2 | 3:be0a3c2ec426 | 80 | static Color CYAN; /**< Cyan Color (Green + Blue) */ |
rominos2 | 3:be0a3c2ec426 | 81 | static Color YELLOW; /**< Yellow Color (Red + Green) */ |
rominos2 | 3:be0a3c2ec426 | 82 | static Color WHITE; /**< White Color (Red + Green + Blue) */ |
rominos2 | 3:be0a3c2ec426 | 83 | }; |