Skip to content

Commit

Permalink
sns/event: change counter variable only in the isr
Browse files Browse the repository at this point in the history
ensure the only operation from main loop() is load
since isr handles incrementing the counter, resulting value is calculated as
offset between the current and the previous reading (always positive)
  • Loading branch information
mcspr committed Sep 30, 2020
1 parent e859f2b commit 735e5c0
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions code/espurna/sensors/EventSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,16 @@ class EventSensor : public BaseSensor {
}

void pre() override {
ETS_GPIO_INTR_DISABLE();
_last = _current;
_current = _counter;
_counter = 0ul;
ETS_GPIO_INTR_ENABLE();
}

double value(unsigned char index) override {
switch (index) {
case 0:
return _current;
return _current - _last;
case 1:
return (_current > 0) ? 1.0 : 0.0;
return (_current - _last > 0) ? 1.0 : 0.0;
default:
return 0.0;
}
Expand Down Expand Up @@ -170,7 +168,9 @@ class EventSensor : public BaseSensor {
// ---------------------------------------------------------------------

unsigned long _counter { 0ul };

unsigned long _current { 0ul };
unsigned long _last { 0ul };

unsigned long _isr_last { 0ul };
unsigned long _isr_debounce { microsecondsToClockCycles(EVENTS1_DEBOUNCE * 1000) };
Expand Down

0 comments on commit 735e5c0

Please sign in to comment.