]> git.itanic.dy.fi Git - rrdd/blobdiff - onewire_parser.c
onewire_parser: Make step changes pass glitch detection
[rrdd] / onewire_parser.c
index b33ee9f696850066e79623d9627cd30c55961d89..7df7aa1afbad411b74622bd49a162a9d79138e57 100644 (file)
@@ -33,24 +33,34 @@ static struct owparser_state *allocate_parser_state(const char **datastr)
        return calloc(sizeof(struct owparser_state), i);
 }
 
-static int might_be_glitch(double data, const struct owparser_state *s)
+static double max_glitch_delta(const struct owparser_state *s)
 {
-       double max_delta = 0, delta;
+       double max_delta = 0;
        int i;
 
        for (i = 0; i < ARRAY_SIZE(s->prev_delta); i++)
                max_delta = max(s->prev_delta[i], max_delta);
 
+       return max_delta;
+}
+
+static int might_be_glitch(double data, const struct owparser_state *s)
+{
+       double max_delta, delta;
+
+       max_delta = max_glitch_delta(s);
+
        /* Probably no enough data yet, so no glitch detection */
-       if (!max_delta)
+       if (max_delta == 0)
                return 0;
 
        /*
         * Simple glitch detection. If delta to previous value is more
-        * than twice as larger as some older delta, we might have a
-        * glit
+        * than twice as larger as any of the older delta, we might
+        * have a glitch
         */
        delta = fabs(data - s->prev_data);
+
        return delta > max_delta * 2;
 }
 
@@ -277,11 +287,15 @@ undefined:
                        tmp = NULL;
 
                        /*
-                        * If we read the same value as previously,
-                        * it's not a glitch
+                        * If we read the almost same value as
+                        * previously, it's not a glitch
                         */
-                       if (glitches && prev_data == data)
-                               break;
+                       if (glitches && prev_data != 85) {
+                               double d = max_glitch_delta(&state[i]);
+
+                               if (fabs(data - prev_data) <= d * 2)
+                                       break;
+                       }
 
                        if (might_be_glitch(data, &state[i]) &&
                                glitches < 2 && retries < 7) {