using PWM to control RGB LED for RedBear Demo
Fork of ChainableLED by
Revision 1:20c17c26e479, committed 2016-04-28
- Comitter:
- Jackson_lv
- Date:
- Thu Apr 28 08:51:39 2016 +0000
- Parent:
- 0:e7513df9d7cb
- Commit message:
- PWM RGB LED
Changed in this revision
diff -r e7513df9d7cb -r 20c17c26e479 ChainableLED.cpp --- a/ChainableLED.cpp Wed Apr 27 09:47:09 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,172 +0,0 @@ -/* - * Copyright (C) 2013 Seeed Technology Inc. - * Copyright (C) 2012 Paulo Marques (pjp.marques@gmail.com) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -/* Information about the P9813 protocol obtained from: - * http://www.seeedstudio.com/wiki/index.php?title=Twig_-_Chainable_RGB_LED - * - * HSB to RGB routine adapted from: - * http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript - * - * This library is ported from Arduino to mbed - */ - - -// -------------------------------------------------------------------------------------- - -#include "ChainableLED.h" - -// Forward declaration -float hue2rgb(float p, float q, float t); - -// -------------------------------------------------------------------------------------- - -ChainableLED::ChainableLED(PinName clk_pin, PinName data_pin, unsigned int number_of_leds) : - _clk_pin(clk_pin), _data_pin(data_pin) -{ - _num_leds = number_of_leds; - - for (uint8_t i=0; i<_num_leds; i++) - setColorRGB(i, 0, 0, 0); -} - -ChainableLED::~ChainableLED() -{ -} - -// -------------------------------------------------------------------------------------- - -void ChainableLED::clk(void) -{ - _clk_pin = 0; - wait_us(_CLK_PULSE_DELAY); - _clk_pin = 1; - wait_us(_CLK_PULSE_DELAY); -} - -void ChainableLED::sendByte(uint8_t b) -{ - // Send one bit at a time, starting with the MSB - for (uint8_t i=0; i<8; i++) - { - // If MSB is 1, write one and clock it, else write 0 and clock - if ((b & 0x80) != 0) - _data_pin = 1; - else - _data_pin = 0; - - clk(); - - // Advance to the next bit to send - b <<= 1; - } -} - -void ChainableLED::sendColor(uint8_t red, uint8_t green, uint8_t blue) -{ - // Start by sending a uint8_t with the format "1 1 /B7 /B6 /G7 /G6 /R7 /R6" - uint8_t prefix = 0xC0; - if ((blue & 0x80) == 0) prefix|= 0x20; - if ((blue & 0x40) == 0) prefix|= 0x10; - if ((green & 0x80) == 0) prefix|= 0x08; - if ((green & 0x40) == 0) prefix|= 0x04; - if ((red & 0x80) == 0) prefix|= 0x02; - if ((red & 0x40) == 0) prefix|= 0x01; - sendByte(prefix); - - // Now must send the 3 colors - sendByte(blue); - sendByte(green); - sendByte(red); -} - -void ChainableLED::setColorRGB(unsigned int led, uint8_t red, uint8_t green, uint8_t blue) -{ - // Send data frame prefix (32x "0") - sendByte(0x00); - sendByte(0x00); - sendByte(0x00); - sendByte(0x00); - - // Send color data for each one of the leds - for (uint8_t i=0; i<_num_leds; i++) - { - if (i == led) - { - _led_state[i*3 + _CL_RED] = red; - _led_state[i*3 + _CL_GREEN] = green; - _led_state[i*3 + _CL_BLUE] = blue; - } - - sendColor(_led_state[i*3 + _CL_RED], - _led_state[i*3 + _CL_GREEN], - _led_state[i*3 + _CL_BLUE]); - } - - // Terminate data frame (32x "0") - sendByte(0x00); - sendByte(0x00); - sendByte(0x00); - sendByte(0x00); -} - -void ChainableLED::setColorHSB(unsigned int led, float hue, float saturation, float brightness) -{ - float r, g, b; - -// constrain(hue, 0.0, 1.0); -// constrain(saturation, 0.0, 1.0); -// constrain(brightness, 0.0, 1.0); - - if(saturation == 0.0) - { - r = g = b = brightness; - } - else - { - float q = brightness < 0.5 ? - brightness * (1.0 + saturation) : brightness + saturation - brightness * saturation; - float p = 2.0 * brightness - q; - r = hue2rgb(p, q, hue + 1.0/3.0); - g = hue2rgb(p, q, hue); - b = hue2rgb(p, q, hue - 1.0/3.0); - } - - setColorRGB(led, (uint8_t)(255.0*r), (uint8_t)(255.0*g), (uint8_t)(255.0*b)); -} - -// -------------------------------------------------------------------------------------- - -float hue2rgb(float p, float q, float t) -{ - if (t < 0.0) - t += 1.0; - if(t > 1.0) - t -= 1.0; - if(t < 1.0/6.0) - return p + (q - p) * 6.0 * t; - if(t < 1.0/2.0) - return q; - if(t < 2.0/3.0) - return p + (q - p) * (2.0/3.0 - t) * 6.0; - - return p; -}
diff -r e7513df9d7cb -r 20c17c26e479 ChainableLED.h --- a/ChainableLED.h Wed Apr 27 09:47:09 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -/* -Copyright (C) 2013 Seeed Technology Inc. -Copyright (C) 2012 Paulo Marques (pjp.marques@gmail.com) - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software is furnished to do so, -subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ - -/* - * Library for controlling a chain of RGB LEDs based on the P9813 protocol. - * E.g., supports the Grove Chainable RGB LED product. - * - * Information about the P9813 protocol obtained from: - * http://www.seeedstudio.com/wiki/index.php?title=Twig_-_Chainable_RGB_LED - */ - - - -#ifndef __ChainableLED_h__ -#define __ChainableLED_h__ - -#include "mbed.h" - -#define _CL_RED 0 -#define _CL_GREEN 1 -#define _CL_BLUE 2 -#define _CLK_PULSE_DELAY 20 - -class ChainableLED -{ -public: - ChainableLED(PinName clk_pin, PinName data_pin, unsigned int number_of_leds); - ~ChainableLED(); - - void setColorRGB(unsigned int led, uint8_t red, uint8_t green, uint8_t blue); - void setColorHSB(unsigned int led, float hue, float saturation, float brightness); - -private: - DigitalOut _clk_pin; - DigitalOut _data_pin; - unsigned int _num_leds; - - uint8_t _led_state[3]; - - void clk(void); - void sendByte(uint8_t b); - void sendColor(uint8_t red, uint8_t green, uint8_t blue); -}; - -#endif
diff -r e7513df9d7cb -r 20c17c26e479 RGBLED.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RGBLED.cpp Thu Apr 28 08:51:39 2016 +0000 @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2013 Seeed Technology Inc. + * Copyright (C) 2012 Paulo Marques (pjp.marques@gmail.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* Information about the P9813 protocol obtained from: + * http://www.seeedstudio.com/wiki/index.php?title=Twig_-_Chainable_RGB_LED + * + * HSB to RGB routine adapted from: + * http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript + * + * This library is ported from Arduino to mbed + */ + + +// -------------------------------------------------------------------------------------- + +#include "RGBLED.h" + +// Forward declaration +float hue2rgb(float p, float q, float t); +PwmOut PWMR(P0_11); +PwmOut PWMG(P0_9); +PwmOut PWMB(P0_10); +// -------------------------------------------------------------------------------------- + +ChainableLED::ChainableLED(PinName r_pin, PinName g_pin, PinName b_pin,unsigned int number_of_leds) : + _r_pin(r_pin), _g_pin(g_pin),_b_pin(b_pin) +{ + _num_leds = number_of_leds; + + + for (uint8_t i=0; i<_num_leds; i++) + setColorRGB(i, 0, 0, 0); +} + +ChainableLED::~ChainableLED() +{ +} + +// -------------------------------------------------------------------------------------- + + + +void ChainableLED::sendColor(uint8_t red, uint8_t green, uint8_t blue) +{ + //control the RGB LED by PWM + PWMR = (float)red/255; + PWMG = (float)green/255; + PWMB = (float)blue/255; + +} + +void ChainableLED::setColorRGB(unsigned int led, float red, float green, float blue) +{ + sendColor(0xff-red, 0xff-green, 0xff-blue ); +} + +void ChainableLED::setColorHSB(unsigned int led, float hue, float saturation, float brightness) +{ + float r, g, b; + + if(saturation == 0.0) + { + r = g = b = brightness; + } + else + { + float q = brightness < 0.5 ? + brightness * (1.0 + saturation) : brightness + saturation - brightness * saturation; + float p = 2.0 * brightness - q; + r = hue2rgb(p, q, hue + 1.0/3.0); + g = hue2rgb(p, q, hue); + b = hue2rgb(p, q, hue - 1.0/3.0); + } + + setColorRGB(led, (uint8_t)(255.0*r), (uint8_t)(255.0*g), (uint8_t)(255.0*b)); +} + +// -------------------------------------------------------------------------------------- + +float hue2rgb(float p, float q, float t) +{ + if (t < 0.0) + t += 1.0; + if(t > 1.0) + t -= 1.0; + if(t < 1.0/6.0) + return p + (q - p) * 6.0 * t; + if(t < 1.0/2.0) + return q; + if(t < 2.0/3.0) + return p + (q - p) * (2.0/3.0 - t) * 6.0; + + return p; +}
diff -r e7513df9d7cb -r 20c17c26e479 RGBLED.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RGBLED.h Thu Apr 28 08:51:39 2016 +0000 @@ -0,0 +1,63 @@ +/* +Copyright (C) 2013 Seeed Technology Inc. +Copyright (C) 2012 Paulo Marques (pjp.marques@gmail.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +/* + * Library for controlling a chain of RGB LEDs based on the P9813 protocol. + * E.g., supports the Grove Chainable RGB LED product. + * + * Information about the P9813 protocol obtained from: + * http://www.seeedstudio.com/wiki/index.php?title=Twig_-_Chainable_RGB_LED + */ + + + +#ifndef __RGBLED_h__ +#define __RGBLED_h__ + +#include "mbed.h" + +#define _CL_RED 0 +#define _CL_GREEN 1 +#define _CL_BLUE 2 +#define _CLK_PULSE_DELAY 20 + +class ChainableLED +{ +public: + ChainableLED(PinName r_pin, PinName g_pin, PinName b_pin, unsigned int number_of_leds); + ~ChainableLED(); + + void setColorRGB(unsigned int led, float red, float green, float blue); + void setColorHSB(unsigned int led, float hue, float saturation, float brightness); + +private: + DigitalOut _r_pin; + DigitalOut _g_pin; + DigitalOut _b_pin; + unsigned int _num_leds; + + uint8_t _led_state[3]; + + void sendColor(uint8_t red, uint8_t green, uint8_t blue); +}; + +#endif