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