diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index d5f2827..d756eb7 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -108,18 +108,23 @@ void Input::Parse() int Input::poll() { if (!isValid()) return -1; + if (0) ; + #ifndef DHT_COUNTER_DISABLE - if (inType & IN_DHT22) + else if (inType & IN_DHT22) dht22Poll(); else if (inType & IN_COUNTER) counterPoll(); else if (inType & IN_UPTIME) uptimePoll(); + #endif + else if (inType & IN_ANALOG) + analogPoll(); else contactPoll(); return 0; - #endif - contactPoll(); + + // contactPoll(); } #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->bouncebounce ++; + + if (store->bouncecurrentValue)) //confirmed change + { + store->logicState = currentInputState; + onAnalogChanged(currentInputState); + store->currentValue = currentInputState; + } + +} + + + void Input::onContactChanged(int newValue) { 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) { char buf[11]; diff --git a/lighthub/inputs.h b/lighthub/inputs.h index 7803325..cee173e 100644 --- a/lighthub/inputs.h +++ b/lighthub/inputs.h @@ -31,6 +31,7 @@ e-mail anklimov@gmail.com #define IN_UPTIME 16 #define SAME_STATE_ATTEMPTS 3 +#define ANALOG_STATE_ATTEMPTS 6 // 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" } @@ -92,6 +93,7 @@ public: boolean isValid(); void onContactChanged(int newValue); + void onAnalogChanged(int newValue); int poll(); @@ -109,6 +111,7 @@ protected: void Parse(); void contactPoll(); + void analogPoll(); void dht22Poll(); diff --git a/lighthub/item.cpp b/lighthub/item.cpp index fd2a046..fef73b5 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -84,7 +84,7 @@ void Item::Parse() { itemType = aJson.getArrayItem(itemArr, I_TYPE)->valueint; itemArg = aJson.getArrayItem(itemArr, I_ARG); 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) { itemCmd->valueint = cmdValue; - debugSerial<type != aJson_Int) return; - debugSerial<")<<_HEX(_reg)<")<<_HEX(_reg)<name << F(" Val: ") << _HEX(data) << eol; + debugSerial << F("MB: ") << itemArr->name << F(" Val: ") << _HEX(data) << endl; checkModbusDimmer(data); } else - debugSerial << F("Modbus polling error=") << _HEX(result) << eol; + debugSerial << F("Modbus polling error=") << _HEX(result) << endl; modbusBusy = 0; // 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<")<")<