From a8bcdf07c4bdcdf46fa83c1f1c710410d5df3260 Mon Sep 17 00:00:00 2001 From: kazu Date: Sat, 16 Jun 2018 12:22:36 +0200 Subject: [PATCH] some WS2812B improvements --- ext/led/WS2812B.h | 148 +++++++++++++++++++++++++--------------------- 1 file changed, 81 insertions(+), 67 deletions(-) diff --git a/ext/led/WS2812B.h b/ext/led/WS2812B.h index d3e656b..9604cd5 100644 --- a/ext/led/WS2812B.h +++ b/ext/led/WS2812B.h @@ -9,25 +9,41 @@ struct Color { uint8_t g; 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) { - this->r = r; - this->g = g; - this->b = b; - } + static inline Color fromRGB(const uint8_t r, const uint8_t g, const uint8_t b) { + return Color(r,g,b); + } - void setOff() { - this->r = 0; - this->g = 0; - this->b = 0; - } + 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) { @@ -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 class WS2812B { @@ -106,47 +140,13 @@ template class WS2812B { } /** flush configured changes */ - void flush() { + void flush() { - 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) { // send each LEDs 24-bit GRB data @@ -163,25 +163,31 @@ template class WS2812B { } - reset(); - - - // sei(); + reset(); + //sei(); } private: inline void sendByte(uint8_t b) { - for (uint8_t i = 0; i < 8; ++i) { - if (b & 0b10000000) { - send1(); - } else { - send0(); - } - b <<= 1; - } +// for (uint8_t i = 0; i < 8; ++i) { +// if (b & 0b10000000) { +// send1(); +// } else { +// send0(); +// } +// 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 @@ -221,6 +227,7 @@ template class WS2812B { delay50(); } + /* __attribute__((always_inline)) inline void delay200() { delay100(); delay100(); @@ -249,10 +256,17 @@ template class WS2812B { delay100(); delay100(); } +*/ __attribute__((always_inline)) inline void delay800() { - delay400(); - delay400(); + delay100(); + delay100(); + delay100(); + delay100(); + delay100(); + delay100(); + delay100(); + delay100(); }