new device type: led

This commit is contained in:
Ronald Schaten 2016-05-17 23:55:41 +02:00
parent 783286849a
commit 7a79f1ca12
3 changed files with 133 additions and 0 deletions

100
things/DeviceLed.cpp Normal file
View File

@ -0,0 +1,100 @@
#include "DeviceLed.h"
void DeviceLed::setLed(int value) {
analogWrite(pinLed, value);
}
void DeviceLed::publishStatus() {
DynamicJsonBuffer json_outBuffer;
JsonObject& json_out = json_outBuffer.createObject();
json_out["brightness"] = brightness;
String response;
json_out.printTo(response);
Serial.print("led state: ");
Serial.println(response);
Homie.setNodeProperty(ledNode, "brightness", response);
}
void DeviceLed::deviceSetup() {
pinMode(pinLed, OUTPUT);
setLed(brightness);
}
bool DeviceLed::ledOnHandler(String value) {
if (value == "true") {
setLed(brightness);
Homie.setNodeProperty(ledNode, "on", "true");
Serial.println("led is on");
} else if (value == "false") {
setLed(0);
Homie.setNodeProperty(ledNode, "on", "false");
Serial.println("led is off");
} else {
return false;
}
return true;
}
bool DeviceLed::ledBrightnessHandler(String message) {
DynamicJsonBuffer json_inBuffer;
JsonObject& json_in = json_inBuffer.parseObject(message);
if (json_in.success()) {
if (json_in.containsKey("brightness")) {
brightness = json_in["brightness"];
}
setLed(brightness);
} else {
Serial.println("parsing of JSON failed");
}
publishStatus();
return true;
}
bool DeviceLed::ledFadeHandler(String message) {
DynamicJsonBuffer json_inBuffer;
JsonObject& json_in = json_inBuffer.parseObject(message);
if (json_in.success()) {
fade_from = brightness;
if (json_in.containsKey("brightness")) {
fade_to = json_in["brightness"];
} else {
fade_to = brightness;
}
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() {
ledNode.subscribe("on", [this](String value) { return ledOnHandler(value); });
ledNode.subscribe("brightness", [this](String value) { return ledBrightnessHandler(value); });
ledNode.subscribe("fade", [this](String value) { return ledFadeHandler(value); });
Homie.registerNode(ledNode);
}
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)) {
brightness = fade_from + (fade_to - fade_from) * progress;
setLed(brightness);
progress_last = progress;
}
} else {
brightness = fade_to;
setLed(brightness);
publishStatus();
fading = false;
}
}
return;
}

28
things/DeviceLed.h Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#include "Device.h"
#include <Homie.h>
class DeviceLed : public Device {
public:
inline DeviceLed(byte ledPin) {
pinLed = ledPin;
}
virtual void deviceSetup();
virtual void deviceRegister();
virtual void deviceLoop();
private:
byte pinLed;
int brightness = 0;
bool fading = false;
int fade_from, fade_to;
unsigned long fade_start = 0;
unsigned long fade_end = 0;
float progress_last = 0;
void setLed(int value);
void publishStatus();
bool ledOnHandler(String value);
bool ledBrightnessHandler(String message);
bool ledFadeHandler(String message);
HomieNode ledNode{"led", "led"};
};

View File

@ -1,5 +1,6 @@
#include <Homie.h> // https://github.com/marvinroger/homie-esp8266
#include "DeviceLed.h"
#include "DeviceRgb.h"
#include "DeviceLdr.h"
#include "DeviceDht.h"
@ -7,6 +8,9 @@
#include "DeviceIrTx.h"
#include "DeviceButton.h"
const byte PIN_LED = D8;
DeviceLed deviceLed(PIN_LED);
const byte PIN_RGB_RED = D8;
const byte PIN_RGB_GREEN = D6;
const byte PIN_RGB_BLUE = D7;
@ -30,6 +34,7 @@ const byte PIN_BUTTON = D2;
DeviceButton deviceButton(PIN_BUTTON);
Device* devices[] = {
&deviceLed,
&deviceRgb,
&deviceLdr,
&deviceDht,