renamed LED to RGB, since that's what it is

This commit is contained in:
Ronald Schaten 2016-05-17 22:14:03 +02:00
parent e15a65c69d
commit 783286849a
3 changed files with 62 additions and 62 deletions

View File

@ -1,60 +1,60 @@
#include "DeviceLed.h" #include "DeviceRgb.h"
void DeviceLed::setLed(int red, int green, int blue) { void DeviceRgb::setRgb(int red, int green, int blue) {
analogWrite(pinRed, red); analogWrite(pinRed, red);
analogWrite(pinGreen, green); analogWrite(pinGreen, green);
analogWrite(pinBlue, blue); analogWrite(pinBlue, blue);
} }
void DeviceLed::publishStatus() { void DeviceRgb::publishStatus() {
DynamicJsonBuffer json_outBuffer; DynamicJsonBuffer json_outBuffer;
JsonObject& json_out = json_outBuffer.createObject(); JsonObject& json_out = json_outBuffer.createObject();
json_out["red"] = led_red; json_out["red"] = rgb_red;
json_out["green"] = led_green; json_out["green"] = rgb_green;
json_out["blue"] = led_blue; json_out["blue"] = rgb_blue;
String response; String response;
json_out.printTo(response); json_out.printTo(response);
Serial.print("led state: "); Serial.print("rgb state: ");
Serial.println(response); Serial.println(response);
Homie.setNodeProperty(ledNode, "color", response); Homie.setNodeProperty(rgbNode, "color", response);
} }
void DeviceLed::deviceSetup() { void DeviceRgb::deviceSetup() {
pinMode(pinRed, OUTPUT); pinMode(pinRed, OUTPUT);
pinMode(pinGreen, OUTPUT); pinMode(pinGreen, OUTPUT);
pinMode(pinBlue, OUTPUT); pinMode(pinBlue, OUTPUT);
setLed(led_red, led_green, led_blue); setRgb(rgb_red, rgb_green, rgb_blue);
} }
bool DeviceLed::ledOnHandler(String value) { bool DeviceRgb::rgbOnHandler(String value) {
if (value == "true") { if (value == "true") {
setLed(led_red, led_green, led_blue); setRgb(rgb_red, rgb_green, rgb_blue);
Homie.setNodeProperty(ledNode, "on", "true"); Homie.setNodeProperty(rgbNode, "on", "true");
Serial.println("led is on"); Serial.println("rgb is on");
} else if (value == "false") { } else if (value == "false") {
setLed(0, 0, 0); setRgb(0, 0, 0);
Homie.setNodeProperty(ledNode, "on", "false"); Homie.setNodeProperty(rgbNode, "on", "false");
Serial.println("led is off"); Serial.println("rgb is off");
} else { } else {
return false; return false;
} }
return true; return true;
} }
bool DeviceLed::ledColorHandler(String message) { bool DeviceRgb::rgbColorHandler(String message) {
DynamicJsonBuffer json_inBuffer; DynamicJsonBuffer json_inBuffer;
JsonObject& json_in = json_inBuffer.parseObject(message); JsonObject& json_in = json_inBuffer.parseObject(message);
if (json_in.success()) { if (json_in.success()) {
if (json_in.containsKey("red")) { if (json_in.containsKey("red")) {
led_red = json_in["red"]; rgb_red = json_in["red"];
} }
if (json_in.containsKey("green")) { if (json_in.containsKey("green")) {
led_green = json_in["green"]; rgb_green = json_in["green"];
} }
if (json_in.containsKey("blue")) { if (json_in.containsKey("blue")) {
led_blue = json_in["blue"]; rgb_blue = json_in["blue"];
} }
setLed(led_red, led_green, led_blue); setRgb(rgb_red, rgb_green, rgb_blue);
} else { } else {
Serial.println("parsing of JSON failed"); Serial.println("parsing of JSON failed");
} }
@ -62,27 +62,27 @@ bool DeviceLed::ledColorHandler(String message) {
return true; return true;
} }
bool DeviceLed::ledFadeHandler(String message) { bool DeviceRgb::rgbFadeHandler(String message) {
DynamicJsonBuffer json_inBuffer; DynamicJsonBuffer json_inBuffer;
JsonObject& json_in = json_inBuffer.parseObject(message); JsonObject& json_in = json_inBuffer.parseObject(message);
if (json_in.success()) { if (json_in.success()) {
fade_from_red = led_red; fade_from_red = rgb_red;
if (json_in.containsKey("red")) { if (json_in.containsKey("red")) {
fade_to_red = json_in["red"]; fade_to_red = json_in["red"];
} else { } else {
fade_to_red = led_red; fade_to_red = rgb_red;
} }
fade_from_green = led_green; fade_from_green = rgb_green;
if (json_in.containsKey("green")) { if (json_in.containsKey("green")) {
fade_to_green = json_in["green"]; fade_to_green = json_in["green"];
} else { } else {
fade_to_green = led_green; fade_to_green = rgb_green;
} }
fade_from_blue = led_blue; fade_from_blue = rgb_blue;
if (json_in.containsKey("blue")) { if (json_in.containsKey("blue")) {
fade_to_blue = json_in["blue"]; fade_to_blue = json_in["blue"];
} else { } else {
fade_to_blue = led_blue; fade_to_blue = rgb_blue;
} }
fade_start = millis(); fade_start = millis();
if (json_in.containsKey("seconds")) { if (json_in.containsKey("seconds")) {
@ -97,29 +97,29 @@ bool DeviceLed::ledFadeHandler(String message) {
return true; return true;
} }
void DeviceLed::deviceRegister() { void DeviceRgb::deviceRegister() {
ledNode.subscribe("on", [this](String value) { return ledOnHandler(value); }); rgbNode.subscribe("on", [this](String value) { return rgbOnHandler(value); });
ledNode.subscribe("color", [this](String value) { return ledColorHandler(value); }); rgbNode.subscribe("color", [this](String value) { return rgbColorHandler(value); });
ledNode.subscribe("fade", [this](String value) { return ledFadeHandler(value); }); rgbNode.subscribe("fade", [this](String value) { return rgbFadeHandler(value); });
Homie.registerNode(ledNode); Homie.registerNode(rgbNode);
} }
void DeviceLed::deviceLoop() { void DeviceRgb::deviceLoop() {
if (fading) { if (fading) {
if (fade_end > millis()) { if (fade_end > millis()) {
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; rgb_red = fade_from_red + (fade_to_red - fade_from_red) * progress;
led_green = fade_from_green + (fade_to_green - fade_from_green) * progress; rgb_green = fade_from_green + (fade_to_green - fade_from_green) * progress;
led_blue = fade_from_blue + (fade_to_blue - fade_from_blue) * progress; rgb_blue = fade_from_blue + (fade_to_blue - fade_from_blue) * progress;
setLed(led_red, led_green, led_blue); setRgb(rgb_red, rgb_green, rgb_blue);
progress_last = progress; progress_last = progress;
} }
} else { } else {
led_red = fade_to_red; rgb_red = fade_to_red;
led_green = fade_to_green; rgb_green = fade_to_green;
led_blue = fade_to_blue; rgb_blue = fade_to_blue;
setLed(led_red, led_green, led_blue); setRgb(rgb_red, rgb_green, rgb_blue);
publishStatus(); publishStatus();
fading = false; fading = false;
} }

View File

@ -3,12 +3,12 @@
#include "Device.h" #include "Device.h"
#include <Homie.h> #include <Homie.h>
class DeviceLed : public Device { class DeviceRgb : public Device {
public: public:
inline DeviceLed(byte ledRed, byte ledGreen, byte ledBlue) { inline DeviceRgb(byte rgbRed, byte rgbGreen, byte rgbBlue) {
pinRed = ledRed; pinRed = rgbRed;
pinGreen = ledGreen; pinGreen = rgbGreen;
pinBlue = ledBlue; pinBlue = rgbBlue;
} }
virtual void deviceSetup(); virtual void deviceSetup();
virtual void deviceRegister(); virtual void deviceRegister();
@ -17,9 +17,9 @@ class DeviceLed : public Device {
byte pinRed; byte pinRed;
byte pinGreen; byte pinGreen;
byte pinBlue; byte pinBlue;
int led_red = 0; int rgb_red = 0;
int led_green = 0; int rgb_green = 0;
int led_blue = 0; int rgb_blue = 0;
bool fading = false; bool fading = false;
int fade_from_red, fade_to_red; int fade_from_red, fade_to_red;
int fade_from_green, fade_to_green; int fade_from_green, fade_to_green;
@ -27,10 +27,10 @@ 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); void setRgb(int red, int green, int blue);
void publishStatus(); void publishStatus();
bool ledOnHandler(String value); bool rgbOnHandler(String value);
bool ledColorHandler(String message); bool rgbColorHandler(String message);
bool ledFadeHandler(String message); bool rgbFadeHandler(String message);
HomieNode ledNode{"led", "rgb"}; HomieNode rgbNode{"rgb", "rgb"};
}; };

View File

@ -1,16 +1,16 @@
#include <Homie.h> // https://github.com/marvinroger/homie-esp8266 #include <Homie.h> // https://github.com/marvinroger/homie-esp8266
#include "DeviceLed.h" #include "DeviceRgb.h"
#include "DeviceLdr.h" #include "DeviceLdr.h"
#include "DeviceDht.h" #include "DeviceDht.h"
#include "DeviceIrRx.h" #include "DeviceIrRx.h"
#include "DeviceIrTx.h" #include "DeviceIrTx.h"
#include "DeviceButton.h" #include "DeviceButton.h"
const byte PIN_LED_RED = D8; const byte PIN_RGB_RED = D8;
const byte PIN_LED_GREEN = D6; const byte PIN_RGB_GREEN = D6;
const byte PIN_LED_BLUE = D7; const byte PIN_RGB_BLUE = D7;
DeviceLed deviceLed(PIN_LED_RED, PIN_LED_GREEN, PIN_LED_BLUE); DeviceRgb deviceRgb(PIN_RGB_RED, PIN_RGB_GREEN, PIN_RGB_BLUE);
const byte PIN_LDR = A0; const byte PIN_LDR = A0;
DeviceLdr deviceLdr(PIN_LDR); DeviceLdr deviceLdr(PIN_LDR);
@ -30,7 +30,7 @@ const byte PIN_BUTTON = D2;
DeviceButton deviceButton(PIN_BUTTON); DeviceButton deviceButton(PIN_BUTTON);
Device* devices[] = { Device* devices[] = {
&deviceLed, &deviceRgb,
&deviceLdr, &deviceLdr,
&deviceDht, &deviceDht,
&deviceIrRx, &deviceIrRx,