Browse Source

Update EventsSensor (#1771)

- cache ms<->clockcycles conversions
- move callback invocation outside of ISR handler
- remember last event value
master
Max Prokhorov 5 years ago
committed by GitHub
parent
commit
0d76910b6d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 18 deletions
  1. +43
    -18
      code/espurna/sensors/EventSensor.h

+ 43
- 18
code/espurna/sensors/EventSensor.h View File

@ -48,8 +48,8 @@ class EventSensor : public BaseSensor {
_interrupt_mode = interrupt_mode; _interrupt_mode = interrupt_mode;
} }
void setDebounceTime(unsigned long debounce) {
_debounce = debounce;
void setDebounceTime(unsigned long ms) {
_debounce = microsecondsToClockCycles(ms * 1000);
} }
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
@ -87,6 +87,16 @@ class EventSensor : public BaseSensor {
_ready = true; _ready = true;
} }
void tick() {
if (!_trigger || !_callback) return;
if (!_trigger_flag) return;
noInterrupts();
_callback(MAGNITUDE_EVENT, _trigger_value);
_trigger_flag = false;
interrupts();
}
// Descriptive name of the sensor // Descriptive name of the sensor
String description() { String description() {
char buffer[20]; char buffer[20];
@ -114,33 +124,42 @@ class EventSensor : public BaseSensor {
// Current value for slot # index // Current value for slot # index
double value(unsigned char index) { double value(unsigned char index) {
if (index == 0) { if (index == 0) {
double value = _events;
_events = 0;
double value = _counter;
_counter = 0;
return value; return value;
}; };
if (index == 1) {
return _value;
}
return 0; return 0;
} }
// Handle interrupt calls
// Handle interrupt calls from isr[GPIO] functions
void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) { void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) {
UNUSED(gpio); UNUSED(gpio);
static unsigned long last = 0;
// clock count in 32bit value, overflowing: // clock count in 32bit value, overflowing:
// ~53s when F_CPU is 80MHz // ~53s when F_CPU is 80MHz
// ~26s when F_CPU is 160MHz // ~26s when F_CPU is 160MHz
// see: cores/esp8266/Arduino.h definitions // see: cores/esp8266/Arduino.h definitions
unsigned long ms = clockCyclesToMicroseconds(ESP.getCycleCount()) / 1000u;
if (ms - last > _debounce) {
last = ms;
_events = _events + 1;
//
// Note:
// To convert to / from normal time values, use:
// - microsecondsToClockCycles(microseconds)
// - clockCyclesToMicroseconds(cycles)
// Since the division operation on this chip is pretty slow,
// avoid doing the conversion here
unsigned long cycles = ESP.getCycleCount();
if (cycles - _last > _debounce) {
_last = cycles;
_counter += 1;
// we are handling callbacks in tick()
if (_trigger) { if (_trigger) {
if (_callback) _callback(MAGNITUDE_EVENT, digitalRead(gpio));
_trigger_value = digitalRead(gpio);
_trigger_flag = true;
} }
} }
} }
@ -168,10 +187,16 @@ class EventSensor : public BaseSensor {
// Protected // Protected
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
volatile unsigned long _events = 0;
unsigned long _debounce = EVENTS_DEBOUNCE;
unsigned char _gpio = GPIO_NONE;
volatile unsigned long _counter = 0;
unsigned char _value = 0;
unsigned long _last = 0;
unsigned long _debounce = microsecondsToClockCycles(EVENTS_DEBOUNCE * 1000);
bool _trigger = false; bool _trigger = false;
bool _trigger_flag = false;
unsigned char _trigger_value = false;
unsigned char _gpio = GPIO_NONE;
unsigned char _pin_mode = INPUT; unsigned char _pin_mode = INPUT;
unsigned char _interrupt_mode = RISING; unsigned char _interrupt_mode = RISING;


Loading…
Cancel
Save