add ChainableLED

This commit is contained in:
Harald Pichler 2016-09-27 09:51:26 +02:00
parent 6f7e68c3d4
commit ee5cd039a1
12 changed files with 687 additions and 3 deletions

View file

@ -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;
}

View file

@ -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

View file

@ -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;
}

View file

@ -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

View file

@ -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.
*/

View file

@ -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);
}
```

View file

@ -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;
}

View file

@ -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);
}

View file

@ -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;
}

View file

@ -0,0 +1,19 @@
#######################################
# Syntax Coloring Map For Ultrasound
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
ChainableLED KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
setColorRGB KEYWORD2
setColorHSB KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################

View file

@ -11,7 +11,7 @@ CONTIKI_WITH_IPV6 = 1
CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
PROJECT_SOURCEFILES += ${SKETCH}.cpp
PROJECT_SOURCEFILES += ${SKETCH}.cpp ChainableLED.cpp
# automatically build RESTful resources
REST_RESOURCES_DIR = ./resources

View file

@ -16,6 +16,7 @@ extern "C" {
#include "er-coap-engine.h"
#include "net/netstack.h"
#include "dev/button-sensor.h"
#include "ChainableLED.h"
extern resource_t
res_led,
@ -42,7 +43,12 @@ uip_ipaddr_t server_ipaddr, tmp_addr;
char server_resource [20] = "button";
extern int32_t event_counter;
#define NUM_LEDS 1
// Merkurboard grove i2c D8, D9
ChainableLED leds(8, 9, NUM_LEDS);
void setup (void)
{
// switch off the led
@ -53,6 +59,8 @@ void setup (void)
pinMode(bled_pin, OUTPUT);
digitalWrite(bled_pin, LOW);
bled_status=0;
// init chainable led
leds.init();
// sensors
SENSORS_ACTIVATE(button_sensor);
// init coap resourcen
@ -93,5 +101,14 @@ int coap_server_post(void)
void loop (void)
{
// test chainable led
static byte power=0;
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;
}