From 35c9c06eb22623468a8f205c0daae73b79081520 Mon Sep 17 00:00:00 2001 From: TheSomeMan Date: Sun, 11 Jun 2023 13:19:45 +0700 Subject: [PATCH] Data format 6: Interpret zero VOC and NOx values as invalid (#55) --- src/ruuvi_endpoint_6.c | 6 ++---- src/ruuvi_endpoint_6.h | 4 ++-- test/test_ruuvi_endpoint_6.c | 14 +++++++------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/ruuvi_endpoint_6.c b/src/ruuvi_endpoint_6.c index 2c70803..a6b5bf4 100644 --- a/src/ruuvi_endpoint_6.c +++ b/src/ruuvi_endpoint_6.c @@ -155,9 +155,8 @@ static void re_6_encode_voc (uint8_t * const p_slot, re_float val) { uint16_t coded_val = RE_6_INVALID_VOC_INDEX; - if (!isnan (val)) + if ( (!isnan (val)) && (val >= RE_6_VOC_INDEX_MIN) && (val <= RE_6_VOC_INDEX_MAX)) { - re_clip (&val, RE_6_VOC_INDEX_MIN, RE_6_VOC_INDEX_MAX); coded_val = (uint16_t) lrintf (val * RE_6_VOC_RATIO); } @@ -187,9 +186,8 @@ static void re_6_encode_nox (uint8_t * const p_slot, re_float val) { uint16_t coded_val = RE_6_INVALID_NOX_INDEX; - if (!isnan (val)) + if ( (!isnan (val)) && (val >= RE_6_NOX_INDEX_MIN) && (val <= RE_6_NOX_INDEX_MAX)) { - re_clip (&val, RE_6_NOX_INDEX_MIN, RE_6_NOX_INDEX_MAX); coded_val = (uint16_t) lrintf (val * RE_6_NOX_RATIO); } diff --git a/src/ruuvi_endpoint_6.h b/src/ruuvi_endpoint_6.h index 467ead5..cf114a9 100644 --- a/src/ruuvi_endpoint_6.h +++ b/src/ruuvi_endpoint_6.h @@ -34,10 +34,10 @@ #define RE_6_HUMIDITY_MIN (0.0f) #define RE_6_VOC_INDEX_MAX (500.0f) -#define RE_6_VOC_INDEX_MIN (0.0f) +#define RE_6_VOC_INDEX_MIN (1.0f) #define RE_6_NOX_INDEX_MAX (500.0f) -#define RE_6_NOX_INDEX_MIN (0.0f) +#define RE_6_NOX_INDEX_MIN (1.0f) #define RE_6_TEMPERATURE_MAX (102.3f) #define RE_6_TEMPERATURE_MIN (-102.3f) diff --git a/test/test_ruuvi_endpoint_6.c b/test/test_ruuvi_endpoint_6.c index 830a1b4..b912297 100644 --- a/test/test_ruuvi_endpoint_6.c +++ b/test/test_ruuvi_endpoint_6.c @@ -79,8 +79,8 @@ void test_ruuvi_endpoint_6_get_ok_max (void) .pm4p0_ppm = 1000.0, .pm10p0_ppm = 1000.0, .co2 = 40000, - .humidity_rh = 100.0, // 3e8 - .voc_index = 500, // 1f4 + .humidity_rh = 100.0, + .voc_index = 500, .nox_index = 500, .temperature_c = 102.3, .measurement_count = 65534, @@ -136,8 +136,8 @@ void test_ruuvi_endpoint_6_get_ok_min (void) .pm10p0_ppm = 0, .co2 = 0, .humidity_rh = 0, - .voc_index = 0, - .nox_index = 0, + .voc_index = 1, + .nox_index = 1, .temperature_c = -102.3, .measurement_count = 0, .address = 0xCBB8334C884F, @@ -147,7 +147,7 @@ void test_ruuvi_endpoint_6_get_ok_min (void) 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x04, 0x01, + 0x00, 0x00, 0x20, 0x14, 0x01, 0x00, 0x00, 0xCB, 0xB8, 0x33, 0x4C, 0x88, 0x4F }; @@ -281,7 +281,7 @@ void test_ruuvi_endpoint_6_underflow (void) 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x04, 0x01, + 0x00, 0x3F, 0xFF, 0xF4, 0x01, 0x00, 0x00, 0xCB, 0xB8, 0x33, 0x4C, 0x88, 0x4F }; @@ -313,7 +313,7 @@ void test_ruuvi_endpoint_6_overflow (void) 0x06, 0x27, 0x10, 0x27, 0x10, 0x27, 0x10, 0x27, 0x10, 0x9C, 0x40, - 0xFA, 0x3E, 0x9F, 0x43, 0xFF, + 0xFA, 0x3F, 0xFF, 0xF3, 0xFF, 0xFF, 0xFF, 0xCB, 0xB8, 0x33, 0x4C, 0x88, 0x4F };