using PWM to control RGB LED for RedBear Demo
Fork of ChainableLED by
RGBLED.cpp
00001 /* 00002 * Copyright (C) 2013 Seeed Technology Inc. 00003 * Copyright (C) 2012 Paulo Marques (pjp.marques@gmail.com) 00004 * 00005 * Permission is hereby granted, free of charge, to any person obtaining a copy of 00006 * this software and associated documentation files (the "Software"), to deal in 00007 * the Software without restriction, including without limitation the rights to 00008 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 00009 * the Software, and to permit persons to whom the Software is furnished to do so, 00010 * subject to the following conditions: 00011 * 00012 * The above copyright notice and this permission notice shall be included in all 00013 * copies or substantial portions of the Software. 00014 * 00015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 00017 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 00018 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 00019 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 00020 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00021 */ 00022 00023 /* Information about the P9813 protocol obtained from: 00024 * http://www.seeedstudio.com/wiki/index.php?title=Twig_-_Chainable_RGB_LED 00025 * 00026 * HSB to RGB routine adapted from: 00027 * http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript 00028 * 00029 * This library is ported from Arduino to mbed 00030 */ 00031 00032 00033 // -------------------------------------------------------------------------------------- 00034 00035 #include "RGBLED.h" 00036 00037 // Forward declaration 00038 float hue2rgb(float p, float q, float t); 00039 PwmOut PWMR(P0_11); 00040 PwmOut PWMG(P0_9); 00041 PwmOut PWMB(P0_10); 00042 // -------------------------------------------------------------------------------------- 00043 00044 ChainableLED::ChainableLED(PinName r_pin, PinName g_pin, PinName b_pin,unsigned int number_of_leds) : 00045 _r_pin(r_pin), _g_pin(g_pin),_b_pin(b_pin) 00046 { 00047 _num_leds = number_of_leds; 00048 00049 00050 for (uint8_t i=0; i<_num_leds; i++) 00051 setColorRGB(i, 0, 0, 0); 00052 } 00053 00054 ChainableLED::~ChainableLED() 00055 { 00056 } 00057 00058 // -------------------------------------------------------------------------------------- 00059 00060 00061 00062 void ChainableLED::sendColor(uint8_t red, uint8_t green, uint8_t blue) 00063 { 00064 //control the RGB LED by PWM 00065 PWMR = (float)red/255; 00066 PWMG = (float)green/255; 00067 PWMB = (float)blue/255; 00068 00069 } 00070 00071 void ChainableLED::setColorRGB(unsigned int led, float red, float green, float blue) 00072 { 00073 sendColor(0xff-red, 0xff-green, 0xff-blue ); 00074 } 00075 00076 void ChainableLED::setColorHSB(unsigned int led, float hue, float saturation, float brightness) 00077 { 00078 float r, g, b; 00079 00080 if(saturation == 0.0) 00081 { 00082 r = g = b = brightness; 00083 } 00084 else 00085 { 00086 float q = brightness < 0.5 ? 00087 brightness * (1.0 + saturation) : brightness + saturation - brightness * saturation; 00088 float p = 2.0 * brightness - q; 00089 r = hue2rgb(p, q, hue + 1.0/3.0); 00090 g = hue2rgb(p, q, hue); 00091 b = hue2rgb(p, q, hue - 1.0/3.0); 00092 } 00093 00094 setColorRGB(led, (uint8_t)(255.0*r), (uint8_t)(255.0*g), (uint8_t)(255.0*b)); 00095 } 00096 00097 // -------------------------------------------------------------------------------------- 00098 00099 float hue2rgb(float p, float q, float t) 00100 { 00101 if (t < 0.0) 00102 t += 1.0; 00103 if(t > 1.0) 00104 t -= 1.0; 00105 if(t < 1.0/6.0) 00106 return p + (q - p) * 6.0 * t; 00107 if(t < 1.0/2.0) 00108 return q; 00109 if(t < 2.0/3.0) 00110 return p + (q - p) * (2.0/3.0 - t) * 6.0; 00111 00112 return p; 00113 }
Generated on Wed Jul 13 2022 20:23:34 by 1.7.2