some WS2812B improvements

This commit is contained in:
2018-06-16 12:22:36 +02:00
parent 79c12bb007
commit a8bcdf07c4

View File

@@ -9,24 +9,40 @@ struct Color {
uint8_t g; uint8_t g;
uint8_t b; uint8_t b;
Color() : r(0), g(0), b(0) { /** no-init */
Color() {
; ;
} }
private:
/** Hidden ctor. RGB */
Color(const uint8_t r, const uint8_t g, const uint8_t b) : r(r), g(g), b(b) { Color(const uint8_t r, const uint8_t g, const uint8_t b) : r(r), g(g), b(b) {
; ;
} }
void setRGB(const uint8_t r, const uint8_t g, const uint8_t b) { public:
this->r = r;
this->g = g; /** get X-percent [0:100] of this color = darker/brighter */
this->b = b; Color inline getPercent(const int percent) const {
return Color(r*percent/100, g*percent/100, b*percent/100);
} }
void setOff() { /** mix the two given colors */
this->r = 0; static Color mix(const Color c1, const Color c2, int percentC1) {
this->g = 0; return Color(
this->b = 0; ((c1.r * percentC1) + (c2.r * (100-percentC1))) / 100,
((c1.g * percentC1) + (c2.g * (100-percentC1))) / 100,
((c1.b * percentC1) + (c2.b * (100-percentC1))) / 100
);
}
static inline Color fromRGB(const uint8_t r, const uint8_t g, const uint8_t b) {
return Color(r,g,b);
}
static inline Color fromHSV(const uint8_t h, const uint8_t s, const uint8_t v) {
Color c; c.setHSV(h,s,v); return c;
} }
void setHSV(const uint8_t h, const uint8_t s, const uint8_t v) { void setHSV(const uint8_t h, const uint8_t s, const uint8_t v) {
@@ -63,6 +79,24 @@ struct Color {
} }
void setRGB(const uint8_t r, const uint8_t g, const uint8_t b) {
this->r = r;
this->g = g;
this->b = b;
}
void setOff() {
this->r = 0;
this->g = 0;
this->b = 0;
}
/** add two colors */
Color operator + (const Color o) const {
return Color(r+o.r, g+o.g, b+o.g);
}
}; };
template <int numLEDs> class WS2812B { template <int numLEDs> class WS2812B {
@@ -110,41 +144,7 @@ template <int numLEDs> class WS2812B {
LED_SET_PIN_TO_OUTPUT; LED_SET_PIN_TO_OUTPUT;
//cli(); // cli();
//reset();
/*
send0();
send1();
send0();
send1();
send0();
send1();
send0();
send1();
*/
/*
send0();
send0();
send0();
send0();
send0();
send0();
send0();
send0();
send1();
send1();
send1();
send1();
send1();
send1();
send1();
// send0();
// send0();
*/
// process each LED // process each LED
for (uint8_t i = 0; i < numLEDs; ++i) { for (uint8_t i = 0; i < numLEDs; ++i) {
@@ -165,23 +165,29 @@ template <int numLEDs> class WS2812B {
reset(); reset();
//sei();
// sei();
} }
private: private:
inline void sendByte(uint8_t b) { inline void sendByte(uint8_t b) {
for (uint8_t i = 0; i < 8; ++i) { // for (uint8_t i = 0; i < 8; ++i) {
if (b & 0b10000000) { // if (b & 0b10000000) {
send1(); // send1();
} else { // } else {
send0(); // send0();
} // }
b <<= 1; // b <<= 1;
} // }
if (b & 0b10000000) {send1();} else {send0();}
if (b & 0b01000000) {send1();} else {send0();}
if (b & 0b00100000) {send1();} else {send0();}
if (b & 0b00010000) {send1();} else {send0();}
if (b & 0b00001000) {send1();} else {send0();}
if (b & 0b00000100) {send1();} else {send0();}
if (b & 0b00000010) {send1();} else {send0();}
if (b & 0b00000001) {send1();} else {send0();}
} }
__attribute__((always_inline)) inline void send1() { // 800ns high, 450ns low __attribute__((always_inline)) inline void send1() { // 800ns high, 450ns low
@@ -221,6 +227,7 @@ template <int numLEDs> class WS2812B {
delay50(); delay50();
} }
/*
__attribute__((always_inline)) inline void delay200() { __attribute__((always_inline)) inline void delay200() {
delay100(); delay100();
delay100(); delay100();
@@ -249,10 +256,17 @@ template <int numLEDs> class WS2812B {
delay100(); delay100();
delay100(); delay100();
} }
*/
__attribute__((always_inline)) inline void delay800() { __attribute__((always_inline)) inline void delay800() {
delay400(); delay100();
delay400(); delay100();
delay100();
delay100();
delay100();
delay100();
delay100();
delay100();
} }