Index: /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/ChainableLED.cpp
===================================================================
--- /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/ChainableLED.cpp	(revision 243)
+++ /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/ChainableLED.cpp	(revision 243)
@@ -0,0 +1,175 @@
+/*
+ * 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
+ */
+
+
+// --------------------------------------------------------------------------------------
+
+#include "ChainableLED.h"
+
+// Forward declaration
+float hue2rgb(float p, float q, float t);
+
+// --------------------------------------------------------------------------------------
+
+ChainableLED::ChainableLED(byte clk_pin, byte data_pin, byte number_of_leds) :
+    _clk_pin(clk_pin), _data_pin(data_pin), _num_leds(number_of_leds)
+{
+    _led_state = (byte*) calloc(_num_leds*3, sizeof(byte));
+}
+
+ChainableLED::~ChainableLED()
+{
+    free(_led_state);
+}
+
+// --------------------------------------------------------------------------------------
+
+void ChainableLED::init()
+{
+    pinMode(_clk_pin, OUTPUT);
+    pinMode(_data_pin, OUTPUT);
+
+    for (byte i=0; i<_num_leds; i++)
+        setColorRGB(i, 0, 0, 0);
+}
+
+void ChainableLED::clk(void)
+{
+    digitalWrite(_clk_pin, LOW);
+    delayMicroseconds(_CLK_PULSE_DELAY); 
+    digitalWrite(_clk_pin, HIGH);
+    delayMicroseconds(_CLK_PULSE_DELAY);   
+}
+
+void ChainableLED::sendByte(byte b)
+{
+    // Send one bit at a time, starting with the MSB
+    for (byte i=0; i<8; i++)
+    {
+        // If MSB is 1, write one and clock it, else write 0 and clock
+        if ((b & 0x80) != 0)
+            digitalWrite(_data_pin, HIGH);
+        else
+            digitalWrite(_data_pin, LOW);
+        clk();
+
+        // Advance to the next bit to send
+        b <<= 1;
+    }
+}
+ 
+void ChainableLED::sendColor(byte red, byte green, byte blue)
+{
+    // Start by sending a byte with the format "1 1 /B7 /B6 /G7 /G6 /R7 /R6"
+    byte prefix = 0b11000000;
+    if ((blue & 0x80) == 0)     prefix|= 0b00100000;
+    if ((blue & 0x40) == 0)     prefix|= 0b00010000; 
+    if ((green & 0x80) == 0)    prefix|= 0b00001000;
+    if ((green & 0x40) == 0)    prefix|= 0b00000100;
+    if ((red & 0x80) == 0)      prefix|= 0b00000010;
+    if ((red & 0x40) == 0)      prefix|= 0b00000001;
+    sendByte(prefix);
+        
+    // Now must send the 3 colors
+    sendByte(blue);
+    sendByte(green);
+    sendByte(red);
+}
+
+void ChainableLED::setColorRGB(byte led, byte red, byte green, byte 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 (byte 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(byte 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, (byte)(255.0*r), (byte)(255.0*g), (byte)(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;
+}
Index: /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/ChainableLED.h
===================================================================
--- /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/ChainableLED.h	(revision 243)
+++ /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/ChainableLED.h	(revision 243)
@@ -0,0 +1,69 @@
+/*
+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__
+
+#if defined (SPARK)
+  #include "application.h"
+#else
+  #include "Arduino.h"
+#endif
+
+#define _CL_RED             0
+#define _CL_GREEN           1
+#define _CL_BLUE            2
+#define _CLK_PULSE_DELAY    20
+
+class ChainableLED
+{
+public:
+    ChainableLED(byte clk_pin, byte data_pin, byte number_of_leds);
+    ~ChainableLED();
+    
+    void init();
+    void setColorRGB(byte led, byte red, byte green, byte blue);
+    void setColorHSB(byte led, float hue, float saturation, float brightness);
+
+private:
+    byte _clk_pin;
+    byte _data_pin;
+    byte _num_leds; 
+
+    byte* _led_state;
+    
+    void clk(void);
+    void sendByte(byte b);
+    void sendColor(byte red, byte green, byte blue);
+};
+
+#endif
+
Index: /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/LICENSE
===================================================================
--- /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/LICENSE	(revision 243)
+++ /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/LICENSE	(revision 243)
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
Index: /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/README.md
===================================================================
--- /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/README.md	(revision 243)
+++ /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/README.md	(revision 243)
@@ -0,0 +1,34 @@
+ChainableLED
+============
+
+Arduino library compatible with Grove Chainable LED and the P9813 chip. It allows controlling a chain of LEDS individually. 
+
+Supports both RGB and HSB color spaces for setting the color of each individual LED.
+
+Compatible with [Particle devices](https://www.particle.io/).
+
+[More information on the wiki](https://github.com/pjpmarques/ChainableLED/wiki)
+
+
+Installation
+============
+1. Grab the latest version from the release section of GitHub.
+(https://github.com/pjpmarques/ChainableLED/releases)
+
+2. Unzip it to your Arduino "libraries" directory. 
+
+3. It should be ready to use. Examples are included.
+
+
+Library Interface
+=================
+```c++
+class ChainableLED {
+public:
+  ChainableLED(byte clk_pin, byte data_pin, byte number_of_leds);
+
+  void init();
+  void setColorRGB(byte led, byte red, byte green, byte blue);
+  void setColorHSB(byte led, float hue, float saturation, float brightness);
+}
+```
Index: /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/examples/CycleThroughColors/CycleThroughColors.ino
===================================================================
--- /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/examples/CycleThroughColors/CycleThroughColors.ino	(revision 243)
+++ /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/examples/CycleThroughColors/CycleThroughColors.ino	(revision 243)
@@ -0,0 +1,39 @@
+
+/* 
+ * Example of using the ChainableRGB library for controlling a Grove RGB.
+ * This code cycles through all the colors in an uniform way. This is accomplished using a HSB color space. 
+ */
+
+
+#include <ChainableLED.h>
+
+#define NUM_LEDS  5
+
+ChainableLED leds(7, 8, NUM_LEDS);
+
+void setup()
+{
+  leds.init();
+}
+
+float hue = 0.0;
+boolean up = true;
+
+void loop()
+{
+  for (byte i=0; i<NUM_LEDS; i++)
+    leds.setColorHSB(i, hue, 1.0, 0.5);
+    
+  delay(50);
+    
+  if (up)
+    hue+= 0.025;
+  else
+    hue-= 0.025;
+    
+  if (hue>=1.0 && up)
+    up = false;
+  else if (hue<=0.0 && !up)
+    up = true;
+}
+
Index: /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/examples/FadeInOut/FadeInOut.ino
===================================================================
--- /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/examples/FadeInOut/FadeInOut.ino	(revision 243)
+++ /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/examples/FadeInOut/FadeInOut.ino	(revision 243)
@@ -0,0 +1,34 @@
+
+/* 
+ * Example of using the ChainableRGB library for controlling a Grove RGB.
+ * This code fades in an out colors in a strip of leds.
+ */
+
+
+#include <ChainableLED.h>
+
+#define NUM_LEDS  5
+
+ChainableLED leds(7, 8, NUM_LEDS);
+
+void setup()
+{
+  leds.init();
+}
+
+byte power = 0;
+
+void loop()
+{
+  for (byte i=0; i<NUM_LEDS; i++)
+  {
+    if (i%2 == 0)
+      leds.setColorRGB(i, power, 0, 0);
+    else
+      leds.setColorRGB(i, 0, 255-power, 0);
+  }  
+  power+= 10;
+  
+  delay(10);
+}
+
Index: /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/examples/SimpleCycle/SimpleCycle.ino
===================================================================
--- /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/examples/SimpleCycle/SimpleCycle.ino	(revision 243)
+++ /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/examples/SimpleCycle/SimpleCycle.ino	(revision 243)
@@ -0,0 +1,33 @@
+
+/* 
+ * Example of using the ChainableRGB library for controlling a Grove RGB.
+ * This code puts a red dot (led) moving along a strip of blue dots.
+ */
+
+#include <ChainableLED.h>
+
+#define NUM_LEDS  5
+
+ChainableLED leds(7, 8, NUM_LEDS);
+
+void setup()
+{
+  leds.init();
+}
+
+byte pos = 0;
+
+void loop()
+{
+  for (byte i=0; i<NUM_LEDS; i++)
+  {
+    if (i==pos)
+      leds.setColorRGB(i, 255, 0, 0);  
+    else
+      leds.setColorRGB(i, 0, 0, 255); 
+  }
+  delay(250);
+  
+  pos = (pos+1) % NUM_LEDS;
+}
+
Index: /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/keywords.txt
===================================================================
--- /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/keywords.txt	(revision 243)
+++ /rtos_arduino/trunk/examples/NCESIoT_RTOS/ChainableLED/keywords.txt	(revision 243)
@@ -0,0 +1,19 @@
+#######################################
+# Syntax Coloring Map For Ultrasound
+#######################################
+
+#######################################
+# Datatypes (KEYWORD1)
+#######################################
+
+ChainableLED	KEYWORD1
+
+#######################################
+# Methods and Functions (KEYWORD2)
+#######################################
+setColorRGB	KEYWORD2
+setColorHSB	KEYWORD2
+
+#######################################
+# Constants (LITERAL1)
+#######################################
Index: /rtos_arduino/trunk/examples/NCESIoT_RTOS/Makefile
===================================================================
--- /rtos_arduino/trunk/examples/NCESIoT_RTOS/Makefile	(revision 242)
+++ /rtos_arduino/trunk/examples/NCESIoT_RTOS/Makefile	(revision 243)
@@ -62,3 +62,7 @@
 APPL_COBJS   += 
 
+APPL_DIR     += ./ChainableLED
+APPL_CXXOBJS += ChainableLED.o
+APPL_COBJS   += 
+
 include $(RCA_DIR)/lib/Makefile.rca
Index: /rtos_arduino/trunk/examples/NCESIoT_RTOS/rca_app.cpp
===================================================================
--- /rtos_arduino/trunk/examples/NCESIoT_RTOS/rca_app.cpp	(revision 242)
+++ /rtos_arduino/trunk/examples/NCESIoT_RTOS/rca_app.cpp	(revision 243)
@@ -6,4 +6,22 @@
 #include <Digital_Light_TSL2561.h>
 #include <SeeedOLED.h>
+#include <ChainableLED.h>
+
+#define LED_PIN  4
+
+void setup()
+{
+    Serial.begin(115200);
+    pinMode(LED_PIN, OUTPUT);
+}
+
+void loop() 
+{
+    digitalWrite(LED_PIN, HIGH);   // turn the LED on (HIGH is the voltage level)
+    delay(1000);                   // wait for a second
+    digitalWrite(LED_PIN, LOW);    // turn the LED off by making the voltage LOW
+    delay(1000);                   // wait for a second    
+}
+
 
 #define TOUCH_PIN 3
@@ -11,7 +29,6 @@
 int is_update_oled;
 
-void setup() 
+void task1_setup() 
 {
-    Serial.begin(115200);
     Wire.begin();
     TSL2561.init();
@@ -23,13 +40,17 @@
 } 
  
-void loop() 
-{    
+void task1_loop() 
+{
+    int lux;
+
+    lux = TSL2561.readVisibleLux();
     Serial.print("The Light value is: ");
-    Serial.println(TSL2561.readVisibleLux());
+    Serial.println(lux);
 
     if (is_update_oled == 1) {
         wai_sem(OLED_SEM);
-        SeeedOled.clearDisplay();
-        SeeedOled.putNumber(TSL2561.readVisibleLux());
+        SeeedOled.setTextXY(0, 0);        
+        SeeedOled.putNumber(lux);
+        SeeedOled.putString("    ");        
         sig_sem(OLED_SEM);
     }
@@ -45,5 +66,5 @@
 #endif /* USE_INTERRUPT */
 
-void task1_setup()
+void task2_setup()
 {
     pinMode(TOUCH_PIN, INPUT_PULLUP);
@@ -53,5 +74,5 @@
 }
 
-void task1_loop() 
+void task2_loop() 
 {
 #ifdef USE_INTERRUPT    
@@ -75,2 +96,33 @@
     }
 }
+
+#define NUM_LEDS  1
+
+ChainableLED leds(8, 9, NUM_LEDS);
+
+void task3_setup()
+{
+    leds.init();
+}
+
+float hue = 0.0;
+boolean up = true;
+int count = 0;
+
+void task3_loop()
+{
+    for (byte i=0; i<NUM_LEDS; i++)
+      leds.setColorHSB(i, hue, 1.0, 0.5);
+    
+    delay(50);
+    
+    if (up)
+      hue+= 0.025;
+    else
+      hue-= 0.025;
+    
+    if (hue>=1.0 && up)
+      up = false;
+    else if (hue<=0.0 && !up)
+      up = true;
+}
Index: /rtos_arduino/trunk/examples/NCESIoT_RTOS/rca_app.h
===================================================================
--- /rtos_arduino/trunk/examples/NCESIoT_RTOS/rca_app.h	(revision 242)
+++ /rtos_arduino/trunk/examples/NCESIoT_RTOS/rca_app.h	(revision 243)
@@ -2,5 +2,5 @@
 #define _RCA_APP_H_
 
-#define RCA_NUM_TASK 1
+#define RCA_NUM_TASK 3
 
 #endif /* _RCA_APP_H_ */
