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

View File

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

View File

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