From 0055ad0463b01c4de6dea6c1f20d05ed71603bc9 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Thu, 3 Dec 2020 00:34:37 +0300 Subject: [PATCH] interim modbus commit (untested) --- lighthub/abstractout.h | 3 +- lighthub/inputs.cpp | 6 ++-- lighthub/item.cpp | 26 +++++++------- lighthub/itemCmd.cpp | 41 ++++++++++++++++++---- lighthub/itemCmd.h | 10 ++++++ lighthub/main.cpp | 2 +- lighthub/modules/out_modbus.cpp | 61 ++++++++++++++++++++++++++------- lighthub/modules/out_modbus.h | 1 - 8 files changed, 112 insertions(+), 38 deletions(-) diff --git a/lighthub/abstractout.h b/lighthub/abstractout.h index 41417a8..943dc97 100644 --- a/lighthub/abstractout.h +++ b/lighthub/abstractout.h @@ -10,8 +10,9 @@ public: abstractOut(Item * _item):abstractCh(){item=_item;}; virtual int Ctrl(itemCmd cmd, char* subItem=NULL, bool toExecute=true) =0; virtual int isActive(){return 0;}; - virtual int getDefaultOnVal(){return 100;}; + virtual itemCmd getDefaultOnVal(){return itemCmd(ST_PERCENTS,CMD_VOID).Percents(100);}; virtual int getChanType(){return 0;} + virtual int getDefaultStorageType(){return ST_PERCENTS;} protected: Item * item; }; diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index 6893ccb..e9a947b 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -115,11 +115,11 @@ void Input::Parse(aJsonObject * configObj) } // Persistant storage - itemBuffer = aJson.getObjectItem(inputObj, "S"); + itemBuffer = aJson.getObjectItem(inputObj, "@S"); if (!itemBuffer) { debugSerial<valueint; diff --git a/lighthub/item.cpp b/lighthub/item.cpp index 482b941..6ac927a 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -119,7 +119,7 @@ void Item::Parse() { if (isValid()) { // Todo - avoid static enlarge for every types for (int i = aJson.getArraySize(itemArr); i < 4; i++) - aJson.addItemToArray(itemArr, aJson.createItem( + aJson.addItemToArray(itemArr, aJson.createItem( (long int) int(defval[i]))); //Enlarge item to 4 elements. VAL=int if no other definition in conf itemType = aJson.getArrayItem(itemArr, I_TYPE)->valueint; itemArg = aJson.getArrayItem(itemArr, I_ARG); @@ -374,7 +374,7 @@ void Item::setExt(long int par) // Only store if VAL is int (autogenerated or c if (!itemExt) { for (int i = aJson.getArraySize(itemArr); i <= 4; i++) - aJson.addItemToArray(itemArr, itemExt=aJson.createItem(0)); + aJson.addItemToArray(itemArr, itemExt=aJson.createItem((long int)0)); //itemExt = aJson.getArrayItem(itemArr, I_EXT); }; @@ -396,7 +396,7 @@ chPersistent * Item::setPersistent(chPersistent * par) if (!itemExt) { for (int i = aJson.getArraySize(itemArr); i <= 4; i++) - aJson.addItemToArray(itemArr, itemExt = aJson.createItem(0)); + aJson.addItemToArray(itemArr, itemExt = aJson.createItem((long int)0)); //itemExt = aJson.getArrayItem(itemArr, I_EXT); }; @@ -858,7 +858,7 @@ switch (itemType) { iaddr = -iaddr; inverse = 1; } - if (iaddr <=PINS_COUNT && iaddr>0) + if (iaddr <= (short) PINS_COUNT && iaddr>0) { pinMode(iaddr, OUTPUT); @@ -1263,17 +1263,17 @@ int Item::checkFM() { debugSerial<<_HEX(data)<type == aJson_Array) { aJsonObject *airGateObj = aJson.getArrayItem(itemArg, 1); if (airGateObj && airGateObj->type == aJson_String) { @@ -1290,11 +1290,11 @@ int Item::checkFM() { if (node.getResponseBuffer(0) & 8) //Active fault { result = node.readHoldingRegisters(2111 - 1, 1); - if (result == node.ku8MBSuccess) aJson.addNumberToObject(out, "flt", (int) node.getResponseBuffer(0)); + if (result == node.ku8MBSuccess) aJson.addNumberToObject(out, "flt", (long int) node.getResponseBuffer(0)); modbusBusy=0; if (isActive()>0) Off(); //Shut down /// modbusBusy=1; - } else aJson.addNumberToObject(out, "flt", 0); + } else aJson.addNumberToObject(out, "flt", (long int)0); delay(50); result = node.readHoldingRegisters(20 - 1, 4); @@ -1316,7 +1316,7 @@ int Item::checkFM() { int16_t pwr = node.getResponseBuffer(3); if (pwr > 0) aJson.addNumberToObject(out, "pwr", pwr / 10.); - else aJson.addNumberToObject(out, "pwr", 0); + else aJson.addNumberToObject(out, "pwr", (long int) 0); if (ftemp > FM_OVERHEAT_CELSIUS && set) { if (mqttClient.connected() && !ethernetIdleCount) diff --git a/lighthub/itemCmd.cpp b/lighthub/itemCmd.cpp index 27695f4..2a0e2e9 100644 --- a/lighthub/itemCmd.cpp +++ b/lighthub/itemCmd.cpp @@ -552,6 +552,7 @@ long int itemCmd::getInt() case ST_UINT32: case ST_RGB: case ST_RGBW: + case ST_TENS: return param.aslong; case ST_PERCENTS: @@ -571,6 +572,11 @@ long int itemCmd::getInt() } } +long int itemCmd::getSingleInt() +{ + if (cmd.cmdCode) return cmd.cmdCode; + return getInt(); +} short itemCmd::getPercents(bool inverse) { @@ -683,8 +689,21 @@ itemCmd itemCmd::Int(int32_t i) return *this; } +itemCmd itemCmd::Int(uint32_t i) + { + cmd.itemArgType=ST_UINT32; + param.asUint32=i; + return *this; + } +itemCmd itemCmd::Tens(int32_t i) + { + cmd.itemArgType=ST_TENS; + param.asInt32=i; + return *this; + } + itemCmd itemCmd::HSV(uint16_t h, uint8_t s, uint8_t v) { cmd.itemArgType=ST_HSV; @@ -737,12 +756,7 @@ itemCmd itemCmd::RGBW(uint8_t r, uint8_t g, uint8_t b, uint8_t w) return *this; } -itemCmd itemCmd::Int(uint32_t i) - { - cmd.itemArgType=ST_UINT32; - param.asUint32=i; - return *this; - } + itemCmd itemCmd::Cmd(uint8_t i) @@ -797,6 +811,17 @@ return false; } + int itemCmd::doMapping(aJsonObject *mappingData) + { + + + } + int itemCmd::doReverseMapping (aJsonObject *mappingData) + + { + + } + char * itemCmd::toString(char * Buffer, int bufLen, int sendFlags ) { @@ -826,7 +851,9 @@ char * itemCmd::toString(char * Buffer, int bufLen, int sendFlags ) break; case ST_INT32: snprintf(argPtr, bufLen, "%ld", param.asInt32); - + break; + case ST_TENS: + snprintf(argPtr, bufLen, "%ld.%d", param.asInt32/10, param.asInt32 % 10); break; case ST_HSV: case ST_HSV255: diff --git a/lighthub/itemCmd.h b/lighthub/itemCmd.h index 8ad5451..f862944 100644 --- a/lighthub/itemCmd.h +++ b/lighthub/itemCmd.h @@ -19,6 +19,7 @@ e-mail anklimov@gmail.com */ #pragma once #include "Arduino.h" +#include "aJson.h" typedef char cmdstr[9]; @@ -110,6 +111,8 @@ ST_FLOAT = 13//, #define ST_STRING 13 /// pointer to string (for further use) #define ST_FLOAT 14 /// generic Float value +#define MAP_SCALE 1 +#define MAP_VAL_CMD 2 #pragma pack(push, 1) @@ -184,6 +187,7 @@ public: itemCmd Int(int32_t i); itemCmd Int(uint32_t i); + itemCmd Tens(int32_t i); itemCmd Cmd(uint8_t i); itemCmd HSV(uint16_t h, uint8_t s, uint8_t v); itemCmd HSV255(uint16_t h, uint8_t s, uint8_t v); @@ -208,6 +212,7 @@ public: bool incrementS(int16_t); long int getInt(); + long int getSingleInt(); short getPercents(bool inverse=false); short getPercents255(bool inverse=false); uint8_t getCmd(); @@ -222,6 +227,11 @@ public: itemCmd setDefault(); itemCmd setChanType(short chanType); void debugOut(); + + int doMapping(aJsonObject *mappingData); + int doReverseMapping (aJsonObject *mappingData); + + }; #pragma pack(pop) diff --git a/lighthub/main.cpp b/lighthub/main.cpp index 0babfdb..1006dad 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -2161,7 +2161,7 @@ short thermoSetCurTemp(char *name, float t) { extArray = aJson.createArray(); //Create Ext Array aJsonObject *ocurt = aJson.createItem(t); //Create float - aJsonObject *oattempts = aJson.createItem(T_ATTEMPTS); //Create int + 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 diff --git a/lighthub/modules/out_modbus.cpp b/lighthub/modules/out_modbus.cpp index beb69b3..8e80f2a 100644 --- a/lighthub/modules/out_modbus.cpp +++ b/lighthub/modules/out_modbus.cpp @@ -36,6 +36,7 @@ struct serial_t #define PAR_I8L 6 #define PAR_U8H 7 #define PAR_U8L 8 +#define PAR_TENS 9 const reg_t regSize_P[] PROGMEM = @@ -47,7 +48,8 @@ const reg_t regSize_P[] PROGMEM = { "i8h", (uint8_t) PAR_I8H }, { "i8l", (uint8_t) PAR_I8L }, { "u8h", (uint8_t) PAR_U8H }, - { "u8l", (uint8_t) PAR_U8L } + { "u8l", (uint8_t) PAR_U8L }, + { "x10", (uint8_t) PAR_TENS } } ; #define regSizeNum sizeof(regSize_P)/sizeof(reg_t) @@ -223,40 +225,75 @@ int out_Modbus::findRegister(int registerNum, int posInBuffer) int8_t regType = PAR_I16; uint32_t param =0; itemCmd mappedParam; - bool isSigned=false; + char buf[16]; + + //bool isSigned=false; if (typeObj && typeObj->type == aJson_String) regType=str2regSize(typeObj->valuestring); switch(regType) { case PAR_I16: - isSigned=true; + //isSigned=true; + mappedParam.Int((int32_t)data); + break; + case PAR_U16: - param=data; + mappedParam.Int((uint32_t)data); + //param=data; break; case PAR_I32: - isSigned=true; + //isSigned=true; + param = data | (node.getResponseBuffer(posInBuffer+1)<<16); + mappedParam.Int((int32_t)param); + break; + case PAR_U32: param = data | (node.getResponseBuffer(posInBuffer+1)<<16); + mappedParam.Int((uint32_t)param); break; + case PAR_U8L: - is8bit=true; + //is8bit=true; param = data & 0xFF; + mappedParam.Int((uint32_t)param); break; + case PAR_U8H: - is8bit=true; + //is8bit=true; param = data << 8; + mappedParam.Int((uint32_t)param); + break; + + case PAR_TENS: + mappedParam.Tens((int32_t) data); } if (mapObj && (mapObj->type==aJson_Array || mapObj->type==aJson_Object)) - mappedParam = mapInt(param,mapObj); - else mappedParam.Int(param); + mappedParam.doMapping(mapObj); + + debugSerial << F("MB got ")<name<type ==aJson_Object) { aJsonObject *execObj = aJson.getObjectItem(itemParametersObj,paramObj->name); - if (execObj) executeCommand(execObj, -1, mappedParam); + if (execObj) + { + bool submitParam=true; + //Retrive previous data + aJsonObject *lastMeasured = aJson.getObjectItem(execObj,"@S"); + if (lastMeasured) + { + if (lastMeasured->valueint == mappedParam.getSingleInt()) + submitParam=false; //supress repeating execution for same val + else lastMeasured->valueint=mappedParam.getSingleInt(); + } + else //No container to store value yet + { + debugSerial<name<name<next; diff --git a/lighthub/modules/out_modbus.h b/lighthub/modules/out_modbus.h index c7dddc3..2ee54d2 100644 --- a/lighthub/modules/out_modbus.h +++ b/lighthub/modules/out_modbus.h @@ -16,7 +16,6 @@ public: uint32_t timestamp; aJsonObject * pollingRegisters; aJsonObject * parameters; - };