Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: m3Dpi MQTT-Thermostat-example Final_project_Tran Final_project_Tran ... more
RGB.h@6:b5a88296bc50, 2015-11-19 (annotated)
- Committer:
- sillevl
- Date:
- Thu Nov 19 08:16:58 2015 +0000
- Revision:
- 6:b5a88296bc50
- Parent:
- 4:a7a26506c62f
- Parent:
- 5:036a21c44cb7
merged and added example
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| sillevl | 0:9509f771054b | 1 | |
| sillevl | 0:9509f771054b | 2 | #include "mbed.h" |
| sillevl | 2:ed46f45e1d66 | 3 | #include "Color.h" |
| sillevl | 0:9509f771054b | 4 | |
| sillevl | 2:ed46f45e1d66 | 5 | #ifndef RGB_H |
| sillevl | 2:ed46f45e1d66 | 6 | #define RGB_H |
| sillevl | 0:9509f771054b | 7 | |
| sillevl | 4:a7a26506c62f | 8 | |
| sillevl | 5:036a21c44cb7 | 9 | /** RGB class |
| sillevl | 5:036a21c44cb7 | 10 | * Used to control RGB leds using PWM modulation to dim the individual colors. By combining red, green and blue |
| sillevl | 6:b5a88296bc50 | 11 | * a great amount of colors can be created. This class can accept color objects or colors in hexadecimal notation (web color notation) |
| sillevl | 6:b5a88296bc50 | 12 | * Example usage: |
| sillevl | 6:b5a88296bc50 | 13 | * @code |
| sillevl | 6:b5a88296bc50 | 14 | * |
| sillevl | 6:b5a88296bc50 | 15 | * #include "mbed.h" |
| sillevl | 6:b5a88296bc50 | 16 | * #include "RGB.h" |
| sillevl | 6:b5a88296bc50 | 17 | * RGB led(p23,p24,p25); |
| sillevl | 6:b5a88296bc50 | 18 | * |
| sillevl | 6:b5a88296bc50 | 19 | * void main(){ |
| sillevl | 6:b5a88296bc50 | 20 | * led.off(); |
| sillevl | 6:b5a88296bc50 | 21 | * |
| sillevl | 6:b5a88296bc50 | 22 | * wait(1.0); |
| sillevl | 6:b5a88296bc50 | 23 | * |
| sillevl | 6:b5a88296bc50 | 24 | * // setting the color using the Color enum with named colors |
| sillevl | 6:b5a88296bc50 | 25 | * led.setColor(Color::RED); |
| sillevl | 6:b5a88296bc50 | 26 | * |
| sillevl | 6:b5a88296bc50 | 27 | * // setting the color using a hexadecimal notated integer (yellow) |
| sillevl | 6:b5a88296bc50 | 28 | * led.setColor(0xFFFF00); |
| sillevl | 6:b5a88296bc50 | 29 | * |
| sillevl | 6:b5a88296bc50 | 30 | * // setting the color using an instance of the Color class |
| sillevl | 6:b5a88296bc50 | 31 | * Color* myColor = new Color(0.0,1.0,0.0); |
| sillevl | 6:b5a88296bc50 | 32 | * led.setColor(myColor); |
| sillevl | 6:b5a88296bc50 | 33 | * delete myColor; |
| sillevl | 6:b5a88296bc50 | 34 | * } |
| sillevl | 6:b5a88296bc50 | 35 | * @endcode |
| sillevl | 5:036a21c44cb7 | 36 | */ |
| sillevl | 0:9509f771054b | 37 | class RGB{ |
| sillevl | 0:9509f771054b | 38 | public: |
| sillevl | 0:9509f771054b | 39 | |
| sillevl | 2:ed46f45e1d66 | 40 | static const int OFF = 0; |
| sillevl | 4:a7a26506c62f | 41 | |
| sillevl | 5:036a21c44cb7 | 42 | /** Create a new RGB instance |
| sillevl | 5:036a21c44cb7 | 43 | * @param r_pin mbed PinName that supports PWM output assigned to the red led |
| sillevl | 5:036a21c44cb7 | 44 | * @param g_pin mbed PinName that supports PWM output assigned to the green led |
| sillevl | 5:036a21c44cb7 | 45 | * @param b_pin mbed PinName that supports PWM output assigned to the blue led |
| sillevl | 5:036a21c44cb7 | 46 | */ |
| sillevl | 0:9509f771054b | 47 | RGB(PinName r_pin, PinName g_pin, PinName b_pin); |
| sillevl | 4:a7a26506c62f | 48 | ~RGB(); |
| sillevl | 5:036a21c44cb7 | 49 | |
| sillevl | 5:036a21c44cb7 | 50 | /** Set the color by giving an instance of an Color object |
| sillevl | 5:036a21c44cb7 | 51 | * @param color Pointer to an instance of an Color object |
| sillevl | 5:036a21c44cb7 | 52 | * @ref Color |
| sillevl | 5:036a21c44cb7 | 53 | */ |
| sillevl | 6:b5a88296bc50 | 54 | |
| sillevl | 2:ed46f45e1d66 | 55 | void setColor(Color* color); |
| sillevl | 5:036a21c44cb7 | 56 | |
| sillevl | 5:036a21c44cb7 | 57 | /** Set the color by giving an integer in hexadecimal notation |
| sillevl | 5:036a21c44cb7 | 58 | * @param color Color in hexadecimal notation (hex triplet). 24-bit RGB color as used in web colors. |
| sillevl | 5:036a21c44cb7 | 59 | * @note Each color is made up of 8 bits, 0xRRGGBB |
| sillevl | 5:036a21c44cb7 | 60 | */ |
| sillevl | 0:9509f771054b | 61 | void setColor(int color); |
| sillevl | 5:036a21c44cb7 | 62 | |
| sillevl | 5:036a21c44cb7 | 63 | /** Get the current color of the RGB led |
| sillevl | 5:036a21c44cb7 | 64 | * @return instance of Color class containing the current set color |
| sillevl | 5:036a21c44cb7 | 65 | * @ref Color |
| sillevl | 5:036a21c44cb7 | 66 | */ |
| sillevl | 2:ed46f45e1d66 | 67 | Color* getColor(); |
| sillevl | 4:a7a26506c62f | 68 | |
| sillevl | 0:9509f771054b | 69 | |
| sillevl | 5:036a21c44cb7 | 70 | /// Turn the led off |
| sillevl | 0:9509f771054b | 71 | void off(); |
| sillevl | 0:9509f771054b | 72 | |
| sillevl | 0:9509f771054b | 73 | private: |
| sillevl | 4:a7a26506c62f | 74 | |
| sillevl | 0:9509f771054b | 75 | PwmOut* r_out; |
| sillevl | 0:9509f771054b | 76 | PwmOut* g_out; |
| sillevl | 0:9509f771054b | 77 | PwmOut* b_out; |
| sillevl | 4:a7a26506c62f | 78 | |
| sillevl | 2:ed46f45e1d66 | 79 | Color* color; |
| sillevl | 4:a7a26506c62f | 80 | |
| sillevl | 2:ed46f45e1d66 | 81 | void setPwmColor(int value, PwmOut* output); |
| sillevl | 4:a7a26506c62f | 82 | |
| sillevl | 0:9509f771054b | 83 | }; |
| sillevl | 2:ed46f45e1d66 | 84 | |
| sillevl | 2:ed46f45e1d66 | 85 | #endif |