wdoekes / pe32me162ir_pub

ProjectEnergy32: read ISKRA ME-162 electric meter through optical port using ESP8266, publish to MQTT
8 stars 2 forks source link

Use unsigned long for time values #1

Closed DigitalBrains1 closed 3 years ago

DigitalBrains1 commented 3 years ago

The computations done on time intervals were somewhat confused. Masking out the sign bit of a signed long does not result in correct interval computations (okay, if the mask was again applied to the result of a computation, it actually would, but this was not done). This resulted in a situation where WattGauge could get stuck until reset() was called. In practice, this call happens often and it would get unstuck, merely reporting wrong values for some time.

The type of millis() is unsigned anyway, As It Should Be(TM).

Along with changing the type of time values to unsigned long, I changed the type of WattGauge._p, because otherwise it would have just stuck out like a sore thumb here:

    unsigned long _t[3];  /* t0, t(end-1), t(end) */
    long _p[3];           /* P(sum) in t[n] */
    unsigned long _tlast; /* latest time, even without changed data */
DigitalBrains1 commented 3 years ago

I changed one change (I thought I had already done this but I thought wrong):

@@ -226,7 +226,7 @@ extern "C" int strcmp(const char *s1, const char *s2) throw();

 static void _test_wattgauge()
 {
-  struct { const char *const tm; unsigned long val; } data[] = {
+  struct { const char *const tm; int val; } data[] = {

These values are actually used as ints. I only changed the _test_energygauge() in the way I intended.

There's no functional difference as they were cast to int anyway.