Functioning RgbLed Class with Fading function

This commit is contained in:
panigrc 2018-06-30 23:33:19 +02:00
parent dbd5706481
commit 93110bd69d
1 changed files with 54 additions and 31 deletions

View File

@ -7,16 +7,20 @@ class RgbLed {
int ledGreenPin; int ledGreenPin;
int ledBluePin; int ledBluePin;
byte ledRedIntensity; byte ledRedIntensityStart;
byte ledGreenIntensity; byte ledGreenIntensityStart;
byte ledBlueIntensity; byte ledBlueIntensityStart;
byte ledRedIntensityCur;
byte ledGreenIntensityCur;
byte ledBlueIntensityCur;
byte ledRedIntensityTo; byte ledRedIntensityTo;
byte ledGreenIntensityTo; byte ledGreenIntensityTo;
byte ledBlueIntensityTo; byte ledBlueIntensityTo;
long durationLeft; long timeStart;
long previousMillis; long timeEnd;
public: public:
RgbLed(int redPin, int greenPin, int bluePin) RgbLed(int redPin, int greenPin, int bluePin)
@ -29,62 +33,81 @@ class RgbLed {
pinMode(ledGreenPin, OUTPUT); pinMode(ledGreenPin, OUTPUT);
pinMode(ledBluePin, OUTPUT); pinMode(ledBluePin, OUTPUT);
ledRedIntensity = 0; ledRedIntensityStart = 0;
ledGreenIntensity = 0; ledGreenIntensityStart = 0;
ledBlueIntensity = 0; ledBlueIntensityStart = 0;
ledRedIntensityTo = ledRedIntensity; ledRedIntensityCur = ledRedIntensityStart;
ledGreenIntensityTo = ledGreenIntensity; ledGreenIntensityCur = ledGreenIntensityStart;
ledBlueIntensityTo = ledBlueIntensity; ledBlueIntensityCur = ledBlueIntensityStart;
durationLeft = 0; ledRedIntensityTo = ledRedIntensityStart;
previousMillis = 0; ledGreenIntensityTo = ledGreenIntensityStart;
ledBlueIntensityTo = ledBlueIntensityStart;
timeStart = 0;
timeEnd = 0;
} }
public: public:
void ChangeColor(int redIntesity, int greenIntensity, int blueIntensity, long duration) void ChangeColor(byte redIntesity, byte greenIntensity, byte blueIntensity, long duration)
{ {
if (ledRedIntensityTo == ledRedIntensity && ledGreenIntensityTo == ledGreenIntensity && ledBlueIntensityTo == ledBlueIntensity if (millis() > timeEnd) {
&& ledRedIntensity != redIntesity && ledGreenIntensity != greenIntensity && ledBlueIntensity != blueIntensity) { timeStart = millis();
durationLeft = duration; timeEnd = timeStart + duration;
ledRedIntensityTo = ledRedIntensity; ledRedIntensityStart = ledRedIntensityCur;
ledGreenIntensityTo = ledGreenIntensity; ledGreenIntensityStart = ledGreenIntensityCur;
ledBlueIntensityTo = ledBlueIntensity; ledBlueIntensityStart = ledBlueIntensityCur;
ledRedIntensityTo = redIntesity;
ledGreenIntensityTo = greenIntensity;
ledBlueIntensityTo = blueIntensity;
} }
} }
public: public:
void Update(long currentMillis) void Update(long currentMillis)
{ {
if (ledRedIntensityTo != ledRedIntensity && ledGreenIntensityTo != ledGreenIntensity && ledBlueIntensityTo != ledBlueIntensity) { if (currentMillis <= timeEnd) {
ledRedIntensity = lerp(ledRedIntensity, ledRedIntensityTo, previousMillis, previousMillis + durationLeft, currentMillis); ledRedIntensityCur = lerp(ledRedIntensityStart, ledRedIntensityTo, timeStart, timeEnd, currentMillis);
ledGreenIntensity = lerp(ledGreenIntensity, ledGreenIntensityTo, previousMillis, previousMillis + durationLeft, currentMillis); ledGreenIntensityCur = lerp(ledGreenIntensityStart, ledGreenIntensityTo, timeStart, timeEnd, currentMillis);
ledBlueIntensity = lerp(ledBlueIntensity, ledBlueIntensityTo, previousMillis, previousMillis + durationLeft, currentMillis); ledBlueIntensityCur = lerp(ledBlueIntensityStart, ledBlueIntensityTo, timeStart, timeEnd, currentMillis);
UpdateLEDs(); UpdateLEDs();
} }
durationLeft = durationLeft - (currentMillis - previousMillis); }
previousMillis = currentMillis;
bool Finished() {
return millis() > timeEnd;
} }
byte lerp(byte intensityStart, byte intensityEnd, long timeStart, long timeEnd, long timeCurrent) { byte lerp(byte intensityStart, byte intensityEnd, long timeStart, long timeEnd, long timeCurrent) {
return ( ( timeCurrent - timeStart )( intensityEnd - intensityStart) / ( timeEnd - timeStart) ) + intensityStart; long result = ( ( timeCurrent - timeStart ) * ( intensityEnd - intensityStart) / ( timeEnd - timeStart) ) + intensityStart;
return (byte) result;
} }
void UpdateLEDs() { void UpdateLEDs() {
digitalWrite(ledRedPin, ledRedIntensity); analogWrite(ledRedPin, ledRedIntensityCur * 4); // correction for NodeMCU 0 - 1023
digitalWrite(ledGreenPin, ledGreenIntensity); analogWrite(ledGreenPin, ledGreenIntensityCur * 4);
digitalWrite(ledBluePin, ledBlueIntensity); analogWrite(ledBluePin, ledBlueIntensityCur * 4);
} }
}; };
RgbLed ledStripe(5, 4, 0); RgbLed ledStripe(5, 4, 0);
int foo = 0;
void setup() { void setup() {
Serial.begin(115200);
} }
void loop() void loop()
{ {
ledStripe.ChangeColor(30, 200, 255, 1000); if (ledStripe.Finished() && foo == 0) {
foo = 1;
ledStripe.ChangeColor(0, 0, 0, 2000);
}
if (ledStripe.Finished() && foo == 1) {
foo = 0;
ledStripe.ChangeColor(255, 255, 255, 2000);
}
ledStripe.Update(millis()); ledStripe.Update(millis());
} }