REFACTORING st2 (still untested & uncomplete)

This commit is contained in:
2020-11-15 20:26:24 +03:00
parent 118fd25186
commit d81308ad4d
16 changed files with 308 additions and 266 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, int suffixCode=0, char* subItem=NULL) =0;
virtual int Ctrl(itemCmd cmd, char* subItem=NULL) =0;
virtual int isActive(){return 0;};
virtual int getDefaultOnVal(){return 100;};
virtual int getChanType(){return 0;}

View File

@@ -44,6 +44,7 @@ e-mail anklimov@gmail.com
#include "modules/out_ac.h"
#include "modules/out_motor.h"
#include "modules/out_modbus.h"
#include "modules/out_dmx.h"
short modbusBusy = 0;
extern aJsonObject *pollingItem;
@@ -123,6 +124,13 @@ void Item::Parse() {
itemExt = aJson.getArrayItem(itemArr, I_EXT);
switch (itemType)
{
#ifndef DMX_DISABLE
case CH_RGBW:
case CH_RGB:
driver = new out_dmx (this);
// debugSerial<<F("SPILED driver created")<<endl;
break;
#endif
#ifndef SPILED_DISABLE
case CH_SPILED:
driver = new out_SPILed (this);
@@ -479,7 +487,7 @@ debugSerial<<F("Txt2Cmd:")<<cmd<<endl;
setCommand=cmd;
case CMD_HSV:
// suffixCode=S_HSV; //override code for known payload
case CMD_NUM:
case CMD_VOID:
{
short i = 0;
@@ -532,9 +540,10 @@ short Item::cmd2changeActivity(int lastActivity, short defaultCmd)
}
*/
int Item::Ctrl(itemCmd cmd, int suffixCode, char* subItem)
int Item::Ctrl(itemCmd cmd, char* subItem)
{
char stringBuffer[16];
int suffixCode = cmd.getSuffix();
bool operation = isNotRetainingStatus() ;
if ((!subItem || !strlen(subItem)) && strlen(defaultSubItem))
@@ -565,7 +574,7 @@ int Item::Ctrl(itemCmd cmd, int suffixCode, char* subItem)
// Group channel
if (! operation) return -1;
int iaddr = getArg();
//int iaddr = getArg();
bool chActive =(isActive()>0);
itemCmd st;
@@ -609,11 +618,13 @@ int Item::Ctrl(itemCmd cmd, int suffixCode, char* subItem)
case CMD_UP:
{
if (itemType == CH_GROUP) break; ////bug here
short step=cmd.getCmdParam();
short step=0;
if (cmd.isValue()) step=cmd.getInt();
if (!step) step=DEFAULT_INC_STEP;
if (cmd.getCmd() == CMD_DN) step=-step;
int cType=getChanType();
//int cType=getChanType();
//debugSerial<<"from: h="<<st.h<<" s="<<st.s <<" v="<<st.v<<endl;
switch (suffixCode)
{
@@ -630,7 +641,7 @@ int Item::Ctrl(itemCmd cmd, int suffixCode, char* subItem)
} //Case UP/DOWN
break;
case CMD_NUM:
case CMD_VOID:
//if (itemType == CH_GROUP || n!=1) break;
if (!cmd.isValue()) break;
//// if ( cType == CH_RGB || cType == CH_RGBW || cType == CH_GROUP )
@@ -662,7 +673,7 @@ if (driver) //New style modular code
if (!chActive>0) //if channel was'nt active before CMD_XON
{
debugSerial<<F("Turning XON\n");
res = driver->Ctrl(cmd.Cmd(CMD_ON), suffixCode, subItem);
res = driver->Ctrl(cmd.Cmd(CMD_ON), subItem);
setCmd(CMD_XON);
}
else
@@ -674,7 +685,7 @@ if (driver) //New style modular code
case CMD_HALT:
if (chActive>0) //if channel was active before CMD_HALT
{
res = driver->Ctrl(cmd.Cmd(CMD_OFF), suffixCode, subItem);
res = driver->Ctrl(cmd.Cmd(CMD_OFF), subItem);
setCmd(CMD_HALT);
return res;
}
@@ -687,7 +698,7 @@ if (driver) //New style modular code
case CMD_OFF:
if (getCmd() != CMD_HALT) //Halted, ignore OFF
{
res = driver->Ctrl(cmd.Cmd(CMD_OFF), suffixCode, subItem);
res = driver->Ctrl(cmd.Cmd(CMD_OFF), subItem);
setCmd(CMD_OFF);
}
else
@@ -697,12 +708,12 @@ if (driver) //New style modular code
}
break;
case CMD_NUM:
res = driver->Ctrl(st, suffixCode, subItem);
case CMD_VOID:
res = driver->Ctrl(st, subItem);
break;
default: //another command
res = driver->Ctrl(cmd, suffixCode, subItem);
res = driver->Ctrl(cmd, subItem);
if (cmd.isCommand()) setCmd(cmd.getCmd());
}
return res;
@@ -719,7 +730,7 @@ switch (itemType) {
if (i->type == aJson_String)
{
Item it(i->valuestring);
it.Ctrl(cmd, suffixCode,subItem);
it.Ctrl(cmd,subItem);
}
i = i->next;
} //while
@@ -833,7 +844,7 @@ bool send = isNotRetainingStatus() ;
case 2: _itemCmd.setH(Parameters[0]);
_itemCmd.setS(Parameters[1]);
_itemCmd.type=ST_HSV;
_itemCmd.setArgType(ST_HSV);
break;
case 3: _itemCmd.HSV(Parameters[0],Parameters[1],Parameters[2]);
@@ -864,7 +875,7 @@ bool send = isNotRetainingStatus() ;
int iaddr = getArg();
int chActive =isActive();
itemStore st;
itemArgStore st;
switch (cmd) {
int t;
/*
@@ -939,7 +950,7 @@ bool send = isNotRetainingStatus() ;
if (Par[0]>100) Par[0]=100;
if (Par[0]<0) Par[0]=0;
n=1;
cmd=CMD_NUM;
cmd=CMD_VOID;
debugSerial<<" to v="<<Par[0]<<endl;
break;
case S_HUE:
@@ -986,14 +997,14 @@ bool send = isNotRetainingStatus() ;
if (Par[2]<0) Par[2]=0;
n=3;
cmd=CMD_NUM;
cmd=CMD_VOID;
suffixCode=S_SET;
debugSerial<<"to: h="<<Par[0]<<" s="<<Par[1] <<" v="<<Par[2]<<endl;
} // if modified
} //RGBx channel
}
break;
case CMD_NUM:
case CMD_VOID:
//if (itemType == CH_GROUP || n!=1) break;
if (n!=1) break;
int cType=getChanType();
@@ -1040,7 +1051,7 @@ bool send = isNotRetainingStatus() ;
{
debugSerial<<F("Turning XON\n");
//res = driver->Ctrl(CMD_ON, n, Par, suffixCode, subItem);
res = driver->Ctrl(_itemCmd.Cmd(CMD_ON), suffixCode, subItem);
res = driver->Ctrl(_itemCmd.Cmd(CMD_ON), subItem);
setCmd(CMD_XON);
}
else
@@ -1052,7 +1063,7 @@ bool send = isNotRetainingStatus() ;
case CMD_HALT:
if (chActive>0) //if channel was active before CMD_HALT
{
res = driver->Ctrl(_itemCmd.Cmd(CMD_OFF), suffixCode, subItem);
res = driver->Ctrl(_itemCmd.Cmd(CMD_OFF), subItem);
setCmd(CMD_HALT);
return res;
}
@@ -1065,7 +1076,7 @@ bool send = isNotRetainingStatus() ;
case CMD_OFF:
if (getCmd() != CMD_HALT) //Halted, ignore OFF
{
res = driver->Ctrl(_itemCmd, suffixCode, subItem);
res = driver->Ctrl(_itemCmd, subItem);
setCmd(CMD_OFF);
}
else
@@ -1080,7 +1091,7 @@ bool send = isNotRetainingStatus() ;
break;
*/
default:
res = driver->Ctrl(_itemCmd, suffixCode, subItem);
res = driver->Ctrl(_itemCmd, subItem);
if (cmd) setCmd(cmd);
}
return res;
@@ -1482,8 +1493,14 @@ bool send = isNotRetainingStatus() ;
return 1;
}
int Item::isActive() {
itemStore st;
itemArgStore st;
int val = 0;
@@ -2106,7 +2123,7 @@ int Item::SendStatus(int sendFlags) {
if (sendFlags & SEND_PARAMETERS)
{
// Preparing parameters payload //////////
itemStore st;
itemArgStore st;
int chanType = itemType;
if (driver) chanType = driver->getChanType();
//retrive stored values

View File

@@ -23,7 +23,7 @@ e-mail anklimov@gmail.com
#include "itemCmd.h"
#define S_NOTFOUND 0
#define S_SETnCMD 0
//#define S_SETnCMD 0
#define S_CMD 1
#define S_SET 2
#define S_HSV 3
@@ -33,7 +33,7 @@ e-mail anklimov@gmail.com
#define S_HUE 7
#define S_SAT 8
#define S_TEMP 9
#define S_ADDITIONAL 64
#define S_ADDITIONAL 9
#define CH_DIMMER 0 //DMX 1 ch
#define CH_RGBW 1 //DMX 4 ch
@@ -103,7 +103,7 @@ class Item
boolean Setup();
void Stop();
int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL);
int Ctrl(itemCmd cmd, int suffixCode=0, char* subItem=NULL);
int Ctrl(itemCmd cmd, char* subItem=NULL);
int Ctrl(char * payload, char * subItem=NULL);
int getArg(short n=0);

View File

@@ -15,7 +15,7 @@ int txt2cmd(char *payload) {
if (!payload || !payload[0]) return cmd;
// Check for command
if (*payload == '-' || (*payload >= '0' && *payload <= '9')) cmd = CMD_NUM;
if (*payload == '-' || (*payload >= '0' && *payload <= '9')) cmd = CMD_VOID;
else if (*payload == '%') cmd = CMD_UP;
else if (*payload == '{') cmd = CMD_JSON;
else if (*payload == '#') cmd = CMD_RGB;
@@ -37,14 +37,15 @@ int txt2cmd(char *payload) {
return cmd;
}
itemCmd::itemCmd(itemStoreType _type)
itemCmd::itemCmd(uint8_t _type, uint8_t _code)
{
type=_type;
cmd.itemArgType=_type;
cmd.cmdCode=_code;
}
itemCmd itemCmd::setDefault()
{
switch (type){
switch (cmd.itemArgType){
case ST_FLOAT_CELSIUS: param.asfloat=20.;
break;
case ST_FLOAT_FARENHEIT: param.asfloat=75.;
@@ -66,10 +67,10 @@ itemCmd itemCmd::setDefault()
itemCmd itemCmd::setH(uint16_t h)
{
int par=h;
switch (type)
switch (cmd.itemArgType)
{
case ST_VOID:
type=ST_HSV;
cmd.itemArgType=ST_HSV;
case ST_HSV:
if (par>100) par=100;
case ST_HSV255:
@@ -83,10 +84,10 @@ itemCmd itemCmd::setH(uint16_t h)
itemCmd itemCmd::setS(uint8_t s)
{
int par=s;
switch (type)
switch (cmd.itemArgType)
{
case ST_VOID:
type=ST_HSV;
cmd.itemArgType=ST_HSV;
case ST_HSV:
if (par>100) par=100;
case ST_HSV255:
@@ -99,7 +100,7 @@ itemCmd itemCmd::setS(uint8_t s)
itemCmd itemCmd::incrementPercents(int16_t dif)
{ int par=param.v;
switch (type)
switch (cmd.itemArgType)
{
case ST_PERCENTS:
case ST_HSV:
@@ -120,7 +121,7 @@ itemCmd itemCmd::incrementPercents(int16_t dif)
itemCmd itemCmd::incrementH(int16_t dif)
{ int par=param.h;
switch (type)
switch (cmd.itemArgType)
{
case ST_HSV:
case ST_HSV255:
@@ -135,7 +136,7 @@ return *this;
itemCmd itemCmd::incrementS(int16_t dif)
{int par=param.s;
switch (type)
switch (cmd.itemArgType)
{
case ST_PERCENTS:
case ST_HSV:
@@ -161,10 +162,10 @@ itemCmd itemCmd::assignFrom(itemCmd from)
bool RGBW_flag = false;
bool HSV255_flag = false;
switch (type){ //Destination
switch (cmd.itemArgType){ //Destination
case ST_HSV:
case ST_PERCENTS:
switch (from.type)
switch (from.cmd.itemArgType)
{
case ST_RGBW:
param.w=from.param.w;
@@ -172,7 +173,7 @@ itemCmd itemCmd::assignFrom(itemCmd from)
param.r=from.param.r;
param.g=from.param.g;
param.b=from.param.b;
type=from.type; //Changing if type
cmd.itemArgType=from.cmd.itemArgType; //Changing if type
break;
case ST_HSV:
param.h=from.param.h;
@@ -191,11 +192,11 @@ itemCmd itemCmd::assignFrom(itemCmd from)
param.v=map(from.param.v,0,255,0,100);
break;
default:
debugSerial<<F("Wrong Assignment ")<<from.type<<F("->")<<type<<endl;
debugSerial<<F("Wrong Assignment ")<<from.cmd.itemArgType<<F("->")<<cmd.itemArgType<<endl;
}
case ST_HSV255:
case ST_PERCENTS255:
switch (from.type)
switch (from.cmd.itemArgType)
{
case ST_RGBW:
param.w=from.param.w;
@@ -203,7 +204,7 @@ itemCmd itemCmd::assignFrom(itemCmd from)
param.r=from.param.r;
param.g=from.param.g;
param.b=from.param.b;
type=from.type;
cmd.itemArgType=from.cmd.itemArgType;
break;
case ST_HSV:
param.h=from.param.h;
@@ -222,11 +223,11 @@ itemCmd itemCmd::assignFrom(itemCmd from)
param.v=from.param.v;
break;
default:
debugSerial<<F("Wrong Assignment ")<<from.type<<F("->")<<type<<endl;
debugSerial<<F("Wrong Assignment ")<<from.cmd.itemArgType<<F("->")<<cmd.itemArgType<<endl;
}
case ST_VOID:
type=from.type;
cmd.itemArgType=from.cmd.itemArgType;
case ST_INT32:
case ST_UINT32:
@@ -239,7 +240,7 @@ itemCmd itemCmd::assignFrom(itemCmd from)
case ST_RGBW:
RGBW_flag=true;
case ST_RGB:
switch (from.type)
switch (from.cmd.itemArgType)
{
case ST_RGBW:
case ST_RGB:
@@ -292,7 +293,7 @@ itemCmd itemCmd::assignFrom(itemCmd from)
#endif
}
default:
debugSerial<<F("Wrong Assignment ")<<from.type<<F("->")<<type<<endl;
debugSerial<<F("Wrong Assignment ")<<from.cmd.itemArgType<<F("->")<<cmd.itemArgType<<endl;
} //Translation to RGB_XX
break;
} //Destination
@@ -301,35 +302,25 @@ itemCmd itemCmd::assignFrom(itemCmd from)
bool itemCmd::isCommand()
{
switch (type) {
case ST_COMMAND:
return true;
default:
return false;
}
return (cmd.cmdCode);
}
bool itemCmd::isValue()
{
switch (type) {
case ST_COMMAND:
case ST_VOID:
return false;
default:
return true;
}
return (cmd.itemArgType);
}
long int itemCmd::getInt()
{
switch (type) {
switch (cmd.itemArgType) {
case ST_INT32:
case ST_PERCENTS:
case ST_UINT32:
case ST_PERCENTS255:
case ST_HSV:
case ST_HSV255:
return param.aslong;
case ST_FLOAT:
@@ -344,7 +335,7 @@ long int itemCmd::getInt()
short itemCmd::getPercents()
{
switch (type) {
switch (cmd.itemArgType) {
case ST_PERCENTS:
case ST_HSV:
@@ -361,7 +352,7 @@ short itemCmd::getPercents()
short itemCmd::getPercents255()
{
switch (type) {
switch (cmd.itemArgType) {
case ST_PERCENTS:
case ST_HSV:
@@ -376,28 +367,77 @@ short itemCmd::getPercents255()
}
}
short itemCmd::getCmd()
uint8_t itemCmd::getCmd()
{
if (type==ST_COMMAND) return param.cmd_code;
return 0;
return cmd.cmdCode;
}
short itemCmd::getCmdParam()
uint8_t itemCmd::getArgType()
{
if (type==ST_COMMAND) return param.cmd_param;
return cmd.itemArgType;
}
itemCmd itemCmd::setArgType(uint8_t type)
{
cmd.itemArgType=type;
return *this;
}
uint8_t itemCmd::getCmdParam()
{
if (isCommand()) return cmd.cmdParam;
return 0;
}
itemCmd itemCmd::Percents(int i)
{
type=ST_PERCENTS;
param.aslong=i;
if (i<0) i=0;
if (i>100) i=100;
switch (cmd.itemArgType)
{
case ST_HSV:
case ST_PERCENTS:
param.v=i;
break;
case ST_HSV255:
case ST_PERCENTS255:
param.v=map(i,0,100,0,255);
default:
cmd.itemArgType=ST_PERCENTS;
param.v=i;
}
return *this;
}
itemCmd itemCmd::Percents255(int i)
{
if (i<0) i=0;
if (i>255) i=255;
switch (cmd.itemArgType)
{
case ST_HSV:
case ST_PERCENTS:
param.v=map(i,0,255,0,100);
break;
case ST_HSV255:
case ST_PERCENTS255:
param.v=i;;
default:
cmd.itemArgType=ST_PERCENTS255;
param.v=i;
}
return *this;
}
itemCmd itemCmd::Int(int32_t i)
{
type=ST_INT32;
cmd.itemArgType=ST_INT32;
param.asInt32=i;
return *this;
}
@@ -406,7 +446,7 @@ itemCmd itemCmd::Int(int32_t i)
itemCmd itemCmd::HSV(uint16_t h, uint8_t s, uint8_t v)
{
type=ST_HSV;
cmd.itemArgType=ST_HSV;
param.h=h;
param.s=s;
param.v=v;
@@ -416,7 +456,7 @@ itemCmd itemCmd::HSV(uint16_t h, uint8_t s, uint8_t v)
itemCmd itemCmd::Int(uint32_t i)
{
type=ST_UINT32;
cmd.itemArgType=ST_UINT32;
param.asUint32=i;
return *this;
}
@@ -424,29 +464,42 @@ itemCmd itemCmd::Int(uint32_t i)
itemCmd itemCmd::Cmd(uint8_t i)
{
type=ST_COMMAND;
param.cmd_code=i;
// cmd.itemArgType=ST_COMMAND;
cmd.cmdCode=i;
return *this;
}
bool itemCmd::loadItem(Item * item)
uint8_t itemCmd::getSuffix()
{
return cmd.suffixCode;
}
itemCmd itemCmd::setSuffix(uint8_t suffix)
{
cmd.suffixCode=suffix;
return *this;
}
bool itemCmd::loadItem(Item * item, bool includeCommand)
{
if (item && item->isValid())
{
param.asInt32=item->getVal();
type=(itemStoreType) item->getSubtype();
return (type!=ST_VOID);
cmd.itemArgType=item->getSubtype();
if (includeCommand) cmd.cmdCode=item->getCmd();
return (cmd.itemArgType!=ST_VOID);
}
return false;
}
bool itemCmd::saveItem(Item * item)
bool itemCmd::saveItem(Item * item, bool includeCommand)
{
if (item && item->isValid())
{
item->setVal(param.asInt32);
item->setSubtype(type);
item->setSubtype(cmd.itemArgType);
if (includeCommand) item->setCmd(cmd.cmdCode);
return true;
}
return false;
@@ -456,45 +509,56 @@ return false;
char * itemCmd::toString(char * Buffer, int bufLen)
{
if (!Buffer) return NULL;
switch (type)
if (!Buffer || !bufLen) return NULL;
*Buffer=0;
char * argPtr=Buffer;
if (isCommand())
{
int len;
strncpy_P(Buffer, commands_P[cmd.cmdCode], bufLen);
strncat(Buffer, " ", bufLen);
len=strlen(Buffer);
argPtr+=len;
bufLen-=len;
}
switch (cmd.itemArgType)
{
case ST_VOID:
return NULL;
case ST_PERCENTS:
case ST_PERCENTS255:
case ST_UINT32:
snprintf(Buffer, bufLen, "%lu", param.asUint32);
snprintf(argPtr, bufLen, "%lu", param.asUint32);
break;
case ST_INT32:
snprintf(Buffer, bufLen, "%ld", param.asInt32);
snprintf(argPtr, bufLen, "%ld", param.asInt32);
break;
case ST_HSV:
case ST_HSV255:
snprintf(Buffer, bufLen, "%d,%d,%d", param.h, param.s, param.v);
snprintf(argPtr, 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);
snprintf(argPtr, bufLen, "%.1f", param.asfloat);
break;
case ST_RGB:
snprintf(Buffer, bufLen, "%d,%d,%d", param.r, param.g, param.b);
snprintf(argPtr, 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);
snprintf(argPtr, bufLen, "%d,%d,%d,%d", param.r, param.g, param.b,param.w);
break;
case ST_STRING:
strncpy(Buffer, param.asString,bufLen);
strncpy(argPtr, param.asString,bufLen);
break;
case ST_COMMAND:
strncpy_P(Buffer, commands_P[param.cmd_code], bufLen);
default:
;
}
return Buffer;
}

View File

@@ -58,7 +58,7 @@ const cmdstr commands_P[] PROGMEM =
#define CMD_MASK 0xff
#define FLAG_MASK 0xff00
#define CMD_NUM 0
#define CMD_VOID 0
#define CMD_UNKNOWN -1
#define CMD_JSON -2
//#define CMD_RGB -3
@@ -74,6 +74,7 @@ const cmdstr commands_P[] PROGMEM =
int txt2cmd (char * payload);
/*
enum itemStoreType {
ST_VOID = 0,
ST_PERCENTS = 1,
@@ -88,12 +89,49 @@ ST_HSV255 = 9,
ST_INT32 = 10,
ST_UINT32 = 11,
ST_STRING = 12,
ST_FLOAT = 13,
ST_COMMAND = 15
ST_FLOAT = 13//,
//ST_COMMAND = 15
};
*/
#define ST_VOID 0
#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
#pragma pack(push, 1)
typedef union
{
long int aslong;
int32_t asInt32;
uint32_t asUint32;
struct
{
uint8_t cmdCode;
union {
uint8_t cmdFlag;
struct
{ uint8_t suffixCode:4;
uint8_t itemArgType:4;
};
};
uint8_t cmdEffect;
uint8_t cmdParam;
};
} itemCmdStore;
typedef union
{
long int aslong;
@@ -101,13 +139,7 @@ typedef union
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;
@@ -127,19 +159,20 @@ typedef union
uint8_t b;
uint8_t w;
};
} itemStore;
} itemArgStore;
class Item;
class itemCmd
{
public:
itemStoreType type;
itemStore param;
itemCmdStore cmd;
itemArgStore param;
itemCmd(itemStoreType _type=ST_VOID);
itemCmd(uint8_t _type=ST_VOID, uint8_t _code=CMD_VOID);
itemCmd assignFrom(itemCmd from);
bool loadItem(Item * item);
bool saveItem(Item * item);
bool loadItem(Item * item, bool includeCommand=false );
bool saveItem(Item * item, bool includeCommand=false);
itemCmd Int(int32_t i);
itemCmd Int(uint32_t i);
@@ -147,7 +180,12 @@ public:
itemCmd HSV(uint16_t h, uint8_t s, uint8_t v);
itemCmd setH(uint16_t);
itemCmd setS(uint8_t);
itemCmd setArgType(uint8_t);
itemCmd Percents(int i);
itemCmd Percents255(int i);
uint8_t getSuffix();
itemCmd setSuffix(uint8_t suffix);
itemCmd incrementPercents(int16_t);
itemCmd incrementH(int16_t);
@@ -156,8 +194,9 @@ public:
long int getInt();
short getPercents();
short getPercents255();
short getCmd();
short getCmdParam();
uint8_t getCmd();
uint8_t getArgType();
uint8_t getCmdParam();
char * toString(char * Buffer, int bufLen);
bool isCommand();

View File

@@ -263,8 +263,9 @@ return INTERVAL_POLLING;
};
//int out_AC::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* subItem)
int out_AC::Ctrl(itemCmd cmd, int suffixCode, char* subItem)
int out_AC::Ctrl(itemCmd cmd, char* subItem)
{char s_mode[10];
int suffixCode = cmd.getSuffix();
// Some additional Subitems
if (strcmp_P(subItem, LOCK_P) == 0) suffixCode = S_LOCK;
else if (strcmp_P(subItem, SWING_P) == 0) suffixCode = S_SWING;

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, int suffixCode=0, char* subItem=NULL) override;
int Ctrl(itemCmd cmd, char* subItem=NULL) override;
protected:
void InsertData(byte data[], size_t size);

View File

@@ -1,5 +1,5 @@
//#ifndef DMX_DISABLE
#ifdef XXXX
#ifndef DMX_DISABLE
#include "modules/out_dmx.h"
#include "Arduino.h"
#include "options.h"
@@ -7,128 +7,79 @@
#include "item.h"
#include "main.h"
#include "dmx.h"
static int driverStatus = CST_UNKNOWN;
int out_dmx::Setup()
{
debugSerial<<F("DMX-Out Init")<<endl;
driverStatus = CST_INITIALIZED;
return 1;
}
int out_SPILed::Stop()
int out_dmx::Stop()
{
debugSerial<<F("DMX-Out stop")<<endl;
driverStatus = CST_UNKNOWN;
return 1;
}
int out_SPILed::Status()
int out_dmx::Status()
{
return driverStatus;
}
int out_SPILed::isActive()
int out_dmx::isActive()
{
itemStore st;
itemArgStore st;
st.aslong = item->getVal(); //Restore old params
debugSerial<< F(" val:")<<st.v<<endl;
return st.v;
}
int out_SPILed::Poll(short cause)
int out_dmx::Poll(short cause)
{
return 0;
};
int out_SPILed::getChanType()
int out_dmx::getChanType()
{
if ((ledsType>>4) == (ledsType>>6))
return CH_RGB;
else
return CH_RGBW;
if (item) return item->itemType;
return 0;
}
int out_SPILed::PixelCtrl(itemCmd cmd, int from, int to, bool show)
int out_dmx::PixelCtrl(itemCmd cmd)
{
if (!item) return 0;
int iaddr = item->getArg(0);
itemCmd st(ST_RGB);
st.assignFrom(cmd);
#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 (getChanType())
{
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
case CH_RGBW:
DmxWrite(iaddr + 3, st.param.w);
case CH_RGB:
DmxWrite(iaddr, st.param.r);
DmxWrite(iaddr + 1, st.param.g);
DmxWrite(iaddr + 2, st.param.b);
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<<F("Show")<<endl;
default: ;
}
return 1;
}
int out_SPILed::Ctrl(itemCmd cmd, int suffixCode, char* subItem)
int out_dmx::Ctrl(itemCmd cmd, char* subItem)
{
int chActive = item->isActive();
bool toExecute = (chActive>0);
bool toExecute = (chActive>0); // execute if channel is active now
int suffixCode = cmd.getSuffix();
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<<F("Range:")<<subItem<<endl;
if (sscanf(subItem,"%d-%d",&from,&to) == 1) to=from;
}
debugSerial<<from<<F("-")<<to<<F(" cmd=")<<cmd.getCmd()<<endl;
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)
{
@@ -137,17 +88,11 @@ case S_NOTFOUND:
toExecute = true;
case S_SET:
case S_HSV:
//st.Int(item->getVal()); //Restore old params
st.loadItem(item);
st.assignFrom(cmd);
PixelCtrl(st,from,to,toExecute);
if (!subItem) //Whole strip
{
//item->setVal(st.getInt()); //Store
if (toExecute) PixelCtrl(st);
st.saveItem(item);
if (!suffixCode)
{
if (chActive>0 && !st.getPercents()) item->setCmd(CMD_OFF);
@@ -155,9 +100,8 @@ case S_HSV:
item->SendStatus(SEND_COMMAND | SEND_PARAMETERS | SEND_DEFFERED);
}
else item->SendStatus(SEND_PARAMETERS | SEND_DEFFERED);
}
return 1;
//break;
/*
case S_HUE:
st.setH(uint16_t);
break;
@@ -165,62 +109,45 @@ case S_HUE:
case S_SAT:
st.setS(uint8_t);
break;
*/
case S_CMD:
item->setCmd(cmd.getCmd());
switch (cmd.getCmd())
{
case CMD_ON:
//retrive stored values
st.loadItem(item);
if (subItem) // LED range, not whole strip
PixelCtrl(st.Cmd(CMD_ON),from,to);
else //whole strip
if (st.loadItem(item))
{
if (st.param.aslong && (st.param.v<MIN_VOLUME) /* && send */) st.Percents(INIT_VOLUME);
//item->setVal(st.getInt());
st.saveItem(item);
if (st.param.aslong && (st.param.v<MIN_VOLUME)) {
st.Percents(INIT_VOLUME);
}
if (st.getInt() ) //Stored smthng
{
item->SendStatus(SEND_COMMAND | SEND_PARAMETERS);
debugSerial<<F("Restored: ")<<st.param.h<<F(",")<<st.param.s<<F(",")<<st.param.v<<endl;
}
else
else // Not restored
{
debugSerial<<st.param.aslong<<F(": No stored values - default\n");
st.setDefault();
//st.param.hsv_flag=1; ///tyta
// Store
//item->setVal(st.getInt());
debugSerial<<st.param.aslong<<F(": No stored values - default\n");
}
st.saveItem(item);
PixelCtrl(st);
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);
PixelCtrl(st);
item->SendStatus(SEND_COMMAND);
// if (send) item->publishTopic(item->itemArr->name,"OFF","/cmd");
}
return 1;
} //switch cmd
break;
} //switch cmd
} //switch suffix
debugSerial<<F("Unknown cmd")<<endl;
return 0;
}
#endif

View File

@@ -1,34 +1,24 @@
#pragma once
#include "options.h"
//#ifndef DMX_DISABLE
#ifdef XXXX
#ifndef DMX_DISABLE
#include <abstractout.h>
#include <item.h>
#ifdef ADAFRUIT_LED
#include <Adafruit_NeoPixel.h>
#else
#include "FastLED.h"
#endif
class out_dmx : public abstractOut {
public:
out_dmx(Item * _item):abstractOut(_item){getConfig();};
out_dmx(Item * _item):abstractOut(_item){};
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;
int Ctrl(itemCmd cmd, char* subItem=NULL) override;
int PixelCtrl(itemCmd cmd);
protected:
void getConfig();
};
#endif

View File

@@ -338,11 +338,13 @@ int out_Modbus::getChanType()
int out_Modbus::Ctrl(itemCmd cmd, int suffixCode, char* subItem)
int out_Modbus::Ctrl(itemCmd cmd, char* subItem)
{
int chActive = item->isActive();
bool toExecute = (chActive>0);
itemCmd st(ST_UINT32);
int suffixCode = cmd.getSuffix();
if (cmd.isCommand() && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it
switch(suffixCode)

View File

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

View File

@@ -211,10 +211,11 @@ int out_Motor::getChanType()
int out_Motor::Ctrl(itemCmd cmd, int suffixCode, char* subItem)
int out_Motor::Ctrl(itemCmd cmd, char* subItem)
{
int chActive = item->isActive();
bool toExecute = (chActive>0);
int suffixCode = cmd.getSuffix();
itemCmd st(ST_PERCENTS);
if (cmd.isCommand() && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it

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, int suffixCode=0, char* subItem=NULL) override;
int Ctrl(itemCmd cmd, char* subItem=NULL) override;
int8_t pinUp;
int8_t pinDown;

View File

@@ -89,7 +89,7 @@ return driverStatus;
int out_SPILed::isActive()
{
itemStore st;
itemArgStore st;
st.aslong = item->getVal(); //Restore old params
debugSerial<< F(" val:")<<st.v<<endl;
return st.v;
@@ -102,6 +102,7 @@ return 0;
int out_SPILed::getChanType()
{
if ((ledsType>>4) == (ledsType>>6))
return CH_RGB;
else
@@ -167,10 +168,11 @@ CRGB pixel;
return 1;
}
int out_SPILed::Ctrl(itemCmd cmd, int suffixCode, char* subItem)
int out_SPILed::Ctrl(itemCmd cmd, char* subItem)
{
int chActive = item->isActive();
bool toExecute = (chActive>0);
int suffixCode = cmd.getSuffix();
itemCmd st(ST_HSV);
if (cmd.isCommand() && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it

View File

@@ -22,7 +22,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, int suffixCode=0, char* subItem=NULL) override;
int Ctrl(itemCmd cmd, char* subItem=NULL) override;
int PixelCtrl(itemCmd cmd, int from =0 , int to = 1024, bool show = 1);
int numLeds;
int8_t pin;

View File

@@ -525,9 +525,8 @@ bool isTimeOver(uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t m
bool executeCommand(aJsonObject* cmd, int8_t toggle)
{
itemCmd _itemCmd;
_itemCmd.type = ST_VOID;
return executeCommand(cmd,toggle,_itemCmd);
//itemCmd _itemCmd;
return executeCommand(cmd,toggle,itemCmd());
}
bool executeCommand(aJsonObject* cmd, int8_t toggle, itemCmd _itemCmd)