refactor function to set led
This commit is contained in:
parent
99dd1918fc
commit
17d862049e
@ -1,25 +1,25 @@
|
|||||||
#include "DeviceLed.h"
|
#include "DeviceLed.h"
|
||||||
|
|
||||||
|
void DeviceLed::setLed(int red, int green, int blue) {
|
||||||
|
analogWrite(pinRed, red);
|
||||||
|
analogWrite(pinGreen, green);
|
||||||
|
analogWrite(pinBlue, blue);
|
||||||
|
}
|
||||||
|
|
||||||
void DeviceLed::deviceSetup() {
|
void DeviceLed::deviceSetup() {
|
||||||
pinMode(pinRed, OUTPUT);
|
pinMode(pinRed, OUTPUT);
|
||||||
pinMode(pinGreen, OUTPUT);
|
pinMode(pinGreen, OUTPUT);
|
||||||
pinMode(pinBlue, OUTPUT);
|
pinMode(pinBlue, OUTPUT);
|
||||||
analogWrite(pinRed, led_red);
|
setLed(led_red, led_green, led_blue);
|
||||||
analogWrite(pinGreen, led_green);
|
|
||||||
analogWrite(pinBlue, led_blue);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DeviceLed::ledOnHandler(String value) {
|
bool DeviceLed::ledOnHandler(String value) {
|
||||||
if (value == "true") {
|
if (value == "true") {
|
||||||
analogWrite(pinRed, led_red);
|
setLed(led_red, led_green, led_blue);
|
||||||
analogWrite(pinGreen, led_green);
|
|
||||||
analogWrite(pinBlue, led_blue);
|
|
||||||
Homie.setNodeProperty(ledNode, "on", "true");
|
Homie.setNodeProperty(ledNode, "on", "true");
|
||||||
Serial.println("led is on");
|
Serial.println("led is on");
|
||||||
} else if (value == "false") {
|
} else if (value == "false") {
|
||||||
analogWrite(pinRed, 0);
|
setLed(0, 0, 0);
|
||||||
analogWrite(pinGreen, 0);
|
|
||||||
analogWrite(pinBlue, 0);
|
|
||||||
Homie.setNodeProperty(ledNode, "on", "false");
|
Homie.setNodeProperty(ledNode, "on", "false");
|
||||||
Serial.println("led is off");
|
Serial.println("led is off");
|
||||||
} else {
|
} else {
|
||||||
@ -34,16 +34,14 @@ bool DeviceLed::ledColorHandler(String message) {
|
|||||||
if (json_in.success()) {
|
if (json_in.success()) {
|
||||||
if (json_in.containsKey("red")) {
|
if (json_in.containsKey("red")) {
|
||||||
led_red = json_in["red"];
|
led_red = json_in["red"];
|
||||||
analogWrite(pinRed, led_red);
|
|
||||||
}
|
}
|
||||||
if (json_in.containsKey("green")) {
|
if (json_in.containsKey("green")) {
|
||||||
led_green = json_in["green"];
|
led_green = json_in["green"];
|
||||||
analogWrite(pinGreen, led_green);
|
|
||||||
}
|
}
|
||||||
if (json_in.containsKey("blue")) {
|
if (json_in.containsKey("blue")) {
|
||||||
led_blue = json_in["blue"];
|
led_blue = json_in["blue"];
|
||||||
analogWrite(pinBlue, led_blue);
|
|
||||||
}
|
}
|
||||||
|
setLed(led_red, led_green, led_blue);
|
||||||
} else {
|
} else {
|
||||||
Serial.println("parsing of JSON failed");
|
Serial.println("parsing of JSON failed");
|
||||||
}
|
}
|
||||||
@ -109,20 +107,16 @@ void DeviceLed::deviceLoop() {
|
|||||||
float progress = 1.0 * (millis() - fade_start) / (fade_end - fade_start);
|
float progress = 1.0 * (millis() - fade_start) / (fade_end - fade_start);
|
||||||
if ((int)(progress * 100) != (int)(progress_last * 100)) {
|
if ((int)(progress * 100) != (int)(progress_last * 100)) {
|
||||||
led_red = fade_from_red + (fade_to_red - fade_from_red) * progress;
|
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;
|
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;
|
led_blue = fade_from_blue + (fade_to_blue - fade_from_blue) * progress;
|
||||||
analogWrite(pinBlue, led_blue);
|
setLed(led_red, led_green, led_blue);
|
||||||
progress_last = progress;
|
progress_last = progress;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
led_red = fade_to_red;
|
led_red = fade_to_red;
|
||||||
analogWrite(pinRed, led_red);
|
|
||||||
led_green = fade_to_green;
|
led_green = fade_to_green;
|
||||||
analogWrite(pinGreen, led_green);
|
|
||||||
led_blue = fade_to_blue;
|
led_blue = fade_to_blue;
|
||||||
analogWrite(pinBlue, led_blue);
|
setLed(led_red, led_green, led_blue);
|
||||||
fading = false;
|
fading = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ class DeviceLed : public Device {
|
|||||||
unsigned long fade_start = 0;
|
unsigned long fade_start = 0;
|
||||||
unsigned long fade_end = 0;
|
unsigned long fade_end = 0;
|
||||||
float progress_last = 0;
|
float progress_last = 0;
|
||||||
|
void setLed(int red, int green, int blue);
|
||||||
bool ledOnHandler(String value);
|
bool ledOnHandler(String value);
|
||||||
bool ledColorHandler(String message);
|
bool ledColorHandler(String message);
|
||||||
bool ledFadeHandler(String message);
|
bool ledFadeHandler(String message);
|
||||||
|
Loading…
Reference in New Issue
Block a user