diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index ea0cc01..b073710 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -29,6 +29,16 @@ e-mail anklimov@gmail.com extern PubSubClient mqttClient; //DHT dht(); +#if defined(__AVR__) +static volatile long encoder_value[6]; +#endif + +#if defined(__SAM3X8E__) +static short encoder_irq_map[54]; + static long encoder_value[54]; + static int encoders_count; +#endif + Input::Input(char * name) //Constructor { if (name) @@ -104,14 +114,12 @@ 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); + const short mega_interrupt_array[6] = {2, 3, 21, 20, 19, 18}; + short real_pin = mega_interrupt_array[interrupt_number]; + attachInterruptPinIrq(real_pin,interrupt_number); } else { Serial.print(F("IRQ:")); Serial.print(pin); @@ -121,19 +129,22 @@ void Input::encoderPoll() { #endif #if defined(__SAM3X8E__) - pinMode(pin, INPUT); - attachInterrupt(pin, onEncoderChanged, RISING); + attachInterruptPinIrq(pin,encoders_count); + encoder_irq_map[encoders_count]=pin; + encoders_count++; #endif store->logicState = 1; return; } + long encoderValue = encoder_value[pin]; + Serial.print(F("IN:"));Serial.print(pin);Serial.print(F(" Encoder type. val="));Serial.print(encoderValue); + 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); + sprintf(valstr, "%d", encoderValue); mqttClient.publish(addrstr, valstr); store->nextPollMillis = millis() + DHT_POLL_DELAY_DEFAULT; Serial.print(F(" NextPollMillis="));Serial.println(store->nextPollMillis); @@ -142,6 +153,33 @@ void Input::encoderPoll() { Serial.print(F(" No emit data!")); } +void Input::attachInterruptPinIrq(int realPin, int irq) { + pinMode(realPin, INPUT); + switch(irq){ + case 0: + attachInterrupt(realPin, onEncoderChanged0, RISING); + break; + case 1: + attachInterrupt(realPin, onEncoderChanged1, RISING); + break; + case 2: + attachInterrupt(realPin, onEncoderChanged2, RISING); + break; + case 3: + attachInterrupt(realPin, onEncoderChanged3, RISING); + break; + case 4: + attachInterrupt(realPin, onEncoderChanged4, RISING); + break; + case 5: + attachInterrupt(realPin, onEncoderChanged5, RISING); + break; + default: + Serial.print(F("Incorrect irq:"));Serial.println(irq); + break; + } +} + void Input::dht22Poll() { #ifndef DHT_DISABLE if (store->nextPollMillis > millis()) @@ -249,6 +287,62 @@ void Input::onContactChanged(int val) } } -static void Input::onEncoderChanged() { - store->aslong++; +void Input::onEncoderChanged0() { +#if defined(__SAM3X8E__) + encoder_value[encoder_irq_map[0]]++; +#endif + +#if defined(__AVR__) + encoder_value[0]++; +#endif +} + +void Input::onEncoderChanged1() { +#if defined(__SAM3X8E__) + encoder_value[encoder_irq_map[1]]++; +#endif + +#if defined(__AVR__) + encoder_value[1]++; +#endif +} + +void Input::onEncoderChanged2() { +#if defined(__SAM3X8E__) + encoder_value[encoder_irq_map[2]]++; +#endif + +#if defined(__AVR__) + encoder_value[2]++; +#endif +} + +void Input::onEncoderChanged3() { +#if defined(__SAM3X8E__) + encoder_value[encoder_irq_map[3]]++; +#endif + +#if defined(__AVR__) + encoder_value[3]++; +#endif +} + +void Input::onEncoderChanged4() { +#if defined(__SAM3X8E__) + encoder_value[encoder_irq_map[4]]++; +#endif + +#if defined(__AVR__) + encoder_value[4]++; +#endif +} + +void Input::onEncoderChanged5() { +#if defined(__SAM3X8E__) + encoder_value[encoder_irq_map[5]]++; +#endif + +#if defined(__AVR__) + encoder_value[5]++; +#endif } diff --git a/lighthub/inputs.h b/lighthub/inputs.h index 843f28b..3cd9aaa 100644 --- a/lighthub/inputs.h +++ b/lighthub/inputs.h @@ -64,8 +64,7 @@ e-mail anklimov@gmail.com extern aJsonObject *inputs; -typedef union -{ +typedef union { long int aslong; struct { int8_t reserve; @@ -76,33 +75,44 @@ typedef union unsigned long nextPollMillis; } inStore; -class Input -{ - public: - aJsonObject *inputObj; - uint8_t inType; - uint8_t pin; - inStore * store; +class Input { +public: + aJsonObject *inputObj; + uint8_t inType; + uint8_t pin; + inStore *store; - Input(int pin); - Input(aJsonObject * obj); - Input(char * name); + Input(int pin); - boolean isValid (); - void onContactChanged(int val); + Input(aJsonObject *obj); - int poll(); - protected: - void Parse(); + Input(char *name); + + boolean isValid(); + + void onContactChanged(int val); + + int poll(); + + static void onEncoderChanged0(); + static void onEncoderChanged1(); + static void onEncoderChanged2(); + static void onEncoderChanged3(); + static void onEncoderChanged4(); + static void onEncoderChanged5(); + + + +protected: + void Parse(); void contactPoll(); void dht22Poll(); - void printFloatValueToStr(float temp, char *valstr); void encoderPoll(); - void onEncoderChanged(); + void attachInterruptPinIrq(int realPin, int irq); };