MASSIVE refactoring. /set scale changed 100 -> 255

This commit is contained in:
2021-04-08 00:32:21 +03:00
parent 0474c0ac48
commit 7c642ec286
37 changed files with 30318 additions and 30425 deletions

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,3 @@
mode com3:1200,n,8,1
pause
..\tools\win\tool-bossac\bossac.exe -i --port=com3 -U false -e -w -v -b firmware.bin -R

View File

@@ -0,0 +1,3 @@
export PORT=cu.usbmodem14101
echo . | stty -f /dev/$PORT speed 1200
../tools/mac/tool-bossac/bossac -U false -p $PORT -i -e -w -v -b firmware.bin -R

23
lighthub/abstractout.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include "item.h"
#include "abstractout.h"
#include "itemCmd.h"
int abstractOut::isActive()
{itemCmd st;
switch (item->getCmd())
{
case CMD_OFF:
case CMD_HALT:
return 0;
break;
default:
st.loadItem(item);
return st.getPercents255();
}
};
int abstractOut::Setup()
{
if (item) item->setCmd(CMD_OFF);
}

View File

@@ -9,10 +9,11 @@ class abstractOut : public abstractCh{
public: public:
abstractOut(Item * _item):abstractCh(){item=_item;}; abstractOut(Item * _item):abstractCh(){item=_item;};
virtual int Ctrl(itemCmd cmd, char* subItem=NULL, bool toExecute=true) =0; virtual int Ctrl(itemCmd cmd, char* subItem=NULL, bool toExecute=true) =0;
virtual int isActive(){return 0;}; virtual int isActive();
virtual itemCmd getDefaultOnVal(){return itemCmd(ST_PERCENTS,CMD_VOID).Percents(100);}; virtual itemCmd getDefaultOnVal(){return itemCmd().Percents255(255);};
virtual int getChanType(){return 0;} virtual int getChanType(){return 0;}
virtual int getDefaultStorageType(){return ST_PERCENTS;} virtual int getDefaultStorageType(){return ST_PERCENTS255;} /// Remove?? Now getChanType used instead
int Setup() override;
protected: protected:
Item * item; Item * item;
}; };

View File

@@ -16,6 +16,23 @@ short colorChannel::getChannelAddr(short n)
return item->getArg(n); return item->getArg(n);
} }
int colorChannel::getDefaultStorageType()
{
if (item)
switch (numArgs)
{
case 3:
case 4:
return ST_HSV255;
case 1:
return ST_PERCENTS255;
}
return ST_VOID;
}
/*
int colorChannel::isActive() int colorChannel::isActive()
{ {
itemCmd st; itemCmd st;
@@ -24,6 +41,7 @@ int val = st.getInt();
debugSerial<< F(" val:")<<val<<endl; debugSerial<< F(" val:")<<val<<endl;
return val; return val;
} }
*/
int colorChannel::Ctrl(itemCmd cmd, char* subItem, bool toExecute) int colorChannel::Ctrl(itemCmd cmd, char* subItem, bool toExecute)
{ {
@@ -39,7 +57,7 @@ case S_NOTFOUND:
// turn on and set // turn on and set
toExecute = true; toExecute = true;
case S_SET: case S_SET:
case S_ESET: //case S_ESET:
case S_HSV: case S_HSV:
PixelCtrl(cmd, subItem, toExecute); PixelCtrl(cmd, subItem, toExecute);
return 1; return 1;

View File

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

View File

@@ -89,7 +89,7 @@ int txt2subItem(char *payload) {
if (!payload || !strlen(payload)) return S_NOTFOUND; if (!payload || !strlen(payload)) return S_NOTFOUND;
// Check for command // Check for command
if (strcmp_P(payload, SET_P) == 0) cmd = S_SET; if (strcmp_P(payload, SET_P) == 0) cmd = S_SET;
else if (strcmp_P(payload, ESET_P) == 0) cmd = S_ESET; //else if (strcmp_P(payload, ESET_P) == 0) cmd = S_ESET;
else if (strcmp_P(payload, CMD_P) == 0) cmd = S_CMD; else if (strcmp_P(payload, CMD_P) == 0) cmd = S_CMD;
else if (strcmp_P(payload, MODE_P) == 0) cmd = S_MODE; else if (strcmp_P(payload, MODE_P) == 0) cmd = S_MODE;
else if (strcmp_P(payload, HSV_P) == 0) cmd = S_HSV; else if (strcmp_P(payload, HSV_P) == 0) cmd = S_HSV;
@@ -476,12 +476,11 @@ int cmd = txt2cmd(payload);
debugSerial<<F("Txt2Cmd:")<<cmd<<endl; debugSerial<<F("Txt2Cmd:")<<cmd<<endl;
itemCmd st(ST_VOID,cmd); itemCmd st(ST_VOID,cmd);
bool set255flag=false; bool set255flag=true;
switch (suffixCode) { switch (suffixCode) {
case S_ESET: case S_NOTFOUND:
set255flag=true; set255flag=false;
suffixCode=S_SET;
break; break;
case S_HSV: case S_HSV:
cmd=CMD_HSV; cmd=CMD_HSV;
@@ -526,10 +525,9 @@ st.setSuffix(suffixCode);
default: default:
switch (i) //Number of params switch (i) //Number of params
{ {
case 1: st=Par0; case 1:
//if (set255flag) if (set255flag) st=Par0;
// st.Percents255(Par[0]); else st.Percents(Par0.getInt());
//else st.Percents(Par[0]); //ToDo float
break; break;
case 2: st.HS(Par0.getInt(),Par[0]); case 2: st.HS(Par0.getInt(),Par[0]);
break; break;
@@ -581,6 +579,7 @@ st.setSuffix(suffixCode);
return 0; return 0;
} }
int Item::Ctrl(itemCmd cmd, char* subItem) int Item::Ctrl(itemCmd cmd, char* subItem)
{ {
//char stringBuffer[16]; //char stringBuffer[16];
@@ -702,15 +701,16 @@ int Item::Ctrl(itemCmd cmd, char* subItem)
case S_NOTFOUND: case S_NOTFOUND:
toExecute=true; toExecute=true;
case S_SET: case S_SET:
case S_ESET: // case S_ESET:
if (st.incrementPercents(step)) if (st.incrementPercents(step))
{ {
st.saveItem(this); st.saveItem(this);
SendStatus(SEND_PARAMETERS | SEND_DEFFERED); SendStatus(SEND_PARAMETERS | SEND_DEFFERED);
} }
break; break;
case S_HUE: case S_HUE:
if (cmd.isColor()) st.convertTo(ST_HSV);//Extend storage for color channel if (cmd.isColor()) st.convertTo(ST_HSV255);//Extend storage for color channel
if (st.incrementH(step)) if (st.incrementH(step))
{ {
st.setSuffix(S_SET); st.setSuffix(S_SET);
@@ -719,12 +719,13 @@ int Item::Ctrl(itemCmd cmd, char* subItem)
} }
break; break;
case S_SAT: case S_SAT:
if (cmd.isColor()) st.convertTo(ST_HSV);//Extend storage for color channel if (cmd.isColor()) st.convertTo(ST_HSV255);//Extend storage for color channel
if (st.incrementS(step)) if (st.incrementS(step))
{ {
st.setSuffix(S_SET); st.setSuffix(S_SET);
st.saveItem(this); st.saveItem(this);
SendStatus(SEND_PARAMETERS | SEND_DEFFERED); SendStatus(SEND_PARAMETERS | SEND_DEFFERED);
} }
} //switch suffix } //switch suffix
@@ -736,7 +737,7 @@ int Item::Ctrl(itemCmd cmd, char* subItem)
//// if ( cType == CH_RGB || cType == CH_RGBW || cType == CH_GROUP ) //// if ( cType == CH_RGB || cType == CH_RGBW || cType == CH_GROUP )
switch (suffixCode) switch (suffixCode)
{ {
case S_NOTFOUND: //For empty (universal) suffix - turn ON/OFF automatically case S_NOTFOUND: //For empty (universal) OPENHAB suffix - turn ON/OFF automatically
toExecute=true; toExecute=true;
if (chActive>0 && !cmd.getInt()) st.Cmd(CMD_OFF); if (chActive>0 && !cmd.getInt()) st.Cmd(CMD_OFF);
if (chActive==0 && cmd.getInt()) st.Cmd(CMD_ON); if (chActive==0 && cmd.getInt()) st.Cmd(CMD_ON);
@@ -745,13 +746,16 @@ int Item::Ctrl(itemCmd cmd, char* subItem)
// continue processing as SET // continue processing as SET
case S_SET: case S_SET:
case S_ESET: // case S_ESET:
// if previous color was in RGB notation but new value is HSV - discard previous val and change type;
if ((st.getArgType() == ST_RGB || st.getArgType() == ST_RGBW) && if ((st.getArgType() == ST_RGB || st.getArgType() == ST_RGBW) &&
(cmd.getArgType() == ST_HSV ) || (cmd.getArgType() == ST_HSV255)) /*(cmd.getArgType() == ST_HSV ) || */(cmd.getArgType() == ST_HSV255))
st.setArgType(cmd.getArgType()); st.setArgType(cmd.getArgType());
if (itemType == CH_GROUP && cmd.isColor()) st.setArgType(ST_HSV);//Extend storage for group channel if (itemType == CH_GROUP && cmd.isColor()) st.setArgType(ST_HSV255);//Extend storage for group channel
st.assignFrom(cmd);
//Convert value to most approptiate type for channel
st.assignFrom(cmd,getChanType());
st.saveItem(this); st.saveItem(this);
SendStatus(SEND_PARAMETERS | SEND_DEFFERED); SendStatus(SEND_PARAMETERS | SEND_DEFFERED);
@@ -761,7 +765,7 @@ int Item::Ctrl(itemCmd cmd, char* subItem)
break; break;
case S_SAT: case S_SAT:
if (cmd.isColor()) st.convertTo(ST_HSV);//Extend storage for color channel //if (cmd.isColor()) st.convertTo(ST_HSV255);//Extend storage for color channel
if (st.setS(cmd.getS())) if (st.setS(cmd.getS()))
{ {
st.setSuffix(S_SET); st.setSuffix(S_SET);
@@ -771,7 +775,7 @@ int Item::Ctrl(itemCmd cmd, char* subItem)
break; break;
case S_HUE: case S_HUE:
if (cmd.isColor()) st.convertTo(ST_HSV);//Extend storage for color channel //if (cmd.isColor()) st.convertTo(ST_HSV255);//Extend storage for color channel
if (st.setH(cmd.getH())) if (st.setH(cmd.getH()))
{ {
st.setSuffix(S_SET); st.setSuffix(S_SET);
@@ -933,7 +937,7 @@ switch (itemType) {
} }
break; break;
case S_SET: case S_SET:
case S_ESET: // case S_ESET:
st.saveItem(this); st.saveItem(this);
break; break;
} }
@@ -983,7 +987,7 @@ if (active) debugSerial<<F(" active ");
} }
int Item::isActive() { int Item::isActive() {
itemArgStore st; itemCmd st;
int val = 0; int val = 0;
@@ -1020,7 +1024,7 @@ int Item::isActive() {
return active; return active;
} }
// No driver - check value // No driver - check value
st.aslong = getVal(); st.loadItem(this);
switch (itemType) { switch (itemType) {
case CH_GROUP: //make recursive calculation - is it some active in group case CH_GROUP: //make recursive calculation - is it some active in group
@@ -1045,19 +1049,20 @@ int Item::isActive() {
break; break;
case CH_RGBW: //case CH_RGBW:
case CH_RGB: //case CH_RGB:
//case CH_DIMMER: //Legacy channels
val = st.v; //Light volume
break;
case CH_DIMMER: //Legacy channels
case CH_MODBUS: case CH_MODBUS:
case CH_VC: case CH_VC:
val = st.getPercents255(); //Light volume
break;
case CH_VCTEMP: case CH_VCTEMP:
case CH_PWM: case CH_PWM:
val = st.aslong; val = st.getInt();
break; break;
default: default:
debugSerial<<F(" unknown\n"); debugSerial<<F(" unknown\n");
@@ -1159,8 +1164,9 @@ int Item::VacomSetFan(itemCmd st) {
int addr = getArg(); int addr = getArg();
debugSerial<<F("VC#")<<addr<<F("=")<<val<<endl; debugSerial<<F("VC#")<<addr<<F("=")<<val<<endl;
if (modbusBusy) { if (modbusBusy) {
setCmd(cmd); // setCmd(cmd);
setVal(val); // setVal(val);
st.saveItem(this,true);
mb_fail(); mb_fail();
return 0; return 0;
} }
@@ -1203,8 +1209,9 @@ int addr;
debugSerial<<F("VC_heat#")<<addr<<F("=")<<val<<F(" cmd=")<<cmd<<endl; debugSerial<<F("VC_heat#")<<addr<<F("=")<<val<<F(" cmd=")<<cmd<<endl;
if (modbusBusy) { if (modbusBusy) {
setCmd(cmd); //setCmd(cmd);
setVal(val); //setVal(val);
st.saveItem(this,true);
mb_fail(); mb_fail();
return 0; return 0;
} }
@@ -1330,7 +1337,7 @@ int Item::checkFM() {
Item item(airGateObj->valuestring); Item item(airGateObj->valuestring);
if (item.isValid()) if (item.isValid())
// item.Ctrl(0, 1, &val); // item.Ctrl(0, 1, &val);
item.Ctrl(itemCmd(ST_PERCENTS,CMD_VOID).Percents(val)); item.Ctrl(itemCmd().Percents(val));
} }
} }
} else } else
@@ -1500,8 +1507,10 @@ return 1;
int Item::checkModbusDimmer(int data) { int Item::checkModbusDimmer(int data) {
short mask = getArg(2); short mask = getArg(2);
if (mask < 0) return 0; itemCmd st;
if (mask < 0) return 0;
st.loadItem(this);
short maxVal = getArg(3); short maxVal = getArg(3);
if (maxVal<=0) maxVal = 0x3f; if (maxVal<=0) maxVal = 0x3f;
@@ -1509,15 +1518,17 @@ int Item::checkModbusDimmer(int data) {
if (mask == 1) d >>= 8; if (mask == 1) d >>= 8;
if (mask == 0 || mask == 1) d &= 0xff; if (mask == 0 || mask == 1) d &= 0xff;
if (maxVal) d = map(d, 0, maxVal, 0, 100); if (maxVal) d = map(d, 0, maxVal, 0, 255);
int cmd = getCmd(); int cmd = getCmd();
//debugSerial<<d); //debugSerial<<d);
if (getVal() != d || d && cmd == CMD_OFF || d && cmd == CMD_HALT) //volume changed or turned on manualy if (st.getPercents255() != d || d && cmd == CMD_OFF || d && cmd == CMD_HALT) //volume changed or turned on manualy
{ {
if (d) { // Actually turned on if (d) { // Actually turned on
if (cmd != CMD_XON && cmd != CMD_ON) setCmd(CMD_ON); //store command if (cmd != CMD_XON && cmd != CMD_ON) setCmd(CMD_ON); //store command
setVal(d); //store value st.Percents255(d);
st.saveItem(this);
//setVal(d); //store value
if (cmd == CMD_OFF || cmd == CMD_HALT) SendStatus(SEND_COMMAND); //update OH with ON if it was turned off before if (cmd == CMD_OFF || cmd == CMD_HALT) SendStatus(SEND_COMMAND); //update OH with ON if it was turned off before
SendStatus(SEND_PARAMETERS); //update OH with value SendStatus(SEND_PARAMETERS); //update OH with value
} else { } else {
@@ -1608,21 +1619,6 @@ int Item::SendStatus(int sendFlags) {
char valstr[20] = ""; char valstr[20] = "";
char cmdstr[8] = ""; char cmdstr[8] = "";
if (sendFlags & SEND_PARAMETERS)
{
// Preparing parameters payload //////////
switch (st.getArgType()) {
case ST_RGB:
case ST_RGBW:
//valstr[0]='#';
st.Cmd(CMD_RGB);
st.toString(valstr, sizeof(valstr), SEND_PARAMETERS|SEND_COMMAND);
break;
default:
st.toString(valstr, sizeof(valstr), SEND_PARAMETERS);
}
}
if (sendFlags & SEND_COMMAND) if (sendFlags & SEND_COMMAND)
{ {
// Preparing Command payload ////////////// // Preparing Command payload //////////////
@@ -1648,18 +1644,27 @@ int Item::SendStatus(int sendFlags) {
sendFlags &= ~SEND_COMMAND; sendFlags &= ~SEND_COMMAND;
} }
} }
if (st.getArgType()!=ST_RGB && st.getArgType()!=ST_RGBW) { // publish to MQTT - OpenHab Legacy style to
//publish to MQTT - OpenHab Legacy style to myhome/s_out/item flat values // myhome/s_out/item - mix: value and command
setTopic(addrstr,sizeof(addrstr),T_OUT); // send only for OH bus supported types
strncat(addrstr, itemArr->name, sizeof(addrstr)-1);
switch (st.getArgType())
{
case ST_PERCENTS255:
case ST_HSV255:
case ST_FLOAT_CELSIUS:
if (mqttClient.connected() && !ethernetIdleCount) if (mqttClient.connected() && !ethernetIdleCount)
{ {
setTopic(addrstr,sizeof(addrstr),T_OUT);
strncat(addrstr, itemArr->name, sizeof(addrstr)-1);
st.toString(valstr, sizeof(valstr), SEND_PARAMETERS,100);
if (sendFlags & SEND_PARAMETERS && st.getCmd() != CMD_OFF && st.getCmd() != CMD_HALT) if (sendFlags & SEND_PARAMETERS && st.getCmd() != CMD_OFF && st.getCmd() != CMD_HALT)
{ {
mqttClient.publish(addrstr, valstr, true); mqttClient.publish(addrstr, valstr, true);
debugSerial<<F("Pub: ")<<addrstr<<F("->")<<valstr<<endl; debugSerial<<F("Pub: ")<<addrstr<<F("->")<<valstr<<endl;
} }
else if (sendFlags & SEND_COMMAND) else if (sendFlags & SEND_COMMAND)
{ {
@@ -1673,7 +1678,9 @@ int Item::SendStatus(int sendFlags) {
setFlag(sendFlags); setFlag(sendFlags);
return 0; return 0;
} }
} }
// publush to MQTT - New style to // publush to MQTT - New style to
// myhome/s_out/item/cmd // myhome/s_out/item/cmd
// myhome/s_out/item/set // myhome/s_out/item/set
@@ -1684,15 +1691,27 @@ int Item::SendStatus(int sendFlags) {
strncat(addrstr, itemArr->name, sizeof(addrstr)-1); strncat(addrstr, itemArr->name, sizeof(addrstr)-1);
strncat(addrstr, "/", sizeof(addrstr)); strncat(addrstr, "/", sizeof(addrstr));
// Preparing parameters payload //////////
switch (st.getArgType()) {
case ST_RGB:
case ST_RGBW:
//valstr[0]='#';
st.Cmd(CMD_RGB);
st.toString(valstr, sizeof(valstr), SEND_PARAMETERS|SEND_COMMAND);
break;
default:
st.toString(valstr, sizeof(valstr), SEND_PARAMETERS);
}
switch (st.getArgType()) { switch (st.getArgType()) {
case ST_RGB: case ST_RGB:
case ST_RGBW: case ST_RGBW:
strncat_P(addrstr, SET_P, sizeof(addrstr)); strncat_P(addrstr, SET_P, sizeof(addrstr));
break; break;
case ST_PERCENTS255: // case ST_PERCENTS255:
case ST_HSV255: // case ST_HSV255:
strncat_P(addrstr, ESET_P, sizeof(addrstr)); // strncat_P(addrstr, ESET_P, sizeof(addrstr));
break; // break;
default: default:
strncat_P(addrstr, SET_P, sizeof(addrstr)); strncat_P(addrstr, SET_P, sizeof(addrstr));
} }

View File

@@ -26,7 +26,7 @@ e-mail anklimov@gmail.com
//#define S_SETnCMD 0 //#define S_SETnCMD 0
#define S_CMD 1 #define S_CMD 1
#define S_SET 2 #define S_SET 2
#define S_ESET 4 //#define S_ESET 4
#define S_HSV 5 #define S_HSV 5
#define S_RGB 6 #define S_RGB 6
#define S_FAN 7 #define S_FAN 7

View File

@@ -77,31 +77,39 @@ itemCmd::itemCmd(Item *item)
itemCmd itemCmd::setChanType(short chanType) itemCmd itemCmd::setChanType(short chanType)
{
cmd.itemArgType=getStoragetypeByChanType(chanType);
return *this;
}
uint8_t itemCmd::getStoragetypeByChanType(short chanType)
{ {
switch (chanType) switch (chanType)
{ {
case CH_RGB: case CH_RGB:
case CH_RGBW: case CH_RGBW:
case CH_SPILED: case CH_SPILED:
cmd.itemArgType=ST_HSV; return ST_HSV255;
break; break;
case CH_AC: case CH_AC:
case CH_THERMO: case CH_THERMO:
case CH_VCTEMP: case CH_VCTEMP:
cmd.itemArgType=ST_FLOAT_CELSIUS; return ST_FLOAT_CELSIUS;
break; break;
case CH_DIMMER: case CH_DIMMER:
case CH_MOTOR: case CH_MOTOR:
case CH_PWM: case CH_PWM:
case CH_RELAY: case CH_RELAY:
cmd.itemArgType=ST_PERCENTS; return ST_PERCENTS255;
break; break;
default: default:
cmd.itemArgType=ST_PERCENTS; return ST_VOID;
} }
return *this;
} }
itemCmd itemCmd::setDefault() itemCmd itemCmd::setDefault()
{ {
switch (cmd.itemArgType){ switch (cmd.itemArgType){
@@ -109,14 +117,10 @@ itemCmd itemCmd::setDefault()
break; break;
case ST_FLOAT_FARENHEIT: param.asfloat=75.; case ST_FLOAT_FARENHEIT: param.asfloat=75.;
break; break;
case ST_HSV:
case ST_HS: case ST_HS:
param.h=100; param.s=0; param.v=100;
break;
case ST_HSV255: param.h=100; param.s=0; param.v=255; case ST_HSV255: param.h=100; param.s=0; param.v=255;
break; break;
case ST_PERCENTS: param.v=100;
break;
case ST_PERCENTS255: param.v=255; case ST_PERCENTS255: param.v=255;
break; break;
default: default:
@@ -131,8 +135,7 @@ bool itemCmd::setH(uint16_t h)
switch (cmd.itemArgType) switch (cmd.itemArgType)
{ {
case ST_VOID: case ST_VOID:
cmd.itemArgType=ST_HSV; cmd.itemArgType=ST_HSV255;
case ST_HSV:
case ST_HSV255: case ST_HSV255:
if (par>365) par=365; if (par>365) par=365;
if (par<0) par=0; if (par<0) par=0;
@@ -152,18 +155,11 @@ bool itemCmd::setS(uint8_t s)
switch (cmd.itemArgType) switch (cmd.itemArgType)
{ {
case ST_VOID: case ST_VOID:
cmd.itemArgType=ST_HSV; cmd.itemArgType=ST_HSV255;
case ST_HSV:
case ST_HSV255: case ST_HSV255:
if (par>100) par=100; if (par>100) par=100;
param.s=par; param.s=par;
break; break;
/*
case ST_HSV255:
if (par>255) par=255;
if (par<0) par=0;
param.s=par;
break;*/
default: default:
// debugSerial<<F("Can't assign saturation to type ")<<cmd.itemArgType<<endl; // debugSerial<<F("Can't assign saturation to type ")<<cmd.itemArgType<<endl;
return false; return false;
@@ -181,10 +177,8 @@ bool itemCmd::setColorTemp(int t)
switch (cmd.itemArgType) switch (cmd.itemArgType)
{ {
case ST_VOID: case ST_VOID:
cmd.itemArgType=ST_HSV; cmd.itemArgType=ST_HSV255;
case ST_HSV:
case ST_HS: case ST_HS:
case ST_PERCENTS:
case ST_PERCENTS255: case ST_PERCENTS255:
case ST_HSV255: case ST_HSV255:
if (par>100) par=100; if (par>100) par=100;
@@ -207,9 +201,7 @@ int itemCmd::getColorTemp()
switch (cmd.itemArgType) switch (cmd.itemArgType)
{ {
case ST_HSV:
case ST_HS: case ST_HS:
case ST_PERCENTS:
case ST_PERCENTS255: case ST_PERCENTS255:
case ST_HSV255: case ST_HSV255:
@@ -233,13 +225,6 @@ bool itemCmd::incrementPercents(int16_t dif)
{ int par=param.v; { int par=param.v;
switch (cmd.itemArgType) switch (cmd.itemArgType)
{ {
case ST_PERCENTS:
case ST_HSV:
par+=dif;
if (par>100) par=100;
if (par<0) par=0;
param.v=par;
break;
case ST_PERCENTS255: case ST_PERCENTS255:
case ST_HSV255: case ST_HSV255:
par+=dif; par+=dif;
@@ -286,7 +271,7 @@ bool itemCmd::incrementH(int16_t dif)
{ int par=param.h; { int par=param.h;
switch (cmd.itemArgType) switch (cmd.itemArgType)
{ {
case ST_HSV: //case ST_HSV:
case ST_HSV255: case ST_HSV255:
par+=dif; par+=dif;
if (par>365) par=0; if (par>365) par=0;
@@ -303,20 +288,12 @@ bool itemCmd::incrementS(int16_t dif)
{int par=param.s; {int par=param.s;
switch (cmd.itemArgType) switch (cmd.itemArgType)
{ {
//case ST_PERCENTS:
case ST_HSV:
case ST_HSV255: case ST_HSV255:
par+=dif; par+=dif;
if (par>100) par=100; if (par>100) par=100;
if (par<0) par=0; if (par<0) par=0;
break; break;
/*
//case ST_PERCENTS255:
case ST_HSV255:
par+=dif;
if (par>255) par=255;
if (par<0) par=0;
break; */
default: return false; default: return false;
} }
param.s=par; param.s=par;
@@ -325,11 +302,11 @@ bool itemCmd::incrementS(int16_t dif)
} }
itemCmd itemCmd::assignFrom(itemCmd from) itemCmd itemCmd::assignFrom(itemCmd from, short chanType)
{ {
bool RGBW_flag = false; bool RGBW_flag = false;
bool HSV255_flag = false;
bool toFarenheit = false; bool toFarenheit = false;
short prefferedStorageType = getStoragetypeByChanType(chanType);
int t=from.getColorTemp(); int t=from.getColorTemp();
@@ -341,58 +318,6 @@ itemCmd itemCmd::assignFrom(itemCmd from)
switch (cmd.itemArgType){ //Destination switch (cmd.itemArgType){ //Destination
case ST_HSV:
case ST_PERCENTS:
switch (from.cmd.itemArgType)
{
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;
cmd.itemArgType=from.cmd.itemArgType; //Changing if type
break;
case ST_HSV:
param.v=from.param.v;
case ST_HS:
param.h=from.param.h;
param.s=from.param.s;
break;
case ST_PERCENTS:
param.v=from.param.v;
break;
case ST_TENS:
param.v=from.param.asInt32/10;
break;
case ST_INT32:
case ST_UINT32:
param.v=constrain(from.param.aslong,0,100);
break;
case ST_HSV255:
param.h=from.param.h;
//param.s=map(from.param.s,0,255,0,100);
param.s=from.param.s;
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;
case ST_FLOAT:
if (cmd.itemArgType == ST_HSV)
param.v = constrain(from.param.asfloat,0,100);
else
{
param=from.param;
cmd.itemArgType=from.cmd.itemArgType;
}
break;
default:
debugSerial<<F("Wrong Assignment ")<<from.cmd.itemArgType<<F("->")<<cmd.itemArgType<<endl;
}
break;
case ST_HSV255: case ST_HSV255:
case ST_PERCENTS255: case ST_PERCENTS255:
switch (from.cmd.itemArgType) switch (from.cmd.itemArgType)
@@ -405,22 +330,16 @@ itemCmd itemCmd::assignFrom(itemCmd from)
param.b=from.param.b; param.b=from.param.b;
cmd.itemArgType=from.cmd.itemArgType; cmd.itemArgType=from.cmd.itemArgType;
break; break;
case ST_HSV:
param.v=map(from.param.v,0,100,0,255);
case ST_HS: case ST_HS:
param.h=from.param.h; param.h=from.param.h;
param.s=from.param.s; param.s=from.param.s;
//param.s=map(from.param.s,0,100,0,255);
break;
case ST_PERCENTS:
param.v=map(from.param.v,0,100,0,255);
break; break;
case ST_INT32: case ST_INT32:
case ST_UINT32: case ST_UINT32:
param.v=map(from.param.asInt32,0,100,0,255); //constr param.v=constrain(from.param.asInt32,0,255);
break; break;
case ST_TENS: case ST_TENS:
param.v=map(from.param.asInt32,0,1000,0,255); param.v=map(from.param.asInt32,0,2550,0,255);
case ST_HSV255: case ST_HSV255:
param.h=from.param.h; param.h=from.param.h;
param.s=from.param.s; param.s=from.param.s;
@@ -430,13 +349,7 @@ itemCmd itemCmd::assignFrom(itemCmd from)
param.v=from.param.v; param.v=from.param.v;
break; break;
case ST_FLOAT: case ST_FLOAT:
if (cmd.itemArgType == ST_HSV255) param.v=constrain(from.param.asfloat,0.,255.);
param.v = constrain(map(from.param.asfloat,0,100,0,255),0,255);
else
{
param=from.param;
cmd.itemArgType=from.cmd.itemArgType;
}
break; break;
default: default:
debugSerial<<F("Wrong Assignment ")<<from.cmd.itemArgType<<F("->")<<cmd.itemArgType<<endl; debugSerial<<F("Wrong Assignment ")<<from.cmd.itemArgType<<F("->")<<cmd.itemArgType<<endl;
@@ -451,18 +364,18 @@ itemCmd itemCmd::assignFrom(itemCmd from)
switch (from.cmd.itemArgType) switch (from.cmd.itemArgType)
{ {
case ST_HS: case ST_HS:
param.v=getPercents(); param.v=getPercents255();
param.h=from.param.h; param.h=from.param.h;
param.s=from.param.s; param.s=from.param.s;
cmd.itemArgType=ST_HSV; cmd.itemArgType=ST_HSV255;
default: default:
param.asInt32=from.param.asInt32; param.asInt32=from.param.asInt32;
cmd.itemArgType=from.cmd.itemArgType; cmd.itemArgType=from.cmd.itemArgType;
} }
break; break;
case ST_HS: case ST_HS:
param.v=from.getPercents(); param.v=from.getPercents255();
cmd.itemArgType=ST_HSV; cmd.itemArgType=ST_HSV255;
break; break;
case ST_FLOAT_FARENHEIT: case ST_FLOAT_FARENHEIT:
toFarenheit = true; toFarenheit = true;
@@ -473,8 +386,6 @@ itemCmd itemCmd::assignFrom(itemCmd from)
case ST_TENS: case ST_TENS:
param.asfloat=from.param.asInt32/10.; param.asfloat=from.param.asInt32/10.;
break; break;
case ST_PERCENTS:
case ST_PERCENTS255: case ST_PERCENTS255:
param.asfloat=from.param.v; param.asfloat=from.param.v;
break; break;
@@ -495,17 +406,16 @@ itemCmd itemCmd::assignFrom(itemCmd from)
param.asfloat=from.param.asfloat; param.asfloat=from.param.asfloat;
break; break;
case ST_HSV255: case ST_HSV255:
case ST_HSV:
case ST_RGB: case ST_RGB:
case ST_RGBW: case ST_RGBW:
cmd.itemArgType=from.cmd.itemArgType; cmd.itemArgType=from.cmd.itemArgType;
param=from.param; param=from.param;
break; break;
case ST_HS: case ST_HS:
param.v=getPercents(); param.v=getPercents255();
param.h=from.param.h; param.h=from.param.h;
param.s=from.param.s; param.s=from.param.s;
cmd.itemArgType=ST_HSV; cmd.itemArgType=ST_HSV255;
break; break;
default: default:
debugSerial<<F("Wrong Assignment ")<<from.cmd.itemArgType<<F("->")<<cmd.itemArgType<<endl; debugSerial<<F("Wrong Assignment ")<<from.cmd.itemArgType<<F("->")<<cmd.itemArgType<<endl;
@@ -530,7 +440,6 @@ itemCmd itemCmd::assignFrom(itemCmd from)
case ST_HS: case ST_HS:
case ST_INT32: case ST_INT32:
case ST_PERCENTS255: case ST_PERCENTS255:
case ST_PERCENTS:
case ST_TENS: case ST_TENS:
case ST_UINT32: case ST_UINT32:
{ {
@@ -551,9 +460,6 @@ itemCmd itemCmd::assignFrom(itemCmd from)
case ST_PERCENTS255: case ST_PERCENTS255:
vol=map(from.param.v,0,255,0,100); vol=map(from.param.v,0,255,0,100);
break; break;
case ST_PERCENTS:
vol=from.param.v;
break;
case ST_INT32: case ST_INT32:
case ST_UINT32: case ST_UINT32:
vol=from.param.asInt32; vol=from.param.asInt32;
@@ -568,7 +474,7 @@ itemCmd itemCmd::assignFrom(itemCmd from)
#ifndef ADAFRUIT_LED #ifndef ADAFRUIT_LED
vol=hsv.v; vol=hsv.v;
#else #else
vol=100; vol=255;
#endif #endif
} }
@@ -581,38 +487,19 @@ itemCmd itemCmd::assignFrom(itemCmd from)
from.param.s=0; from.param.s=0;
#endif #endif
from.cmd.itemArgType=ST_HSV; from.cmd.itemArgType=ST_HSV255;
from.param.v=vol; from.param.v=vol;
} }
// Continue processing with filled from HSV // Continue processing with filled from HSV
/*
//Converting current obj to HSV
debugSerial<<F("Conv RGB2HSV:"); from.debugOut();
// Do not convert to RGBx ?
param=from.param;
cmd=from.cmd;
return *this; */
case ST_HSV255: case ST_HSV255:
if (from.cmd.itemArgType==ST_HSV255) HSV255_flag=true;
case ST_HSV:
{ // HSV_XX to RGB_XX translation code { // HSV_XX to RGB_XX translation code
int rgbSaturation; int rgbSaturation;
int rgbValue; int rgbValue;
if (HSV255_flag)
{
//rgbSaturation = from.param.s;
rgbSaturation =map(from.param.s, 0, 100, 0, 255); rgbSaturation =map(from.param.s, 0, 100, 0, 255);
rgbValue = from.param.v; rgbValue = from.param.v;
}
else
{
rgbSaturation =map(from.param.s, 0, 100, 0, 255);
rgbValue = map(from.param.v, 0, 100, 0, 255);
}
short colorT=from.param.colorTemp-1; short colorT=from.param.colorTemp-1;
@@ -679,6 +566,9 @@ itemCmd itemCmd::assignFrom(itemCmd from)
} //Translation to RGB_XX } //Translation to RGB_XX
break; break;
} //Destination } //Destination
if (prefferedStorageType) convertTo(prefferedStorageType);
return *this; return *this;
} }
@@ -694,7 +584,9 @@ return (cmd.itemArgType);
bool itemCmd::isColor() bool itemCmd::isColor()
{ {
return (cmd.itemArgType==ST_HS || cmd.itemArgType==ST_HSV || cmd.itemArgType==ST_HSV255 || cmd.itemArgType==ST_RGB || cmd.itemArgType==ST_RGBW);
return (cmd.itemArgType==ST_HS || cmd.itemArgType==ST_HSV255 || cmd.itemArgType==ST_RGB || cmd.itemArgType==ST_RGBW);
} }
@@ -708,10 +600,7 @@ long int itemCmd::getInt()
case ST_RGBW: case ST_RGBW:
return param.aslong; return param.aslong;
case ST_PERCENTS:
case ST_PERCENTS255: case ST_PERCENTS255:
case ST_HSV:
case ST_HSV255: case ST_HSV255:
return param.v; return param.v;
@@ -743,9 +632,7 @@ float itemCmd::getFloat()
case ST_TENS: case ST_TENS:
return param.aslong/10; return param.aslong/10;
case ST_PERCENTS:
case ST_PERCENTS255: case ST_PERCENTS255:
case ST_HSV:
case ST_HSV255: case ST_HSV255:
return param.v; return param.v;
@@ -772,10 +659,6 @@ short itemCmd::getPercents(bool inverse)
{ {
switch (cmd.itemArgType) { switch (cmd.itemArgType) {
case ST_PERCENTS:
case ST_HSV:
if (inverse) return 100-param.v; else return param.v;
case ST_INT32: case ST_INT32:
case ST_UINT32: case ST_UINT32:
if (inverse) return constrain(100-param.asInt32,0,100); if (inverse) return constrain(100-param.asInt32,0,100);
@@ -804,23 +687,19 @@ bool itemCmd::setPercents(int percents)
{ {
switch (cmd.itemArgType) { switch (cmd.itemArgType) {
case ST_PERCENTS:
case ST_HSV:
param.v=percents;
break;
case ST_INT32: case ST_INT32:
case ST_UINT32: case ST_UINT32:
param.asInt32=percents; param.asInt32=map(percents,0,100,0,255);
break; break;
case ST_PERCENTS255: case ST_PERCENTS255:
case ST_HSV255: case ST_HSV255:
param.v=map(percents,0,100,0,255); param.v=map(percents,0,100,0,255);
break; break;
case ST_FLOAT: case ST_FLOAT:
param.asfloat=percents; param.asfloat=map(percents,0,100,0,255);;
break; break;
case ST_TENS: case ST_TENS:
param.asInt32 = percents*10; param.asInt32 = map(percents,0,100,0,2550);;
default: default:
return false; return false;
} }
@@ -831,29 +710,19 @@ short itemCmd::getPercents255(bool inverse)
{ {
switch (cmd.itemArgType) { switch (cmd.itemArgType) {
case ST_PERCENTS:
case ST_HSV:
if (inverse) return map(param.v,0,100,255,0);
else return map(param.v,0,100,0,255);
case ST_INT32: case ST_INT32:
case ST_UINT32: case ST_UINT32:
if (inverse) return map(param.asInt32,0,100,255,0); if (inverse) return 255-constrain(param.asInt32,0,255); else return constrain(param.asInt32,0,255);
else return map(param.asInt32,0,100,0,255);
case ST_PERCENTS255: case ST_PERCENTS255:
case ST_HSV255: case ST_HSV255:
if (inverse) return 255-param.v; else return param.v; if (inverse) return 255-param.v; else return param.v;
case ST_FLOAT: case ST_FLOAT:
if (inverse) return map(param.asfloat,0,100,255,0); if (inverse) return 255-constrain(param.asfloat,0,255); else return constrain(param.asfloat,0,255);
else return map(param.asfloat,0,100,0,255);
case ST_TENS: case ST_TENS:
if (inverse) return map(param.asInt32,0,1000,255,0); if (inverse) return 255-constrain(param.asInt32/10,0,255); else return constrain(param.asInt32/10,0,255);
else return map(param.asInt32,0,1000,0,255);
default: default:
return -1; return -1;
@@ -879,9 +748,12 @@ itemCmd itemCmd::setArgType(uint8_t type)
itemCmd itemCmd::convertTo(uint8_t type) itemCmd itemCmd::convertTo(uint8_t type)
{ {
if (cmd.itemArgType == type) return *this;
debugSerial << F("Converting ") << cmd.itemArgType << F("->") << type << F(" ");
itemCmd out(type,cmd.cmdCode); itemCmd out(type,cmd.cmdCode);
out.assignFrom(*this); *this=out.assignFrom(*this);
return out; debugOut();
return *this;
} }
uint8_t itemCmd::getCmdParam() uint8_t itemCmd::getCmdParam()
@@ -897,19 +769,13 @@ itemCmd itemCmd::Percents(int i)
switch (cmd.itemArgType) switch (cmd.itemArgType)
{ {
case ST_HSV:
case ST_PERCENTS:
param.v=i;
break;
case ST_HSV255: case ST_HSV255:
case ST_PERCENTS255: case ST_PERCENTS255:
param.v=map(i,0,100,0,255);
break; break;
default: default:
cmd.itemArgType=ST_PERCENTS; cmd.itemArgType=ST_PERCENTS255;
param.v=i;
} }
param.v=map(i,0,100,0,255);
return *this; return *this;
} }
@@ -920,11 +786,6 @@ itemCmd itemCmd::Percents(int i)
switch (cmd.itemArgType) switch (cmd.itemArgType)
{ {
case ST_HSV:
case ST_PERCENTS:
param.v=map(i,0,255,0,100);
break;
case ST_HSV255: case ST_HSV255:
case ST_PERCENTS255: case ST_PERCENTS255:
param.v=i; param.v=i;
@@ -966,10 +827,10 @@ itemCmd itemCmd::Tens(int32_t i)
itemCmd itemCmd::HSV(uint16_t h, uint8_t s, uint8_t v) itemCmd itemCmd::HSV(uint16_t h, uint8_t s, uint8_t v)
{ {
cmd.itemArgType=ST_HSV; cmd.itemArgType=ST_HSV255;
param.h=h; param.h=h;
param.s=s; param.s=s;
param.v=v; param.v=map(v,0,100,0,255);
return *this; return *this;
} }
@@ -1074,7 +935,7 @@ bool itemCmd::saveItem(Item * item, bool includeCommand)
item->setVal(param.asInt32); item->setVal(param.asInt32);
item->setSubtype(cmd.itemArgType); item->setSubtype(cmd.itemArgType);
if (includeCommand) item->setCmd(cmd.cmdCode); if (includeCommand) item->setCmd(cmd.cmdCode);
debugSerial<<F("Saved :"); debugSerial<<F("Saved:");
debugOut(); debugOut();
return true; return true;
} }
@@ -1104,7 +965,7 @@ int itemCmd::doMappingCmd(aJsonObject *mappingData)
} }
char * itemCmd::toString(char * Buffer, int bufLen, int sendFlags ) char * itemCmd::toString(char * Buffer, int bufLen, int sendFlags, int base )
{ {
if (!Buffer || !bufLen) return NULL; if (!Buffer || !bufLen) return NULL;
@@ -1124,9 +985,8 @@ char * itemCmd::toString(char * Buffer, int bufLen, int sendFlags )
switch (cmd.itemArgType) switch (cmd.itemArgType)
{ short colorTemp; { short colorTemp;
case ST_PERCENTS:
case ST_PERCENTS255: case ST_PERCENTS255:
snprintf(argPtr, bufLen, "%u", param.v); snprintf(argPtr, bufLen, "%u", map (param.v,0,255,0,base));
break; break;
case ST_UINT32: case ST_UINT32:
snprintf(argPtr, bufLen, "%lu", param.asUint32); snprintf(argPtr, bufLen, "%lu", param.asUint32);
@@ -1137,14 +997,13 @@ char * itemCmd::toString(char * Buffer, int bufLen, int sendFlags )
case ST_TENS: case ST_TENS:
snprintf(argPtr, bufLen, "%ld.%d", param.asInt32/10, abs(param.asInt32 % 10)); snprintf(argPtr, bufLen, "%ld.%d", param.asInt32/10, abs(param.asInt32 % 10));
break; break;
case ST_HSV:
case ST_HSV255: case ST_HSV255:
colorTemp=getColorTemp(); colorTemp=getColorTemp();
if (colorTemp<0) if (colorTemp<0)
snprintf(argPtr, bufLen, "%d,%d,%d", param.h, param.s, param.v); snprintf(argPtr, bufLen, "%d,%d,%d", param.h, param.s, map (param.v,0,255,0,base));
else else
snprintf(argPtr, bufLen, "%d,%d,%d,%d", param.h, param.s, param.v, colorTemp); snprintf(argPtr, bufLen, "%d,%d,%d,%d", param.h, param.s, map (param.v,0,255,0,base), colorTemp);
break; break;
case ST_HS: case ST_HS:
@@ -1155,18 +1014,17 @@ char * itemCmd::toString(char * Buffer, int bufLen, int sendFlags )
case ST_FLOAT_FARENHEIT: case ST_FLOAT_FARENHEIT:
case ST_FLOAT: case ST_FLOAT:
{ {
//char *tmpSign = (param.asfloat < 0) ? "-" : "";
float tmpVal = (param.asfloat < 0) ? -param.asfloat : param.asfloat; float tmpVal = (param.asfloat < 0) ? -param.asfloat : param.asfloat;
int tmpInt1 = tmpVal; // Get the integer int tmpInt1 = tmpVal; // Get the integer
float tmpFrac = tmpVal - tmpInt1; // Get fraction float tmpFrac = tmpVal - tmpInt1; // Get fraction
int tmpInt2 = trunc(tmpFrac * 10000); // Turn into integer int tmpInt2 = trunc(tmpFrac * 100); // Turn into integer
// Print as parts, note that you need 0-padding for fractional bit. // Print as parts, note that you need 0-padding for fractional bit.
if (param.asfloat < 0) if (param.asfloat < 0)
snprintf (argPtr, bufLen, "-%d.%04d", tmpInt1, tmpInt2); snprintf (argPtr, bufLen, "-%d.%02d", tmpInt1, tmpInt2);
else snprintf (argPtr, bufLen, "%d.%04d", tmpInt1, tmpInt2); else snprintf (argPtr, bufLen, "%d.%02d", tmpInt1, tmpInt2);
} }
// snprintf(argPtr, bufLen, "%.1f", param.asfloat);
break; break;
case ST_RGB: case ST_RGB:
snprintf(argPtr, bufLen, "%d,%d,%d", param.r, param.g, param.b); snprintf(argPtr, bufLen, "%d,%d,%d", param.r, param.g, param.b);

View File

@@ -74,42 +74,26 @@ const cmdstr commands_P[] PROGMEM =
int txt2cmd (char * payload); 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
};
*/
///Definition of all possible types of argument, contained in class ///Definition of all possible types of argument, contained in class
#define ST_VOID 0 /// Not defined #define ST_VOID 0 /// Not defined
#define ST_PERCENTS 1 /// Percent value 0..100 #define ST_PERCENTS255 1 /// Percent value 0..255
#define ST_TENS 2 /// Int representation of Float point value in tens part (ex 12.3 = 123 in "tens")
#define ST_HSV 3 /// HUE-SATURATION-VALUE representation of color (0..365, 0..100, 0..100) #define ST_HSV255 2 /// HUE-SATURATION-VALUE representation of color (0..365, 0..100, 0..255)
#define ST_HS 4 /// just hue and saturation #define ST_HS 3 /// just hue and saturation
#define ST_FLOAT_CELSIUS 5 /// Float value - temperature in Celsium
#define ST_FLOAT_FARENHEIT 6 /// Float value - temperature in Farenheit #define ST_RGB 4 /// RGB replesentation of color
#define ST_RGB 7 /// RGB replesentation of color #define ST_RGBW 5 /// RGB + White channel
#define ST_RGBW 8 /// RGB + White channel
#define ST_PERCENTS255 9 /// Percent value 0..255 #define ST_TENS 6 /// Int representation of Float point value in tens part (ex 12.3 = 123 in "tens")
#define ST_HSV255 10 /// HUE-SATURATION-VALUE representation of color (0..365, 0..255, 0..255) #define ST_FLOAT 7 /// generic Float value
#define ST_INT32 11 /// 32 bits signed integer #define ST_FLOAT_CELSIUS 8 /// Float value - temperature in Celsium
#define ST_UINT32 12 /// 32 bits unsigned integer #define ST_FLOAT_FARENHEIT 9 /// Float value - temperature in Farenheit
#define ST_STRING 13 /// pointer to string (for further use)
#define ST_FLOAT 14 /// generic Float value #define ST_INT32 10 /// 32 bits signed integer
#define ST_UINT32 11 /// 32 bits unsigned integer
#define ST_STRING 12 /// pointer to string (for further use)
#define MAP_SCALE 1 #define MAP_SCALE 1
#define MAP_VAL_CMD 2 #define MAP_VAL_CMD 2
@@ -182,7 +166,7 @@ public:
itemCmd(float val); itemCmd(float val);
itemCmd(Item *item); itemCmd(Item *item);
itemCmd assignFrom(itemCmd from); itemCmd assignFrom(itemCmd from, short chanType=-1);
bool loadItem(Item * item, bool includeCommand=false ); bool loadItem(Item * item, bool includeCommand=false );
bool saveItem(Item * item, bool includeCommand=false); bool saveItem(Item * item, bool includeCommand=false);
@@ -205,6 +189,8 @@ public:
uint16_t getS(); uint16_t getS();
itemCmd setArgType(uint8_t); itemCmd setArgType(uint8_t);
itemCmd convertTo(uint8_t); itemCmd convertTo(uint8_t);
uint8_t getStoragetypeByChanType(short chanType);
itemCmd Percents(int i); itemCmd Percents(int i);
itemCmd Percents255(int i); itemCmd Percents255(int i);
@@ -224,7 +210,7 @@ public:
uint8_t getCmd(); uint8_t getCmd();
uint8_t getArgType(); uint8_t getArgType();
uint8_t getCmdParam(); uint8_t getCmdParam();
char * toString(char * Buffer, int bufLen, int sendFlags = SEND_COMMAND | SEND_PARAMETERS ); char * toString(char * Buffer, int bufLen, int sendFlags = SEND_COMMAND | SEND_PARAMETERS, int base = 255);
bool isCommand(); bool isCommand();
bool isValue(); bool isValue();

View File

@@ -2177,129 +2177,3 @@ publishStat();
} }
/*
bool isThermostatWithMinArraySize(aJsonObject *item, int minimalArraySize) {
return (item->type == aJson_Array) && (aJson.getArrayItem(item, I_TYPE)->valueint == CH_THERMO) &&
(aJson.getArraySize(item) >= minimalArraySize);
}
bool thermoDisabledOrDisconnected(aJsonObject *thermoExtensionArray, int thermoStateCommand) {
if (aJson.getArrayItem(thermoExtensionArray, IET_ATTEMPTS)->valueint == 0) return true;
switch (thermoStateCommand) {
case CMD_ON:
case CMD_XON:
case CMD_AUTO:
case CMD_HEAT:
return false;
default: return true;
}
}
//TODO: refactoring
void thermoLoop(void) {
// if (millis() < timerThermostatCheck)
if (!isTimeOver(timerThermostatCheck,millis(),THERMOSTAT_CHECK_PERIOD))
return;
if (!items) return;
bool thermostatCheckPrinted = false;
configLocked++;
for (aJsonObject *thermoItem = items->child; thermoItem; thermoItem = thermoItem->next) {
if (isThermostatWithMinArraySize(thermoItem, 5)) {
aJsonObject *thermoExtensionArray = aJson.getArrayItem(thermoItem, I_EXT);
if (thermoExtensionArray && (aJson.getArraySize(thermoExtensionArray) > 1)) {
Item thermostat(thermoItem);
if (!thermostat.isValid()) continue;
itemCmd thermostatCmd(&thermostat);
//int thermoPin = aJson.getArrayItem(thermoItem, I_ARG)->valueint;
int thermoPin = thermostat.getArg(0);
//float thermoSetting = aJson.getArrayItem(thermoItem, I_VAL)->valueint; ///
float thermoSetting = thermostatCmd.getFloat();
int thermoStateCommand = aJson.getArrayItem(thermoItem, I_CMD)->valueint;
float curTemp = aJson.getArrayItem(thermoExtensionArray, IET_TEMP)->valuefloat;
if (!aJson.getArrayItem(thermoExtensionArray, IET_ATTEMPTS)->valueint) {
errorSerial<<thermoItem->name<<F(" Expired\n");
} else {
if (!(--aJson.getArrayItem(thermoExtensionArray, IET_ATTEMPTS)->valueint))
mqttClient.publish("/alarm/snsr", thermoItem->name);
}
if (curTemp > THERMO_OVERHEAT_CELSIUS) mqttClient.publish("/alarm/ovrht", thermoItem->name);
debugSerial << endl << thermoItem->name << F(" Set:") << thermoSetting << F(" Cur:") << curTemp
<< F(" cmd:") << thermoStateCommand;
if (thermoPin<0) pinMode(-thermoPin, OUTPUT); else pinMode(thermoPin, OUTPUT);
if (thermoDisabledOrDisconnected(thermoExtensionArray, thermoStateCommand)) {
if (thermoPin<0) digitalWrite(-thermoPin, LOW); else digitalWrite(thermoPin, LOW);
// Caution - for water heaters (negative pin#) if some comes wrong (or no connection with termometers output is LOW - valve OPEN)
// OFF - also VALVE is OPEN (no teat control)
debugSerial<<F(" OFF");
} else {
if (curTemp < thermoSetting - THERMO_GIST_CELSIUS) {
if (thermoPin<0) digitalWrite(-thermoPin, LOW); else digitalWrite(thermoPin, HIGH);
debugSerial<<F(" ON");
} //too cold
else if (curTemp >= thermoSetting) {
if (thermoPin<0) digitalWrite(-thermoPin, HIGH); else digitalWrite(thermoPin, LOW);
debugSerial<<F(" OFF");
} //Reached settings
else debugSerial<<F(" -target zone-"); // Nothing to do
}
thermostatCheckPrinted = true;
}
}
}
configLocked--;
timerThermostatCheck = millis();// + THERMOSTAT_CHECK_PERIOD;
publishStat();
#ifndef DISABLE_FREERAM_PRINT
(thermostatCheckPrinted) ? debugSerial<<F("\nRAM=")<<freeRam()<<" " : debugSerial<<F(" ")<<freeRam()<<F(" ");
#endif
}
short thermoSetCurTemp(char *name, float t) {
if (!name || !strlen(name)) return 0;
if (items) {
aJsonObject *thermoItem = aJson.getObjectItem(items, name);
if (!thermoItem) return 0;
if (isThermostatWithMinArraySize(thermoItem, 4)) {
aJsonObject *extArray = NULL;
if (aJson.getArraySize(thermoItem) == 4) //No thermo extension yet
{
extArray = aJson.createArray(); //Create Ext Array
aJsonObject *ocurt = aJson.createItem(t); //Create float
aJsonObject *oattempts = aJson.createItem((long int) T_ATTEMPTS); //Create int
aJson.addItemToArray(extArray, ocurt);
aJson.addItemToArray(extArray, oattempts);
aJson.addItemToArray(thermoItem, extArray); //Adding to thermoItem
}
else if (extArray = aJson.getArrayItem(thermoItem, I_EXT)) {
aJsonObject *att = aJson.getArrayItem(extArray, IET_ATTEMPTS);
aJson.getArrayItem(extArray, IET_TEMP)->valuefloat = t;
if (att->valueint == 0) mqttClient.publish("/alarmoff/snsr", thermoItem->name);
att->valueint = (int) T_ATTEMPTS;
}
}
return 1;}
return 0;}
*/

View File

@@ -213,6 +213,7 @@ inline unsigned char toHex( char ch ){
int out_AC::Setup() int out_AC::Setup()
{ {
abstractOut::Setup();
Serial.println("AC Init"); Serial.println("AC Init");
AC_Serial.begin(9600); AC_Serial.begin(9600);
driverStatus = CST_INITIALIZED; driverStatus = CST_INITIALIZED;
@@ -280,7 +281,7 @@ int out_AC::Ctrl(itemCmd cmd, char* subItem , bool toExecute)
switch(suffixCode) switch(suffixCode)
{ {
case S_SET: case S_SET:
case S_ESET: //case S_ESET:
set_tmp = cmd.getInt(); set_tmp = cmd.getInt();
if (set_tmp >= 10 && set_tmp <= 30) if (set_tmp >= 10 && set_tmp <= 30)
{ {

View File

@@ -2,6 +2,7 @@
#pragma once #pragma once
#ifndef AC_DISABLE #ifndef AC_DISABLE
#include <abstractout.h> #include <abstractout.h>
#include "itemCmd.h"
#define LEN_B 37 #define LEN_B 37
#define B_CUR_TMP 13 //Текущая температура #define B_CUR_TMP 13 //Текущая температура
@@ -29,7 +30,7 @@ public:
int Stop() override; int Stop() override;
int Status() override; int Status() override;
int isActive() override; int isActive() override;
//int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL) override; int getDefaultStorageType(){return ST_FLOAT_CELSIUS;};
int Ctrl(itemCmd cmd, char* subItem=NULL, bool toExecute=true) override; int Ctrl(itemCmd cmd, char* subItem=NULL, bool toExecute=true) override;
protected: protected:

View File

@@ -13,6 +13,7 @@ static int driverStatus = CST_UNKNOWN;
int out_dmx::Setup() int out_dmx::Setup()
{ {
abstractOut::Setup();
debugSerial<<F("DMX-Out Init")<<endl; debugSerial<<F("DMX-Out Init")<<endl;
driverStatus = CST_INITIALIZED; driverStatus = CST_INITIALIZED;
return 1; return 1;
@@ -88,7 +89,7 @@ if (cType==CH_DIMMER) //Single channel
storageType=ST_RGBW; storageType=ST_RGBW;
break; break;
default: default:
storageType=ST_PERCENTS; storageType=ST_PERCENTS255;
} }
itemCmd st(storageType,CMD_VOID); itemCmd st(storageType,CMD_VOID);

View File

@@ -155,6 +155,7 @@ bool out_Modbus::getConfig()
int out_Modbus::Setup() int out_Modbus::Setup()
{ {
abstractOut::Setup();
if (!store) store= (mbPersistent *)item->setPersistent(new mbPersistent); if (!store) store= (mbPersistent *)item->setPersistent(new mbPersistent);
if (!store) if (!store)
{ errorSerial<<F("MBUS: Out of memory")<<endl; { errorSerial<<F("MBUS: Out of memory")<<endl;
@@ -194,10 +195,6 @@ if (store)
return CST_UNKNOWN; return CST_UNKNOWN;
} }
int out_Modbus::isActive()
{
return item->getVal();
}
bool readModbus(uint16_t reg, int regType, int count) bool readModbus(uint16_t reg, int regType, int count)
@@ -464,7 +461,7 @@ case S_NOTFOUND:
toExecute = true; toExecute = true;
debugSerial<<F("Forced execution"); debugSerial<<F("Forced execution");
case S_SET: case S_SET:
case S_ESET: //case S_ESET:
if (!cmd.isValue()) return 0; if (!cmd.isValue()) return 0;
//TODO //TODO

View File

@@ -3,6 +3,7 @@
#ifndef MBUS_DISABLE #ifndef MBUS_DISABLE
#include <abstractout.h> #include <abstractout.h>
#include <item.h> #include <item.h>
#include "itemCmd.h"
#if defined(ESP32) #if defined(ESP32)
#define serialParamType uint32_t #define serialParamType uint32_t
@@ -34,9 +35,9 @@ public:
int Poll(short cause) override; int Poll(short cause) override;
int Stop() override; int Stop() override;
int Status() override; int Status() override;
int isActive() override;
int getChanType() override; int getChanType() override;
int Ctrl(itemCmd cmd, char* subItem=NULL, bool toExecute=true) override; int Ctrl(itemCmd cmd, char* subItem=NULL, bool toExecute=true) override;
int getDefaultStorageType(){return ST_INT32;};
//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;
protected: protected:

View File

@@ -48,6 +48,7 @@ void out_Motor::getConfig()
int out_Motor::Setup() int out_Motor::Setup()
{ {
abstractOut::Setup();
getConfig(); getConfig();
Serial.println("Motor Init"); Serial.println("Motor Init");
pinMode(pinUp,OUTPUT); pinMode(pinUp,OUTPUT);
@@ -82,7 +83,7 @@ int out_Motor::Status()
{ {
return driverStatus; return driverStatus;
} }
/*
int out_Motor::isActive() int out_Motor::isActive()
{ {
itemCmd st; itemCmd st;
@@ -94,10 +95,10 @@ switch (item->getCmd())
break; break;
default: default:
st.loadItem(item); st.loadItem(item);
return st.getPercents(); return st.getPercents255();
} }
} }
*/
int out_Motor::Poll(short cause) int out_Motor::Poll(short cause)
{ {
int curPos = -1; int curPos = -1;
@@ -118,7 +119,7 @@ if (!item->getFlag(ACTION_IN_PROCESS))
uint32_t motorOfftime = item->getExt(); uint32_t motorOfftime = item->getExt();
itemCmd st; itemCmd st;
st.loadItem(item); st.loadItem(item);
targetPos = st.getPercents();// item->getVal(); targetPos = st.getPercents255();// item->getVal();
switch (item->getCmd()) switch (item->getCmd())
{ {
@@ -137,18 +138,17 @@ int fb=-1;
if (pinFeedback && isAnalogPin(pinFeedback)) if (pinFeedback && isAnalogPin(pinFeedback))
{ {
curPos=map((fb=analogRead(pinFeedback)),feedbackClosed,feedbackOpen,0,100); curPos=map((fb=analogRead(pinFeedback)),feedbackClosed,feedbackOpen,0,255);
if (curPos<0) curPos=0; if (curPos<0) curPos=0;
if (curPos>100) curPos=100; if (curPos>255) curPos=255;
} }
//if (motorOfftime && motorOfftime<millis()) //Time over
if (motorOfftime && isTimeOver(motorOfftime,millis(),maxOnTime)) if (motorOfftime && isTimeOver(motorOfftime,millis(),maxOnTime))
{dif = 0; debugSerial<<F("Motor timeout")<<endl;} {dif = 0; debugSerial<<F("Motor timeout")<<endl;}
else if (curPos>=0) else if (curPos>=0)
dif=targetPos-curPos; dif=targetPos-curPos;
else else
dif=targetPos-50; // Have No feedback dif=targetPos-255/2; // Have No feedback
debugSerial<<F("In:")<<pinFeedback<<F(" Val:")<<fb<<F("/")<<curPos<<F("->")<<targetPos<<F(" delta:")<<dif<<endl; debugSerial<<F("In:")<<pinFeedback<<F(" Val:")<<fb<<F("/")<<curPos<<F("->")<<targetPos<<F(" delta:")<<dif<<endl;
@@ -168,8 +168,8 @@ if (digitalPinHasPWM(pinUp))
{ {
//Serial.println("pinUP PWM"); //Serial.println("pinUP PWM");
int velocity; int velocity;
if (inverted) velocity = map(-dif, 0, 10, 255, 0); if (inverted) velocity = map(-dif, 0, 255/10, 255, 0);
else velocity = map(-dif, 0, 10, 0, 255); else velocity = map(-dif, 0, 255/10, 0, 255);
velocity = constrain (velocity, MIN_PWM, 255); velocity = constrain (velocity, MIN_PWM, 255);
@@ -183,8 +183,8 @@ else if (digitalPinHasPWM(pinDown))
int velocity; int velocity;
if (inverted) if (inverted)
velocity = map(-dif, 0, 10, 0, 255); velocity = map(-dif, 0, 255/10, 0, 255);
else velocity = map(-dif, 0, 10, 255, 0); else velocity = map(-dif, 0, 255/10, 255, 0);
velocity = constrain (velocity, MIN_PWM, 255); velocity = constrain (velocity, MIN_PWM, 255);
analogWrite(pinDown,velocity); analogWrite(pinDown,velocity);
@@ -209,8 +209,8 @@ if (digitalPinHasPWM(pinDown))
{ {
//Serial.println("pinDown PWM"); //Serial.println("pinDown PWM");
int velocity; int velocity;
if (inverted) velocity = map(dif, 0, 20, 255, 0); if (inverted) velocity = map(dif, 0, 255/5, 255, 0);
else velocity = map(dif, 0, 20, 0, 255); else velocity = map(dif, 0, 255/5, 0, 255);
velocity = constrain (velocity, MIN_PWM, 255); velocity = constrain (velocity, MIN_PWM, 255);
@@ -223,8 +223,8 @@ if (digitalPinHasPWM(pinUp))
digitalWrite(pinDown,ACTIVE); digitalWrite(pinDown,ACTIVE);
int velocity; int velocity;
if (inverted) velocity = map(dif, 0, 10, 0, 255); if (inverted) velocity = map(dif, 0, 255/10, 0, 255);
else velocity = map(dif, 0, 10, 255, 0); else velocity = map(dif, 0, 255/10, 255, 0);
if (velocity>255) velocity=255; if (velocity>255) velocity=255;
if (velocity<0) velocity=0; if (velocity<0) velocity=0;
@@ -277,7 +277,7 @@ case S_NOTFOUND:
toExecute = true; toExecute = true;
debugSerial<<F("Forced execution"); debugSerial<<F("Forced execution");
case S_SET: case S_SET:
case S_ESET: //case S_ESET:
if (!cmd.isValue()) return 0; if (!cmd.isValue()) return 0;
// item->setVal(cmd.getPercents()); // item->setVal(cmd.getPercents());
if (item->getExt()) item->setExt(millisNZ()); //Extend motor time if (item->getExt()) item->setExt(millisNZ()); //Extend motor time

View File

@@ -3,9 +3,10 @@
#ifndef MOTOR_DISABLE #ifndef MOTOR_DISABLE
#include <abstractout.h> #include <abstractout.h>
#include <item.h> #include <item.h>
#include "itemCmd.h"
#ifndef POS_ERR #ifndef POS_ERR
#define POS_ERR 5 #define POS_ERR 10
#endif #endif
#define MIN_PWM 70 #define MIN_PWM 70
@@ -24,8 +25,9 @@ public:
int Poll(short cause) override; int Poll(short cause) override;
int Stop() override; int Stop() override;
int Status() override; int Status() override;
int isActive() override; //int isActive() override;
int getChanType() override; int getChanType() override;
int getDefaultStorageType(){return ST_PERCENTS255;};
//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, char* subItem=NULL, bool toExecute=true) override; int Ctrl(itemCmd cmd, char* subItem=NULL, bool toExecute=true) override;

View File

@@ -29,7 +29,7 @@ bool out_pid::getConfig()
return false; return false;
} }
double outMin=0.; double outMin=0.;
double outMax=100.; double outMax=255.;
aJsonObject * param; aJsonObject * param;
switch (aJson.getArraySize(kPIDObj)) switch (aJson.getArraySize(kPIDObj))
{ case 5: { case 5:
@@ -89,6 +89,7 @@ bool out_pid::getConfig()
int out_pid::Setup() int out_pid::Setup()
{ {
abstractOut::Setup();
if (!store) store= (pidPersistent *)item->setPersistent(new pidPersistent); if (!store) store= (pidPersistent *)item->setPersistent(new pidPersistent);
if (!store) if (!store)
{ errorSerial<<F("PID: Out of memory")<<endl; { errorSerial<<F("PID: Out of memory")<<endl;
@@ -98,6 +99,7 @@ store->pid=NULL;
if (getConfig()) if (getConfig())
{ {
infoSerial<<F("PID config loaded ")<< item->itemArr->name<<endl; infoSerial<<F("PID config loaded ")<< item->itemArr->name<<endl;
item->On(); // Turn ON pid by default
store->driverStatus = CST_INITIALIZED; store->driverStatus = CST_INITIALIZED;
return 1; return 1;
} }
@@ -141,7 +143,7 @@ if (store && store->pid && (Status() == CST_INITIALIZED) && item && (item->getCm
//if (abs(store->output-store-prevOut)>OUTPUT_TRESHOLD) //if (abs(store->output-store-prevOut)>OUTPUT_TRESHOLD)
{ {
aJsonObject * oCmd = aJson.getArrayItem(item->itemArg, 1); aJsonObject * oCmd = aJson.getArrayItem(item->itemArg, 1);
itemCmd value((float) (store->output * (100./255.))); itemCmd value((float) (store->output));// * (100./255.)));
executeCommand(oCmd,-1,value); executeCommand(oCmd,-1,value);
} }
@@ -183,7 +185,7 @@ return 1;
case S_NOTFOUND: case S_NOTFOUND:
case S_SET: case S_SET:
case S_ESET: //case S_ESET:
// Setpoint for PID // Setpoint for PID
if (!cmd.isValue()) return 0; if (!cmd.isValue()) return 0;
store->setpoint=cmd.getFloat(); store->setpoint=cmd.getFloat();

View File

@@ -4,6 +4,7 @@
#include <abstractout.h> #include <abstractout.h>
#include <item.h> #include <item.h>
#include <PID_v1.h> #include <PID_v1.h>
#include "itemCmd.h"
#define OUTPUT_TRESHOLD 1 #define OUTPUT_TRESHOLD 1
@@ -29,6 +30,7 @@ public:
int Status() override; int Status() override;
int isActive() override; int isActive() override;
int getChanType() override; int getChanType() override;
int getDefaultStorageType(){return ST_FLOAT;};
int Ctrl(itemCmd cmd, char* subItem=NULL, bool toExecute=true) override; int Ctrl(itemCmd cmd, char* subItem=NULL, bool toExecute=true) override;

View File

@@ -19,6 +19,7 @@ void analogWrite(int pin, int val)
int out_pwm::Setup() int out_pwm::Setup()
{ {
abstractOut::Setup();
debugSerial<<F("PWM-Out Init")<<endl; debugSerial<<F("PWM-Out Init")<<endl;
if (!item || iaddr) return 0; if (!item || iaddr) return 0;
@@ -142,7 +143,7 @@ switch (cmd.getCmd()){
storageType=ST_RGBW; storageType=ST_RGBW;
break; break;
default: default:
storageType=ST_PERCENTS; storageType=ST_PERCENTS255;
} }
itemCmd st(storageType,CMD_VOID); itemCmd st(storageType,CMD_VOID);

View File

@@ -43,6 +43,7 @@ void out_SPILed::getConfig()
int out_SPILed::Setup() int out_SPILed::Setup()
{ {
abstractOut::Setup();
getConfig(); getConfig();
Serial.println("SPI-LED Init"); Serial.println("SPI-LED Init");

View File

@@ -37,7 +37,7 @@
#define TIMEOUT_REINIT 5000UL #define TIMEOUT_REINIT 5000UL
#define TIMEOUT_RETAIN 5000UL #define TIMEOUT_RETAIN 5000UL
#define INTERVAL_1W 5000UL #define INTERVAL_1W 5000UL
#define PERIOD_THERMOSTAT_FAILED (300 * 1000UL)>>8 #define PERIOD_THERMOSTAT_FAILED (500 * 1000UL)>>8
//#define T_ATTEMPTS 200 //#define T_ATTEMPTS 200
//#define IET_TEMP 0 //#define IET_TEMP 0