Skip to content

Commit

Permalink
Data format 6: Interpret zero VOC and NOx values as invalid (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSomeMan authored Jun 11, 2023
1 parent ad9ab93 commit 35c9c06
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
6 changes: 2 additions & 4 deletions src/ruuvi_endpoint_6.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/ruuvi_endpoint_6.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions test/test_ruuvi_endpoint_6.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
};
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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
};
Expand Down

0 comments on commit 35c9c06

Please sign in to comment.