diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index 9a512af..ea0cc01 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -90,7 +90,9 @@ int Input::poll() { if (!isValid()) return -1; if (inType & IN_DHT22) dht22Poll(); -/* example +else if(inType & IN_ENCODER) + encoderPoll(); + /* example else if (inType & IN_ANALOG) analogPoll(); */ else @@ -98,6 +100,48 @@ int Input::poll() { return 0; } +void Input::encoderPoll() { + if (store->nextPollMillis > millis()) + return; + if (store->logicState == 0) { + short real_pin; +#if defined(__AVR__) +#define interrupt_number pin + if (interrupt_number >= 0 && interrupt_number < 6) { + short mega_interrupt_array[6] = {2, 3, 21, 20, 19, 18}; + real_pin = mega_interrupt_array[interrupt_number]; + pinMode(real_pin, INPUT); + attachInterrupt(interrupt_number, onEncoderChanged, RISING); + } else { + Serial.print(F("IRQ:")); + Serial.print(pin); + Serial.print(F(" Encoder type. INCORRECT Interrupt number!!!")); + return; + } +#endif + +#if defined(__SAM3X8E__) + pinMode(pin, INPUT); + attachInterrupt(pin, onEncoderChanged, RISING); +#endif + store->logicState = 1; + return; + } + aJsonObject *emit = aJson.getObjectItem(inputObj, "emit"); + Serial.print(F("IN:"));Serial.print(pin);Serial.print(F(" Encoder type. val="));Serial.print(store->aslong); + if (emit) { + char valstr[10]; + char addrstr[100] = ""; + strcat(addrstr, emit->valuestring); + sprintf(valstr, "%d", store->aslong); + mqttClient.publish(addrstr, valstr); + store->nextPollMillis = millis() + DHT_POLL_DELAY_DEFAULT; + Serial.print(F(" NextPollMillis="));Serial.println(store->nextPollMillis); + } + else + Serial.print(F(" No emit data!")); +} + void Input::dht22Poll() { #ifndef DHT_DISABLE if (store->nextPollMillis > millis()) @@ -204,3 +248,7 @@ void Input::onContactChanged(int val) } } } + +static void Input::onEncoderChanged() { + store->aslong++; +} diff --git a/lighthub/inputs.h b/lighthub/inputs.h index 6055503..843f28b 100644 --- a/lighthub/inputs.h +++ b/lighthub/inputs.h @@ -27,6 +27,7 @@ e-mail anklimov@gmail.com #define IN_PUSH_ON 0 // PUSH - ON, Release - OFF (ovverrided by pcmd/rcmd) - DEFAULT #define IN_PUSH_TOGGLE 1 // Every physicall push toggle logical switch on/off #define IN_DHT22 4 +#define IN_ENCODER 8 #define SAME_STATE_ATTEMPTS 3 @@ -100,4 +101,8 @@ class Input void printFloatValueToStr(float temp, char *valstr); + + void encoderPoll(); + + void onEncoderChanged(); }; diff --git a/platformio.ini b/platformio.ini index 679d3d3..19eee9e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,8 +10,8 @@ [platformio] src_dir = lighthub env_default = - - megaatmega2560-net + megaatmega2560 +; megaatmega2560-net ; due ; esp8266 ; megaatmega2560-5500