Browse Source

sns: explicitly load before storing new counter value

resolve #2368

- Fixes "undefined reference to `__atomic_fetch_add_4'"
  Notably, std::atomic_fetch_add(&counter, 1) would also trigger this.
  std::atomic<T>::operator+= does not allow in-place change, since it
  returns T instead of T&
- Another work reduction in the isr, treat 'value' as `counter > 0`
  instead of relying on the notion that we can sometimes read the value
  with very low counter frequency
- Fix duplicated initializations
mcspr-patch-1
Maxim Prokhorov 3 years ago
parent
commit
8252f2ae18
1 changed files with 5 additions and 8 deletions
  1. +5
    -8
      code/espurna/sensors/EventSensor.h

+ 5
- 8
code/espurna/sensors/EventSensor.h View File

@ -26,7 +26,7 @@ class EventSensor : public BaseSensor {
// ---------------------------------------------------------------------
EventSensor() {
_count = 1;
_count = 2;
_sensor_id = SENSOR_EVENTS_ID;
}
@ -78,10 +78,8 @@ class EventSensor : public BaseSensor {
// Defined outside the class body
void begin() {
pinMode(_gpio, _pin_mode);
_value = digitalRead(_gpio);
_counter = 0;
_enableInterrupts(true);
_count = 2;
_ready = true;
}
@ -111,12 +109,12 @@ class EventSensor : public BaseSensor {
// Current value for slot # index
double value(unsigned char index) {
auto copy = _counter.load();
if (index == 0) {
auto copy = _counter.load();
_counter = 0ul;
return copy;
} else if (index == 1) {
return _value.load();
return (copy > 0) ? 1.0 : 0.0;
}
return 0.0;
@ -138,9 +136,9 @@ class EventSensor : public BaseSensor {
auto cycles = ESP.getCycleCount();
if (cycles - _isr_last > _isr_debounce) {
auto counter = _counter.load();
_counter.store(counter + 1ul);
_isr_last = cycles;
_value = digitalRead(_gpio);
_counter += 1;
}
}
@ -168,7 +166,6 @@ class EventSensor : public BaseSensor {
// XXX: cannot have default values with GCC 4.8
std::atomic<unsigned long> _counter;
std::atomic<int> _value;
unsigned long _isr_last = 0;
unsigned long _isr_debounce = microsecondsToClockCycles(EVENTS1_DEBOUNCE * 1000);


Loading…
Cancel
Save