From c3e2b0816ff13da2ee7b3e4374f56fdbae608c06 Mon Sep 17 00:00:00 2001 From: Roberto Aguilar Date: Sun, 20 Mar 2011 22:08:13 -0700 Subject: [PATCH] Added check for valid data before returning. Make sure the temperature are within spec; +/- 200 for temperature and 0..100 for humidity. If the data is not correct, retry twice. It appears that there is some initial data in the buffer that is not temp or humidity when a computer is first booted up. This retry mechanism clears out the buffer until there is reasonable data in the pipe. --- lascar.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lascar.c b/lascar.c index 0827eec..df0be41 100644 --- a/lascar.c +++ b/lascar.c @@ -66,7 +66,17 @@ get_reading_r(HIDInterface* hid, char* packet, *hum = get_hum((unsigned)packet[1]); - return ret; + /* check to make sure the values found are within spec, otherwise retry */ + if(*temp >= -200.0 && *temp <= 200.0 && *hum >= 0.0 && *hum <= 100.0) { + return ret; + } else if(retries) { + /* if the values were bad, try another two times before giving up */ + /*fprintf(stderr, + "Bad values for temp (%.1f) and hum (%.1f)\n", temp, hum);*/ + return get_reading_r(hid, packet, temp, hum, get_f, 2); + } else { + return HID_RET_NOT_FOUND; + } } HIDInterface* init_termo(HIDInterface* hid) {