implement fading

This commit is contained in:
Ronald Schaten 2016-04-27 21:24:55 +02:00
parent d9d2ccd9bf
commit 99dd1918fc
2 changed files with 66 additions and 0 deletions

View File

@ -61,12 +61,70 @@ bool DeviceLed::ledColorHandler(String message) {
return true; return true;
} }
bool DeviceLed::ledFadeHandler(String message) {
DynamicJsonBuffer json_inBuffer;
JsonObject& json_in = json_inBuffer.parseObject(message);
if (json_in.success()) {
fade_from_red = led_red;
if (json_in.containsKey("red")) {
fade_to_red = json_in["red"];
} else {
fade_to_red = led_red;
}
fade_from_green = led_green;
if (json_in.containsKey("green")) {
fade_to_green = json_in["green"];
} else {
fade_to_green = led_green;
}
fade_from_blue = led_blue;
if (json_in.containsKey("blue")) {
fade_to_blue = json_in["blue"];
} else {
fade_to_blue = led_blue;
}
fade_start = millis();
if (json_in.containsKey("seconds")) {
fade_end = fade_start + 1000 * (int)json_in["seconds"];
} else {
fade_end = 0;
}
fading = true;
} else {
Serial.println("parsing of JSON failed");
}
return true;
}
void DeviceLed::deviceRegister() { void DeviceLed::deviceRegister() {
ledNode.subscribe("on", [this](String value) { return ledOnHandler(value); }); ledNode.subscribe("on", [this](String value) { return ledOnHandler(value); });
ledNode.subscribe("color", [this](String value) { return ledColorHandler(value); }); ledNode.subscribe("color", [this](String value) { return ledColorHandler(value); });
ledNode.subscribe("fade", [this](String value) { return ledFadeHandler(value); });
Homie.registerNode(ledNode); Homie.registerNode(ledNode);
} }
void DeviceLed::deviceLoop() { void DeviceLed::deviceLoop() {
if (fading) {
if (fade_end > millis()) {
float progress = 1.0 * (millis() - fade_start) / (fade_end - fade_start);
if ((int)(progress * 100) != (int)(progress_last * 100)) {
led_red = fade_from_red + (fade_to_red - fade_from_red) * progress;
analogWrite(pinRed, led_red);
led_green = fade_from_green + (fade_to_green - fade_from_green) * progress;
analogWrite(pinGreen, led_green);
led_blue = fade_from_blue + (fade_to_blue - fade_from_blue) * progress;
analogWrite(pinBlue, led_blue);
progress_last = progress;
}
} else {
led_red = fade_to_red;
analogWrite(pinRed, led_red);
led_green = fade_to_green;
analogWrite(pinGreen, led_green);
led_blue = fade_to_blue;
analogWrite(pinBlue, led_blue);
fading = false;
}
}
return; return;
} }

View File

@ -20,7 +20,15 @@ class DeviceLed : public Device {
int led_red = 0; int led_red = 0;
int led_green = 0; int led_green = 0;
int led_blue = 0; int led_blue = 0;
bool fading = false;
int fade_from_red, fade_to_red;
int fade_from_green, fade_to_green;
int fade_from_blue, fade_to_blue;
unsigned long fade_start = 0;
unsigned long fade_end = 0;
float progress_last = 0;
bool ledOnHandler(String value); bool ledOnHandler(String value);
bool ledColorHandler(String message); bool ledColorHandler(String message);
bool ledFadeHandler(String message);
HomieNode ledNode{"led", "rgb"}; HomieNode ledNode{"led", "rgb"};
}; };