diff --git a/compiled/lighthub21/2020-10-27 23.51.11.jpg b/compiled/lighthub21/2020-10-27 23.51.11.jpg new file mode 100644 index 0000000..146033d Binary files /dev/null and b/compiled/lighthub21/2020-10-27 23.51.11.jpg differ diff --git a/lighthub/abstractout.h b/lighthub/abstractout.h index 39c1bb2..e88226a 100644 --- a/lighthub/abstractout.h +++ b/lighthub/abstractout.h @@ -1,13 +1,14 @@ #pragma once #include "Arduino.h" #include "abstractch.h" +#include "itemCmd.h" class Item; class chPersistent {}; class abstractOut : public abstractCh{ public: abstractOut(Item * _item):abstractCh(){item=_item;}; - virtual int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) =0; + virtual int Ctrl(itemCmd cmd, int suffixCode=0, char* subItem=NULL) =0; virtual int isActive(){return 0;}; virtual int getDefaultOnVal(){return 100;}; virtual int getChanType(){return 0;} diff --git a/lighthub/item.cpp b/lighthub/item.cpp index 059d1fe..64320fc 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -55,134 +55,6 @@ extern lan_status lanStatus; int retrieveCode(char **psubItem); - - itemCmd itemCmd::Percents(int i) - { - type=ST_PERCENTS; - param.aslong=i; - return *this; - } - - itemCmd itemCmd::Int(int32_t i) - { - type=ST_INT32; - param.asInt32=i; - return *this; - } - - itemCmd itemCmd::Int(uint32_t i) - { - type=ST_UINT32; - param.asUint32=i; - return *this; - } - - - itemCmd itemCmd::Cmd(uint8_t i) - { - type=ST_COMMAND; - param.cmd_code=i; - return *this; - } - - char * itemCmd::toString(char * Buffer, int bufLen) - { - if (!Buffer) return NULL; - switch (type) - { - case ST_VOID: - return NULL; - - case ST_PERCENTS: - case ST_PERCENTS255: - case ST_UINT32: - snprintf(Buffer, bufLen, "%u", param.asUint32); - break; - case ST_INT32: - snprintf(Buffer, bufLen, "%d", param.asInt32); - - break; - case ST_HS: - case ST_HSV: - case ST_HSV255: - snprintf(Buffer, bufLen, "%d,%d,%d", param.h, param.s, param.v); - - break; - case ST_FLOAT_CELSIUS: - case ST_FLOAT_FARENHEIT: - case ST_FLOAT: - snprintf(Buffer, bufLen, "%d", param.asfloat); - break; - case ST_RGB: - snprintf(Buffer, bufLen, "%d,%d,%d", param.r, param.g, param.b); - break; - - case ST_RGBW: - snprintf(Buffer, bufLen, "%d,%d,%d", param.r, param.g, param.b,param.w); - break; - - case ST_STRING: - strncpy(Buffer, param.asString,bufLen); - - break; - case ST_COMMAND: - strncpy_P(Buffer, commands_P[param.cmd_code], bufLen); - } - return Buffer; - } - -int txt2cmd(char *payload) { - int cmd = CMD_UNKNOWN; - if (!payload || !payload[0]) return cmd; - - // Check for command - if (*payload == '-' || (*payload >= '0' && *payload <= '9')) cmd = CMD_NUM; - else if (*payload == '%') cmd = CMD_UP; -/* - else if (strcmp_P(payload, ON_P) == 0) cmd = CMD_ON; - else if (strcmp_P(payload, OFF_P) == 0) cmd = CMD_OFF; - else if (strcmp_P(payload, REST_P) == 0) cmd = CMD_RESTORE; - else if (strcmp_P(payload, TOGGLE_P) == 0) cmd = CMD_TOGGLE; - else if (strcmp_P(payload, HALT_P) == 0) cmd = CMD_HALT; - else if (strcmp_P(payload, XON_P) == 0) cmd = CMD_XON; - else if (strcmp_P(payload, XOFF_P) == 0) cmd = CMD_XOFF; - else if (strcmp_P(payload, HEAT_P) == 0) cmd = CMD_HEAT; - else if (strcmp_P(payload, COOL_P) == 0) cmd = CMD_COOL; - else if (strcmp_P(payload, AUTO_P) == 0) cmd = CMD_AUTO; - else if (strcmp_P(payload, FAN_ONLY_P) == 0) cmd = CMD_FAN; - else if (strcmp_P(payload, DRY_P) == 0) cmd = CMD_DRY; - else if (strcmp_P(payload, TRUE_P) == 0) cmd = CMD_ON; - else if (strcmp_P(payload, FALSE_P) == 0) cmd = CMD_OFF; - else if (strcmp_P(payload, ENABLED_P) == 0) cmd = CMD_ON; - else if (strcmp_P(payload, DISABLED_P) == 0) cmd = CMD_OFF; - else if (strcmp_P(payload, INCREASE_P) == 0) cmd = CMD_UP; - else if (strcmp_P(payload, DECREASE_P) == 0) cmd = CMD_DN; - else if (strcmp_P(payload, HIGH_P) == 0) cmd = CMD_HIGH; - else if (strcmp_P(payload, MED_P) == 0) cmd = CMD_MED; - else if (strcmp_P(payload, LOW_P) == 0) cmd = CMD_LOW; -*/ - else if (*payload == '{') cmd = CMD_JSON; - else if (*payload == '#') cmd = CMD_RGB; - else - { - for(uint8_t i=1; i (SERIAL_8N1); - } - - /* - else if (strncmp_P(payload, HSV_P, strlen (HSV_P)) == 0) cmd = CMD_HSV; - else if (strncmp_P(payload, RGB_P, strlen (RGB_P)) == 0) cmd = CMD_RGB; - */ - - return cmd; -} - int subitem2cmd(char *payload) { int cmd = 0; @@ -220,6 +92,7 @@ int txt2subItem(char *payload) { else if (strcmp_P(payload, FAN_P) == 0) cmd = S_FAN; else if (strcmp_P(payload, HUE_P) == 0) cmd = S_HUE; else if (strcmp_P(payload, SAT_P) == 0) cmd = S_SAT; + else if (strcmp_P(payload, TEMP_P) == 0) cmd = S_TEMP; /* UnUsed now else if (strcmp_P(payload, SETPOINT_P) == 0) cmd = S_SETPOINT; else if (strcmp_P(payload, TEMP_P) == 0) cmd = S_TEMP; @@ -426,6 +299,16 @@ long int Item::getVal() //Return Val if val is int or first elem of Value array } else return 0;//-2; } +uint8_t Item::getSubtype() +{ + if (!itemVal) return 0;//-1; + if (itemVal->type == aJson_Int) return itemVal->subtype; + else if (itemVal->type == aJson_Array) { + aJsonObject *t = aJson.getArrayItem(itemVal, 0); + if (t) return t->subtype; + else return 0;//-3; + } else return 0;//-2; +} /* void Item::setVal(short n, int par) // Only store if VAL is array defined in config to avoid waste of RAM { @@ -445,6 +328,12 @@ void Item::setVal(long int par) // Only store if VAL is int (autogenerated or c itemVal->valueint = par; } +void Item::setSubtype(uint8_t par) // Only store if VAL is int (autogenerated or config-defined) +{ + if (!itemVal || itemVal->type != aJson_Int) return; + //debugSerial<0); + //threating Toggle, Restore, XOFF special conditional commands/ convert to ON, OFF - switch (cmd.toCmd()) { + switch (cmd.getCmd()) { int t; case CMD_TOGGLE: if (chActive) cmd.Cmd(CMD_OFF); - else cmd.Cmd(CMD_ON); + else cmd.Cmd(CMD_ON); break; case CMD_RESTORE: @@ -718,117 +609,128 @@ int Ctrl(itemCmd cmd, int suffixCode, char* subItem) case CMD_UP: { if (itemType == CH_GROUP) break; ////bug here - if (!n || !Par[0]) Par[0] = DEFAULT_INC_STEP; - if (cmd == CMD_DN) Par[0]=-Par[0]; - st.aslong = getVal(); - int cType=getChanType(); + short step=cmd.getCmdParam(); + if (!step) step=DEFAULT_INC_STEP; + if (cmd.getCmd() == CMD_DN) step=-step; - debugSerial<<"from: h="<Ctrl(cmd.Cmd(CMD_ON), suffixCode, subItem); + setCmd(CMD_XON); + } + else + { //cmd = CMD_ON; + debugSerial<0) //if channel was active before CMD_HALT + { + res = driver->Ctrl(cmd.Cmd(CMD_OFF), suffixCode, subItem); + setCmd(CMD_HALT); + return res; + } + else + { + debugSerial<Ctrl(cmd.Cmd(CMD_OFF), suffixCode, subItem); + setCmd(CMD_OFF); + } + else + { + debugSerial<Ctrl(st, suffixCode, subItem); + break; + + default: //another command + res = driver->Ctrl(cmd, suffixCode, subItem); + if (cmd.isCommand()) setCmd(cmd.getCmd()); } - - - - -========= - int chActive = item->isActive(); + return res; + } +// Legacy monolite core code +//================== +switch (itemType) { + case CH_GROUP://Group + { + if (itemArg->type == aJson_Array) { + aJsonObject *i = itemArg->child; + configLocked++; + while (i) { + if (i->type == aJson_String) + { + Item it(i->valuestring); + it.Ctrl(cmd, suffixCode,subItem); + } + i = i->next; + } //while + configLocked--; + } //if + } //case + } //switch +//========= +/* bool toExecute = (chActive>0); - long st; + if (cmd>0 && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it @@ -919,6 +821,27 @@ bool send = isNotRetainingStatus() ; } debugSerial<0) //if channel was'nt active before CMD_XON { debugSerial<Ctrl(CMD_ON, n, Par, suffixCode, subItem); + //res = driver->Ctrl(CMD_ON, n, Par, suffixCode, subItem); + res = driver->Ctrl(_itemCmd.Cmd(CMD_ON), suffixCode, subItem); setCmd(CMD_XON); } else @@ -1127,7 +1052,7 @@ bool send = isNotRetainingStatus() ; case CMD_HALT: if (chActive>0) //if channel was active before CMD_HALT { - res = driver->Ctrl(CMD_OFF, n, Par, suffixCode, subItem); + res = driver->Ctrl(_itemCmd.Cmd(CMD_OFF), suffixCode, subItem); setCmd(CMD_HALT); return res; } @@ -1140,7 +1065,7 @@ bool send = isNotRetainingStatus() ; case CMD_OFF: if (getCmd() != CMD_HALT) //Halted, ignore OFF { - res = driver->Ctrl(cmd, n, Par, suffixCode, subItem); + res = driver->Ctrl(_itemCmd, suffixCode, subItem); setCmd(CMD_OFF); } else @@ -1155,7 +1080,7 @@ bool send = isNotRetainingStatus() ; break; */ default: - res = driver->Ctrl(cmd, n, Par, suffixCode, subItem); + res = driver->Ctrl(_itemCmd, suffixCode, subItem); if (cmd) setCmd(cmd); } return res; @@ -1180,7 +1105,7 @@ bool send = isNotRetainingStatus() ; case 1: st.v = Par[0]; //Volume only - if (st.hsv_flag) + if (getSubtype()==ST_HSV || getSubtype()==ST_RGB) { Par[0] = st.h; Par[1] = st.s; @@ -1191,14 +1116,16 @@ bool send = isNotRetainingStatus() ; st.h = Par[0]; st.s = Par[1]; Par[2] = st.v; - st.hsv_flag = 1; + //st.hsv_flag = 1; + setSubtype(ST_HSV); n = 3; break; case 3: //complete triplet st.h = Par[0]; st.s = Par[1]; st.v = Par[2]; - st.hsv_flag = 1; + //st.hsv_flag = 1; + setSubtype(ST_HSV); } setVal(st.aslong); if (!suffixCode) @@ -2191,7 +2118,8 @@ int Item::SendStatus(int sendFlags) { snprintf(valstr, sizeof(valstr), "%d,%d,%d", st.h,st.s,st.v); break; case CH_GROUP: - if (st.hsv_flag) + //if (st.hsv_flag) + if (getSubtype()==ST_HSV) snprintf(valstr, sizeof(valstr), "%d,%d,%d", st.h,st.s,st.v); else snprintf(valstr, sizeof(valstr), "%d", st.v); @@ -2200,7 +2128,7 @@ int Item::SendStatus(int sendFlags) { sendFlags &= ~SEND_PARAMETERS; //No need to send value for relay break; default: - snprintf(valstr, sizeof(valstr), "%d", st.aslong); + snprintf(valstr, sizeof(valstr), "%ld", st.aslong); }//itemtype } if (sendFlags & SEND_COMMAND) diff --git a/lighthub/item.h b/lighthub/item.h index 0a37d19..2f6be9b 100644 --- a/lighthub/item.h +++ b/lighthub/item.h @@ -20,10 +20,7 @@ e-mail anklimov@gmail.com #pragma once #include "options.h" #include "abstractout.h" - -#define POLLING_SLOW 1 -#define POLLING_FAST 2 -#define POLLING_INT 3 +#include "itemCmd.h" #define S_NOTFOUND 0 #define S_SETnCMD 0 @@ -35,6 +32,7 @@ e-mail anklimov@gmail.com #define S_MODE 6 #define S_HUE 7 #define S_SAT 8 +#define S_TEMP 9 #define S_ADDITIONAL 64 #define CH_DIMMER 0 //DMX 1 ch @@ -58,64 +56,14 @@ e-mail anklimov@gmail.com #define CH_WHITE 127// -#define CMD_NUM 0 -#define CMD_UNKNOWN -1 -#define CMD_JSON -2 -//#define CMD_RGB -3 -//#define CMD_HSV -4 - -typedef char cmdstr[9]; - -const cmdstr commands_P[] PROGMEM = -{ -"","ON","OFF","REST","TOGGLE","HALT","XON","XOFF","INCREASE","DECREASE", -"HEAT","COOL","AUTO","FAN_ONLY","DRY","STOP","HIGH","MEDIUM","LOW", -"TRUE","FALSE","ENABLED","DISABLED","RGB","HSV" -}; -#define commandsNum sizeof(commands_P)/sizeof(cmdstr) - -#define CMD_ON 1 -#define CMD_OFF 2 -#define CMD_RESTORE 3 //on only if was turned off by CMD_HALT -#define CMD_TOGGLE 4 -#define CMD_HALT 5 //just Off -#define CMD_XON 6 //just on -#define CMD_XOFF 7 //off only if was previously turned on by CMD_XON -#define CMD_UP 8 //increase -#define CMD_DN 9 //decrease -#define CMD_HEAT 0xa -#define CMD_COOL 0xb -#define CMD_AUTO 0xc -#define CMD_FAN 0xd -#define CMD_DRY 0xe -#define CMD_STOP 0xf -#define CMD_HIGH 0x10 //AC fan leve -#define CMD_MED 0x11 -#define CMD_LOW 0x12 -#define CMD_ENABLED 0x13 -#define CMD_DISABLED 0x14 -#define CMD_TRUE 0x15 -#define CMD_FALSE 0x16 -#define CMD_RGB 0x17 -#define CMD_HSV 0x18 -//#define CMD_CURTEMP 0xf -#define CMD_MASK 0xff -#define FLAG_MASK 0xff00 - - -#define SEND_COMMAND 0x100 -#define SEND_PARAMETERS 0x200 -#define SEND_RETRY 0x400 -#define SEND_DEFFERED 0x800 -#define ACTION_NEEDED 0x1000 -#define ACTION_IN_PROCESS 0x2000 +#define POLLING_SLOW 1 +#define POLLING_FAST 2 +#define POLLING_INT 3 -//#define CMD_REPORT 32 - #define I_TYPE 0 //Type of item #define I_ARG 1 //Chanel-type depended argument or array of arguments (pin, address etc) #define I_VAL 2 //Latest preset (int or array of presets) @@ -140,71 +88,6 @@ extern short thermoSetCurTemp(char *name, float t); int txt2cmd (char * payload); -enum itemStoreType { -ST_VOID = 0, -ST_PERCENTS = 1, -ST_HS = 2, -ST_HSV = 3, -ST_FLOAT_CELSIUS= 4, -ST_FLOAT_FARENHEIT= 5, -ST_RGB = 6, -ST_RGBW = 7, -ST_PERCENTS255 = 8, -ST_HSV255 = 9, -ST_INT32 = 10, -ST_UINT32 = 11, -ST_STRING = 12, -ST_FLOAT = 13, -ST_COMMAND = 15 - -}; - -#pragma pack(push, 1) -typedef union -{ - long int aslong; - int32_t asInt32; - uint32_t asUint32; - char* asString; - float asfloat; - struct - { - uint8_t cmd_code; - uint8_t cmd_flag; - uint8_t cmd_effect; - uint8_t cmd_effect_param; - }; - struct - { uint8_t v; - uint8_t s; - uint16_t h:15; - uint16_t hsv_flag:1; - }; - struct - { - uint8_t r; - uint8_t g; - uint8_t b; - uint8_t w;//:7; -// uint8_t rgb_flag:1; - }; -} itemStore; - -class itemCmd -{ -public: - itemStoreType type; - itemStore param; - itemCmd Percents(int i); - itemCmd Int(int32_t i); - itemCmd Int(uint32_t i); - itemCmd Cmd(uint8_t i); - char * toString(char * Buffer, int bufLen); - short toCmd(); - } ; - -#pragma pack(pop) - class Item { public: @@ -226,6 +109,7 @@ class Item int getArg(short n=0); //int getVal(short n); //From VAL array. Negative if no array long int getVal(); //From int val OR array + uint8_t getSubtype(); uint8_t getCmd(); long int getExt(); //From int val OR array void setExt(long int par); @@ -236,6 +120,7 @@ class Item void setFlag (short flag); void clearFlag (short flag); void setVal(long int par); + void setSubtype(uint8_t par); int Poll(int cause); int SendStatus(int sendFlags); int isActive(); diff --git a/lighthub/itemCmd.cpp b/lighthub/itemCmd.cpp new file mode 100644 index 0000000..aeed57a --- /dev/null +++ b/lighthub/itemCmd.cpp @@ -0,0 +1,500 @@ +#include +#include "itemCmd.h" +#include "main.h" +#include "Streaming.h" +#include "item.h" + +#ifdef ADAFRUIT_LED +#include +#else +#include "FastLED.h" +#endif + +int txt2cmd(char *payload) { + int cmd = CMD_UNKNOWN; + if (!payload || !payload[0]) return cmd; + + // Check for command + if (*payload == '-' || (*payload >= '0' && *payload <= '9')) cmd = CMD_NUM; + else if (*payload == '%') cmd = CMD_UP; + else if (*payload == '{') cmd = CMD_JSON; + else if (*payload == '#') cmd = CMD_RGB; + else + { + for(uint8_t i=1; i100) par=100; + case ST_HSV255: + if (par>255) par=255; + if (par<0) par=0; + param.h=par; + } + return *this; +} + +itemCmd itemCmd::setS(uint8_t s) +{ + int par=s; + switch (type) + { + case ST_VOID: + type=ST_HSV; + case ST_HSV: + if (par>100) par=100; + case ST_HSV255: + if (par>255) par=255; + if (par<0) par=0; + param.s=par; + } + return *this; +} + +itemCmd itemCmd::incrementPercents(int16_t dif) +{ int par=param.v; + switch (type) + { + case ST_PERCENTS: + case ST_HSV: + par+=dif; + if (par>100) par=100; + if (par<0) par=0; + break; + case ST_PERCENTS255: + case ST_HSV255: + par+=dif; + if (par>255) par=255; + if (par<0) par=0; + break; + } + param.v=par; + return *this; +} + +itemCmd itemCmd::incrementH(int16_t dif) +{ int par=param.h; + switch (type) + { + case ST_HSV: + case ST_HSV255: + par+=dif; + if (par>365) par=0; + if (par<0) par=365; + break; +} +param.h=par; +return *this; +} + +itemCmd itemCmd::incrementS(int16_t dif) +{int par=param.s; + switch (type) + { + case ST_PERCENTS: + case ST_HSV: + par+=dif; + if (par>100) par=100; + if (par<0) par=0; + break; + case ST_PERCENTS255: + case ST_HSV255: + par+=dif; + if (par>255) par=255; + if (par<0) par=0; + break; + } + param.s=par; + return *this; + +} + + +itemCmd itemCmd::assignFrom(itemCmd from) +{ + bool RGBW_flag = false; + bool HSV255_flag = false; + + switch (type){ //Destination + case ST_HSV: + case ST_PERCENTS: + switch (from.type) + { + case ST_RGBW: + param.w=from.param.w; + case ST_RGB: + param.r=from.param.r; + param.g=from.param.g; + param.b=from.param.b; + type=from.type; //Changing if type + break; + case ST_HSV: + param.h=from.param.h; + param.s=from.param.s; + param.v=from.param.v; + break; + case ST_PERCENTS: + param.v=from.param.v; + break; + case ST_HSV255: + param.h=from.param.h; + param.s=map(from.param.s,0,255,0,100); + param.v=map(from.param.v,0,255,0,100); + break; + case ST_PERCENTS255: + param.v=map(from.param.v,0,255,0,100); + break; + default: + debugSerial<")<")<255) rgbValue = 255; + } + else + { + rgbSaturation = map(rgbSaturation, 128, 255, 100, 255); + param.w=0; + } + } + #ifdef ADAFRUIT_LED + Adafruit_NeoPixel strip(0, 0, 0); + uint32_t rgb = strip.ColorHSV(map(from.param.h, 0, 365, 0, 65535), rgbSaturation, rgbValue); + param.r=(rgb >> 16)& 0xFF; + param.g=(rgb >> 8) & 0xFF; + param.b=rgb & 0xFF; + #else + CRGB rgb = CHSV(map(from.param.h, 0, 365, 0, 255), rgbSaturation, rgbValue); + param.r=rgb.r; + param.g=rgb.g; + param.b=rgb.b; + #endif + } + default: + debugSerial<")<isValid()) + { + param.asInt32=item->getVal(); + type=(itemStoreType) item->getSubtype(); + return (type!=ST_VOID); + } +return false; +} + +bool itemCmd::saveItem(Item * item) +{ + if (item && item->isValid()) + { + item->setVal(param.asInt32); + item->setSubtype(type); + return true; + } +return false; +} + + + +char * itemCmd::toString(char * Buffer, int bufLen) + { + if (!Buffer) return NULL; + switch (type) + { + case ST_VOID: + return NULL; + + case ST_PERCENTS: + case ST_PERCENTS255: + case ST_UINT32: + snprintf(Buffer, bufLen, "%lu", param.asUint32); + break; + case ST_INT32: + snprintf(Buffer, bufLen, "%ld", param.asInt32); + + break; + case ST_HSV: + case ST_HSV255: + snprintf(Buffer, bufLen, "%d,%d,%d", param.h, param.s, param.v); + + break; + case ST_FLOAT_CELSIUS: + case ST_FLOAT_FARENHEIT: + case ST_FLOAT: + snprintf(Buffer, bufLen, "%.1f", param.asfloat); + break; + case ST_RGB: + snprintf(Buffer, bufLen, "%d,%d,%d", param.r, param.g, param.b); + break; + + case ST_RGBW: + snprintf(Buffer, bufLen, "%d,%d,%d,%d", param.r, param.g, param.b,param.w); + break; + + case ST_STRING: + strncpy(Buffer, param.asString,bufLen); + + break; + case ST_COMMAND: + strncpy_P(Buffer, commands_P[param.cmd_code], bufLen); + } + return Buffer; + } diff --git a/lighthub/itemCmd.h b/lighthub/itemCmd.h new file mode 100644 index 0000000..32a0cf6 --- /dev/null +++ b/lighthub/itemCmd.h @@ -0,0 +1,170 @@ +/* Copyright © 2017-2020 Andrey Klimov. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Homepage: http://lazyhome.ru +GIT: https://github.com/anklimov/lighthub +e-mail anklimov@gmail.com + +*/ +#pragma once +#include "Arduino.h" + +typedef char cmdstr[9]; + +const cmdstr commands_P[] PROGMEM = +{ +"","ON","OFF","REST","TOGGLE","HALT","XON","XOFF","INCREASE","DECREASE", +"HEAT","COOL","AUTO","FAN_ONLY","DRY","STOP","HIGH","MEDIUM","LOW", +"TRUE","FALSE","ENABLED","DISABLED","RGB","HSV" +}; +#define commandsNum sizeof(commands_P)/sizeof(cmdstr) + +#define CMD_ON 1 +#define CMD_OFF 2 +#define CMD_RESTORE 3 //on only if was turned off by CMD_HALT +#define CMD_TOGGLE 4 +#define CMD_HALT 5 //just Off +#define CMD_XON 6 //just on +#define CMD_XOFF 7 //off only if was previously turned on by CMD_XON +#define CMD_UP 8 //increase +#define CMD_DN 9 //decrease +#define CMD_HEAT 0xa +#define CMD_COOL 0xb +#define CMD_AUTO 0xc +#define CMD_FAN 0xd +#define CMD_DRY 0xe +#define CMD_STOP 0xf +#define CMD_HIGH 0x10 //AC fan leve +#define CMD_MED 0x11 +#define CMD_LOW 0x12 +#define CMD_ENABLED 0x13 +#define CMD_DISABLED 0x14 +#define CMD_TRUE 0x15 +#define CMD_FALSE 0x16 +#define CMD_RGB 0x17 +#define CMD_HSV 0x18 +//#define CMD_CURTEMP 0xf +#define CMD_MASK 0xff +#define FLAG_MASK 0xff00 + +#define CMD_NUM 0 +#define CMD_UNKNOWN -1 +#define CMD_JSON -2 +//#define CMD_RGB -3 +//#define CMD_HSV -4 + +#define SEND_COMMAND 0x100 +#define SEND_PARAMETERS 0x200 +#define SEND_RETRY 0x400 +#define SEND_DEFFERED 0x800 +#define ACTION_NEEDED 0x1000 +#define ACTION_IN_PROCESS 0x2000 + + +int txt2cmd (char * payload); + +enum itemStoreType { +ST_VOID = 0, +ST_PERCENTS = 1, +ST_TENS = 2, +ST_HSV = 3, +ST_FLOAT_CELSIUS= 4, +ST_FLOAT_FARENHEIT= 5, +ST_RGB = 6, +ST_RGBW = 7, +ST_PERCENTS255 = 8, +ST_HSV255 = 9, +ST_INT32 = 10, +ST_UINT32 = 11, +ST_STRING = 12, +ST_FLOAT = 13, +ST_COMMAND = 15 + +}; + +#pragma pack(push, 1) +typedef union +{ + long int aslong; + int32_t asInt32; + uint32_t asUint32; + char* asString; + float asfloat; + struct + { + uint8_t cmd_code; + uint8_t cmd_flag; + uint8_t cmd_effect; + uint8_t cmd_param; + }; + struct + { uint8_t v; + uint8_t s; + uint16_t h:9; + uint16_t colorTemp:7; + }; + struct + { int8_t signed_v; + int8_t signed_s; + int16_t signed_h:9; + int16_t signed_colorTemp:7; + }; + struct + { + uint8_t r; + uint8_t g; + uint8_t b; + uint8_t w; + }; +} itemStore; +class Item; +class itemCmd +{ +public: + itemStoreType type; + itemStore param; + + itemCmd(itemStoreType _type=ST_VOID); + itemCmd assignFrom(itemCmd from); + + bool loadItem(Item * item); + bool saveItem(Item * item); + + itemCmd Int(int32_t i); + itemCmd Int(uint32_t i); + itemCmd Cmd(uint8_t i); + itemCmd HSV(uint16_t h, uint8_t s, uint8_t v); + itemCmd setH(uint16_t); + itemCmd setS(uint8_t); + itemCmd Percents(int i); + + itemCmd incrementPercents(int16_t); + itemCmd incrementH(int16_t); + itemCmd incrementS(int16_t); + + long int getInt(); + short getPercents(); + short getPercents255(); + short getCmd(); + short getCmdParam(); + char * toString(char * Buffer, int bufLen); + + bool isCommand(); + bool isValue(); + bool isHSV(); + + itemCmd setDefault(); + }; + +#pragma pack(pop) diff --git a/lighthub/main.cpp b/lighthub/main.cpp index 9f55adf..0babfdb 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -66,6 +66,7 @@ PWM Out */ #include "main.h" +#include "statusled.h" #ifdef WIFI_ENABLE WiFiClient ethClient; diff --git a/lighthub/modules/out_ac.cpp b/lighthub/modules/out_ac.cpp index 4e4f161..7dab40b 100644 --- a/lighthub/modules/out_ac.cpp +++ b/lighthub/modules/out_ac.cpp @@ -129,7 +129,7 @@ void out_AC::InsertData(byte data[], size_t size){ ///////////////////////////////// publishTopic(item->itemArr->name,(long)set_tmp,"/set"); - publishTopic(item->itemArr->name, (long)cur_tmp, "/temp"); + if (cur_tmp!=255) publishTopic(item->itemArr->name, (long)cur_tmp, "/temp"); //////////////////////////////////// s_mode[0]='\0'; @@ -148,6 +148,9 @@ void out_AC::InsertData(byte data[], size_t size){ else if (mode == 0x04){ strcpy_P(s_mode,DRY_P); } + else if (mode == 109){ + strcpy_P(s_mode,ERROR_P); + } publishTopic(item->itemArr->name, (long) mode, "/mode"); @@ -259,7 +262,8 @@ delay(100); return INTERVAL_POLLING; }; -int out_AC::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* subItem) +//int out_AC::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* subItem) +int out_AC::Ctrl(itemCmd cmd, int suffixCode, char* subItem) {char s_mode[10]; // Some additional Subitems if (strcmp_P(subItem, LOCK_P) == 0) suffixCode = S_LOCK; @@ -267,12 +271,14 @@ int out_AC::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* su else if (strcmp_P(subItem, QUIET_P) == 0) suffixCode = S_QUIET; else if (strcmp_P(subItem, RAW_P) == 0) suffixCode = S_RAW; + if (cmd.isCommand() && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it + //data[B_POWER] = power; // debugSerial<= 10 && set_tmp <= 30) { data[B_SET_TMP] = set_tmp -16; @@ -282,7 +288,7 @@ int out_AC::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* su case S_CMD: s_mode[0]='\0'; - switch (cmd) + switch (cmd.getCmd()) { case CMD_ON: case CMD_XON: @@ -340,7 +346,7 @@ int out_AC::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* su case S_FAN: s_mode[0]='\0'; - switch (cmd) + switch (cmd.getCmd()) { case CMD_AUTO: data[B_FAN_SPD] = 3; @@ -359,18 +365,20 @@ int out_AC::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* su strcpy_P(s_mode,LOW_P); break; default: - if (n) data[B_FAN_SPD] = Parameters[0]; + //if (n) data[B_FAN_SPD] = Parameters[0]; + data[B_FAN_SPD] = cmd.getInt(); //TODO - mapping digits to speed } publishTopic(item->itemArr->name,s_mode,"/fan"); break; case S_MODE: - data[B_MODE] = Parameters[0]; + //data[B_MODE] = Parameters[0]; + data[B_MODE] = cmd.getInt(); break; case S_LOCK: - switch (cmd) + switch (cmd.getCmd()) { case CMD_ON: data[B_LOCK_REM] = 80; @@ -382,7 +390,7 @@ int out_AC::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* su break; case S_SWING: - switch (cmd) + switch (cmd.getCmd()) { case CMD_ON: data[B_LOCK_REM] = 3; @@ -391,12 +399,13 @@ int out_AC::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* su data[B_LOCK_REM] = 0; break; default: - if (n) data[B_SWING] = Parameters[0]; + //if (n) data[B_SWING] = Parameters[0]; + data[B_SWING] = cmd.getInt(); } break; case S_QUIET: - switch (cmd) + switch (cmd.getCmd()) { case CMD_ON: data[B_POWER] |= 8; diff --git a/lighthub/modules/out_ac.h b/lighthub/modules/out_ac.h index 33bd691..88aa161 100644 --- a/lighthub/modules/out_ac.h +++ b/lighthub/modules/out_ac.h @@ -29,7 +29,8 @@ public: int Stop() override; int Status() override; int isActive() override; - int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) override; + //int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) override; + int Ctrl(itemCmd cmd, int suffixCode=0, char* subItem=NULL) override; protected: void InsertData(byte data[], size_t size); diff --git a/lighthub/modules/out_dmx.cpp b/lighthub/modules/out_dmx.cpp new file mode 100644 index 0000000..d246153 --- /dev/null +++ b/lighthub/modules/out_dmx.cpp @@ -0,0 +1,226 @@ +//#ifndef DMX_DISABLE +#ifdef XXXX +#include "modules/out_dmx.h" +#include "Arduino.h" +#include "options.h" +#include "Streaming.h" + +#include "item.h" +#include "main.h" + +static int driverStatus = CST_UNKNOWN; + + + + +int out_dmx::Setup() +{ + +debugSerial<getVal(); //Restore old params +debugSerial<< F(" val:")<>4) == (ledsType>>6)) + return CH_RGB; + else + return CH_RGBW; +} + +int out_SPILed::PixelCtrl(itemCmd cmd, int from, int to, bool show) +{ +itemCmd st(ST_RGB); + +#ifdef ADAFRUIT_LED +uint32_t pixel; +#else +CRGB pixel; +#endif + + if (to>numLeds-1) to=numLeds-1; + if (from<0) from=0; + + for (int i=from;i<=to;i++) + { + switch (cmd.getCmd()) { + case CMD_ON: + + #ifdef ADAFRUIT_LED + if (!leds->getPixelColor(i)) leds->setPixelColor(i, leds->Color(255, 255, 255)); + #else + if (!leds[i].r && !leds[i].g &&!leds[i].b) leds[i] = CRGB::White; + #endif + break; + + case CMD_OFF: + #ifdef ADAFRUIT_LED + leds->setPixelColor(i, leds->Color(0, 0, 0)); + #else + leds[i] = CRGB::Black; + #endif + + break; + default: + st.assignFrom(cmd); + + #ifdef ADAFRUIT_LED + leds->setPixelColor(i, leds->Color(st.param.r,st.param.g,st.param.b)); + #else + leds[i] = CRGB(st.param.r,st.param.g,st.param.b); + #endif + } + } //for + + + + if (show) + { + #ifdef ADAFRUIT_LED + leds->show(); + #else + FastLED.show(); + #endif + + debugSerial<isActive(); +bool toExecute = (chActive>0); +itemCmd st(ST_HSV); +if (cmd.isCommand() && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it + +int from=0, to=numLeds-1; //All LEDs on the strip by default +// retrive LEDs range from suffix +if (subItem) +{ //Just single LED to control todo - range +// debugSerial<setVal(st.getInt()); + st.saveItem(item); + item->SendStatus(SEND_COMMAND | SEND_PARAMETERS ); + } + + PixelCtrl(st,from,to); + } + + return 1; + + case CMD_OFF: + if (subItem) // LED range, not whole strip + PixelCtrl(st.Cmd(CMD_OFF)); + else + { + st.Percents(0); + PixelCtrl(st,from,to); + item->SendStatus(SEND_COMMAND); + // if (send) item->publishTopic(item->itemArr->name,"OFF","/cmd"); + } + return 1; + +} //switch cmd + +break; +} //switch suffix +debugSerial< +#include + +#ifdef ADAFRUIT_LED +#include +#else +#include "FastLED.h" +#endif + +class out_dmx : public abstractOut { +public: + + out_dmx(Item * _item):abstractOut(_item){getConfig();}; + int Setup() override; + int Poll(short cause) override; + int Stop() override; + int Status() override; + int isActive() override; + int getChanType() override; + int Ctrl(itemCmd cmd, int suffixCode=0, char* subItem=NULL) override; + int PixelCtrl(itemCmd cmd, int from =0 , int to = 1024, bool show = 1); + int numLeds; + int8_t pin; + int ledsType; +protected: + void getConfig(); +}; +#endif diff --git a/lighthub/modules/out_modbus.cpp b/lighthub/modules/out_modbus.cpp index ec7861a..767b932 100644 --- a/lighthub/modules/out_modbus.cpp +++ b/lighthub/modules/out_modbus.cpp @@ -338,14 +338,12 @@ int out_Modbus::getChanType() -int out_Modbus::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* subItem) +int out_Modbus::Ctrl(itemCmd cmd, int suffixCode, char* subItem) { int chActive = item->isActive(); bool toExecute = (chActive>0); -long st; -if (cmd>0 && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it - -//item->setFlag(ACTION_NEEDED); +itemCmd st(ST_UINT32); +if (cmd.isCommand() && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it switch(suffixCode) { @@ -354,12 +352,12 @@ case S_NOTFOUND: toExecute = true; debugSerial<setVal(st=Parameters[0]); //Store + if (!cmd.isValue()) return 0; + //////item->setVal(st=Parameters[0]); //Store if (!suffixCode) { - if (chActive>0 && !st) item->setCmd(CMD_OFF); - if (chActive==0 && st) item->setCmd(CMD_ON); + if (chActive>0 && !st.getInt()) item->setCmd(CMD_OFF); + if (chActive==0 && st.getInt()) item->setCmd(CMD_ON); item->SendStatus(SEND_COMMAND | SEND_PARAMETERS | SEND_DEFFERED); // if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time } @@ -369,28 +367,29 @@ case S_SET: //break; case S_CMD: - item->setCmd(cmd); - switch (cmd) + //item->setCmd(cmd.getCmd()); + st.loadItem(item); + switch (cmd.getCmd()) { case CMD_ON: //retrive stored values - st = item->getVal(); + st.loadItem(item); - if (st && (stsetVal(st); + if (st.getPercents() && (st.getPercents()SendStatus(SEND_COMMAND | SEND_PARAMETERS); - debugSerial<setVal(st); + st.setDefault(); + st.saveItem(item); item->SendStatus(SEND_COMMAND | SEND_PARAMETERS ); } // if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time diff --git a/lighthub/modules/out_modbus.h b/lighthub/modules/out_modbus.h index af150c2..82110b6 100644 --- a/lighthub/modules/out_modbus.h +++ b/lighthub/modules/out_modbus.h @@ -31,8 +31,8 @@ public: int Status() override; int isActive() override; int getChanType() override; - int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) override; - + int Ctrl(itemCmd cmd, int suffixCode=0, char* subItem=NULL) override; + //int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) override; protected: mbPersistent * store; diff --git a/lighthub/modules/out_motor.cpp b/lighthub/modules/out_motor.cpp index 6394994..1082964 100644 --- a/lighthub/modules/out_motor.cpp +++ b/lighthub/modules/out_motor.cpp @@ -211,12 +211,12 @@ int out_Motor::getChanType() -int out_Motor::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* subItem) +int out_Motor::Ctrl(itemCmd cmd, int suffixCode, char* subItem) { int chActive = item->isActive(); bool toExecute = (chActive>0); -long st; -if (cmd>0 && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it +itemCmd st(ST_PERCENTS); +if (cmd.isCommand() && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it item->setFlag(ACTION_NEEDED); @@ -227,12 +227,14 @@ case S_NOTFOUND: toExecute = true; debugSerial<setVal(st=Parameters[0]); //Store + if (!cmd.isValue()) return 0; + st.assignFrom(cmd); + //Store + st.saveItem(item); if (!suffixCode) { - if (chActive>0 && !st) item->setCmd(CMD_OFF); - if (chActive==0 && st) item->setCmd(CMD_ON); + if (chActive>0 && !st.getPercents()) item->setCmd(CMD_OFF); + if (chActive==0 && st.getPercents()) item->setCmd(CMD_ON); item->SendStatus(SEND_COMMAND | SEND_PARAMETERS | SEND_DEFFERED); if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time } @@ -242,30 +244,32 @@ case S_SET: //break; case S_CMD: - item->setCmd(cmd); - switch (cmd) + item->setCmd(cmd.getCmd()); + switch (cmd.getCmd()) { case CMD_ON: //retrive stored values - st = item->getVal(); - - - if (st && (stsetVal(st); - - if (st) //Stored smthng + if (st.loadItem(item)) { - item->SendStatus(SEND_COMMAND | SEND_PARAMETERS); - debugSerial<SendStatus(SEND_COMMAND | SEND_PARAMETERS); + } + debugSerial<setVal(st); + st.setDefault(); + st.saveItem(item); + //st=100; + //item->setVal(st); item->SendStatus(SEND_COMMAND | SEND_PARAMETERS ); } + if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time return 1; diff --git a/lighthub/modules/out_motor.h b/lighthub/modules/out_motor.h index 156d623..3197368 100644 --- a/lighthub/modules/out_motor.h +++ b/lighthub/modules/out_motor.h @@ -25,7 +25,8 @@ public: int Status() override; int isActive() override; int getChanType() override; - int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) override; + //int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) override; + int Ctrl(itemCmd cmd, int suffixCode=0, char* subItem=NULL) override; int8_t pinUp; int8_t pinDown; diff --git a/lighthub/modules/out_spiled.cpp b/lighthub/modules/out_spiled.cpp index 3dba380..a7d73e2 100644 --- a/lighthub/modules/out_spiled.cpp +++ b/lighthub/modules/out_spiled.cpp @@ -108,35 +108,22 @@ int out_SPILed::getChanType() return CH_RGBW; } -int out_SPILed::PixelCtrl(itemStore *st, short cmd, int from, int to, bool show, bool rgb) +int out_SPILed::PixelCtrl(itemCmd cmd, int from, int to, bool show) { - //debugSerial<s, 0, 100, 0, 255); - int Value = map(st->v, 0, 100, 0, 255); -#ifdef ADAFRUIT_LED - uint16_t Hue = map(st->h, 0, 365, 0, 65535); - pixel = leds->ColorHSV(Hue, Saturation, Value); -#else - int Hue = map(st->h, 0, 365, 0, 255); - pixel = CHSV(Hue, Saturation, Value); -#endif - - debugSerial<h<s<v<numLeds-1) to=numLeds-1; if (from<0) from=0; for (int i=from;i<=to;i++) { - switch (cmd) { + switch (cmd.getCmd()) { case CMD_ON: #ifdef ADAFRUIT_LED @@ -144,9 +131,8 @@ CRGB pixel; #else if (!leds[i].r && !leds[i].g &&!leds[i].b) leds[i] = CRGB::White; #endif - - break; + case CMD_OFF: #ifdef ADAFRUIT_LED leds->setPixelColor(i, leds->Color(0, 0, 0)); @@ -156,26 +142,18 @@ CRGB pixel; break; default: - if (rgb) - { - #ifdef ADAFRUIT_LED - leds->setPixelColor(i, leds->Color(st->r,st->g,st->b)); - #else - leds[i] = CRGB(st->r,st->g,st->b); - #endif - } - else - { - #ifdef ADAFRUIT_LED - leds->setPixelColor(i, pixel); - #else - leds[i] = pixel; - #endif - - } + st.assignFrom(cmd); + #ifdef ADAFRUIT_LED + leds->setPixelColor(i, leds->Color(st.param.r,st.param.g,st.param.b)); + #else + leds[i] = CRGB(st.param.r,st.param.g,st.param.b); + #endif } } //for + + + if (show) { #ifdef ADAFRUIT_LED @@ -189,12 +167,12 @@ CRGB pixel; return 1; } -int out_SPILed::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* subItem) +int out_SPILed::Ctrl(itemCmd cmd, int suffixCode, char* subItem) { int chActive = item->isActive(); bool toExecute = (chActive>0); -itemStore st; -if (cmd>0 && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it +itemCmd st(ST_HSV); +if (cmd.isCommand() && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it int from=0, to=numLeds-1; //All LEDs on the strip by default // retrive LEDs range from suffix @@ -203,7 +181,7 @@ if (subItem) // debugSerial<setVal(st.aslong); + //item->setVal(st.getInt()); + st.saveItem(item); item->SendStatus(SEND_COMMAND | SEND_PARAMETERS ); } - PixelCtrl(&st,0,from,to); + PixelCtrl(st,from,to); } return 1; case CMD_OFF: if (subItem) // LED range, not whole strip - PixelCtrl(&st,CMD_OFF,from,to); + PixelCtrl(st.Cmd(CMD_OFF),from,to); else { - st.v=0; - PixelCtrl(&st,0,from,to); + st.Percents(0); + PixelCtrl(st,from,to); item->SendStatus(SEND_COMMAND); // if (send) item->publishTopic(item->itemArr->name,"OFF","/cmd"); } diff --git a/lighthub/modules/out_spiled.h b/lighthub/modules/out_spiled.h index 240bdd1..0a8334d 100644 --- a/lighthub/modules/out_spiled.h +++ b/lighthub/modules/out_spiled.h @@ -21,8 +21,9 @@ public: int Status() override; int isActive() override; int getChanType() override; - int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) override; - int PixelCtrl(itemStore *st, short cmd, int from =0 , int to = 1024, bool show = 1, bool rgb = 0); + //int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) override; + int Ctrl(itemCmd cmd, int suffixCode=0, char* subItem=NULL) override; + int PixelCtrl(itemCmd cmd, int from =0 , int to = 1024, bool show = 1); int numLeds; int8_t pin; int ledsType; diff --git a/lighthub/statusled.cpp b/lighthub/statusled.cpp new file mode 100644 index 0000000..a11af27 --- /dev/null +++ b/lighthub/statusled.cpp @@ -0,0 +1,89 @@ +/* Copyright © 2017-2018 Andrey Klimov. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Homepage: http://lazyhome.ru +GIT: https://github.com/anklimov/lighthub +e-mail anklimov@gmail.com + +*/ + +#include "statusled.h" + + +statusLED::statusLED(uint8_t pattern) +{ +#if defined (STATUSLED) + pinMode(pinRED, OUTPUT); + pinMode(pinGREEN, OUTPUT); + pinMode(pinBLUE, OUTPUT); + set(pattern); + timestamp=millis()+ledDelayms; +#endif +} + +void statusLED::show (uint8_t pattern) +{ +#if defined (STATUSLED) + digitalWrite(pinRED,(pattern & ledRED)?HIGH:LOW ); + digitalWrite(pinGREEN,(pattern & ledGREEN)?HIGH:LOW); + digitalWrite(pinBLUE,(pattern & ledBLUE)?HIGH:LOW); +#endif +} + +void statusLED::set (uint8_t pattern) +{ +#if defined (STATUSLED) + short newStat = pattern & ledParams; + + if (newStat!=(curStat & ledParams)) + { + //if (!(curStat & ledHidden)) + show(pattern); + curStat=newStat | (curStat & ~ledParams); + } +#endif +} + +void statusLED::flash(uint8_t pattern) +{ +#if defined (STATUSLED) + show(pattern); + curStat|=ledFlash; +#endif +} + +void statusLED::poll() +{ +#if defined (STATUSLED) + if (curStat & ledFlash) + { + curStat&=~ledFlash; + show(curStat); + } +if (millis()>timestamp) + { + + if (curStat & ledFASTBLINK) timestamp=millis()+ledFastDelayms; + else timestamp=millis()+ledDelayms; + + if (( curStat & ledBLINK) || (curStat & ledFASTBLINK)) + { + curStat^=ledHidden; + if (curStat & ledHidden) + show(0); + else show(curStat); + } + } +#endif +} diff --git a/lighthub/statusled.h b/lighthub/statusled.h new file mode 100644 index 0000000..159b520 --- /dev/null +++ b/lighthub/statusled.h @@ -0,0 +1,50 @@ +/* Copyright © 2017-2018 Andrey Klimov. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Homepage: http://lazyhome.ru +GIT: https://github.com/anklimov/lighthub +e-mail anklimov@gmail.com + +*/ +#pragma once +#include + +#define ledRED 1 +#define ledGREEN 2 +#define ledBLUE 4 +#define ledBLINK 8 +#define ledFASTBLINK 16 +#define ledParams (ledRED | ledGREEN | ledBLUE | ledBLINK | ledFASTBLINK) + +#define ledFlash 32 +#define ledHidden 64 + +#define pinRED 50 +#define pinGREEN 51 +#define pinBLUE 52 + +#define ledDelayms 1000UL +#define ledFastDelayms 300UL + +class statusLED { +public: + statusLED(uint8_t pattern = 0); + void set (uint8_t pattern); + void show (uint8_t pattern); + void poll(); + void flash(uint8_t pattern); +private: + uint8_t curStat; + uint32_t timestamp; +}; diff --git a/lighthub/streamlog.cpp b/lighthub/streamlog.cpp index 1c4d40b..c045e7e 100644 --- a/lighthub/streamlog.cpp +++ b/lighthub/streamlog.cpp @@ -1,6 +1,6 @@ #include "streamlog.h" #include -#include "utils.h" +#include "statusled.h" #if defined (STATUSLED) extern statusLED LED; diff --git a/lighthub/textconst.h b/lighthub/textconst.h index bfe3a9e..8ae023f 100644 --- a/lighthub/textconst.h +++ b/lighthub/textconst.h @@ -100,7 +100,7 @@ const char DRY_P[] PROGMEM = "DRY"; const char HIGH_P[] PROGMEM = "HIGH"; const char MED_P[] PROGMEM = "MEDIUM"; const char LOW_P[] PROGMEM = "LOW"; - +const char ERROR_P[] PROGMEM = "ERR"; // SubTopics @@ -110,7 +110,7 @@ const char MODE_P[] PROGMEM = "mode"; const char FAN_P[] PROGMEM = "fan"; const char HUE_P[] PROGMEM = "hue"; const char SAT_P[] PROGMEM = "sat"; - +const char TEMP_P[] PROGMEM = "temp"; const char HSV_P[] PROGMEM = "HSV"; const char RGB_P[] PROGMEM = "RGB"; diff --git a/lighthub/utils.cpp b/lighthub/utils.cpp index 1c75adb..d710a19 100644 --- a/lighthub/utils.cpp +++ b/lighthub/utils.cpp @@ -630,73 +630,6 @@ itemCmd mapInt(int32_t arg, aJsonObject* map) return _itemCmd.Int(arg); } -statusLED::statusLED(uint8_t pattern) -{ -#if defined (STATUSLED) - pinMode(pinRED, OUTPUT); - pinMode(pinGREEN, OUTPUT); - pinMode(pinBLUE, OUTPUT); - set(pattern); - timestamp=millis()+ledDelayms; -#endif -} - -void statusLED::show (uint8_t pattern) -{ -#if defined (STATUSLED) - digitalWrite(pinRED,(pattern & ledRED)?HIGH:LOW ); - digitalWrite(pinGREEN,(pattern & ledGREEN)?HIGH:LOW); - digitalWrite(pinBLUE,(pattern & ledBLUE)?HIGH:LOW); -#endif -} - -void statusLED::set (uint8_t pattern) -{ -#if defined (STATUSLED) - short newStat = pattern & ledParams; - - if (newStat!=(curStat & ledParams)) - { - //if (!(curStat & ledHidden)) - show(pattern); - curStat=newStat | (curStat & ~ledParams); - } -#endif -} - -void statusLED::flash(uint8_t pattern) -{ -#if defined (STATUSLED) - show(pattern); - curStat|=ledFlash; -#endif -} - -void statusLED::poll() -{ -#if defined (STATUSLED) - if (curStat & ledFlash) - { - curStat&=~ledFlash; - show(curStat); - } -if (millis()>timestamp) - { - - if (curStat & ledFASTBLINK) timestamp=millis()+ledFastDelayms; - else timestamp=millis()+ledDelayms; - - if (( curStat & ledBLINK) || (curStat & ledFASTBLINK)) - { - curStat^=ledHidden; - if (curStat & ledHidden) - show(0); - else show(curStat); - } - } -#endif -} - #pragma message(VAR_NAME_VALUE(debugSerial)) #pragma message(VAR_NAME_VALUE(SERIAL_BAUD)) diff --git a/lighthub/utils.h b/lighthub/utils.h index bc45745..3fdc4cf 100644 --- a/lighthub/utils.h +++ b/lighthub/utils.h @@ -65,32 +65,3 @@ bool isTimeOver(uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t m bool executeCommand(aJsonObject* cmd, int8_t toggle = -1); bool executeCommand(aJsonObject* cmd, int8_t toggle, itemCmd _itemCmd); itemCmd mapInt(int32_t arg, aJsonObject* map); - -#define ledRED 1 -#define ledGREEN 2 -#define ledBLUE 4 -#define ledBLINK 8 -#define ledFASTBLINK 16 -#define ledParams (ledRED | ledGREEN | ledBLUE | ledBLINK | ledFASTBLINK) - -#define ledFlash 32 -#define ledHidden 64 - -#define pinRED 50 -#define pinGREEN 51 -#define pinBLUE 52 - -#define ledDelayms 1000UL -#define ledFastDelayms 300UL - -class statusLED { -public: - statusLED(uint8_t pattern = 0); - void set (uint8_t pattern); - void show (uint8_t pattern); - void poll(); - void flash(uint8_t pattern); -private: - uint8_t curStat; - uint32_t timestamp; -}; diff --git a/platformio.ini b/platformio.ini index 155e30d..7246a53 100644 --- a/platformio.ini +++ b/platformio.ini @@ -23,13 +23,13 @@ default_envs = ; mega2560-5500 ; LightHub controller HW revision 2.1 and above (Wiznet 5500 CS on pin 53) - lighthub21 +; lighthub21 ; Arduino DUE + Ethernet shield Wiznet 5100 ; due-5100 ; Generic DUE -; due + due ; Arduino DUE + Ethernet shield Wiznet 5500 ; due-5500 @@ -280,8 +280,8 @@ build_flags = !python get_build_flags.py due ; Need to place arduinoOTA utility from Arduino IDE distribution to folder in your PATH ;fix address and password ;upload_flags = -;upload_command = arduinoOTA -address 192.168.88.21 -port 65280 -username arduino -password password -b -upload /sketch -sketch $SOURCE -;upload_protocol = custom +upload_command = arduinoOTA -address 192.168.88.21 -port 65280 -username arduino -password password -b -upload /sketch -sketch $SOURCE +upload_protocol = custom lib_ignore = ;DS2482_OneWire //UNCOMMENT for software 1-wire driver DHT sensor library for ESPx