Updated WS2812 library to b5931ec4b0c3ee044e77297fc8387d9fe9046414

pull/18/head
Jesse Vincent 10 years ago
parent e79dd812d7
commit 2251cc92af

@ -19,9 +19,11 @@ WS2812::WS2812(uint16_t num_leds) {
count_led = num_leds;
pixels = (uint8_t*)malloc(count_led*3);
offsetGreen = 0;
offsetRed = 1;
offsetBlue = 2;
#ifdef RGB_ORDER_ON_RUNTIME
offsetGreen = 0;
offsetRed = 1;
offsetBlue = 2;
#endif
}
cRGB WS2812::get_crgb_at(uint16_t index) {
@ -32,10 +34,10 @@ cRGB WS2812::get_crgb_at(uint16_t index) {
uint16_t tmp;
tmp = index * 3;
px_value.r = pixels[tmp+offsetRed];
px_value.g = pixels[tmp+offsetGreen];
px_value.b = pixels[tmp+offsetBlue];
px_value.r = pixels[OFFSET_R(tmp)];
px_value.g = pixels[OFFSET_G(tmp)];
px_value.b = pixels[OFFSET_B(tmp)];
}
return px_value;
@ -48,20 +50,31 @@ uint8_t WS2812::set_crgb_at(uint16_t index, cRGB px_value) {
uint16_t tmp;
tmp = index * 3;
pixels[tmp+offsetGreen] = px_value.g;
pixels[tmp+offsetRed] = px_value.r;
pixels[tmp+offsetBlue] = px_value.b;
pixels[OFFSET_R(tmp)] = px_value.r;
pixels[OFFSET_G(tmp)] = px_value.g;
pixels[OFFSET_B(tmp)] = px_value.b;
return 0;
}
return 1;
}
uint8_t WS2812::set_subpixel_at(uint16_t index, uint8_t offset, uint8_t px_value) {
if (index < count_led) {
uint16_t tmp;
tmp = index * 3;
pixels[tmp + offset] = px_value;
return 0;
}
return 1;
}
void WS2812::sync() {
*ws2812_port_reg |= pinMask; // Enable DDR
ws2812_sendarray_mask(pixels,3*count_led,pinMask,(uint8_t*) ws2812_port,(uint8_t*) ws2812_port_reg );
}
#ifdef RGB_ORDER_ON_RUNTIME
void WS2812::setColorOrderGRB() { // Default color order
offsetGreen = 0;
offsetRed = 1;
@ -79,6 +92,7 @@ void WS2812::setColorOrderBRG() {
offsetRed = 1;
offsetGreen = 2;
}
#endif
WS2812::~WS2812() {

@ -8,6 +8,11 @@
*
* September 6, 2014 Added option to switch between most popular color orders
* (RGB, GRB, and BRG) -- Windell H. Oskay
*
* January 24, 2015 Added option to make color orders static again
* Moved cRGB to a header so it is easier to replace / expand
* (added SetHSV based on code from kasperkamperman.com)
* -- Freezy
*
* License: GNU GPL v2 (see License.txt)
*/
@ -32,7 +37,22 @@
#endif
#endif
struct cRGB { uint8_t g; uint8_t r; uint8_t b; };
//Easier to change cRGB into any other rgb struct
#include "cRGB.h"
// If you want to use the setColorOrder functions, enable this line
#define RGB_ORDER_ON_RUNTIME
#ifdef RGB_ORDER_ON_RUNTIME
#define OFFSET_R(r) r+offsetRed
#define OFFSET_G(g) g+offsetGreen
#define OFFSET_B(b) b+offsetBlue
#else
// CHANGE YOUR STATIC RGB ORDER HERE
#define OFFSET_R(r) r+1
#define OFFSET_G(g) g
#define OFFSET_B(b) b+2
#endif
class WS2812 {
public:
@ -47,18 +67,25 @@ public:
cRGB get_crgb_at(uint16_t index);
uint8_t set_crgb_at(uint16_t index, cRGB px_value);
uint8_t set_subpixel_at(uint16_t index, uint8_t offset, uint8_t px_value);
void sync();
#ifdef RGB_ORDER_ON_RUNTIME
void setColorOrderRGB();
void setColorOrderGRB();
void setColorOrderBRG();
#endif
private:
uint16_t count_led;
uint8_t *pixels;
#ifdef RGB_ORDER_ON_RUNTIME
uint8_t offsetRed;
uint8_t offsetGreen;
uint8_t offsetBlue;
#endif
void ws2812_sendarray_mask(uint8_t *array,uint16_t length, uint8_t pinmask,uint8_t *port, uint8_t *portreg);
@ -69,4 +96,4 @@ private:
#endif /* WS2812_H_ */
#endif /* WS2812_H_ */

115
cRGB.h

@ -0,0 +1,115 @@
#ifndef CRGB_H
#define CRGB_H
/*
Control a RGB led with Hue, Saturation and Brightness (HSB / HSV )
Hue is change by an analog input.
Brightness is changed by a fading function.
Saturation stays constant at 255
getRGB() function based on <http://www.codeproject.com/miscctrl/CPicker.asp>
dim_curve idea by Jims
created 05-01-2010 by kasperkamperman.com
*/
/*
dim_curve 'lookup table' to compensate for the nonlinearity of human vision.
Used in the getRGB function on saturation and brightness to make 'dimming' look more natural.
Exponential function used to create values below :
x from 0 - 255 : y = round(pow( 2.0, x+64/40.0) - 1)
*/
// uncomment this line if you use HSV is many projects
// #define USE_HSV
#ifdef USE_HSV
const byte dim_curve[] = {
0, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11,
11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15,
15, 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20,
20, 20, 21, 21, 22, 22, 22, 23, 23, 24, 24, 25, 25, 25, 26, 26,
27, 27, 28, 28, 29, 29, 30, 30, 31, 32, 32, 33, 33, 34, 35, 35,
36, 36, 37, 38, 38, 39, 40, 40, 41, 42, 43, 43, 44, 45, 46, 47,
48, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
63, 64, 65, 66, 68, 69, 70, 71, 73, 74, 75, 76, 78, 79, 81, 82,
83, 85, 86, 88, 90, 91, 93, 94, 96, 98, 99, 101, 103, 105, 107, 109,
110, 112, 114, 116, 118, 121, 123, 125, 127, 129, 132, 134, 136, 139, 141, 144,
146, 149, 151, 154, 157, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 190,
193, 196, 200, 203, 207, 211, 214, 218, 222, 226, 230, 234, 238, 242, 248, 255,
};
#endif
struct cRGB {
uint8_t g;
uint8_t r;
uint8_t b;
#ifdef USE_HSV
void SetHSV(int hue, byte sat, byte val) {
/* convert hue, saturation and brightness ( HSB/HSV ) to RGB
The dim_curve is used only on brightness/value and on saturation (inverted).
This looks the most natural.
*/
val = dim_curve[val];
sat = 255 - dim_curve[255 - sat];
int base;
if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
r = val;
g = val;
b = val;
}
else {
base = ((255 - sat) * val) >> 8;
switch (hue / 60) {
case 0:
r = val;
g = (((val - base)*hue) / 60) + base;
b = base;
break;
case 1:
r = (((val - base)*(60 - (hue % 60))) / 60) + base;
g = val;
b = base;
break;
case 2:
r = base;
g = val;
b = (((val - base)*(hue % 60)) / 60) + base;
break;
case 3:
r = base;
g = (((val - base)*(60 - (hue % 60))) / 60) + base;
b = val;
break;
case 4:
r = (((val - base)*(hue % 60)) / 60) + base;
g = base;
b = val;
break;
case 5:
r = val;
g = base;
b = (((val - base)*(60 - (hue % 60))) / 60) + base;
break;
}
}
}
#endif
};
#endif
Loading…
Cancel
Save