Change timer reset to 70 minutes

Change timer reset to 70 minutes (#6349)
This commit is contained in:
Theo Arends 2019-10-22 14:56:06 +02:00
parent 8e657be6a9
commit b2d1e385c0

View File

@ -34,8 +34,11 @@ const char kCounterCommands[] PROGMEM = D_PRFX_COUNTER "|" // Prefix
void (* const CounterCommand[])(void) PROGMEM = void (* const CounterCommand[])(void) PROGMEM =
{ &CmndCounter, &CmndCounterType, &CmndCounterDebounce }; { &CmndCounter, &CmndCounterType, &CmndCounterDebounce };
unsigned long last_counter_timer[MAX_COUNTERS]; // Last counter time in micro seconds struct COUNTER {
uint8_t counter_no_pullup = 0; // Counter input pullup flag (1 = No pullup) uint32_t timer[MAX_COUNTERS]; // Last counter time in micro seconds
uint8_t no_pullup = 0; // Counter input pullup flag (1 = No pullup)
bool any_counter = false;
} Counter;
#ifndef ARDUINO_ESP8266_RELEASE_2_3_0 // Fix core 2.5.x ISR not in IRAM Exception #ifndef ARDUINO_ESP8266_RELEASE_2_3_0 // Fix core 2.5.x ISR not in IRAM Exception
void CounterUpdate(uint8_t index) ICACHE_RAM_ATTR; void CounterUpdate(uint8_t index) ICACHE_RAM_ATTR;
@ -47,37 +50,36 @@ void CounterUpdate4(void) ICACHE_RAM_ATTR;
void CounterUpdate(uint8_t index) void CounterUpdate(uint8_t index)
{ {
unsigned long counter_debounce_time = micros() - last_counter_timer[index -1]; uint32_t time = micros();
if (counter_debounce_time > Settings.pulse_counter_debounce * 1000) { uint32_t debounce_time = time - Counter.timer[index];
last_counter_timer[index -1] = micros(); if (debounce_time > Settings.pulse_counter_debounce * 1000) {
if (bitRead(Settings.pulse_counter_type, index -1)) { Counter.timer[index] = time;
RtcSettings.pulse_counter[index -1] = counter_debounce_time; if (bitRead(Settings.pulse_counter_type, index)) {
RtcSettings.pulse_counter[index] = debounce_time;
} else { } else {
RtcSettings.pulse_counter[index -1]++; RtcSettings.pulse_counter[index]++;
} }
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("CNTR: Interrupt %d"), index);
} }
} }
void CounterUpdate1(void) void CounterUpdate1(void)
{ {
CounterUpdate(1); CounterUpdate(0);
} }
void CounterUpdate2(void) void CounterUpdate2(void)
{ {
CounterUpdate(2); CounterUpdate(1);
} }
void CounterUpdate3(void) void CounterUpdate3(void)
{ {
CounterUpdate(3); CounterUpdate(2);
} }
void CounterUpdate4(void) void CounterUpdate4(void)
{ {
CounterUpdate(4); CounterUpdate(3);
} }
/********************************************************************************************/ /********************************************************************************************/
@ -85,7 +87,7 @@ void CounterUpdate4(void)
bool CounterPinState(void) bool CounterPinState(void)
{ {
if ((XdrvMailbox.index >= GPIO_CNTR1_NP) && (XdrvMailbox.index < (GPIO_CNTR1_NP + MAX_COUNTERS))) { if ((XdrvMailbox.index >= GPIO_CNTR1_NP) && (XdrvMailbox.index < (GPIO_CNTR1_NP + MAX_COUNTERS))) {
bitSet(counter_no_pullup, XdrvMailbox.index - GPIO_CNTR1_NP); bitSet(Counter.no_pullup, XdrvMailbox.index - GPIO_CNTR1_NP);
XdrvMailbox.index -= (GPIO_CNTR1_NP - GPIO_CNTR1); XdrvMailbox.index -= (GPIO_CNTR1_NP - GPIO_CNTR1);
return true; return true;
} }
@ -99,12 +101,27 @@ void CounterInit(void)
for (uint32_t i = 0; i < MAX_COUNTERS; i++) { for (uint32_t i = 0; i < MAX_COUNTERS; i++) {
if (pin[GPIO_CNTR1 +i] < 99) { if (pin[GPIO_CNTR1 +i] < 99) {
pinMode(pin[GPIO_CNTR1 +i], bitRead(counter_no_pullup, i) ? INPUT : INPUT_PULLUP); Counter.any_counter = true;
pinMode(pin[GPIO_CNTR1 +i], bitRead(Counter.no_pullup, i) ? INPUT : INPUT_PULLUP);
attachInterrupt(pin[GPIO_CNTR1 +i], counter_callbacks[i], FALLING); attachInterrupt(pin[GPIO_CNTR1 +i], counter_callbacks[i], FALLING);
} }
} }
} }
void CounterEverySecond(void)
{
for (uint32_t i = 0; i < MAX_COUNTERS; i++) {
if (pin[GPIO_CNTR1 +i] < 99) {
if (bitRead(Settings.pulse_counter_type, i)) {
uint32_t time = micros() - Counter.timer[i];
if (time > 4200000000) { // 70 minutes
RtcSettings.pulse_counter[i] = 4200000000; // Set Timer to max in case of no more interrupts due to stall of measured device
}
}
}
}
}
void CounterSaveState(void) void CounterSaveState(void)
{ {
for (uint32_t i = 0; i < MAX_COUNTERS; i++) { for (uint32_t i = 0; i < MAX_COUNTERS; i++) {
@ -147,9 +164,6 @@ void CounterShow(bool json)
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
} }
} }
if (bitRead(Settings.pulse_counter_type, i)) {
RtcSettings.pulse_counter[i] = 0xFFFFFFFF; // Set Timer to max in case of no more interrupts due to stall of measured device
}
} }
if (header) { if (header) {
ResponseJsonEnd(); ResponseJsonEnd();
@ -204,7 +218,11 @@ bool Xsns01(uint8_t function)
{ {
bool result = false; bool result = false;
if (Counter.any_counter) {
switch (function) { switch (function) {
case FUNC_EVERY_SECOND:
CounterEverySecond();
break;
case FUNC_JSON_APPEND: case FUNC_JSON_APPEND:
CounterShow(1); CounterShow(1);
break; break;
@ -220,6 +238,9 @@ bool Xsns01(uint8_t function)
case FUNC_COMMAND: case FUNC_COMMAND:
result = DecodeCommand(kCounterCommands, CounterCommand); result = DecodeCommand(kCounterCommands, CounterCommand);
break; break;
}
} else {
switch (function) {
case FUNC_INIT: case FUNC_INIT:
CounterInit(); CounterInit();
break; break;
@ -227,6 +248,7 @@ bool Xsns01(uint8_t function)
result = CounterPinState(); result = CounterPinState();
break; break;
} }
}
return result; return result;
} }