From a7683e96ce4bb083bbc370945d6931c1c76ecb16 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Tue, 21 Feb 2023 12:19:50 +0100 Subject: [PATCH] sys/phydat: Fix unit confusion Previously, `UNIT_G` was used for g-force with the correct symbol `g`, `UNIT_GR` for gram (as in kilogram) with the incorrect symbol `G` (which would be correct for Gauss), and `UNIT_GS` for Gauss with symbol `Gs` (which is an alternative correct symbol). To avoid confusion between G-Force, Gauss, and Gram the units have been renamed to `UNIT_G_FORCE`, `UNIT_GRAM`, and `UNIT_GAUSS`. In addition, gram now uses the correct symbol `g`; which sadly is the same as for g-force. But usually there is enough context to tell them apart. --- drivers/adxl345/adxl345_saul.c | 2 +- drivers/bmx055/bmx055_saul.c | 4 ++-- drivers/fxos8700/fxos8700_saul.c | 4 ++-- drivers/hmc5883l/hmc5883l_saul.c | 2 +- drivers/lis2dh12/lis2dh12_saul.c | 2 +- drivers/lis3dh/lis3dh_saul.c | 2 +- drivers/lis3mdl/lis3mdl_saul.c | 2 +- drivers/lsm303dlhc/lsm303dlhc_saul.c | 4 ++-- drivers/lsm6dsl/lsm6dsl_saul.c | 2 +- drivers/mag3110/mag3110_saul.c | 2 +- drivers/mma7660/mma7660_saul.c | 2 +- drivers/mma8x5x/mma8x5x_saul.c | 2 +- drivers/mpu9x50/mpu9x50_saul.c | 4 ++-- drivers/qmc5883l/qmc5883l_saul.c | 2 +- sys/include/phydat.h | 6 +++--- sys/phydat/phydat_str.c | 6 +++--- sys/senml/phydat.c | 12 ++++++------ tests/phydat_dump/tests/01-run.py | 2 +- tests/senml_phydat/main.c | 4 ++-- tests/unittests/tests-phydat/tests-phydat.c | 8 ++++---- 20 files changed, 37 insertions(+), 37 deletions(-) diff --git a/drivers/adxl345/adxl345_saul.c b/drivers/adxl345/adxl345_saul.c index 432e249595c5c..594961e8d94a8 100644 --- a/drivers/adxl345/adxl345_saul.c +++ b/drivers/adxl345/adxl345_saul.c @@ -25,7 +25,7 @@ static int read_acc(const void *dev, phydat_t *res) { adxl345_read((const adxl345_t *)dev, (adxl345_data_t *)res->val); - res->unit = UNIT_G; + res->unit = UNIT_G_FORCE; res->scale = -3; return 3; } diff --git a/drivers/bmx055/bmx055_saul.c b/drivers/bmx055/bmx055_saul.c index a84cd8e15fff6..5d738b8e33428 100644 --- a/drivers/bmx055/bmx055_saul.c +++ b/drivers/bmx055/bmx055_saul.c @@ -29,7 +29,7 @@ static int read_mag(const void *dev, phydat_t *res) if (bmx055_mag_read(mydev, res->val) != BMX055_OK) { return 0; } - res->unit = UNIT_GS; + res->unit = UNIT_GAUSS; res->scale = 0; return 3; } @@ -41,7 +41,7 @@ static int read_acc(const void *dev, phydat_t *res) if (bmx055_acc_read(mydev, res->val) != BMX055_OK) { return 0; } - res->unit = UNIT_G; + res->unit = UNIT_G_FORCE; res->scale = -3; return 3; } diff --git a/drivers/fxos8700/fxos8700_saul.c b/drivers/fxos8700/fxos8700_saul.c index e9954f9461b5f..7a46b5864c135 100644 --- a/drivers/fxos8700/fxos8700_saul.c +++ b/drivers/fxos8700/fxos8700_saul.c @@ -31,7 +31,7 @@ static int read_mag(const void *dev, phydat_t *res) /* Read failure */ return -ECANCELED; } - res->unit = UNIT_GS; + res->unit = UNIT_GAUSS; res->scale = -3; return 3; } @@ -48,7 +48,7 @@ static int read_acc(const void *dev, phydat_t *res) res->scale = 0; } else { - res->unit = UNIT_G; + res->unit = UNIT_G_FORCE; if (((fxos8700_t *)dev)->p.acc_range == FXOS8700_REG_XYZ_DATA_CFG_FS__2G) { res->scale = -4; } diff --git a/drivers/hmc5883l/hmc5883l_saul.c b/drivers/hmc5883l/hmc5883l_saul.c index 63cf50de5fce1..ae1b5e2d82f94 100644 --- a/drivers/hmc5883l/hmc5883l_saul.c +++ b/drivers/hmc5883l/hmc5883l_saul.c @@ -28,7 +28,7 @@ static int read(const void *dev, phydat_t *res) res->val[0] = data.x; res->val[1] = data.y; res->val[2] = data.z; - res->unit = UNIT_GS; + res->unit = UNIT_GAUSS; res->scale = -3; return 3; } diff --git a/drivers/lis2dh12/lis2dh12_saul.c b/drivers/lis2dh12/lis2dh12_saul.c index 5fe3d319c89f2..6f4f574af9276 100644 --- a/drivers/lis2dh12/lis2dh12_saul.c +++ b/drivers/lis2dh12/lis2dh12_saul.c @@ -26,7 +26,7 @@ static int read_accelerometer(const void *dev, phydat_t *res) if (lis2dh12_read(dev, (lis2dh12_fifo_data_t*)res->val) != LIS2DH12_OK) { return 0; } - res->unit = UNIT_G; + res->unit = UNIT_G_FORCE; res->scale = -3; return 3; } diff --git a/drivers/lis3dh/lis3dh_saul.c b/drivers/lis3dh/lis3dh_saul.c index 03cb933a098a4..bbf6bd94e4aca 100644 --- a/drivers/lis3dh/lis3dh_saul.c +++ b/drivers/lis3dh/lis3dh_saul.c @@ -39,7 +39,7 @@ static int read_acc(const void *dev, phydat_t *res) res->val[2] = xyz.acc_z; /* unit: milli-G */ res->scale = -3; - res->unit = UNIT_G; + res->unit = UNIT_G_FORCE; return 3; } diff --git a/drivers/lis3mdl/lis3mdl_saul.c b/drivers/lis3mdl/lis3mdl_saul.c index 93efec2ecbf32..92c95c6a079e4 100644 --- a/drivers/lis3mdl/lis3mdl_saul.c +++ b/drivers/lis3mdl/lis3mdl_saul.c @@ -29,7 +29,7 @@ static int read_mag(const void *dev, phydat_t *res) lis3mdl_read_mag(d, (lis3mdl_3d_data_t *)res); - res->unit = UNIT_GS; + res->unit = UNIT_GAUSS; res->scale = -3; return 3; } diff --git a/drivers/lsm303dlhc/lsm303dlhc_saul.c b/drivers/lsm303dlhc/lsm303dlhc_saul.c index 461ad66f05d24..f89b31f7f85ba 100644 --- a/drivers/lsm303dlhc/lsm303dlhc_saul.c +++ b/drivers/lsm303dlhc/lsm303dlhc_saul.c @@ -34,7 +34,7 @@ static int read_acc(const void *dev, phydat_t *res) res->val[i] *= fac; } - res->unit = UNIT_G; + res->unit = UNIT_G_FORCE; res->scale = -3; return 3; } @@ -63,7 +63,7 @@ static int read_mag(const void *dev, phydat_t *res) res->val[i] = (int16_t)tmp; } - res->unit = UNIT_GS; + res->unit = UNIT_GAUSS; res->scale = -3; return 3; } diff --git a/drivers/lsm6dsl/lsm6dsl_saul.c b/drivers/lsm6dsl/lsm6dsl_saul.c index 1a51f25fe209d..39ac219cc482d 100644 --- a/drivers/lsm6dsl/lsm6dsl_saul.c +++ b/drivers/lsm6dsl/lsm6dsl_saul.c @@ -31,7 +31,7 @@ static int read_acc(const void *dev, phydat_t *res) } res->scale = -3; - res->unit = UNIT_G; + res->unit = UNIT_G_FORCE; return 3; } diff --git a/drivers/mag3110/mag3110_saul.c b/drivers/mag3110/mag3110_saul.c index 0821ce4c204a4..f81ba62487b72 100644 --- a/drivers/mag3110/mag3110_saul.c +++ b/drivers/mag3110/mag3110_saul.c @@ -28,7 +28,7 @@ static int read_mag(const void *dev, phydat_t *res) { mag3110_read((const mag3110_t *)dev, (mag3110_data_t *)res->val); - res->unit = UNIT_GS; + res->unit = UNIT_GAUSS; res->scale = 2; return 3; diff --git a/drivers/mma7660/mma7660_saul.c b/drivers/mma7660/mma7660_saul.c index 411e0d65e8631..39860ac548b0c 100644 --- a/drivers/mma7660/mma7660_saul.c +++ b/drivers/mma7660/mma7660_saul.c @@ -28,7 +28,7 @@ static int read_acc(const void *dev, phydat_t *res) { mma7660_read((const mma7660_t *)dev, (mma7660_data_t *)res->val); - res->unit = UNIT_G; + res->unit = UNIT_G_FORCE; res->scale = -3; return 3; diff --git a/drivers/mma8x5x/mma8x5x_saul.c b/drivers/mma8x5x/mma8x5x_saul.c index 8a925d740c252..5c4be8dea371b 100644 --- a/drivers/mma8x5x/mma8x5x_saul.c +++ b/drivers/mma8x5x/mma8x5x_saul.c @@ -29,7 +29,7 @@ static int read_acc(const void *dev, phydat_t *res) { mma8x5x_read((const mma8x5x_t *)dev, (mma8x5x_data_t *)res->val); - res->unit = UNIT_G; + res->unit = UNIT_G_FORCE; res->scale = -3; return 3; diff --git a/drivers/mpu9x50/mpu9x50_saul.c b/drivers/mpu9x50/mpu9x50_saul.c index 302cc4538ede9..f06b94e2ab6ea 100644 --- a/drivers/mpu9x50/mpu9x50_saul.c +++ b/drivers/mpu9x50/mpu9x50_saul.c @@ -33,7 +33,7 @@ static int read_acc(const void *dev, phydat_t *res) } res->scale = -3; - res->unit = UNIT_G; + res->unit = UNIT_G_FORCE; return 3; } @@ -59,7 +59,7 @@ static int read_mag(const void *dev, phydat_t *res) } res->scale = -2; - res->unit = UNIT_GS; + res->unit = UNIT_GAUSS; return 3; } diff --git a/drivers/qmc5883l/qmc5883l_saul.c b/drivers/qmc5883l/qmc5883l_saul.c index 266b46e155efa..ed68e4aaaf9f1 100644 --- a/drivers/qmc5883l/qmc5883l_saul.c +++ b/drivers/qmc5883l/qmc5883l_saul.c @@ -27,7 +27,7 @@ static int read(const void *dev, phydat_t *res) { int ret = qmc5883l_read((const qmc5883l_t *)dev, res->val); if ((ret == QMC5883L_OK) || (ret == QMC5883L_OVERFLOW)) { - res->unit = UNIT_GS; + res->unit = UNIT_GAUSS; res->scale = -3; return 3; } diff --git a/sys/include/phydat.h b/sys/include/phydat.h index d4083b2a25936..bf7fedfc2cd42 100644 --- a/sys/include/phydat.h +++ b/sys/include/phydat.h @@ -87,16 +87,16 @@ enum { UNIT_M2, /**< square meters */ UNIT_M3, /**< cubic meters */ /* kinetic */ - UNIT_G, /**< gravitational force */ + UNIT_G_FORCE, /**< gravitational force equivalent */ UNIT_DPS, /**< degree per second */ /* weight */ - UNIT_GR, /**< grams - not using the SI unit (kg) here to make scale + UNIT_GRAM, /**< grams - not using the SI unit (kg) here to make scale * handling simpler */ /* electricity */ UNIT_A, /**< Ampere */ UNIT_V, /**< Volts */ UNIT_W, /**< Watt */ - UNIT_GS, /**< gauss */ + UNIT_GAUSS, /**< gauss */ UNIT_T, /**< Tesla */ UNIT_DBM, /**< decibel-milliwatts */ UNIT_COULOMB, /**< coulomb */ diff --git a/sys/phydat/phydat_str.c b/sys/phydat/phydat_str.c index bad60643f3423..4d6f1485bdc15 100644 --- a/sys/phydat/phydat_str.c +++ b/sys/phydat/phydat_str.c @@ -106,14 +106,14 @@ const char *phydat_unit_to_str(uint8_t unit) case UNIT_M: return "m"; case UNIT_M2: return "m^2"; case UNIT_M3: return "m^3"; - case UNIT_G: return "g"; + case UNIT_G_FORCE: return "g"; case UNIT_DPS: return "dps"; - case UNIT_GR: return "G"; + case UNIT_GRAM: return "g"; case UNIT_A: return "A"; case UNIT_V: return "V"; case UNIT_W: return "W"; case UNIT_DBM: return "dBm"; - case UNIT_GS: return "Gs"; + case UNIT_GAUSS: return "Gs"; case UNIT_T: return "T"; case UNIT_BAR: return "Bar"; case UNIT_PA: return "Pa"; diff --git a/sys/senml/phydat.c b/sys/senml/phydat.c index 31b3c2dd4831e..d08a93a96bb01 100644 --- a/sys/senml/phydat.c +++ b/sys/senml/phydat.c @@ -24,7 +24,7 @@ static uint8_t phydat_unit_to_senml_unit(uint8_t unit) case UNIT_M: return SENML_UNIT_METER; case UNIT_M2: return SENML_UNIT_SQUARE_METER; case UNIT_M3: return SENML_UNIT_CUBIC_METER; - case UNIT_GR: return SENML_UNIT_GRAM; + case UNIT_GRAM: return SENML_UNIT_GRAM; case UNIT_A: return SENML_UNIT_AMPERE; case UNIT_V: return SENML_UNIT_VOLT; case UNIT_W: return SENML_UNIT_WATT; @@ -45,8 +45,8 @@ static uint8_t phydat_unit_to_senml_unit(uint8_t unit) /* Incompatible units */ case UNIT_TEMP_F: return SENML_UNIT_NONE; /* use K or Cel instead */ - case UNIT_GS: return SENML_UNIT_NONE; /* use T instead */ - case UNIT_G: return SENML_UNIT_NONE; /* use m/s2 instead */ + case UNIT_GAUSS: return SENML_UNIT_NONE; /* use T instead */ + case UNIT_G_FORCE: return SENML_UNIT_NONE; /* use m/s2 instead */ case UNIT_BAR: return SENML_UNIT_NONE; /* use Pa or hPa instead */ case UNIT_TIME: return SENML_UNIT_NONE; /* split into second/minute/hour */ case UNIT_DATE: return SENML_UNIT_NONE; /* split into day/month/year */ @@ -87,7 +87,7 @@ void phydat_to_senml_float(senml_value_t *senml, const phydat_t *phydat, const u value = (value + 459.67) * (5. / 9.); senml->attr.unit = SENML_UNIT_KELVIN; break; - case UNIT_G: + case UNIT_G_FORCE: /* convert gravitational acceleration to acceleration */ value *= 9.80665; senml->attr.unit = SENML_UNIT_METER_PER_SQUARE_SECOND; @@ -100,7 +100,7 @@ void phydat_to_senml_float(senml_value_t *senml, const phydat_t *phydat, const u value *= 0.001; senml->attr.unit = SENML_UNIT_KILOGRAM_PER_CUBIC_METER; break; - case UNIT_GS: + case UNIT_GAUSS: value *= 0.0001; senml->attr.unit = SENML_UNIT_TESLA; break; @@ -138,7 +138,7 @@ void phydat_to_senml_decimal(senml_value_t *senml, const phydat_t *phydat, const e -= 3; senml->attr.unit = SENML_UNIT_KILOGRAM_PER_CUBIC_METER; break; - case UNIT_GS: + case UNIT_GAUSS: e -= 4; senml->attr.unit = SENML_UNIT_TESLA; break; diff --git a/tests/phydat_dump/tests/01-run.py b/tests/phydat_dump/tests/01-run.py index 670c589e06d1a..e9b71ceb82614 100755 --- a/tests/phydat_dump/tests/01-run.py +++ b/tests/phydat_dump/tests/01-run.py @@ -39,7 +39,7 @@ def testfunc(child): child.expect(r'Data:\t\[0\] 123 mdps\r\n') child.expect(r'\t\[1\] 345 mdps\r\n') child.expect(r'\t\[2\] 678 mdps\r\n') - child.expect(r'Data:\t 1234.5 G\r\n') + child.expect(r'Data:\t 1234.5 g\r\n') child.expect(r'Data:\t 12345 kA\r\n') child.expect(r'Data:\t 12345 MV\r\n') child.expect(r'Data:\t 12345e7 W\r\n') diff --git a/tests/senml_phydat/main.c b/tests/senml_phydat/main.c index 0781912f11dc2..2704bb168dc09 100644 --- a/tests/senml_phydat/main.c +++ b/tests/senml_phydat/main.c @@ -84,7 +84,7 @@ static value_test_t value_tests[] = { .senml2 = senml_df(0, 3, SENML_UNIT_NONE), }, { - .phydat = { { 314, 0, 0 }, UNIT_G, -2 }, + .phydat = { { 314, 0, 0 }, UNIT_G_FORCE, -2 }, .senml1 = senml_f(30.792881, SENML_UNIT_METER_PER_SQUARE_SECOND), .senml2 = senml_df(314, -2, SENML_UNIT_NONE), }, @@ -99,7 +99,7 @@ static value_test_t value_tests[] = { .senml2 = senml_df(193, 2, SENML_UNIT_KILOGRAM_PER_CUBIC_METER), }, { - .phydat = { { 220, 0, 0 }, UNIT_GS, 3 }, + .phydat = { { 220, 0, 0 }, UNIT_GAUSS, 3 }, .senml1 = senml_f(22, SENML_UNIT_TESLA), .senml2 = senml_df(220, -1, SENML_UNIT_TESLA), } diff --git a/tests/unittests/tests-phydat/tests-phydat.c b/tests/unittests/tests-phydat/tests-phydat.c index 504f029db5814..e8ab3f274d97b 100644 --- a/tests/unittests/tests-phydat/tests-phydat.c +++ b/tests/unittests/tests-phydat/tests-phydat.c @@ -58,7 +58,7 @@ static tdat_t data[] = { }, { .dim = 3, - .dat = { { 1032, 10, -509 }, UNIT_G, -3 }, + .dat = { { 1032, 10, -509 }, UNIT_G_FORCE, -3 }, .json = "{\"d\":[1.032,0.010,-0.509],\"u\":\"g\"}", }, { @@ -204,14 +204,14 @@ static void test_unitstr__success(void) TEST_ASSERT_EQUAL_STRING("m", phydat_unit_to_str_verbose(UNIT_M)); TEST_ASSERT_EQUAL_STRING("m^2", phydat_unit_to_str_verbose(UNIT_M2)); TEST_ASSERT_EQUAL_STRING("m^3", phydat_unit_to_str_verbose(UNIT_M3)); - TEST_ASSERT_EQUAL_STRING("g", phydat_unit_to_str_verbose(UNIT_G)); + TEST_ASSERT_EQUAL_STRING("g", phydat_unit_to_str_verbose(UNIT_G_FORCE)); TEST_ASSERT_EQUAL_STRING("dps", phydat_unit_to_str_verbose(UNIT_DPS)); - TEST_ASSERT_EQUAL_STRING("G", phydat_unit_to_str_verbose(UNIT_GR)); + TEST_ASSERT_EQUAL_STRING("g", phydat_unit_to_str_verbose(UNIT_GRAM)); TEST_ASSERT_EQUAL_STRING("A", phydat_unit_to_str_verbose(UNIT_A)); TEST_ASSERT_EQUAL_STRING("V", phydat_unit_to_str_verbose(UNIT_V)); TEST_ASSERT_EQUAL_STRING("W", phydat_unit_to_str_verbose(UNIT_W)); TEST_ASSERT_EQUAL_STRING("dBm", phydat_unit_to_str_verbose(UNIT_DBM)); - TEST_ASSERT_EQUAL_STRING("Gs", phydat_unit_to_str_verbose(UNIT_GS)); + TEST_ASSERT_EQUAL_STRING("Gs", phydat_unit_to_str_verbose(UNIT_GAUSS)); TEST_ASSERT_EQUAL_STRING("T", phydat_unit_to_str_verbose(UNIT_T)); TEST_ASSERT_EQUAL_STRING("Bar", phydat_unit_to_str_verbose(UNIT_BAR)); TEST_ASSERT_EQUAL_STRING("Pa", phydat_unit_to_str_verbose(UNIT_PA));