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