some WS2812B improvements

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

View File

@@ -9,25 +9,41 @@ 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) {
;
}
public:
/** get X-percent [0:100] of this color = darker/brighter */
Color inline getPercent(const int percent) const {
return Color(r*percent/100, g*percent/100, b*percent/100);
} }
Color(const uint8_t r, const uint8_t g, const uint8_t b) : r(r), g(g), b(b) { /** mix the two given colors */
; static Color mix(const Color c1, const Color c2, int percentC1) {
} return Color(
((c1.r * percentC1) + (c2.r * (100-percentC1))) / 100,
((c1.g * percentC1) + (c2.g * (100-percentC1))) / 100,
((c1.b * percentC1) + (c2.b * (100-percentC1))) / 100
);
}
void setRGB(const uint8_t r, const uint8_t g, const uint8_t b) { static inline Color fromRGB(const uint8_t r, const uint8_t g, const uint8_t b) {
this->r = r; return Color(r,g,b);
this->g = g; }
this->b = b;
}
void setOff() { static inline Color fromHSV(const uint8_t h, const uint8_t s, const uint8_t v) {
this->r = 0; Color c; c.setHSV(h,s,v); return c;
this->g = 0; }
this->b = 0;
}
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 {
@@ -106,47 +140,13 @@ template <int numLEDs> class WS2812B {
} }
/** flush configured changes */ /** flush configured changes */
void flush() { void flush() {
LED_SET_PIN_TO_OUTPUT; LED_SET_PIN_TO_OUTPUT;
//cli(); // cli();
//reset(); // process each LED
/*
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
for (uint8_t i = 0; i < numLEDs; ++i) { for (uint8_t i = 0; i < numLEDs; ++i) {
// send each LEDs 24-bit GRB data // send each LEDs 24-bit GRB data
@@ -163,25 +163,31 @@ 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();
} }