From 80930aa3448564d27a93ad178e18cd2f044a78b3 Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Thu, 26 Jan 2017 20:28:32 +0200 Subject: [PATCH] onewire_parser: Make step changes pass glitch detection The glitch detection code does not recognize step changes at all. Currently it re-reads the temperature values until maximum glitch threshold (two reads) and then just uses the last data. This is not ideal on noisy lines as the last data might be a glitch itself. Improve the logic so that after considering for a glitch, we compare two consequent readings and see if the delta between those two is less than the glitch threshold. If it is less, we consider the data good. This is needed in case the temperature is changing quickly and we don't get two identical values, but almost same. Signed-off-by: Timo Kokkonen --- onewire_parser.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/onewire_parser.c b/onewire_parser.c index b33ee9f..7df7aa1 100644 --- a/onewire_parser.c +++ b/onewire_parser.c @@ -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) { -- 2.45.0