Browse Source

Add debouncing to pulse meter sensor

alexa
Jack Wilsdon 6 years ago
parent
commit
ca59d233a6
No known key found for this signature in database GPG Key ID: D657C01A7BC820AE
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

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


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

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


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

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


Loading…
Cancel
Save