From 6bed360f5f44ff75f9954bd3e3add0a9836b449b Mon Sep 17 00:00:00 2001 From: Bruno Date: Fri, 5 Jan 2018 23:43:31 +0100 Subject: [PATCH] Cleanup tab characters --- src/Ala.cpp | 13 +- src/Ala.h | 26 +- src/AlaLed.cpp | 438 ++++++++++----------- src/AlaLed.h | 20 +- src/AlaLedRgb.cpp | 974 +++++++++++++++++++++++----------------------- src/AlaLedRgb.h | 136 +++---- 6 files changed, 800 insertions(+), 807 deletions(-) diff --git a/src/Ala.cpp b/src/Ala.cpp index 9b65b83..1d718c8 100644 --- a/src/Ala.cpp +++ b/src/Ala.cpp @@ -68,18 +68,11 @@ AlaColor alaPalHeat_[] = }; AlaPalette alaPalHeat = { 4, alaPalHeat_ }; -/* -AlaColor alaPalFire_[] = -{ - 0x000000, 0x440000, - 0x990000, 0xFF0000, - 0xFF6600, 0xFFCC00 -}; -*/ + AlaColor alaPalFire_[] = { - 0x000000, 0x220000, - 0x880000, 0xFF0000, + 0x000000, 0x220000, + 0x880000, 0xFF0000, 0xFF6600, 0xFFCC00 }; AlaPalette alaPalFire = { 6, alaPalFire_ }; diff --git a/src/Ala.h b/src/Ala.h index 7496f71..9ed5da3 100644 --- a/src/Ala.h +++ b/src/Ala.h @@ -95,11 +95,11 @@ struct AlaColor }; uint8_t raw[3]; }; - + inline AlaColor() __attribute__((always_inline)) { } - + // allow construction from R, G, B inline AlaColor( uint8_t ir, uint8_t ig, uint8_t ib) __attribute__((always_inline)) : r(ir), g(ig), b(ib) @@ -111,8 +111,8 @@ struct AlaColor : r((colorcode >> 16) & 0xFF), g((colorcode >> 8) & 0xFF), b((colorcode >> 0) & 0xFF) { } - - bool operator == (const AlaColor &c) const + + bool operator == (const AlaColor &c) const { return(this->r == c.r and this->g == c.g and this->b == c.b); } @@ -137,7 +137,7 @@ struct AlaColor int b0 = x*(color.b - b) + b; return AlaColor(r0, g0, b0); } - + AlaColor scale(float k) { int r0 = min(r*k, 255); @@ -145,8 +145,8 @@ struct AlaColor int b0 = min(b*k, 255); return AlaColor(r0, g0, b0); } - - + + typedef enum { Aqua = 0x00FFFF, Black = 0x000000, @@ -177,7 +177,7 @@ struct AlaPalette { int numColors; AlaColor *colors; - + /** * Get the interpolated color from the palette. * The argument is a floating number between 0 and numColors. @@ -186,19 +186,19 @@ struct AlaPalette { int i0 = (int)i%(numColors); int i1 = (int)(i+1)%(numColors); - + // decimal part is used to interpolate between the two colors float t0 = i - trunc(i); //float t0 = i - (int)i; return colors[i0].interpolate(colors[i1], t0); } - - bool operator == (const AlaPalette &c) const + + bool operator == (const AlaPalette &c) const { if (!(this->numColors == c.numColors)) return false; - + for(int i=0; icolors[i] == c.colors[i])) @@ -206,7 +206,7 @@ struct AlaPalette } return true; } - + }; diff --git a/src/AlaLed.cpp b/src/AlaLed.cpp index 17539f4..f8a2aec 100644 --- a/src/AlaLed.cpp +++ b/src/AlaLed.cpp @@ -5,111 +5,111 @@ AlaLed::AlaLed() { - maxOut = 255; - speed = 1000; - animSeqLen = 0; - lastRefreshTime = 0; - refreshMillis = 1000/50; + maxOut = 255; + speed = 1000; + animSeqLen = 0; + lastRefreshTime = 0; + refreshMillis = 1000/50; } void AlaLed::initPWM(byte pin) { - byte *pins_ = (byte *)malloc(1); + byte *pins_ = (byte *)malloc(1); pins_[0] = pin; - + initPWM(1, pins_); } void AlaLed::initPWM(int numLeds, byte *pins) { - this->driver = ALA_PWM; - this->numLeds = numLeds; - this->pins = pins; + this->driver = ALA_PWM; + this->numLeds = numLeds; + this->pins = pins; - for (int x=0; xdriver = ALA_TLC5940; - this->numLeds = numLeds; - this->pins = pins; + this->driver = ALA_TLC5940; + this->numLeds = numLeds; + this->pins = pins; - // allocate and clear leds array - leds = (byte *)malloc(numLeds); - memset(leds, 0, numLeds); + // allocate and clear leds array + leds = (byte *)malloc(numLeds); + memset(leds, 0, numLeds); - // call Tlc.init only once - static bool isTlcInit = false; - if(!isTlcInit) - { - Tlc.init(0); - isTlcInit=true; - } + // call Tlc.init only once + static bool isTlcInit = false; + if(!isTlcInit) + { + Tlc.init(0); + isTlcInit=true; + } } - + void AlaLed::setBrightness(byte maxOut) { - this->maxOut = maxOut; + this->maxOut = maxOut; } void AlaLed::setRefreshRate(int refreshRate) { - this->refreshMillis = 1000/refreshRate; + this->refreshMillis = 1000/refreshRate; } void AlaLed::setAnimation(int animation, long speed) { - if (this->animation == animation && this->speed == speed) - return; - - this->animation = animation; - this->speed = speed; + if (this->animation == animation && this->speed == speed) + return; - setAnimationFunc(animation); - animStartTime = millis(); + this->animation = animation; + this->speed = speed; + + setAnimationFunc(animation); + animStartTime = millis(); } void AlaLed::setAnimation(AlaSeq animSeq[]) { - this->animSeq = animSeq; + this->animSeq = animSeq; - // initialize animSeqDuration and animSeqLen variables + // initialize animSeqDuration and animSeqLen variables animSeqDuration = 0; for(animSeqLen=0; animSeq[animSeqLen].animation!=ALA_ENDSEQ; animSeqLen++) { - animSeqDuration = animSeqDuration + animSeq[animSeqLen].duration; + animSeqDuration = animSeqDuration + animSeq[animSeqLen].duration; } } void AlaLed::nextAnimation() { - currAnim = (currAnim+1)%animSeqLen; + currAnim = (currAnim+1)%animSeqLen; } void AlaLed::runAnimation() { // skip the refresh if now enough time has passed since last update - if (millis() < lastRefreshTime + refreshMillis) - return; - lastRefreshTime = millis(); - - if (animSeqLen != 0) + if (millis() < lastRefreshTime + refreshMillis) + return; + lastRefreshTime = millis(); + + if (animSeqLen != 0) { // calculate the current animation function // NOTE: this can be optimized @@ -118,35 +118,35 @@ void AlaLed::runAnimation() for(int i=0; i=c && t<(c+animSeq[i].duration)) { - setAnimation(animSeq[i].animation, animSeq[i].speed); - break; + setAnimation(animSeq[i].animation, animSeq[i].speed); + break; } c = c + animSeq[i].duration; } } - // can the animation function and refresh if necessary + // can the animation function and refresh if necessary if (animFunc != NULL) { - (this->*animFunc)(); - - if(driver==ALA_PWM) - { - for(int i=0; i*animFunc)(); + + if(driver==ALA_PWM) + { + for(int i=0; is-l) - leds[x] = maxOut; - else - leds[x] = 0; - } + for(int x=0; xs-l) + leds[x] = maxOut; + else + leds[x] = 0; + } } void AlaLed::barShiftLeft() { - int l = numLeds/2+1; // length of the bar + int l = numLeds/2+1; // length of the bar - int s = map(getStep(animStartTime, speed, 1000), 0, 1000, 0, numLeds+l); + int s = map(getStep(animStartTime, speed, 1000), 0, 1000, 0, numLeds+l); - for(int x=0; xs-l) - leds[numLeds-x-1] = maxOut; - else - leds[numLeds-x-1] = 0; - } + for(int x=0; xs-l) + leds[numLeds-x-1] = maxOut; + else + leds[numLeds-x-1] = 0; + } } void AlaLed::pixelSmoothBounce() { - // see larsonScanner - float t = getStepFloat(animStartTime, speed, 2*numLeds-2); + // see larsonScanner + float t = getStepFloat(animStartTime, speed, 2*numLeds-2); - for(int x=0; x7, 3-11, 4-14 - float t = getStepFloat(animStartTime, speed, 2*numLeds+(l*4-1)); + float l = numLeds/4; // 2>7, 3-11, 4-14 + float t = getStepFloat(animStartTime, speed, 2*numLeds+(l*4-1)); - for(int x=0; x xxx resistor to GND * pin 10 > +5V * pin 11 > Arduino 3 - * + * * It can be used also to drive a single led. */ void initTLC5940(int numLeds, byte *pins); @@ -56,14 +56,14 @@ class AlaLed * May be useful to reduce flickering in some cases. */ void setRefreshRate(int refreshRate); - - int getRefreshRate(); + + int getRefreshRate(); void setAnimation(int animation, long speed); void setAnimation(AlaSeq animSeq[]); void runAnimation(); - void nextAnimation(); + void nextAnimation(); private: @@ -76,36 +76,36 @@ class AlaLed void sparkle(); void sparkle2(); void strobo(); - + void pixelShiftRight(); void pixelShiftLeft(); void pixelBounce(); void pixelSmoothShiftRight(); void pixelSmoothShiftLeft(); - void pixelSmoothBounce(); + void pixelSmoothBounce(); void comet(); void barShiftRight(); void barShiftLeft(); void larsonScanner(); void larsonScanner2(); - + void fadeIn(); void fadeOut(); void fadeInOut(); void glow(); - void flame(); + void flame(); byte driver; // type of led driver: ALA_PWM, ALA_TLC5940 byte *pins; // pins where the leds are attached to byte *leds; // array to store leds brightness values int numLeds; // number of leds - + int maxOut; int refreshMillis; int animation; - int currAnim; + int currAnim; long speed; AlaSeq *animSeq; int animSeqLen; diff --git a/src/AlaLedRgb.cpp b/src/AlaLedRgb.cpp index 441904f..9a8f63e 100644 --- a/src/AlaLedRgb.cpp +++ b/src/AlaLedRgb.cpp @@ -9,232 +9,232 @@ AlaLedRgb::AlaLedRgb() { - // set default values - - maxOut = 0xFFFFFF; - speed = 1000; - animSeqLen = 0; - lastRefreshTime = 0; - refreshMillis = 1000/50; - pxPos = NULL; - pxSpeed = NULL; + // set default values + + maxOut = 0xFFFFFF; + speed = 1000; + animSeqLen = 0; + lastRefreshTime = 0; + refreshMillis = 1000/50; + pxPos = NULL; + pxSpeed = NULL; } void AlaLedRgb::initPWM(byte pinRed, byte pinGreen, byte pinBlue) { - byte *pins_ = (byte *)malloc(3); + byte *pins_ = (byte *)malloc(3); pins_[0] = pinRed; pins_[1] = pinGreen; pins_[2] = pinBlue; - + initPWM(1, pins_); } void AlaLedRgb::initPWM(int numLeds, byte *pins) { - this->driver = ALA_PWM; - this->numLeds = numLeds; - this->pins = pins; - - for (int x=0; x<3*numLeds ; x++) - { - pinMode(pins[x], OUTPUT); - } + this->driver = ALA_PWM; + this->numLeds = numLeds; + this->pins = pins; + + for (int x=0; x<3*numLeds ; x++) + { + pinMode(pins[x], OUTPUT); + } - // allocate and clear leds array - leds = (AlaColor *)malloc(3*numLeds); - memset(leds, 0, 3*numLeds); + // allocate and clear leds array + leds = (AlaColor *)malloc(3*numLeds); + memset(leds, 0, 3*numLeds); } void AlaLedRgb::initTLC5940(int numLeds, byte *pins) { - this->driver = ALA_TLC5940; - this->numLeds = numLeds; - this->pins = pins; + this->driver = ALA_TLC5940; + this->numLeds = numLeds; + this->pins = pins; - // allocate and clear leds array - leds = (AlaColor *)malloc(3*numLeds); - memset(leds, 0, 3*numLeds); + // allocate and clear leds array + leds = (AlaColor *)malloc(3*numLeds); + memset(leds, 0, 3*numLeds); - // call Tlc.init only once - static bool isTlcInit = false; - if(!isTlcInit) - { - Tlc.init(0); - isTlcInit=true; - } + // call Tlc.init only once + static bool isTlcInit = false; + if(!isTlcInit) + { + Tlc.init(0); + isTlcInit=true; + } } void AlaLedRgb::initWS2812(int numLeds, byte pin, byte type) { - this->driver = ALA_WS2812; - this->numLeds = numLeds; - this->pins = 0; + this->driver = ALA_WS2812; + this->numLeds = numLeds; + this->pins = 0; + + // allocate and clear leds array + leds = (AlaColor *)malloc(3*numLeds); + memset(leds, 0, 3*numLeds); - // allocate and clear leds array - leds = (AlaColor *)malloc(3*numLeds); - memset(leds, 0, 3*numLeds); + neopixels = new Adafruit_NeoPixel(numLeds, pin, type); - neopixels = new Adafruit_NeoPixel(numLeds, pin, type); - - neopixels->begin(); + neopixels->begin(); } void AlaLedRgb::setBrightness(AlaColor maxOut) { - this->maxOut = maxOut; + this->maxOut = maxOut; } void AlaLedRgb::setRefreshRate(int refreshRate) { - this->refreshMillis = 1000/refreshRate; + this->refreshMillis = 1000/refreshRate; } int AlaLedRgb::getCurrentRefreshRate() { - return refreshRate; + return refreshRate; } - + void AlaLedRgb::setAnimation(int animation, long speed, AlaColor color) { - // is there any change? - if (this->animation == animation && this->speed == speed && this->palette.numColors == 1 && this->palette.colors[0] == color) - return; - - // delete any previously allocated array - if (pxPos!=NULL) - { delete[] pxPos; pxPos=NULL; } - if (pxSpeed!=NULL) - { delete[] pxSpeed; pxSpeed=NULL; } - - this->animation = animation; - this->speed = speed; - - this->palette.numColors = 1; - this->palette.colors = (AlaColor*)malloc(3); - this->palette.colors[0] = color; - - setAnimationFunc(animation); - animStartTime = millis(); + // is there any change? + if (this->animation == animation && this->speed == speed && this->palette.numColors == 1 && this->palette.colors[0] == color) + return; + + // delete any previously allocated array + if (pxPos!=NULL) + { delete[] pxPos; pxPos=NULL; } + if (pxSpeed!=NULL) + { delete[] pxSpeed; pxSpeed=NULL; } + + this->animation = animation; + this->speed = speed; + + this->palette.numColors = 1; + this->palette.colors = (AlaColor*)malloc(3); + this->palette.colors[0] = color; + + setAnimationFunc(animation); + animStartTime = millis(); } void AlaLedRgb::setAnimation(int animation, long speed, AlaPalette palette) { - // is there any change? - if (this->animation == animation && this->speed == speed && this->palette == palette) - return; - - // delete any previously allocated array - if (pxPos!=NULL) - { delete[] pxPos; pxPos=NULL; } - if (pxSpeed!=NULL) - { delete[] pxSpeed; pxSpeed=NULL; } - - this->animation = animation; - this->speed = speed; - this->palette = palette; + // is there any change? + if (this->animation == animation && this->speed == speed && this->palette == palette) + return; + + // delete any previously allocated array + if (pxPos!=NULL) + { delete[] pxPos; pxPos=NULL; } + if (pxSpeed!=NULL) + { delete[] pxSpeed; pxSpeed=NULL; } + + this->animation = animation; + this->speed = speed; + this->palette = palette; - setAnimationFunc(animation); - animStartTime = millis(); + setAnimationFunc(animation); + animStartTime = millis(); } void AlaLedRgb::setAnimation(AlaSeq animSeq[]) { - this->animSeq = animSeq; + this->animSeq = animSeq; - // initialize animSeqDuration and animSeqLen variables + // initialize animSeqDuration and animSeqLen variables animSeqDuration = 0; for(animSeqLen=0; animSeq[animSeqLen].animation!=ALA_ENDSEQ; animSeqLen++) { - animSeqDuration = animSeqDuration + animSeq[animSeqLen].duration; + animSeqDuration = animSeqDuration + animSeq[animSeqLen].duration; } - animSeqStartTime = millis(); + animSeqStartTime = millis(); } void AlaLedRgb::nextAnimation() { - currAnim = (currAnim+1)%animSeqLen; + currAnim = (currAnim+1)%animSeqLen; } bool AlaLedRgb::runAnimation() { // skip the refresh if not enough time has passed since last update - unsigned long cTime = millis(); - if (cTime < lastRefreshTime + refreshMillis) - return false; - - // calculate real refresh rate - refreshRate = 1000/(cTime - lastRefreshTime); - - lastRefreshTime = cTime; - - - if (animSeqLen != 0) - { - if(animSeq[currAnim].duration == 0) - { - setAnimation(animSeq[currAnim].animation, animSeq[currAnim].speed, animSeq[currAnim].palette); - } - else - { - long c = 0; - long t = (cTime-animSeqStartTime) % animSeqDuration; - for(int i=0; i=c && t<(c+animSeq[i].duration)) - { - setAnimation(animSeq[i].animation, animSeq[i].speed, animSeq[i].palette); - break; - } - c = c + animSeq[i].duration; - } - } - } - + unsigned long cTime = millis(); + if (cTime < lastRefreshTime + refreshMillis) + return false; + + // calculate real refresh rate + refreshRate = 1000/(cTime - lastRefreshTime); + + lastRefreshTime = cTime; + + + if (animSeqLen != 0) + { + if(animSeq[currAnim].duration == 0) + { + setAnimation(animSeq[currAnim].animation, animSeq[currAnim].speed, animSeq[currAnim].palette); + } + else + { + long c = 0; + long t = (cTime-animSeqStartTime) % animSeqDuration; + for(int i=0; i=c && t<(c+animSeq[i].duration)) + { + setAnimation(animSeq[i].animation, animSeq[i].speed, animSeq[i].palette); + break; + } + c = c + animSeq[i].duration; + } + } + } + if (animFunc != NULL) { - (this->*animFunc)(); - - // use an 8 bit shift to divide by 256 - - if(driver==ALA_PWM) - { - for(int i=0; i>8); - analogWrite(pins[j+1], (leds[i].g*maxOut.g)>>8); - analogWrite(pins[j+2], (leds[i].b*maxOut.b)>>8); - } - } - else if(driver==ALA_TLC5940) - { - // TLC5940 maximum output is 4095 so shifts only 4 bits - for(int i=0; i>4); - Tlc.set(pins[j+1], (leds[i].g*maxOut.g)>>4); - Tlc.set(pins[j+2], (leds[i].b*maxOut.b)>>4); - } - Tlc.update(); - } - else if(driver==ALA_WS2812) - { - // this is not really so smart... - for(int i=0; isetPixelColor(i, neopixels->Color((leds[i].r*maxOut.r)>>8, (leds[i].g*maxOut.g)>>8, (leds[i].b*maxOut.b)>>8)); - - neopixels->show(); - } - } - - return true; + (this->*animFunc)(); + + // use an 8 bit shift to divide by 256 + + if(driver==ALA_PWM) + { + for(int i=0; i>8); + analogWrite(pins[j+1], (leds[i].g*maxOut.g)>>8); + analogWrite(pins[j+2], (leds[i].b*maxOut.b)>>8); + } + } + else if(driver==ALA_TLC5940) + { + // TLC5940 maximum output is 4095 so shifts only 4 bits + for(int i=0; i>4); + Tlc.set(pins[j+1], (leds[i].g*maxOut.g)>>4); + Tlc.set(pins[j+2], (leds[i].b*maxOut.b)>>4); + } + Tlc.update(); + } + else if(driver==ALA_WS2812) + { + // this is not really so smart... + for(int i=0; isetPixelColor(i, neopixels->Color((leds[i].r*maxOut.r)>>8, (leds[i].g*maxOut.g)>>8, (leds[i].b*maxOut.b)>>8)); + + neopixels->show(); + } + } + + return true; } @@ -243,116 +243,116 @@ bool AlaLedRgb::runAnimation() void AlaLedRgb::setAnimationFunc(int animation) { - switch(animation) - { - case ALA_ON: animFunc = &AlaLedRgb::on; break; - case ALA_OFF: animFunc = &AlaLedRgb::off; break; - case ALA_BLINK: animFunc = &AlaLedRgb::blink; break; - case ALA_BLINKALT: animFunc = &AlaLedRgb::blinkAlt; break; - case ALA_SPARKLE: animFunc = &AlaLedRgb::sparkle; break; - case ALA_SPARKLE2: animFunc = &AlaLedRgb::sparkle2; break; - case ALA_STROBO: animFunc = &AlaLedRgb::strobo; break; - case ALA_CYCLECOLORS: animFunc = &AlaLedRgb::cycleColors; break; - - case ALA_PIXELSHIFTRIGHT: animFunc = &AlaLedRgb::pixelShiftRight; break; - case ALA_PIXELSHIFTLEFT: animFunc = &AlaLedRgb::pixelShiftLeft; break; - case ALA_PIXELBOUNCE: animFunc = &AlaLedRgb::pixelBounce; break; - case ALA_PIXELSMOOTHSHIFTRIGHT: animFunc = &AlaLedRgb::pixelSmoothShiftRight; break; - case ALA_PIXELSMOOTHSHIFTLEFT: animFunc = &AlaLedRgb::pixelSmoothShiftLeft; break; - case ALA_PIXELSMOOTHBOUNCE: animFunc = &AlaLedRgb::pixelSmoothBounce; break; - case ALA_COMET: animFunc = &AlaLedRgb::comet; break; - case ALA_COMETCOL: animFunc = &AlaLedRgb::cometCol; break; - case ALA_MOVINGBARS: animFunc = &AlaLedRgb::movingBars; break; - case ALA_MOVINGGRADIENT: animFunc = &AlaLedRgb::movingGradient; break; - case ALA_LARSONSCANNER: animFunc = &AlaLedRgb::larsonScanner; break; - case ALA_LARSONSCANNER2: animFunc = &AlaLedRgb::larsonScanner2; break; - - case ALA_FADEIN: animFunc = &AlaLedRgb::fadeIn; break; - case ALA_FADEOUT: animFunc = &AlaLedRgb::fadeOut; break; - case ALA_FADEINOUT: animFunc = &AlaLedRgb::fadeInOut; break; - case ALA_GLOW: animFunc = &AlaLedRgb::glow; break; - case ALA_PIXELSFADECOLORS: animFunc = &AlaLedRgb::pixelsFadeColors; break; - case ALA_FADECOLORS: animFunc = &AlaLedRgb::fadeColors; break; - case ALA_FADECOLORSLOOP: animFunc = &AlaLedRgb::fadeColorsLoop; break; - - case ALA_FIRE: animFunc = &AlaLedRgb::fire; break; - case ALA_BOUNCINGBALLS: animFunc = &AlaLedRgb::bouncingBalls; break; - case ALA_BUBBLES: animFunc = &AlaLedRgb::bubbles; break; - - default: animFunc = &AlaLedRgb::off; - } + switch(animation) + { + case ALA_ON: animFunc = &AlaLedRgb::on; break; + case ALA_OFF: animFunc = &AlaLedRgb::off; break; + case ALA_BLINK: animFunc = &AlaLedRgb::blink; break; + case ALA_BLINKALT: animFunc = &AlaLedRgb::blinkAlt; break; + case ALA_SPARKLE: animFunc = &AlaLedRgb::sparkle; break; + case ALA_SPARKLE2: animFunc = &AlaLedRgb::sparkle2; break; + case ALA_STROBO: animFunc = &AlaLedRgb::strobo; break; + case ALA_CYCLECOLORS: animFunc = &AlaLedRgb::cycleColors; break; + + case ALA_PIXELSHIFTRIGHT: animFunc = &AlaLedRgb::pixelShiftRight; break; + case ALA_PIXELSHIFTLEFT: animFunc = &AlaLedRgb::pixelShiftLeft; break; + case ALA_PIXELBOUNCE: animFunc = &AlaLedRgb::pixelBounce; break; + case ALA_PIXELSMOOTHSHIFTRIGHT: animFunc = &AlaLedRgb::pixelSmoothShiftRight; break; + case ALA_PIXELSMOOTHSHIFTLEFT: animFunc = &AlaLedRgb::pixelSmoothShiftLeft; break; + case ALA_PIXELSMOOTHBOUNCE: animFunc = &AlaLedRgb::pixelSmoothBounce; break; + case ALA_COMET: animFunc = &AlaLedRgb::comet; break; + case ALA_COMETCOL: animFunc = &AlaLedRgb::cometCol; break; + case ALA_MOVINGBARS: animFunc = &AlaLedRgb::movingBars; break; + case ALA_MOVINGGRADIENT: animFunc = &AlaLedRgb::movingGradient; break; + case ALA_LARSONSCANNER: animFunc = &AlaLedRgb::larsonScanner; break; + case ALA_LARSONSCANNER2: animFunc = &AlaLedRgb::larsonScanner2; break; + + case ALA_FADEIN: animFunc = &AlaLedRgb::fadeIn; break; + case ALA_FADEOUT: animFunc = &AlaLedRgb::fadeOut; break; + case ALA_FADEINOUT: animFunc = &AlaLedRgb::fadeInOut; break; + case ALA_GLOW: animFunc = &AlaLedRgb::glow; break; + case ALA_PIXELSFADECOLORS: animFunc = &AlaLedRgb::pixelsFadeColors; break; + case ALA_FADECOLORS: animFunc = &AlaLedRgb::fadeColors; break; + case ALA_FADECOLORSLOOP: animFunc = &AlaLedRgb::fadeColorsLoop; break; + + case ALA_FIRE: animFunc = &AlaLedRgb::fire; break; + case ALA_BOUNCINGBALLS: animFunc = &AlaLedRgb::bouncingBalls; break; + case ALA_BUBBLES: animFunc = &AlaLedRgb::bubbles; break; + + default: animFunc = &AlaLedRgb::off; + } } void AlaLedRgb::on() { - for(int i=0; i7, 3-11, 4-14 - float t = getStepFloat(animStartTime, speed, 2*numLeds+(l*4-1)); - AlaColor c = palette.getPalColor(getStepFloat(animStartTime, speed, palette.numColors)); + float l = numLeds/4; // 2>7, 3-11, 4-14 + float t = getStepFloat(animStartTime, speed, 2*numLeds+(l*4-1)); + AlaColor c = palette.getPalColor(getStepFloat(animStartTime, speed, palette.numColors)); - for(int x=0; x=3; k--) - { + { heat[k] = ((int)heat[k - 1] + (int)heat[k - 2] + (int)heat[k - 3] ) / 3; } - + // Step 3. Randomly ignite new 'sparks' of heat near the bottom if(random(255) < SPARKING) - { + { int y = random(7); heat[y] = min(heat[y] + random(160, 255), 255); } // Step 4. Map from heat cells to LED colors for(int j=0; j-0.04 and pxSpeed[i]<0 and pxPos[i]>0 and pxPos[i]<0.1) - pxSpeed[i]=(0.09)-((float)random(10)/1000); - - pxPos[i] = pxPos[i] + pxSpeed[i]; - if(pxPos[i]>=1) - { - pxPos[i]=1; - } - if(pxPos[i]<0) - { - pxPos[i]=-pxPos[i]; - pxSpeed[i]=-0.91*pxSpeed[i]; - } - - pxSpeed[i] = pxSpeed[i]-speedReduction; - } - - for (int x=0; x-0.04 and pxSpeed[i]<0 and pxPos[i]>0 and pxPos[i]<0.1) + pxSpeed[i]=(0.09)-((float)random(10)/1000); + + pxPos[i] = pxPos[i] + pxSpeed[i]; + if(pxPos[i]>=1) + { + pxPos[i]=1; + } + if(pxPos[i]<0) + { + pxPos[i]=-pxPos[i]; + pxSpeed[i]=-0.91*pxSpeed[i]; + } + + pxSpeed[i] = pxSpeed[i]-speedReduction; + } + + for (int x=0; x=1) - { - pxPos[i]=0; - pxSpeed[i]=0; - } - if(random(20)==0 and pxPos[i]==0) - { - pxPos[i]=0.0001; - pxSpeed[i]=0.0001; - } - if(pxPos[i]>0) - { - pxPos[i] = pxPos[i] + pxSpeed[i]; - pxSpeed[i] = pxSpeed[i] + speedDelta; - } - } - - for (int x=0; x0) - { - int p = mapfloat(pxPos[i], 0, 1, 0, numLeds-1); - AlaColor c = palette.colors[i].scale(1-(float)random(10)/30); // add a little flickering - leds[p] = c; - } - } + static long lastRefresh; + + if (pxPos==NULL) + { + // allocate new arrays + pxPos = new float[palette.numColors]; + pxSpeed = new float[palette.numColors]; + + for (int i=0; i=1) + { + pxPos[i]=0; + pxSpeed[i]=0; + } + if(random(20)==0 and pxPos[i]==0) + { + pxPos[i]=0.0001; + pxSpeed[i]=0.0001; + } + if(pxPos[i]>0) + { + pxPos[i] = pxPos[i] + pxSpeed[i]; + pxSpeed[i] = pxSpeed[i] + speedDelta; + } + } + + for (int x=0; x0) + { + int p = mapfloat(pxPos[i], 0, 1, 0, numLeds-1); + AlaColor c = palette.colors[i].scale(1-(float)random(10)/30); // add a little flickering + leds[p] = c; + } + } } diff --git a/src/AlaLedRgb.h b/src/AlaLedRgb.h index 70f88c2..b5eccbf 100644 --- a/src/AlaLedRgb.h +++ b/src/AlaLedRgb.h @@ -14,106 +14,106 @@ class AlaLedRgb public: - AlaLedRgb(); + AlaLedRgb(); + + void initPWM(byte pinsRed, byte pinGreen, byte pinBlue); + void initPWM(int numLeds, byte *pins); + void initTLC5940(int numLeds, byte *pins); - void initPWM(byte pinsRed, byte pinGreen, byte pinBlue); - void initPWM(int numLeds, byte *pins); - void initTLC5940(int numLeds, byte *pins); - /** * Initializes WS2812 LEDs. It be invoked in the setup() function of the main Arduino sketch. - * + * * The type field can be used to set the RGB order and chipset frequency. Constants are Adafruit_NeoPixel.h file. * It is set by default to NEO_GRB + NEO_KHZ800. */ - void initWS2812(int numLeds, byte pin, byte type=0x01+0x02); + void initWS2812(int numLeds, byte pin, byte type=0x01+0x02); - /** - * Sets the maximum brightness level. - */ - void setBrightness(AlaColor maxOut); + /** + * Sets the maximum brightness level. + */ + void setBrightness(AlaColor maxOut); - /** - * Sets the maximum refresh rate in Hz (default value is 50 Hz). - * May be useful to reduce flickering in some cases. - */ - void setRefreshRate(int refreshRate); - - int getCurrentRefreshRate(); + /** + * Sets the maximum refresh rate in Hz (default value is 50 Hz). + * May be useful to reduce flickering in some cases. + */ + void setRefreshRate(int refreshRate); + + int getCurrentRefreshRate(); - void setAnimation(int animation, long speed, AlaColor color); - void setAnimation(int animation, long speed, AlaPalette palette); - void setAnimation(AlaSeq animSeq[]); - bool runAnimation(); - void nextAnimation(); + void setAnimation(int animation, long speed, AlaColor color); + void setAnimation(int animation, long speed, AlaPalette palette); + void setAnimation(AlaSeq animSeq[]); + bool runAnimation(); + void nextAnimation(); private: - void setAnimationFunc(int animation); + void setAnimationFunc(int animation); void on(); void off(); - void blink(); - void blinkAlt(); - void sparkle(); - void sparkle2(); - void strobo(); - void cycleColors(); - - void pixelShiftRight(); + void blink(); + void blinkAlt(); + void sparkle(); + void sparkle2(); + void strobo(); + void cycleColors(); + + void pixelShiftRight(); void pixelShiftLeft(); void pixelBounce(); - void pixelSmoothShiftRight(); + void pixelSmoothShiftRight(); void pixelSmoothShiftLeft(); void comet(); void cometCol(); - void pixelSmoothBounce(); - void larsonScanner(); - void larsonScanner2(); + void pixelSmoothBounce(); + void larsonScanner(); + void larsonScanner2(); - void fadeIn(); + void fadeIn(); void fadeOut(); void fadeInOut(); void glow(); - void fadeColors(); - void pixelsFadeColors(); - void fadeColorsLoop(); - - void movingBars(); - void movingGradient(); - - void fire(); - void bouncingBalls(); - void bubbles(); - - - byte driver; // type of led driver: ALA_PWM, ALA_TLC5940 - byte *pins; // pins where the leds are attached to - AlaColor *leds; // array to store leds brightness values - int numLeds; // number of leds - - int animation; - int currAnim; + void fadeColors(); + void pixelsFadeColors(); + void fadeColorsLoop(); + + void movingBars(); + void movingGradient(); + + void fire(); + void bouncingBalls(); + void bubbles(); + + + byte driver; // type of led driver: ALA_PWM, ALA_TLC5940 + byte *pins; // pins where the leds are attached to + AlaColor *leds; // array to store leds brightness values + int numLeds; // number of leds + + int animation; + int currAnim; long speed; - AlaPalette palette; + AlaPalette palette; AlaSeq *animSeq; int animSeqLen; - long animSeqDuration; + long animSeqDuration; void (AlaLedRgb::*animFunc)(); AlaColor maxOut; - int refreshMillis; - int refreshRate; // current refresh rate - unsigned long animStartTime; - unsigned long animSeqStartTime; - unsigned long lastRefreshTime; - - float *pxPos; - float *pxSpeed; - - Adafruit_NeoPixel *neopixels; + int refreshMillis; + int refreshRate; // current refresh rate + unsigned long animStartTime; + unsigned long animSeqStartTime; + unsigned long lastRefreshTime; + + float *pxPos; + float *pxSpeed; + + Adafruit_NeoPixel *neopixels; };