Browse Source

Merge pull request #1395 from jackwilsdon/pulse-meter-debounce

Add debouncing to pulse meter sensor
alexa
Xose Pérez 6 years ago
committed by GitHub
parent
commit
c0514da085
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions
  1. +2
    -0
      code/espurna/config/sensors.h
  2. +1
    -0
      code/espurna/sensor.ino
  3. +13
    -1
      code/espurna/sensors/PulseMeterSensor.h

+ 2
- 0
code/espurna/config/sensors.h View File

@ -636,6 +636,8 @@
#define PULSEMETER_INTERRUPT_ON FALLING
#endif
#define PULSEMETER_DEBOUNCE 50 // Do not register pulses within less than 50 millis
//------------------------------------------------------------------------------
// PZEM004T based power monitor
// Enable support by passing PZEM004T_SUPPORT=1 build flag


+ 1
- 0
code/espurna/sensor.ino View File

@ -649,6 +649,7 @@ void _sensorLoad() {
PulseMeterSensor * sensor = new PulseMeterSensor();
sensor->setGPIO(PULSEMETER_PIN);
sensor->setEnergyRatio(PULSEMETER_ENERGY_RATIO);
sensor->setDebounceTime(PULSEMETER_DEBOUNCE);
_sensors.push_back(sensor);
}
#endif


+ 13
- 1
code/espurna/sensors/PulseMeterSensor.h View File

@ -43,6 +43,10 @@ class PulseMeterSensor : public BaseSensor {
if (ratio > 0) _ratio = ratio;
}
void setDebounceTime(unsigned long debounce) {
_debounce = debounce;
}
// ---------------------------------------------------------------------
unsigned char getGPIO() {
@ -53,6 +57,10 @@ class PulseMeterSensor : public BaseSensor {
return _ratio;
}
unsigned long getDebounceTime() {
return _debounce;
}
// ---------------------------------------------------------------------
// Sensors API
// ---------------------------------------------------------------------
@ -113,7 +121,10 @@ class PulseMeterSensor : public BaseSensor {
// Handle interrupt calls
void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) {
if (gpio == _gpio) {
static unsigned long last = 0;
if (millis() - last > _debounce) {
last = millis();
_pulses++;
}
}
@ -151,6 +162,7 @@ class PulseMeterSensor : public BaseSensor {
unsigned char _previous = GPIO_NONE;
unsigned char _gpio = GPIO_NONE;
unsigned long _ratio = PULSEMETER_ENERGY_RATIO;
unsigned long _debounce = PULSEMETER_DEBOUNCE;
double _active = 0;
double _energy = 0;


Loading…
Cancel
Save