AnalogInput developed, some other fixes: eol-to-endl debugSerial; volume RGB channels changing by single argument passing (GoodleHome/analogue input control) fixed

This commit is contained in:
2018-11-25 04:46:35 +03:00
parent 8fccde7686
commit 37a6c677ce
5 changed files with 103 additions and 34 deletions

View File

@@ -108,18 +108,23 @@ void Input::Parse()
int Input::poll() { int Input::poll() {
if (!isValid()) return -1; if (!isValid()) return -1;
if (0) ;
#ifndef DHT_COUNTER_DISABLE #ifndef DHT_COUNTER_DISABLE
if (inType & IN_DHT22) else if (inType & IN_DHT22)
dht22Poll(); dht22Poll();
else if (inType & IN_COUNTER) else if (inType & IN_COUNTER)
counterPoll(); counterPoll();
else if (inType & IN_UPTIME) else if (inType & IN_UPTIME)
uptimePoll(); uptimePoll();
#endif
else if (inType & IN_ANALOG)
analogPoll();
else else
contactPoll(); contactPoll();
return 0; return 0;
#endif
contactPoll(); // contactPoll();
} }
#ifndef DHT_COUNTER_DISABLE #ifndef DHT_COUNTER_DISABLE
@@ -361,6 +366,44 @@ void Input::contactPoll() {
} }
void Input::analogPoll() {
uint8_t currentInputState;
#if defined(ARDUINO_ARCH_STM32F1)
WiringPinMode inputPinMode;
#endif
#if defined(__SAM3X8E__)||defined(ARDUINO_ARCH_AVR)||defined(ARDUINO_ARCH_ESP8266)||defined(ARDUINO_ARCH_ESP32)
uint32_t inputPinMode;
#endif
uint8_t inputOnLevel;
if (inType & IN_ACTIVE_HIGH) {
inputOnLevel = HIGH;
inputPinMode = INPUT;
} else {
inputOnLevel = LOW;
inputPinMode = INPUT_PULLUP;
}
pinMode(pin, inputPinMode);
currentInputState = map (analogRead(pin),0,900,0,100);
if (abs(currentInputState - store->currentValue)>1) // value changed >1
{
if (store->bounce) store->bounce = 0;
} else // no change
if (store->bounce<ANALOG_STATE_ATTEMPTS) store->bounce ++;
if (store->bounce<ANALOG_STATE_ATTEMPTS-1 && (currentInputState != store->currentValue)) //confirmed change
{
store->logicState = currentInputState;
onAnalogChanged(currentInputState);
store->currentValue = currentInputState;
}
}
void Input::onContactChanged(int newValue) { void Input::onContactChanged(int newValue) {
debugSerial << F("IN:") << (pin) << F("=") << newValue << endl; debugSerial << F("IN:") << (pin) << F("=") << newValue << endl;
@@ -402,6 +445,31 @@ void Input::onContactChanged(int newValue) {
} }
void Input::onAnalogChanged(int newValue) {
debugSerial << F("IN:") << (pin) << F("=") << newValue << endl;
aJsonObject *item = aJson.getObjectItem(inputObj, "item");
aJsonObject *emit = aJson.getObjectItem(inputObj, "emit");
if (emit) {
//#ifdef WITH_DOMOTICZ
// if (getIdxField()) {
// (newValue)? publishDataToDomoticz(0, emit, "{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"On\"}", getIdxField())
// : publishDataToDomoticz(0,emit,"{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"Off\"}",getIdxField());
// } else
//#endif
char strVal[16];
itoa(newValue,strVal,10);
mqttClient.publish(emit->valuestring, strVal, true);
}
if (item) {
Item it(item->valuestring);
if (it.isValid()) {
it.Ctrl(0, 1, &newValue, true);
}
}
}
void Input::printUlongValueToStr(char *valstr, unsigned long value) { void Input::printUlongValueToStr(char *valstr, unsigned long value) {
char buf[11]; char buf[11];

View File

@@ -31,6 +31,7 @@ e-mail anklimov@gmail.com
#define IN_UPTIME 16 #define IN_UPTIME 16
#define SAME_STATE_ATTEMPTS 3 #define SAME_STATE_ATTEMPTS 3
#define ANALOG_STATE_ATTEMPTS 6
// in syntaxis // in syntaxis
// "pin": { "T":"N", "emit":"out_emit", item:"out_item", "scmd": "ON,OFF,TOGGLE,INCREASE,DECREASE", "rcmd": "ON,OFF,TOGGLE,INCREASE,DECREASE", "rcmd":"repeat_command" } // "pin": { "T":"N", "emit":"out_emit", item:"out_item", "scmd": "ON,OFF,TOGGLE,INCREASE,DECREASE", "rcmd": "ON,OFF,TOGGLE,INCREASE,DECREASE", "rcmd":"repeat_command" }
@@ -92,6 +93,7 @@ public:
boolean isValid(); boolean isValid();
void onContactChanged(int newValue); void onContactChanged(int newValue);
void onAnalogChanged(int newValue);
int poll(); int poll();
@@ -109,6 +111,7 @@ protected:
void Parse(); void Parse();
void contactPoll(); void contactPoll();
void analogPoll();
void dht22Poll(); void dht22Poll();

View File

@@ -84,7 +84,7 @@ void Item::Parse() {
itemType = aJson.getArrayItem(itemArr, I_TYPE)->valueint; itemType = aJson.getArrayItem(itemArr, I_TYPE)->valueint;
itemArg = aJson.getArrayItem(itemArr, I_ARG); itemArg = aJson.getArrayItem(itemArr, I_ARG);
itemVal = aJson.getArrayItem(itemArr, I_VAL); itemVal = aJson.getArrayItem(itemArr, I_VAL);
// debugSerial << F(" Item:") << itemArr->name << F(" T:") << itemType << F(" =") << getArg() << eol; // debugSerial << F(" Item:") << itemArr->name << F(" T:") << itemType << F(" =") << getArg() << endl;
} }
} }
@@ -112,7 +112,7 @@ void Item::setCmd(uint8_t cmdValue) {
if (itemCmd) if (itemCmd)
{ {
itemCmd->valueint = cmdValue; itemCmd->valueint = cmdValue;
debugSerial<<F("SetCmd:")<<cmdValue<<eol; debugSerial<<F("SetCmd:")<<cmdValue<<endl;
} }
} }
@@ -165,7 +165,7 @@ void Item::setVal(short n, int par) // Only store if VAL is array defined in c
void Item::setVal(long int par) // Only store if VAL is int (autogenerated or config-defined) void Item::setVal(long int par) // Only store if VAL is int (autogenerated or config-defined)
{ {
if (!itemVal || itemVal->type != aJson_Int) return; if (!itemVal || itemVal->type != aJson_Int) return;
debugSerial<<F(" Store ")<<F(" Val=")<<par<<eol; debugSerial<<F(" Store ")<<F(" Val=")<<par<<endl;
itemVal->valueint = par; itemVal->valueint = par;
} }
@@ -201,7 +201,7 @@ boolean Item::getEnableCMD(int delta) {
int Item::Ctrl(char * payload, boolean send){ int Item::Ctrl(char * payload, boolean send){
if (!payload) return 0; if (!payload) return 0;
// debugSerial<<F("'")<<payload<<F("'")<<eol; // debugSerial<<F("'")<<payload<<F("'")<<endl;
int cmd = txt2cmd(payload); int cmd = txt2cmd(payload);
switch (cmd) { switch (cmd) {
case 0: { case 0: {
@@ -259,7 +259,7 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) {
Par[i] = Parameters[i]; Par[i] = Parameters[i];
debugSerial<<Par[i]<<F(","); debugSerial<<Par[i]<<F(",");
} }
debugSerial<<eol; debugSerial<<endl;
int iaddr = getArg(); int iaddr = getArg();
HSVstore st; HSVstore st;
switch (cmd) { switch (cmd) {
@@ -273,7 +273,7 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) {
if (itemType != CH_GROUP) //individual threating of channels. Ignore restore command for groups if (itemType != CH_GROUP) //individual threating of channels. Ignore restore command for groups
switch (t = getCmd()) { switch (t = getCmd()) {
case CMD_HALT: //previous command was HALT ? case CMD_HALT: //previous command was HALT ?
debugSerial << F("Restored from:") << t << eol; debugSerial << F("Restored from:") << t << endl;
cmd = CMD_ON; //turning on cmd = CMD_ON; //turning on
break; break;
default: default:
@@ -284,11 +284,11 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) {
if (itemType != CH_GROUP) //individual threating of channels. Ignore restore command for groups if (itemType != CH_GROUP) //individual threating of channels. Ignore restore command for groups
switch (t = getCmd()) { switch (t = getCmd()) {
case CMD_XON: //previous command was CMD_XON ? case CMD_XON: //previous command was CMD_XON ?
debugSerial << F("Turned off from:") << t << eol; debugSerial << F("Turned off from:") << t << endl;
cmd = CMD_OFF; //turning Off cmd = CMD_OFF; //turning Off
break; break;
default: default:
debugSerial<<F("XOFF skipped. Prev cmd:")<<t<<eol; debugSerial<<F("XOFF skipped. Prev cmd:")<<t<<endl;
return -3; return -3;
} }
break; break;
@@ -302,7 +302,7 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) {
switch (itemType) { switch (itemType) {
case CH_RGBW: //only if configured VAL array case CH_RGBW: //only if configured VAL array
if (!Par[1]) itemType = CH_WHITE; if (!Par[1] && (n == 3)) itemType = CH_WHITE;
case CH_RGB: case CH_RGB:
if (n == 3) { // Full triplet passed if (n == 3) { // Full triplet passed
st.h = Par[0]; st.h = Par[0];
@@ -458,7 +458,7 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) {
} }
} // default handler } // default handler
for (short i = 0; i < params; i++) { for (short i = 0; i < params; i++) {
debugSerial<<F("Restored: ")<<i<<F("=")<<Par[i]<<eol; debugSerial<<F("Restored: ")<<i<<F("=")<<Par[i]<<endl;
} }
/* /*
} else { //Double ON - apply special preset - clean white full power } else { //Double ON - apply special preset - clean white full power
@@ -526,7 +526,7 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) {
{ {
int k; int k;
DmxWrite(iaddr + 3, k = map((100 - Par[1]) * Par[2], 0, 10000, 0, 255)); DmxWrite(iaddr + 3, k = map((100 - Par[1]) * Par[2], 0, 10000, 0, 255));
debugSerial<<F("W:")<<k<<eol; debugSerial<<F("W:")<<k<<endl;
} }
case CH_RGB: // RGB case CH_RGB: // RGB
{ {
@@ -581,7 +581,7 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) {
int k; int k;
pinMode(iaddr, OUTPUT); pinMode(iaddr, OUTPUT);
digitalWrite(iaddr, k = ((cmd == CMD_ON || cmd == CMD_XON) ? HIGH : LOW)); digitalWrite(iaddr, k = ((cmd == CMD_ON || cmd == CMD_XON) ? HIGH : LOW));
debugSerial<<F("Pin:")<<iaddr<<F("=")<<k<<eol; debugSerial<<F("Pin:")<<iaddr<<F("=")<<k<<endl;
break; break;
case CH_THERMO: case CH_THERMO:
///thermoSet(name,cmd,Par1); all cativities done - update temp & cmd ///thermoSet(name,cmd,Par1); all cativities done - update temp & cmd
@@ -617,7 +617,7 @@ int Item::Ctrl(short cmd, short n, int *Parameters, boolean send) {
if (inverse) k = map(Par[0], 100, 0, 0, 255); if (inverse) k = map(Par[0], 100, 0, 0, 255);
else k = map(Par[0], 0, 100, 0, 255); else k = map(Par[0], 0, 100, 0, 255);
analogWrite(iaddr, k); analogWrite(iaddr, k);
debugSerial<<F("Pin:")<<iaddr<<F("=")<<k<<eol; debugSerial<<F("Pin:")<<iaddr<<F("=")<<k<<endl;
break; break;
} }
#ifdef _modbus #ifdef _modbus
@@ -782,7 +782,7 @@ extern ModbusMaster node;
int Item::VacomSetFan(int8_t val, int8_t cmd) { int Item::VacomSetFan(int8_t val, int8_t cmd) {
int addr = getArg(); int addr = getArg();
debugSerial<<F("VC#")<<addr<<F("=")<<val<<eol; debugSerial<<F("VC#")<<addr<<F("=")<<val<<endl;
if (modbusBusy) { if (modbusBusy) {
mb_fail(1, addr, val, cmd); mb_fail(1, addr, val, cmd);
return -1; return -1;
@@ -808,7 +808,7 @@ int Item::VacomSetFan(int8_t val, int8_t cmd) {
int Item::VacomSetHeat(int addr, int8_t val, int8_t cmd) { int Item::VacomSetHeat(int addr, int8_t val, int8_t cmd) {
debugSerial<<F("VC_heat#")<<addr<<F("=")<<val<<F(" cmd=")<<cmd<<eol; debugSerial<<F("VC_heat#")<<addr<<F("=")<<val<<F(" cmd=")<<cmd<<endl;
if (modbusBusy) { if (modbusBusy) {
mb_fail(2, addr, val, cmd); mb_fail(2, addr, val, cmd);
return -1; return -1;
@@ -859,7 +859,7 @@ int Item::modbusDimmerSet(int addr, uint16_t _reg, int _regType, int _mask, uint
value &= 0xff; value &= 0xff;
value |= (0xff00); value |= (0xff00);
} }
debugSerial<<addr<<F("=>")<<_HEX(_reg)<<F("(T:")<<_regType<<F("):")<<_HEX(value)<<eol; debugSerial<<addr<<F("=>")<<_HEX(_reg)<<F("(T:")<<_regType<<F("):")<<_HEX(value)<<endl;
switch (_regType) { switch (_regType) {
case MODBUS_HOLDING_REG_TYPE: case MODBUS_HOLDING_REG_TYPE:
node.writeSingleRegister(_reg, value); node.writeSingleRegister(_reg, value);
@@ -906,7 +906,7 @@ int Item::checkFM() {
data = node.getResponseBuffer(j); data = node.getResponseBuffer(j);
debugSerial<<_HEX(data)<<F("-"); debugSerial<<_HEX(data)<<F("-");
} }
debugSerial<<eol; debugSerial<<endl;
int RPM; int RPM;
// aJson.addNumberToObject(out,"gsw", (int) node.getResponseBuffer(1)); // aJson.addNumberToObject(out,"gsw", (int) node.getResponseBuffer(1));
aJson.addNumberToObject(out, "V", (int) node.getResponseBuffer(2) / 100.); aJson.addNumberToObject(out, "V", (int) node.getResponseBuffer(2) / 100.);
@@ -928,7 +928,7 @@ int Item::checkFM() {
} }
} }
} else } else
debugSerial << F("Modbus polling error=") << _HEX(result) << eol; debugSerial << F("Modbus polling error=") << _HEX(result) << endl;
if (node.getResponseBuffer(0) & 8) //Active fault if (node.getResponseBuffer(0) & 8) //Active fault
{ {
@@ -946,7 +946,7 @@ int Item::checkFM() {
data = node.getResponseBuffer(j); data = node.getResponseBuffer(j);
debugSerial << data << F("-"); debugSerial << data << F("-");
} }
debugSerial << eol; debugSerial << endl;
int set = node.getResponseBuffer(0); int set = node.getResponseBuffer(0);
float ftemp, fset = set * a + b; float ftemp, fset = set * a + b;
if (set) if (set)
@@ -1037,10 +1037,10 @@ int Item::checkModbusDimmer() {
if (result == node.ku8MBSuccess) { if (result == node.ku8MBSuccess) {
data = node.getResponseBuffer(0); data = node.getResponseBuffer(0);
debugSerial << F("MB: ") << itemArr->name << F(" Val: ") << _HEX(data) << eol; debugSerial << F("MB: ") << itemArr->name << F(" Val: ") << _HEX(data) << endl;
checkModbusDimmer(data); checkModbusDimmer(data);
} else } else
debugSerial << F("Modbus polling error=") << _HEX(result) << eol; debugSerial << F("Modbus polling error=") << _HEX(result) << endl;
modbusBusy = 0; modbusBusy = 0;
// Looking 1 step ahead for modbus item, which uses same register // Looking 1 step ahead for modbus item, which uses same register
@@ -1201,7 +1201,7 @@ int Item::SendStatus(short cmd, short n, int *Par, boolean deffered) {
debugSerial<<F("Unknown cmd \n"); debugSerial<<F("Unknown cmd \n");
return -1; return -1;
} }
debugSerial<<F("Pub: ")<<addrstr<<F("->")<<valstr<<eol; debugSerial<<F("Pub: ")<<addrstr<<F("->")<<valstr<<endl;
mqttClient.publish(addrstr, valstr,true); mqttClient.publish(addrstr, valstr,true);
return 0; return 0;
} }

View File

@@ -391,7 +391,7 @@ void ip_ready_config_loaded_connecting_to_broker() {
wdt_dis(); //potential unsafe for ethernetIdle(), but needed to avoid cyclic reboot if mosquitto out of order wdt_dis(); //potential unsafe for ethernetIdle(), but needed to avoid cyclic reboot if mosquitto out of order
if (mqttClient.connect(client_id, user, password)) { if (mqttClient.connect(client_id, user, password)) {
mqttErrorRate = 0; mqttErrorRate = 0;
debugSerial<<F("connected as ")<<client_id <<eol; debugSerial<<F("connected as ")<<client_id <<endl;
wdt_en(); wdt_en();
configOk = true; configOk = true;
// ... Temporary subscribe to status topic // ... Temporary subscribe to status topic
@@ -579,7 +579,7 @@ void Changed(int i, DeviceAddress addr, float currentTemp) {
if (owEmitString) { if (owEmitString) {
printFloatValueToStr(currentTemp,valstr); printFloatValueToStr(currentTemp,valstr);
debugSerial<<owEmitString<<F("=")<<valstr<<eol; debugSerial<<owEmitString<<F("=")<<valstr<<endl;
if ((currentTemp == -127.0) || (currentTemp == 85.0) || (currentTemp == 0.0)) //ToDo: 1-w short circuit mapped to "0" celsium if ((currentTemp == -127.0) || (currentTemp == 85.0) || (currentTemp == 0.0)) //ToDo: 1-w short circuit mapped to "0" celsium
return; return;
@@ -603,7 +603,7 @@ void Changed(int i, DeviceAddress addr, float currentTemp) {
if (owItem) if (owItem)
thermoSetCurTemp(owItem, currentTemp); ///TODO: Refactore using Items interface thermoSetCurTemp(owItem, currentTemp); ///TODO: Refactore using Items interface
else debugSerial<<F("1w-item not found in config")<<eol; else debugSerial<<F("1w-item not found in config")<<endl;
} }
} }
@@ -745,7 +745,7 @@ void printConfigSummary() {
debugSerial<<F("\nudp syslog "); debugSerial<<F("\nudp syslog ");
printBool(udpSyslogArr); printBool(udpSyslogArr);
#endif #endif
debugSerial << eol; debugSerial << endl;
} }
void cmdFunctionLoad(int arg_cnt, char **args) { void cmdFunctionLoad(int arg_cnt, char **args) {
@@ -941,7 +941,7 @@ lan_status loadConfigFromHttp(int arg_cnt, char **args)
strncat(URI, "_config.json", sizeof(URI)); strncat(URI, "_config.json", sizeof(URI));
#endif #endif
#endif #endif
debugSerial<<F("Config URI: http://")<<configServer<<URI<<eol; debugSerial<<F("Config URI: http://")<<configServer<<URI<<endl;
#if defined(ARDUINO_ARCH_AVR) #if defined(ARDUINO_ARCH_AVR)
FILE *configStream; FILE *configStream;
@@ -1136,7 +1136,7 @@ void setup_main() {
} }
void printFirmwareVersionAndBuildOptions() { void printFirmwareVersionAndBuildOptions() {
debugSerial<<F("\nLazyhome.ru LightHub controller ")<<F(QUOTE(PIO_SRC_REV))<<F(" C++ version:")<<F(QUOTE(__cplusplus))<<eol; debugSerial<<F("\nLazyhome.ru LightHub controller ")<<F(QUOTE(PIO_SRC_REV))<<F(" C++ version:")<<F(QUOTE(__cplusplus))<<endl;
#ifdef CONTROLLINO #ifdef CONTROLLINO
debugSerial<<F("\n(+)CONTROLLINO"); debugSerial<<F("\n(+)CONTROLLINO");
#endif #endif
@@ -1200,7 +1200,7 @@ void printFirmwareVersionAndBuildOptions() {
#else #else
debugSerial<<F("\n(-)RESTART_LAN_ON_MQTT_ERRORS"); debugSerial<<F("\n(-)RESTART_LAN_ON_MQTT_ERRORS");
#endif #endif
debugSerial<<eol; debugSerial<<endl;
// WDT_Disable( WDT ) ; // WDT_Disable( WDT ) ;

View File

@@ -161,5 +161,3 @@
#else #else
#define W5500_ETHERNET_SHIELD #define W5500_ETHERNET_SHIELD
#endif #endif
#define eol "\n"