Browse Source

Fix overflow in CSE7766 energy calculation (#856)

rfm69
Xose Pérez 6 years ago
parent
commit
d9e025fbb7
1 changed files with 9 additions and 3 deletions
  1. +9
    -3
      code/espurna/sensors/CSE7766Sensor.h

+ 9
- 3
code/espurna/sensors/CSE7766Sensor.h View File

@ -270,10 +270,16 @@ class CSE7766Sensor : public BaseSensor {
} }
// Calculate energy // Calculate energy
static unsigned long cf_pulses_last = 0;
unsigned long cf_pulses = _data[21] << 8 | _data[22];
unsigned int difference;
static unsigned int cf_pulses_last = 0;
unsigned int cf_pulses = _data[21] << 8 | _data[22];
if (0 == cf_pulses_last) cf_pulses_last = cf_pulses; if (0 == cf_pulses_last) cf_pulses_last = cf_pulses;
_energy += (cf_pulses - cf_pulses_last) * (float) _coefP / 1000000.0;
if (cf_pulses < cf_pulses_last) {
difference = cf_pulses + (0xFFFF - cf_pulses_last) + 1;
} else {
difference = cf_pulses - cf_pulses_last;
}
_energy += difference * (float) _coefP / 1000000.0;
cf_pulses_last = cf_pulses; cf_pulses_last = cf_pulses;
} }


Loading…
Cancel
Save