From 50c38f4060bb419caae077ca870eec9110795c53 Mon Sep 17 00:00:00 2001 From: Ronald Schaten Date: Fri, 22 Apr 2016 22:26:52 +0200 Subject: [PATCH] split C code into header- and implementation-files --- things/DeviceDht.cpp | 66 +++++++++++++++++++++++++++++++++++ things/DeviceDht.h | 65 ---------------------------------- things/DeviceIrRx.cpp | 23 ++++++++++++ things/DeviceIrRx.h | 22 ------------ things/DeviceIrTx.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++ things/DeviceIrTx.h | 80 ------------------------------------------ things/DeviceLdr.cpp | 27 +++++++++++++++ things/DeviceLdr.h | 26 -------------- things/DeviceLed.cpp | 72 ++++++++++++++++++++++++++++++++++++++ things/DeviceLed.h | 71 ------------------------------------- 10 files changed, 269 insertions(+), 264 deletions(-) create mode 100644 things/DeviceDht.cpp create mode 100644 things/DeviceIrRx.cpp create mode 100644 things/DeviceIrTx.cpp create mode 100644 things/DeviceLdr.cpp create mode 100644 things/DeviceLed.cpp diff --git a/things/DeviceDht.cpp b/things/DeviceDht.cpp new file mode 100644 index 0000000..1027e2d --- /dev/null +++ b/things/DeviceDht.cpp @@ -0,0 +1,66 @@ +#include "DeviceDht.h" + +void DeviceDht::deviceSetup() { + pinMode(pin, INPUT); + dht.begin(); +} + +void DeviceDht::deviceRegister() { + Homie.registerNode(humidityNode); + Homie.registerNode(temperatureNode); + Homie.registerNode(heatindexNode); +} + +// compare float values +bool isEqual(float a, float b, float epsilon=0.001) { + return fabs(a - b) <= epsilon * fabs(a); +} + +void DeviceDht::deviceLoop() { + if (millis() - lastSentDHT >= INTERVAL_DHT * 1000UL || lastSentDHT == 0) { + float previousHumidity = humidity; + float previousTemperature = temperature; + float previousHeatindex = heatindex; + humidity = dht.readHumidity(); // read humidity as a percent + temperature = dht.readTemperature(); // read temperature as Celsius + heatindex = dht.computeHeatIndex(temperature, humidity, false); + + // check if any reads failed and exit early + if (isnan(humidity) || isnan(temperature)) { + Serial.println("Failed to read from DHT sensor!"); + return; + } + + if (!isEqual(humidity, previousHumidity)) { + Serial.print("humidity: "); + Serial.println(humidity); + if (!Homie.setNodeProperty(humidityNode, "value", String(humidity), true)) { + Serial.println("Sending failed"); + } + } else { + Serial.println("humidity unchanged"); + } + + if (!isEqual(temperature, previousTemperature)) { + Serial.print("temperature: "); + Serial.println(temperature); + if (!Homie.setNodeProperty(temperatureNode, "value", String(temperature), true)) { + Serial.println("Sending failed"); + } + } else { + Serial.println("temperature unchanged"); + } + + if (!isEqual(heatindex, previousHeatindex)) { + Serial.print("heatindex: "); + Serial.println(heatindex); + if (!Homie.setNodeProperty(heatindexNode, "value", String(heatindex), true)) { + Serial.println("Sending failed"); + } + } else { + Serial.println("heatindex unchanged"); + } + + lastSentDHT = millis(); + } +} diff --git a/things/DeviceDht.h b/things/DeviceDht.h index 20668cf..b48eb71 100644 --- a/things/DeviceDht.h +++ b/things/DeviceDht.h @@ -27,69 +27,4 @@ class DeviceDht : public Device { HomieNode heatindexNode{"heatindex", "heatindex"}; }; -void DeviceDht::deviceSetup() { - pinMode(pin, INPUT); - dht.begin(); -} - -void DeviceDht::deviceRegister() { - Homie.registerNode(humidityNode); - Homie.registerNode(temperatureNode); - Homie.registerNode(heatindexNode); -} - -// compare float values -bool isEqual(float a, float b, float epsilon=0.001) { - return fabs(a - b) <= epsilon * fabs(a); -} - -void DeviceDht::deviceLoop() { - if (millis() - lastSentDHT >= INTERVAL_DHT * 1000UL || lastSentDHT == 0) { - float previousHumidity = humidity; - float previousTemperature = temperature; - float previousHeatindex = heatindex; - humidity = dht.readHumidity(); // read humidity as a percent - temperature = dht.readTemperature(); // read temperature as Celsius - heatindex = dht.computeHeatIndex(temperature, humidity, false); - - // check if any reads failed and exit early - if (isnan(humidity) || isnan(temperature)) { - Serial.println("Failed to read from DHT sensor!"); - return; - } - - if (!isEqual(humidity, previousHumidity)) { - Serial.print("humidity: "); - Serial.println(humidity); - if (!Homie.setNodeProperty(humidityNode, "value", String(humidity), true)) { - Serial.println("Sending failed"); - } - } else { - Serial.println("humidity unchanged"); - } - - if (!isEqual(temperature, previousTemperature)) { - Serial.print("temperature: "); - Serial.println(temperature); - if (!Homie.setNodeProperty(temperatureNode, "value", String(temperature), true)) { - Serial.println("Sending failed"); - } - } else { - Serial.println("temperature unchanged"); - } - - if (!isEqual(heatindex, previousHeatindex)) { - Serial.print("heatindex: "); - Serial.println(heatindex); - if (!Homie.setNodeProperty(heatindexNode, "value", String(heatindex), true)) { - Serial.println("Sending failed"); - } - } else { - Serial.println("heatindex unchanged"); - } - - lastSentDHT = millis(); - } -} - #endif diff --git a/things/DeviceIrRx.cpp b/things/DeviceIrRx.cpp new file mode 100644 index 0000000..290536a --- /dev/null +++ b/things/DeviceIrRx.cpp @@ -0,0 +1,23 @@ +#include "DeviceIrRx.h" + +void DeviceIrRx::deviceSetup() { + // pinMode(pin_irrx, INPUT); // this is done by enableIRIn() + irrecv.enableIRIn(); + pinMode(pin_power, OUTPUT); + digitalWrite(pin_power, HIGH); +} + +void DeviceIrRx::deviceRegister() { + Homie.registerNode(irRxNode); +} + +void DeviceIrRx::deviceLoop() { + if (irrecv.decode(&results)) { + Serial.print("IR-Rx: "); + Serial.println(results.value, HEX); + if (!Homie.setNodeProperty(irRxNode, "value", String(results.value), false)) { + Serial.println("Sending failed"); + } + irrecv.resume(); // Receive the next value + } +} diff --git a/things/DeviceIrRx.h b/things/DeviceIrRx.h index 0eb845e..7cb9a15 100644 --- a/things/DeviceIrRx.h +++ b/things/DeviceIrRx.h @@ -22,26 +22,4 @@ class DeviceIrRx : public Device { HomieNode irRxNode = HomieNode("irrx", "irrx"); }; -void DeviceIrRx::deviceSetup() { - // pinMode(pin_irrx, INPUT); // this is done by enableIRIn() - irrecv.enableIRIn(); - pinMode(pin_power, OUTPUT); - digitalWrite(pin_power, HIGH); -} - -void DeviceIrRx::deviceRegister() { - Homie.registerNode(irRxNode); -} - -void DeviceIrRx::deviceLoop() { - if (irrecv.decode(&results)) { - Serial.print("IR-Rx: "); - Serial.println(results.value, HEX); - if (!Homie.setNodeProperty(irRxNode, "value", String(results.value), false)) { - Serial.println("Sending failed"); - } - irrecv.resume(); // Receive the next value - } -} - #endif diff --git a/things/DeviceIrTx.cpp b/things/DeviceIrTx.cpp new file mode 100644 index 0000000..14a8ae1 --- /dev/null +++ b/things/DeviceIrTx.cpp @@ -0,0 +1,81 @@ +#include "DeviceIrTx.h" + +void DeviceIrTx::deviceSetup() { + // pinMode(pin_irtx, OUTPUT); // this is done by begin() + irsend.begin(); +} + +bool DeviceIrTx::irTxHandler(String message) { + DynamicJsonBuffer json_inBuffer; + JsonObject& json_in = json_inBuffer.parseObject(message); + if (json_in.success()) { + unsigned long data = 0; + int nbits = 0; + if (json_in.containsKey("data")) { + data = json_in["data"]; + } + if (json_in.containsKey("nbits")) { + nbits = json_in["nbits"]; + } + if (json_in.containsKey("protocol")) { + String protocol = json_in["protocol"]; + protocol.toUpperCase(); + Serial.print("IR-Tx: sending "); + Serial.print(data); + Serial.print(" ("); + Serial.print(nbits); + Serial.print(") via "); + Serial.print(protocol); + Serial.println(" Protocol"); + for (int i = 0; i < 5; i++) { + if (protocol == "NEC") { + irsend.sendNEC(data, nbits); + } else if (protocol == "SONY") { + irsend.sendSony(data, nbits); + } else if (protocol == "RC5") { + irsend.sendRC5(data, nbits); + } else if (protocol == "RC6") { + irsend.sendRC6(data, nbits); + } else if (protocol == "DISH") { + irsend.sendDISH(data, nbits); + } else if (protocol == "SHARP") { + irsend.sendSharp(data, nbits); + } else if (protocol == "PANASONIC") { + irsend.sendPanasonic(data, nbits); + /* JVC needs a repeat parameter, will be implemented if necessary + } else if (protocol == "JVC") { + int repeat = 0; + irsend.sendJVC(data, nbits, repeat); + */ + /* Sanyo and Mitsubishi aren't implemented in the library + } else if (protocol == "SANYO") { + irsend.sendSanyo(data, nbits); + } else if (protocol == "MITSUBISHI") { + irsend.sendMitsubishi(data, nbits); + */ + } else if (protocol == "SAMSUNG") { + irsend.sendSAMSUNG(data, nbits); + } else if (protocol == "LG") { + irsend.sendLG(data, nbits); + } else if (protocol == "WHYNTER") { + irsend.sendLG(data, nbits); + } else { + Serial.println("IR protocol not implemented"); + } + delay(40); + } + } + } else { + Serial.println("parsing of JSON failed"); + } + return true; +} + +void DeviceIrTx::deviceRegister() { + irTxNode.subscribe("irtx", [this](String value) { return irTxHandler(value); }); + Homie.registerNode(irTxNode); +} + +void DeviceIrTx::deviceLoop() { + return; +} diff --git a/things/DeviceIrTx.h b/things/DeviceIrTx.h index 9a7b346..2bca4d3 100644 --- a/things/DeviceIrTx.h +++ b/things/DeviceIrTx.h @@ -20,84 +20,4 @@ class DeviceIrTx : public Device { HomieNode irTxNode = HomieNode("irtx", "irtx"); }; -void DeviceIrTx::deviceSetup() { - // pinMode(pin_irtx, OUTPUT); // this is done by begin() - irsend.begin(); -} - -bool DeviceIrTx::irTxHandler(String message) { - DynamicJsonBuffer json_inBuffer; - JsonObject& json_in = json_inBuffer.parseObject(message); - if (json_in.success()) { - unsigned long data = 0; - int nbits = 0; - if (json_in.containsKey("data")) { - data = json_in["data"]; - } - if (json_in.containsKey("nbits")) { - nbits = json_in["nbits"]; - } - if (json_in.containsKey("protocol")) { - String protocol = json_in["protocol"]; - protocol.toUpperCase(); - Serial.print("IR-Tx: sending "); - Serial.print(data); - Serial.print(" ("); - Serial.print(nbits); - Serial.print(") via "); - Serial.print(protocol); - Serial.println(" Protocol"); - for (int i = 0; i < 5; i++) { - if (protocol == "NEC") { - irsend.sendNEC(data, nbits); - } else if (protocol == "SONY") { - irsend.sendSony(data, nbits); - } else if (protocol == "RC5") { - irsend.sendRC5(data, nbits); - } else if (protocol == "RC6") { - irsend.sendRC6(data, nbits); - } else if (protocol == "DISH") { - irsend.sendDISH(data, nbits); - } else if (protocol == "SHARP") { - irsend.sendSharp(data, nbits); - } else if (protocol == "PANASONIC") { - irsend.sendPanasonic(data, nbits); - /* JVC needs a repeat parameter, will be implemented if necessary - } else if (protocol == "JVC") { - int repeat = 0; - irsend.sendJVC(data, nbits, repeat); - */ - /* Sanyo and Mitsubishi aren't implemented in the library - } else if (protocol == "SANYO") { - irsend.sendSanyo(data, nbits); - } else if (protocol == "MITSUBISHI") { - irsend.sendMitsubishi(data, nbits); - */ - } else if (protocol == "SAMSUNG") { - irsend.sendSAMSUNG(data, nbits); - } else if (protocol == "LG") { - irsend.sendLG(data, nbits); - } else if (protocol == "WHYNTER") { - irsend.sendLG(data, nbits); - } else { - Serial.println("IR protocol not implemented"); - } - delay(40); - } - } - } else { - Serial.println("parsing of JSON failed"); - } - return true; -} - -void DeviceIrTx::deviceRegister() { - irTxNode.subscribe("irtx", [this](String value) { return irTxHandler(value); }); - Homie.registerNode(irTxNode); -} - -void DeviceIrTx::deviceLoop() { - return; -} - #endif diff --git a/things/DeviceLdr.cpp b/things/DeviceLdr.cpp new file mode 100644 index 0000000..12ebd4d --- /dev/null +++ b/things/DeviceLdr.cpp @@ -0,0 +1,27 @@ +#include "DeviceLdr.h" + +void DeviceLdr::deviceSetup() { + pinMode(pin, INPUT); +} + +void DeviceLdr::deviceRegister() { + Homie.registerNode(ldrNode); +} + +void DeviceLdr::deviceLoop() { + if (millis() - lastSentLDR >= INTERVAL_LDR * 1000UL || lastSentLDR == 0) { + int ldr_new = analogRead(pin); + if (ldr_new != ldr) { + ldr = ldr_new; + float ldr_float = map(ldr, 0, 1023, 0, 10000) / 100.0; + Serial.print("LDR: "); + Serial.println(ldr_float); + if (!Homie.setNodeProperty(ldrNode, "value", String(ldr_float), true)) { + Serial.println("Sending failed"); + } + } else { + Serial.println("LDR value unchanged"); + } + lastSentLDR = millis(); + } +} diff --git a/things/DeviceLdr.h b/things/DeviceLdr.h index 37d8af3..446f310 100644 --- a/things/DeviceLdr.h +++ b/things/DeviceLdr.h @@ -20,30 +20,4 @@ class DeviceLdr : public Device { HomieNode ldrNode = HomieNode("ldr", "ldr"); }; -void DeviceLdr::deviceSetup() { - pinMode(pin, INPUT); -} - -void DeviceLdr::deviceRegister() { - Homie.registerNode(ldrNode); -} - -void DeviceLdr::deviceLoop() { - if (millis() - lastSentLDR >= INTERVAL_LDR * 1000UL || lastSentLDR == 0) { - int ldr_new = analogRead(pin); - if (ldr_new != ldr) { - ldr = ldr_new; - float ldr_float = map(ldr, 0, 1023, 0, 10000) / 100.0; - Serial.print("LDR: "); - Serial.println(ldr_float); - if (!Homie.setNodeProperty(ldrNode, "value", String(ldr_float), true)) { - Serial.println("Sending failed"); - } - } else { - Serial.println("LDR value unchanged"); - } - lastSentLDR = millis(); - } -} - #endif diff --git a/things/DeviceLed.cpp b/things/DeviceLed.cpp new file mode 100644 index 0000000..1d42965 --- /dev/null +++ b/things/DeviceLed.cpp @@ -0,0 +1,72 @@ +#include "DeviceLed.h" + +void DeviceLed::deviceSetup() { + pinMode(pinRed, OUTPUT); + pinMode(pinGreen, OUTPUT); + pinMode(pinBlue, OUTPUT); + analogWrite(pinRed, led_red); + analogWrite(pinGreen, led_green); + analogWrite(pinBlue, led_blue); +} + +bool DeviceLed::ledOnHandler(String value) { + if (value == "true") { + analogWrite(pinRed, led_red); + analogWrite(pinGreen, led_green); + analogWrite(pinBlue, 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); + Homie.setNodeProperty(ledNode, "on", "false"); + Serial.println("led is off"); + } else { + return false; + } + return true; +} + +bool DeviceLed::ledColorHandler(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"]; + 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); + } + } else { + Serial.println("parsing of JSON failed"); + } + + DynamicJsonBuffer json_outBuffer; + JsonObject& json_out = json_outBuffer.createObject(); + json_out["red"] = led_red; + json_out["green"] = led_green; + json_out["blue"] = led_blue; + String response; + json_out.printTo(response); + Serial.print("led state: "); + Serial.println(response); + Homie.setNodeProperty(ledNode, "color", response); + return true; +} + +void DeviceLed::deviceRegister() { + ledNode.subscribe("on", [this](String value) { return ledOnHandler(value); }); + ledNode.subscribe("color", [this](String value) { return ledColorHandler(value); }); + Homie.registerNode(ledNode); +} + +void DeviceLed::deviceLoop() { + return; +} diff --git a/things/DeviceLed.h b/things/DeviceLed.h index cc5434c..edb10b5 100644 --- a/things/DeviceLed.h +++ b/things/DeviceLed.h @@ -26,75 +26,4 @@ class DeviceLed : public Device { HomieNode ledNode{"led", "rgb"}; }; -void DeviceLed::deviceSetup() { - pinMode(pinRed, OUTPUT); - pinMode(pinGreen, OUTPUT); - pinMode(pinBlue, OUTPUT); - analogWrite(pinRed, led_red); - analogWrite(pinGreen, led_green); - analogWrite(pinBlue, led_blue); -} - -bool DeviceLed::ledOnHandler(String value) { - if (value == "true") { - analogWrite(pinRed, led_red); - analogWrite(pinGreen, led_green); - analogWrite(pinBlue, 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); - Homie.setNodeProperty(ledNode, "on", "false"); - Serial.println("led is off"); - } else { - return false; - } - return true; -} - -bool DeviceLed::ledColorHandler(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"]; - 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); - } - } else { - Serial.println("parsing of JSON failed"); - } - - DynamicJsonBuffer json_outBuffer; - JsonObject& json_out = json_outBuffer.createObject(); - json_out["red"] = led_red; - json_out["green"] = led_green; - json_out["blue"] = led_blue; - String response; - json_out.printTo(response); - Serial.print("led state: "); - Serial.println(response); - Homie.setNodeProperty(ledNode, "color", response); - return true; -} - -void DeviceLed::deviceRegister() { - ledNode.subscribe("on", [this](String value) { return ledOnHandler(value); }); - ledNode.subscribe("color", [this](String value) { return ledColorHandler(value); }); - Homie.registerNode(ledNode); -} - -void DeviceLed::deviceLoop() { - return; -} - #endif