From a24e56e941b52070011f524163b225d80a0709e4 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Fri, 19 Jul 2019 01:11:48 +0300 Subject: [PATCH] generic output channel and first example on SPI LED --- lighthub/abstractch.cpp | 10 +-- lighthub/abstractch.h | 11 ++-- lighthub/abstractin.cpp | 3 +- lighthub/abstractin.h | 2 - lighthub/abstractout.h | 10 +-- lighthub/inputs.cpp | 4 +- lighthub/item.cpp | 89 ++++++++++++++++++-------- lighthub/item.h | 8 ++- lighthub/modules/in_ccs811_hdc1080.cpp | 4 +- lighthub/modules/in_ccs811_hdc1080.h | 4 +- lighthub/modules/out_spiled.cpp | 69 ++++++++++++++++++++ lighthub/modules/out_spiled.h | 16 +++++ 12 files changed, 179 insertions(+), 51 deletions(-) create mode 100644 lighthub/modules/out_spiled.cpp create mode 100644 lighthub/modules/out_spiled.h diff --git a/lighthub/abstractch.cpp b/lighthub/abstractch.cpp index bbb9e62..7c45b55 100644 --- a/lighthub/abstractch.cpp +++ b/lighthub/abstractch.cpp @@ -8,21 +8,21 @@ extern lan_status lanStatus; extern PubSubClient mqttClient; -int abstractCh::publish(char* topic, long value, char* subtopic) +int abstractCh::publishTopic(char* topic, long value, char* subtopic) { char valstr[16]; printUlongValueToStr(valstr, value); - return publish(topic, valstr,subtopic); + return publishTopic(topic, valstr,subtopic); }; -int abstractCh::publish(char* topic, float value, char* subtopic) +int abstractCh::publishTopic(char* topic, float value, char* subtopic) { char valstr[16]; printFloatValueToStr(value, valstr); - return publish(topic, valstr,subtopic); + return publishTopic(topic, valstr,subtopic); }; -int abstractCh::publish(char* topic, char * value, char* subtopic) +int abstractCh::publishTopic(char* topic, char * value, char* subtopic) { char addrstr[MQTT_TOPIC_LENGTH]; diff --git a/lighthub/abstractch.h b/lighthub/abstractch.h index a223db8..bcf27a5 100644 --- a/lighthub/abstractch.h +++ b/lighthub/abstractch.h @@ -4,13 +4,14 @@ class abstractCh { public: abstractCh(){}; -// virtual int Setup(int addr) = 0; + virtual ~abstractCh(){}; virtual int Poll() = 0; + virtual int Setup() =0; + virtual int Anounce () {}; protected: -// Input * in; -int publish(char* topic, long value, char* subtopic = NULL); -int publish(char* topic, float value, char* subtopic = NULL ); -int publish(char* topic, char * value, char* subtopic = NULL); +virtual int publishTopic(char* topic, long value, char* subtopic = NULL); +virtual int publishTopic(char* topic, float value, char* subtopic = NULL ); +virtual int publishTopic(char* topic, char * value, char* subtopic = NULL); //friend Input; }; diff --git a/lighthub/abstractin.cpp b/lighthub/abstractin.cpp index f371c92..3ec1e5d 100644 --- a/lighthub/abstractin.cpp +++ b/lighthub/abstractin.cpp @@ -1,5 +1,6 @@ #include "abstractin.h" +#include "abstractch.h" #include #include "utils.h" #include @@ -31,7 +32,7 @@ int abstractIn::publish(char * value, char* subtopic) aJsonObject *emit = aJson.getObjectItem(in->inputObj, "emit"); if (emit) { - return publish(emit->valuestring,value,subtopic); + return publishTopic(emit->valuestring,value,subtopic); } } return 0; diff --git a/lighthub/abstractin.h b/lighthub/abstractin.h index ed24538..cb857c6 100644 --- a/lighthub/abstractin.h +++ b/lighthub/abstractin.h @@ -6,8 +6,6 @@ class Input; class abstractIn : public abstractCh{ public: abstractIn(Input * _in):abstractCh(){in=_in;}; - virtual int Setup(int addr) = 0; - virtual int Poll() = 0; protected: Input * in; diff --git a/lighthub/abstractout.h b/lighthub/abstractout.h index 1453e73..0e441d6 100644 --- a/lighthub/abstractout.h +++ b/lighthub/abstractout.h @@ -1,12 +1,12 @@ #pragma once #include "Arduino.h" +#include "abstractch.h" - +class Item; class abstractOut : public abstractCh{ public: - abstractOut(Input * _in):abstractCh(){in=_in;}; - virtual int Setup(int addr) = 0; - virtual int Poll() = 0; + abstractOut(Item * _item):abstractCh(){item=_item;}; + virtual int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true, int suffixCode=0, char* subItem=NULL) =0; protected: - + Item * item; }; diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index 41f49a3..1bfd4ad 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -118,12 +118,12 @@ if (!isValid() || (!root)) return; if (inType == IN_CCS811) { in_ccs811 ccs811(this); - ccs811.Setup(pin); + ccs811.Setup(); } else if (inType == IN_HDC1080) { in_hdc1080 hdc1080(this); - hdc1080.Setup(pin); + hdc1080.Setup(); } // TODO rest types setup #endif diff --git a/lighthub/item.cpp b/lighthub/item.cpp index 4660b4e..f6ca397 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -33,6 +33,8 @@ e-mail anklimov@gmail.com #endif #include +#include "modules/out_spiled.h" + const char ON_P[] PROGMEM = "ON"; const char OFF_P[] PROGMEM = "OFF"; const char REST_P[] PROGMEM = "REST"; @@ -95,8 +97,8 @@ int txt2cmd(char *payload) { int txt2subItem(char *payload) { - int cmd = -1; - if (!payload || !strlen(payload)) return 0; + int cmd = S_NOTFOUND; + if (!payload || !strlen(payload)) return S_NOTFOUND; // Check for command if (strcmp_P(payload, SET_P) == 0) cmd = S_SET; else if (strcmp_P(payload, TEMP_P) == 0) cmd = S_TEMP; @@ -115,6 +117,7 @@ const short defval[4] = {0, 0, 0, 0}; //Type,Arg,Val,Cmd Item::Item(aJsonObject *obj)//Constructor { itemArr = obj; + driver = NULL; Parse(); } @@ -127,18 +130,32 @@ void Item::Parse() { itemType = aJson.getArrayItem(itemArr, I_TYPE)->valueint; itemArg = aJson.getArrayItem(itemArr, I_ARG); itemVal = aJson.getArrayItem(itemArr, I_VAL); + + switch (itemType) + { + case CH_SPILED: + driver = new out_SPILed (this); + break; + } // debugSerial << F(" Item:") << itemArr->name << F(" T:") << itemType << F(" =") << getArg() << endl; + } } + +Item::~Item() +{ + if (driver) delete driver; } Item::Item(char *name) //Constructor { + driver = NULL; if (name && items) itemArr = aJson.getObjectItem(items, name); else itemArr = NULL; Parse(); } + uint8_t Item::getCmd(bool ext) { aJsonObject *t = aJson.getArrayItem(itemArr, I_CMD); if (t) @@ -240,34 +257,50 @@ boolean Item::getEnableCMD(int delta) { #define MAXCTRLPAR 3 - +// myhome/dev/item/subItem int Item::Ctrl(char * payload, boolean send, char * subItem){ if (!payload) return 0; -char* subsubItem = NULL; -int subItemN = 0; -int subsubItemN = 0; +char* suffix = NULL; +//int subItemN = 0; +int suffixCode = 0; bool isSet = false; if (subItem && strlen(subItem)) { -if (subsubItem = strchr(subItem, '/')) +if (suffix = strrchr(subItem, '/')) //Trying to retrieving right part { - *subsubItem = 0; - subsubItem++; - subsubItemN = txt2subItem(subsubItem); + *suffix= 0; //Truncate subItem string + suffix++; + suffixCode = txt2subItem(suffix); + // myhome/dev/item/sub.....Item/suffix } - subItemN = txt2subItem(subItem); - if (subItemN==S_SET || subsubItemN==S_SET) isSet = true; -} else isSet = true; /// To be removed - old compatmode +else + { + suffix = subItem; + suffixCode = txt2subItem(suffix); + if (suffixCode) + subItem = NULL; + // myhome/dev/item/suffix -if (isSet) -{ - int cmd = txt2cmd(payload); - debugSerial< "); } debugSerial<Ctrl(cmd, n, Parameters, send, suffixCode, subItem); + int iaddr = getArg(); HSVstore st; switch (cmd) { @@ -653,7 +690,7 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send, int subItemN) while (i) { Item it(i->valuestring); // it.copyPar(itemVal); - it.Ctrl(cmd, n, Par, send,subItemN); //// was true + it.Ctrl(cmd, n, Par, send,suffixCode,subItem); //// was true i = i->next; } //while } //if diff --git a/lighthub/item.h b/lighthub/item.h index e3cd221..8924203 100644 --- a/lighthub/item.h +++ b/lighthub/item.h @@ -18,7 +18,9 @@ e-mail anklimov@gmail.com */ #include "options.h" +#include "abstractout.h" +#define S_NOTFOUND 0 #define S_SET 1 #define S_TEMP 2 #define S_MODE 3 @@ -41,6 +43,7 @@ e-mail anklimov@gmail.com #define CH_VCTEMP 8 //Vacom PID regulator #define CH_VC 9 //Vacom modbus motor regulator #define CH_AC_HAIER 10 //AC Haier +#define CH_SPILED 11 #define CH_WHITE 127// #define CMD_NUM 0 @@ -118,12 +121,15 @@ class Item public: aJsonObject *itemArr, *itemArg,*itemVal; uint8_t itemType; + abstractOut * driver; Item(char * name); Item(aJsonObject * obj); + ~Item(); + boolean isValid (); - virtual int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true, int subItem=0); + virtual int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true, int suffixCode=0, char* subItem=NULL); virtual int Ctrl(char * payload, boolean send=true, char * subItem=NULL); int getArg(short n=0); diff --git a/lighthub/modules/in_ccs811_hdc1080.cpp b/lighthub/modules/in_ccs811_hdc1080.cpp index b3aa18f..9ea2aec 100644 --- a/lighthub/modules/in_ccs811_hdc1080.cpp +++ b/lighthub/modules/in_ccs811_hdc1080.cpp @@ -17,7 +17,7 @@ static bool HDC1080ready = false; static bool CCS811ready = false; -int in_ccs811::Setup(int addr) +int in_ccs811::Setup() { if (CCS811ready) {debugSerial<(leds, NUM_LEDS); +return 1; +} + +int out_SPILed::Poll() +{ + FastLED.show(); +return 1; +}; + +int out_SPILed::Ctrl(short cmd, short n, int * Parameters, boolean send, int suffixCode, char* subItem) +{ +int from=0, to=NUM_LEDS-1; + +if (subItem) +{ //Just single LED to control + from=atoi(subItem); + to=from; +} + + for (n=from;n<=to;n++) + { + + switch(cmd) + { + CMD_ON: + leds[n] = CRGB::White; + break; + CMD_OFF: + leds[n] = CRGB::Black; + break; + CMD_NUM: + switch (suffixCode) + { + + S_POWER: + S_VOL: + //leds[n].setBrightness(Parameters[0]); + break; + S_SET: + S_HSV: + leds[n] = CHSV(Parameters[0],Parameters[1],Parameters[2]); + break; + S_RGB: + leds[n] = CRGB(Parameters[0],Parameters[1],Parameters[2]); + break; + } + } + } + FastLED.show(); +return 1; +} + +#endif diff --git a/lighthub/modules/out_spiled.h b/lighthub/modules/out_spiled.h new file mode 100644 index 0000000..b3b0e18 --- /dev/null +++ b/lighthub/modules/out_spiled.h @@ -0,0 +1,16 @@ + +#pragma once +#ifndef SPILED_DISABLE +#include + +class out_SPILed : public abstractOut { +public: + + out_SPILed(Item * _item):abstractOut(_item){}; + int Setup() override; + int Poll() override; + virtual int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true, int suffixCode=0, char* subItem=NULL) override; + +protected: +}; +#endif