Encoder. need to change irq function

This commit is contained in:
livello
2018-07-01 01:56:16 +03:00
parent 34c618db72
commit f8355cfb7b
3 changed files with 56 additions and 3 deletions

View File

@@ -90,6 +90,8 @@ int Input::poll() {
if (!isValid()) return -1;
if (inType & IN_DHT22)
dht22Poll();
else if(inType & IN_ENCODER)
encoderPoll();
/* example
else if (inType & IN_ANALOG)
analogPoll(); */
@@ -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++;
}

View File

@@ -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();
};

View File

@@ -10,8 +10,8 @@
[platformio]
src_dir = lighthub
env_default =
megaatmega2560-net
megaatmega2560
; megaatmega2560-net
; due
; esp8266
; megaatmega2560-5500