RGB LED library for Grove - Chainable RGB LED using P9813

Dependents:   Seeed_Grove_Chainable_RGB_LED_Example Temperature_Color SEEED_RGB_COLOR_LED_Example LoRaWAN_actility ... more

Fork of Chainable_RGB_LED by Seeed Studio

Files at this revision

API Documentation at this revision

Comitter:
sam_grove
Date:
Sun May 03 01:34:41 2015 +0000
Parent:
0:24631ceaa38a
Commit message:
Bug fixes including RAM being clobbered if more than one LED was used. Other speed optimizations and simplifications.

Changed in this revision

ChainableLED.cpp Show annotated file Show diff for this revision Revisions of this file
ChainableLED.h Show annotated file Show diff for this revision Revisions of this file
--- a/ChainableLED.cpp	Fri Aug 02 08:05:28 2013 +0000
+++ b/ChainableLED.cpp	Sun May 03 01:34:41 2015 +0000
@@ -2,21 +2,21 @@
  * 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 
+ * 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 
+ *
+ * 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 
+ * 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.
  */
 
@@ -29,96 +29,94 @@
  * This library is ported from Arduino to mbed
  */
 
+#include "ChainableLED.h"
+
+float hue2rgb(float p, float q, float t)
+{
+    if (t < 0.0f) {
+        t += 1.0f;
+    }
+    if(t > 1.0f) {
+        t -= 1.0f;
+    }
+    if(t < 1.0f/6.0f) {
+        return p + (q - p) * 6.0f * t;
+    }
+    if(t < 1.0f/2.0f) {
+        return q;
+    }
+    if(t < 2.0f/3.0f) {
+        return p + (q - p) * (2.0f/3.0f - t) * 6.0f;
+    }
+
+    return p;
+}
 
 // --------------------------------------------------------------------------------------
 
-#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)
+ChainableLED::ChainableLED(PinName clk_pin, PinName data_pin, uint32_t number_of_leds) :
+    _clk_pin(clk_pin),
+    _data_pin(data_pin),
+    _num_leds(number_of_leds)
 {
-    _num_leds = number_of_leds;
-    
-    for (uint8_t i=0; i<_num_leds; i++)
-        setColorRGB(i, 0, 0, 0);
+    _leds = new led_val_t [number_of_leds];
+    _clk_pin = 0;
+    _data_pin = 0;
+    ledsOff();
 }
 
 ChainableLED::~ChainableLED()
 {
+    delete [] _leds;
 }
 
-// --------------------------------------------------------------------------------------
-
-void ChainableLED::clk(void)
+void ChainableLED::ledsOff(void)
 {
-    _clk_pin = 0;
-    wait_us(_CLK_PULSE_DELAY);
-    _clk_pin = 1;
-    wait_us(_CLK_PULSE_DELAY);
+    for (uint8_t i=0; i<_num_leds; i++) {
+        setColorRGB(i, 0, 0, 0);
+    }
 }
 
 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
+    for (uint8_t i=0; i<8; i++) {
+        _data_pin = (b & 0x80) ? 1 : 0;
+        _clk_pin = 1;
+        _clk_pin = 0;
         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
+    uint8_t r67 = ~(red & 0xC0) >> 6;;
+    uint8_t g67 = ~(green & 0xC0) >> 4;
+    uint8_t b67 = ~(blue & 0xC0) >> 2;
+    uint8_t verify = 0xC0 | r67 | g67 | b67;
+    
+    sendByte(verify);
     sendByte(blue);
     sendByte(green);
     sendByte(red);
 }
 
-void ChainableLED::setColorRGB(unsigned int led, uint8_t red, uint8_t green, uint8_t blue)
+void ChainableLED::setColorRGB(uint32_t 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;
+    for (uint8_t i=0; i<_num_leds; i++) {
+        if (i == led) {
+            _leds->rgb[i*3 + 0] = red;
+            _leds->rgb[i*3 + 1] = green;
+            _leds->rgb[i*3 + 2] = blue;
         }
-                    
-        sendColor(_led_state[i*3 + _CL_RED], 
-                  _led_state[i*3 + _CL_GREEN], 
-                  _led_state[i*3 + _CL_BLUE]);
+        sendColor(_leds->rgb[i*3 + 0], _leds->rgb[i*3 + 1], _leds->rgb[i*3 + 2]);
     }
 
     // Terminate data frame (32x "0")
@@ -128,45 +126,32 @@
     sendByte(0x00);
 }
 
-void ChainableLED::setColorHSB(unsigned int led, float hue, float saturation, float brightness)
+template<typename T>
+T min(T a, T b) { return (a < b) ? a : b; }
+
+template<typename T>
+static inline T max(T a, T b) { return (a > b) ? a : b; }
+
+void ChainableLED::setColorHSB(uint32_t 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);
+    hue = min<float>(hue, 1.0f);//constrain(hue, 0.0, 1.0);
+    hue = max<float>(hue, 0.0f);
+    saturation = min<float>(saturation, 1.0f);//constrain(saturation, 0.0, 1.0);
+    saturation = max<float>(saturation, 0.0f);
+    brightness = min<float>(brightness, 1.0f);//constrain(brightness, 0.0, 1.0);
+    brightness = max<float>(brightness, 0.0f);
 
-    if(saturation == 0.0)
-    {
+    if(saturation == 0.0f) {
         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);
+    } else {
+        float q = (brightness < 0.5f) ? (brightness * (1.0f + saturation)) : (brightness + saturation - brightness * saturation);
+        float p = 2.0f * brightness - q;
+        r = hue2rgb(p, q, hue + 1.0f/3.0f);
         g = hue2rgb(p, q, hue);
-        b = hue2rgb(p, q, hue - 1.0/3.0);
+        b = hue2rgb(p, q, hue - 1.0f/3.0f);
     }
 
-    setColorRGB(led, (uint8_t)(255.0*r), (uint8_t)(255.0*g), (uint8_t)(255.0*b));
+    setColorRGB(led, (uint8_t)(255.0f*r), (uint8_t)(255.0f*g), (uint8_t)(255.0f*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;
-}
--- a/ChainableLED.h	Fri Aug 02 08:05:28 2013 +0000
+++ b/ChainableLED.h	Sun May 03 01:34:41 2015 +0000
@@ -2,25 +2,25 @@
 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 
+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 
+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 
+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.
  *
@@ -30,33 +30,34 @@
 
 
 
-#ifndef __ChainableLED_h__
-#define __ChainableLED_h__
+#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(PinName clk_pin, PinName data_pin, uint32_t 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);
+
+    void setColorRGB(uint32_t led, uint8_t red, uint8_t green, uint8_t blue);
+    void setColorHSB(uint32_t led, float hue, float saturation, float brightness);
+    void ledsOff(void);
 
 private:
     DigitalOut _clk_pin;
     DigitalOut _data_pin;
-    unsigned int _num_leds; 
+    uint32_t   _num_leds;
+    
+    typedef union {
+        uint8_t rgb[3];
+        struct {
+            uint8_t r, g, b;
+        };
+    } led_val_t;
+    led_val_t *_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);
 };