Refactoring/smoke tested

This commit is contained in:
2020-11-19 16:39:06 +03:00
parent 14ff55fd59
commit 9fa9e0f481
14 changed files with 369 additions and 910 deletions

View File

@@ -8,7 +8,7 @@ class chPersistent {};
class abstractOut : public abstractCh{
public:
abstractOut(Item * _item):abstractCh(){item=_item;};
virtual int Ctrl(itemCmd cmd, char* subItem=NULL) =0;
virtual int Ctrl(itemCmd cmd, char* subItem=NULL, bool toExecute=true) =0;
virtual int isActive(){return 0;};
virtual int getDefaultOnVal(){return 100;};
virtual int getChanType(){return 0;}

View File

@@ -16,16 +16,30 @@ short colorChannel::getChannelAddr(short n)
return item->getArg(n);
}
int colorChannel::Ctrl(itemCmd cmd, char* subItem)
int colorChannel::Ctrl(itemCmd cmd, char* subItem, bool toExecute)
{
int chActive = item->isActive();
bool toExecute = (chActive>0); // execute if channel is active now
debugSerial<<F("clrCtr: ");
cmd.debugOut();
//int chActive = item->isActive();
//bool toExecute = (chActive>0); // execute if channel is active now
int suffixCode = cmd.getSuffix();
itemCmd st(ST_HSV,CMD_VOID);
/*
// Since this driver working both, for single-dimmed or PWM channel and color - define storage type
uint8_t storageType;
switch (getChanType())
{
case CH_RGB:
case CH_RGBW:
storageType=ST_HSV;
break;
default:
storageType=ST_PERCENTS;
}
itemCmd st(storageType,CMD_VOID);
if (!suffixCode) toExecute=true; //forced execute if no suffix
if (cmd.isCommand() && !suffixCode) suffixCode=S_CMD; //if some known command recognized , but w/o correct cmd suffix - threat it as command
*/
switch(suffixCode)
{
@@ -34,62 +48,32 @@ case S_NOTFOUND:
toExecute = true;
case S_SET:
case S_HSV:
st.loadItem(item);
st.assignFrom(cmd);
PixelCtrl(st, subItem, toExecute);
st.saveItem(item);
if (!suffixCode)
{
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);
}
else item->SendStatus(SEND_PARAMETERS | SEND_DEFFERED);
PixelCtrl(cmd, subItem, toExecute);
return 1;
/*
case S_HUE:
st.setH(uint16_t);
break;
case S_SAT:
st.setS(uint8_t);
break;
*/
case S_CMD:
item->setCmd(cmd.getCmd());
switch (cmd.getCmd())
{
case CMD_ON:
//retrive stored values
if (st.loadItem(item))
{
if (st.param.aslong && (st.param.v<MIN_VOLUME)) {
st.Percents(INIT_VOLUME);
}
debugSerial<<F("Restored: ")<<st.param.h<<F(",")<<st.param.s<<F(",")<<st.param.v<<endl;
}
else // Not restored
{
st.setDefault();
debugSerial<<st.param.aslong<<F(": No stored values - default\n");
}
st.saveItem(item, true);
PixelCtrl(st);
PixelCtrl(cmd,subItem, true);
item->SendStatus(SEND_COMMAND | SEND_PARAMETERS );
return 1;
case CMD_OFF:
st.Percents(0);
PixelCtrl(st, subItem, true);
cmd.Percents(0);
PixelCtrl(cmd, subItem, true);
item->SendStatus(SEND_COMMAND);
return 1;
default:
debugSerial<<F("Unknown cmd ")<<cmd.getCmd()<<endl;
} //switch cmd
default:
debugSerial<<F("Unknown suffix ")<<suffixCode<<endl;
} //switch suffix
debugSerial<<F("Unknown cmd")<<endl;
cmd.debugOut();
return 0;
}

View File

@@ -14,7 +14,7 @@ public:
if (iaddr<0) iaddr=-iaddr;
numArgs = item->getArgCount(); // and how many addresses is configured
};
int Ctrl(itemCmd cmd, char* subItem=NULL) override;
int Ctrl(itemCmd cmd, char* subItem=NULL, bool toExecute=true) override;
virtual int PixelCtrl(itemCmd cmd, char* subItem=NULL, bool show=true ) =0;
short getChannelAddr(short n =0);
protected:

View File

@@ -23,7 +23,7 @@ e-mail anklimov@gmail.com
#include "utils.h"
#include <PubSubClient.h>
#include "main.h"
#include "itemCmd.h""
#include "itemCmd.h"
#ifndef DHT_DISABLE
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)

File diff suppressed because it is too large Load Diff

View File

@@ -39,17 +39,48 @@ int txt2cmd(char *payload) {
itemCmd::itemCmd(uint8_t _type, uint8_t _code)
{
cmd.aslong=0;
param.aslong=0;
cmd.itemArgType=_type;
cmd.cmdCode=_code;
}
itemCmd::itemCmd(float val)
{
cmd.cmdCode=0;
cmd.aslong=0;
param.aslong=0;
cmd.itemArgType=ST_FLOAT;
param.asfloat=val;
}
itemCmd itemCmd::setChanType(short chanType)
{
switch (chanType)
{
case CH_RGB:
case CH_RGBW:
case CH_SPILED:
cmd.itemArgType=ST_HSV;
break;
case CH_AC:
case CH_THERMO:
case CH_VCTEMP:
cmd.itemArgType=ST_FLOAT_CELSIUS;
break;
case CH_DIMMER:
case CH_MOTOR:
case CH_PWM:
case CH_RELAY:
cmd.itemArgType=ST_PERCENTS;
break;
default:
cmd.itemArgType=ST_PERCENTS;
}
return *this;
}
itemCmd itemCmd::setDefault()
{
switch (cmd.itemArgType){
@@ -71,7 +102,7 @@ itemCmd itemCmd::setDefault()
return *this;
}
itemCmd itemCmd::setH(uint16_t h)
bool itemCmd::setH(uint16_t h)
{
int par=h;
switch (cmd.itemArgType)
@@ -79,16 +110,20 @@ itemCmd itemCmd::setH(uint16_t h)
case ST_VOID:
cmd.itemArgType=ST_HSV;
case ST_HSV:
if (par>100) par=100;
case ST_HSV255:
if (par>255) par=255;
if (par>365) par=365;
if (par<0) par=0;
param.h=par;
break;
default:
// debugSerial<<F("Can't assign HUE to type ")<<cmd.itemArgType<<endl;
return false;
}
return *this;
return true;
}
itemCmd itemCmd::setS(uint8_t s)
bool itemCmd::setS(uint8_t s)
{
int par=s;
switch (cmd.itemArgType)
@@ -97,15 +132,31 @@ itemCmd itemCmd::setS(uint8_t s)
cmd.itemArgType=ST_HSV;
case ST_HSV:
if (par>100) par=100;
param.s=par;
break;
case ST_HSV255:
if (par>255) par=255;
if (par<0) par=0;
param.s=par;
break;
default:
// debugSerial<<F("Can't assign saturation to type ")<<cmd.itemArgType<<endl;
return false;
}
return *this;
return true;
}
itemCmd itemCmd::incrementPercents(int16_t dif)
uint16_t itemCmd::getH()
{
return param.h;
}
uint16_t itemCmd::getS()
{
return param.s;
}
bool itemCmd::incrementPercents(int16_t dif)
{ int par=param.v;
switch (cmd.itemArgType)
{
@@ -121,12 +172,13 @@ itemCmd itemCmd::incrementPercents(int16_t dif)
if (par>255) par=255;
if (par<0) par=0;
break;
default: return false;
}
param.v=par;
return *this;
return true;
}
itemCmd itemCmd::incrementH(int16_t dif)
bool itemCmd::incrementH(int16_t dif)
{ int par=param.h;
switch (cmd.itemArgType)
{
@@ -135,31 +187,34 @@ itemCmd itemCmd::incrementH(int16_t dif)
par+=dif;
if (par>365) par=0;
if (par<0) par=365;
break;
default: return false;
break;
}
param.h=par;
return *this;
return true;
}
itemCmd itemCmd::incrementS(int16_t dif)
bool itemCmd::incrementS(int16_t dif)
{int par=param.s;
switch (cmd.itemArgType)
{
case ST_PERCENTS:
//case ST_PERCENTS:
case ST_HSV:
par+=dif;
if (par>100) par=100;
if (par<0) par=0;
break;
case ST_PERCENTS255:
//case ST_PERCENTS255:
case ST_HSV255:
par+=dif;
if (par>255) par=255;
if (par<0) par=0;
break;
default: return false;
}
param.s=par;
return *this;
return true;
}
@@ -168,6 +223,7 @@ itemCmd itemCmd::assignFrom(itemCmd from)
{
bool RGBW_flag = false;
bool HSV255_flag = false;
cmd.suffixCode=from.cmd.suffixCode;
switch (cmd.itemArgType){ //Destination
case ST_HSV:
@@ -201,6 +257,7 @@ itemCmd itemCmd::assignFrom(itemCmd from)
default:
debugSerial<<F("Wrong Assignment ")<<from.cmd.itemArgType<<F("->")<<cmd.itemArgType<<endl;
}
break;
case ST_HSV255:
case ST_PERCENTS255:
switch (from.cmd.itemArgType)
@@ -232,7 +289,7 @@ itemCmd itemCmd::assignFrom(itemCmd from)
default:
debugSerial<<F("Wrong Assignment ")<<from.cmd.itemArgType<<F("->")<<cmd.itemArgType<<endl;
}
break;
case ST_VOID:
cmd.itemArgType=from.cmd.itemArgType;
@@ -298,6 +355,7 @@ itemCmd itemCmd::assignFrom(itemCmd from)
param.g=rgb.g;
param.b=rgb.b;
#endif
break;
}
default:
debugSerial<<F("Wrong Assignment ")<<from.cmd.itemArgType<<F("->")<<cmd.itemArgType<<endl;
@@ -388,7 +446,7 @@ uint8_t itemCmd::getArgType()
itemCmd itemCmd::setArgType(uint8_t type)
{
cmd.itemArgType=type;
cmd.itemArgType=type & 0xF;
return *this;
}
@@ -515,10 +573,16 @@ bool itemCmd::loadItem(Item * item, bool includeCommand)
{
if (item && item->isValid())
{
param.asInt32=item->getVal();
cmd.itemArgType=item->getSubtype();
if (includeCommand) cmd.cmdCode=item->getCmd();
return (cmd.itemArgType!=ST_VOID);
short subtype =item->getSubtype();
if (subtype)
{
param.asInt32=item->getVal();
cmd.itemArgType= subtype;
if (includeCommand) cmd.cmdCode=item->getCmd();
debugSerial<<F("Loaded :");
debugOut();
return 1;
}
}
return false;
}
@@ -530,6 +594,8 @@ bool itemCmd::saveItem(Item * item, bool includeCommand)
item->setVal(param.asInt32);
item->setSubtype(cmd.itemArgType);
if (includeCommand) item->setCmd(cmd.cmdCode);
debugSerial<<F("Saved :");
debugOut();
return true;
}
return false;
@@ -537,13 +603,13 @@ return false;
char * itemCmd::toString(char * Buffer, int bufLen)
char * itemCmd::toString(char * Buffer, int bufLen, int sendFlags )
{
if (!Buffer || !bufLen) return NULL;
*Buffer=0;
char * argPtr=Buffer;
if (isCommand())
if (isCommand() && (sendFlags & SEND_COMMAND))
{
int len;
strncpy_P(Buffer, commands_P[cmd.cmdCode], bufLen);
@@ -552,12 +618,14 @@ char * itemCmd::toString(char * Buffer, int bufLen)
argPtr+=len;
bufLen-=len;
}
if (sendFlags & SEND_PARAMETERS)
switch (cmd.itemArgType)
{
case ST_PERCENTS:
case ST_PERCENTS255:
snprintf(argPtr, bufLen, "%u", param.v);
break;
case ST_UINT32:
snprintf(argPtr, bufLen, "%lu", param.asUint32);
break;
@@ -592,3 +660,10 @@ char * itemCmd::toString(char * Buffer, int bufLen)
}
return Buffer;
}
void itemCmd::debugOut()
{
char buf[32];
toString(buf,sizeof(buf));
debugSerial<<buf<<F(" AT:")<<getArgType()<<F(" Suff:")<<getSuffix()<<endl;
}

View File

@@ -19,6 +19,7 @@ e-mail anklimov@gmail.com
*/
#pragma once
#include "Arduino.h"
///#include "item.h"
typedef char cmdstr[9];
@@ -98,16 +99,17 @@ ST_FLOAT = 13//,
#define ST_PERCENTS 1
#define ST_TENS 2
#define ST_HSV 3
#define ST_FLOAT_CELSIUS 4
#define ST_FLOAT_FARENHEIT 5
#define ST_RGB 6
#define ST_RGBW 7
#define ST_PERCENTS255 8
#define ST_HSV255 9
#define ST_INT32 10
#define ST_UINT32 11
#define ST_STRING 12
#define ST_FLOAT 13
#define ST_HSVW 4
#define ST_FLOAT_CELSIUS 5
#define ST_FLOAT_FARENHEIT 6
#define ST_RGB 7
#define ST_RGBW 8
#define ST_PERCENTS255 9
#define ST_HSV255 10
#define ST_INT32 11
#define ST_UINT32 12
#define ST_STRING 13
#define ST_FLOAT 14
#pragma pack(push, 1)
@@ -122,12 +124,18 @@ typedef union
uint8_t cmdCode;
union {
uint8_t cmdFlag;
/*
struct
{ uint8_t suffixCode:4;
uint8_t itemArgType:4;
};
*/
};
uint8_t cmdEffect;
struct
{ uint8_t suffixCode:4;
uint8_t itemArgType:4;
};
// uint8_t cmdEffect;
uint8_t cmdParam;
};
} itemCmdStore;
@@ -181,8 +189,10 @@ public:
itemCmd HSV(uint16_t h, uint8_t s, uint8_t v);
itemCmd RGB(uint8_t r, uint8_t g, uint8_t b);
itemCmd RGBW(uint8_t r, uint8_t g, uint8_t b, uint8_t w);
itemCmd setH(uint16_t);
itemCmd setS(uint8_t);
bool setH(uint16_t);
bool setS(uint8_t);
uint16_t getH();
uint16_t getS();
itemCmd setArgType(uint8_t);
itemCmd Percents(int i);
itemCmd Percents255(int i);
@@ -190,9 +200,9 @@ public:
uint8_t getSuffix();
itemCmd setSuffix(uint8_t suffix);
itemCmd incrementPercents(int16_t);
itemCmd incrementH(int16_t);
itemCmd incrementS(int16_t);
bool incrementPercents(int16_t);
bool incrementH(int16_t);
bool incrementS(int16_t);
long int getInt();
short getPercents(bool inverse=false);
@@ -200,13 +210,15 @@ public:
uint8_t getCmd();
uint8_t getArgType();
uint8_t getCmdParam();
char * toString(char * Buffer, int bufLen);
char * toString(char * Buffer, int bufLen, int sendFlags = SEND_COMMAND | SEND_PARAMETERS );
bool isCommand();
bool isValue();
bool isHSV();
itemCmd setDefault();
itemCmd setChanType(short chanType);
void debugOut();
};
#pragma pack(pop)

View File

@@ -263,7 +263,7 @@ return INTERVAL_POLLING;
};
//int out_AC::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* subItem)
int out_AC::Ctrl(itemCmd cmd, char* subItem)
int out_AC::Ctrl(itemCmd cmd, char* subItem , bool toExecute)
{char s_mode[10];
int suffixCode = cmd.getSuffix();
// Some additional Subitems

View File

@@ -30,7 +30,7 @@ public:
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(itemCmd cmd, char* subItem=NULL) override;
int Ctrl(itemCmd cmd, char* subItem=NULL, bool toExecute=true) override;
protected:
void InsertData(byte data[], size_t size);

View File

@@ -338,11 +338,11 @@ int out_Modbus::getChanType()
int out_Modbus::Ctrl(itemCmd cmd, char* subItem)
int out_Modbus::Ctrl(itemCmd cmd, char* subItem, bool toExecute)
{
int chActive = item->isActive();
bool toExecute = (chActive>0);
itemCmd st(ST_UINT32,CMD_VOID);
//int chActive = item->isActive();
//bool toExecute = (chActive>0);
//itemCmd st(ST_UINT32,CMD_VOID);
int suffixCode = cmd.getSuffix();
if (cmd.isCommand() && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it
@@ -355,58 +355,30 @@ toExecute = true;
debugSerial<<F("Forced execution");
case S_SET:
if (!cmd.isValue()) return 0;
//////item->setVal(st=Parameters[0]); //Store
if (!suffixCode)
{
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
}
else item->SendStatus(SEND_PARAMETERS | SEND_DEFFERED);
//TODO
return 1;
//break;
case S_CMD:
//item->setCmd(cmd.getCmd());
st.loadItem(item);
switch (cmd.getCmd())
{
case CMD_ON:
//retrive stored values
st.loadItem(item);
if (st.getPercents() && (st.getPercents()<MIN_VOLUME) /* && send */) st.Percents(INIT_VOLUME);
st.saveItem(item);
if (st.getPercents()) //Stored smthng
{
item->SendStatus(SEND_COMMAND | SEND_PARAMETERS);
debugSerial<<F("Restored: ")<<st.getPercents()<<endl;
}
else
{
debugSerial<<F(": No stored values - default\n");
// Store
st.setDefault();
st.saveItem(item);
item->SendStatus(SEND_COMMAND | SEND_PARAMETERS );
}
// if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time
return 1;
case CMD_OFF:
item->SendStatus(SEND_COMMAND);
// if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time
return 1;
} //switch cmd
default:
debugSerial<<F("Unknown cmd ")<<cmd.getCmd()<<endl;
} //switch cmd
break;
default:
debugSerial<<F("Unknown suffix ")<<suffixCode<<endl;
} //switch suffix
debugSerial<<F("Unknown cmd")<<endl;
return 0;
}

View File

@@ -31,7 +31,7 @@ public:
int Status() override;
int isActive() override;
int getChanType() override;
int Ctrl(itemCmd cmd, char* subItem=NULL) override;
int Ctrl(itemCmd cmd, char* subItem=NULL, bool toExecute=true) override;
//int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) override;
protected:

View File

@@ -211,12 +211,12 @@ int out_Motor::getChanType()
int out_Motor::Ctrl(itemCmd cmd, char* subItem)
int out_Motor::Ctrl(itemCmd cmd, char* subItem , bool toExecute)
{
int chActive = item->isActive();
bool toExecute = (chActive>0);
//int chActive = item->isActive();
//bool toExecute = (chActive>0);
int suffixCode = cmd.getSuffix();
itemCmd st(ST_PERCENTS,CMD_VOID);
//itemCmd st(ST_PERCENTS,CMD_VOID);
if (cmd.isCommand() && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it
item->setFlag(ACTION_NEEDED);
@@ -229,6 +229,8 @@ toExecute = true;
debugSerial<<F("Forced execution");
case S_SET:
if (!cmd.isValue()) return 0;
if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time
/*
st.assignFrom(cmd);
//Store
st.saveItem(item);
@@ -240,7 +242,7 @@ case S_SET:
if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time
}
else item->SendStatus(SEND_PARAMETERS | SEND_DEFFERED);
*/
return 1;
//break;
@@ -249,10 +251,12 @@ case S_CMD:
switch (cmd.getCmd())
{
case CMD_ON:
/*
//retrive stored values
if (st.loadItem(item))
{
if (st.getPercents() && (st.getPercents()<MIN_VOLUME) /* && send */)
if (st.getPercents() && (st.getPercents()<MIN_VOLUME))
{ //Volume too low
st.Percents(INIT_VOLUME);
st.saveItem(item);
@@ -270,12 +274,12 @@ case S_CMD:
//item->setVal(st);
item->SendStatus(SEND_COMMAND | SEND_PARAMETERS );
}
*/
if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time
return 1;
case CMD_OFF:
item->SendStatus(SEND_COMMAND);
////item->SendStatus(SEND_COMMAND);
if (item->getExt()) item->setExt(millis()+maxOnTime); //Extend motor time
return 1;

View File

@@ -26,7 +26,7 @@ public:
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, char* subItem=NULL) override;
int Ctrl(itemCmd cmd, char* subItem=NULL, bool toExecute=true) override;
int8_t pinUp;
int8_t pinDown;

View File

@@ -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
@@ -581,8 +581,8 @@ framework = arduino
board = due
build_flags = !python get_build_flags.py lighthub21
;upload_command = arduinoOTA -address 192.168.11.172 -port 65280 -username arduino -password password -b -upload /sketch -sketch $SOURCE
;upload_command = arduinoOTA -address 192.168.88.34 -port 65280 -username arduino -password password -b -upload /sketch -sketch $SOURCE
;upload_protocol = custom
upload_command = arduinoOTA -address 192.168.88.28 -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