Skip to content

Commit

Permalink
sys/phydat: Fix unit confusion
Browse files Browse the repository at this point in the history
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 uncommon but 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` and Gauss uses `G`.
  • Loading branch information
maribu committed Feb 23, 2023
1 parent be29a00 commit 3c287c0
Show file tree
Hide file tree
Showing 22 changed files with 50 additions and 47 deletions.
2 changes: 1 addition & 1 deletion drivers/adxl345/adxl345_saul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/bmx055/bmx055_saul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/fxos8700/fxos8700_saul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/hmc5883l/hmc5883l_saul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/lis2dh12/lis2dh12_saul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/lis3dh/lis3dh_saul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/lis3mdl/lis3mdl_saul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/lsm303dlhc/lsm303dlhc_saul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/lsm6dsl/lsm6dsl_saul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/mag3110/mag3110_saul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion drivers/mma7660/mma7660_saul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion drivers/mma8x5x/mma8x5x_saul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions drivers/mpu9x50/mpu9x50_saul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/qmc5883l/qmc5883l_saul.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 6 additions & 3 deletions sys/include/phydat.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,19 @@ enum {
UNIT_M2, /**< square meters */
UNIT_M3, /**< cubic meters */
/* kinetic */
UNIT_G, /**< gravitational force */
UNIT_G_FORCE, /**< gravitational force equivalent */
UNIT_G = UNIT_G_FORCE, /**< @deprecated, use UNIT_G_FORCE instead */
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 */
UNIT_GR = UNIT_GRAM, /**< @deprecated, use UNIT_GRAM instead */
/* electricity */
UNIT_A, /**< Ampere */
UNIT_V, /**< Volts */
UNIT_W, /**< Watt */
UNIT_GS, /**< gauss */
UNIT_GAUSS, /**< gauss */
UNIT_GS = UNIT_GAUSS, /**< @deprecated, use UNIT_GAUSS instead */
UNIT_T, /**< Tesla */
UNIT_DBM, /**< decibel-milliwatts */
UNIT_COULOMB, /**< coulomb */
Expand Down
6 changes: 3 additions & 3 deletions sys/include/senml/phydat.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ void phydat_to_senml_bool(senml_bool_value_t *senml, const phydat_t *phydat, con
*
* - @ref UNIT_TIME is converted to @ref SENML_UNIT_SECOND.
* - @ref UNIT_F is converted to @ref SENML_UNIT_KELVIN.
* - @ref UNIT_G is converted to @ref SENML_UNIT_METER_PER_SQUARE_SECOND.
* - @ref UNIT_G_FORCE is converted to @ref SENML_UNIT_METER_PER_SQUARE_SECOND.
* - @ref UNIT_BAR is converted to @ref SENML_UNIT_PASCAL.
* - @ref UNIT_GPM3 is converted to @ref SENML_UNIT_KILOGRAM_PER_CUBIC_METER.
* - @ref UNIT_GS is converted to @ref SENML_UNIT_TESLA.
* - @ref UNIT_GAUSS is converted to @ref SENML_UNIT_TESLA.
* - Compatible units are set to their SenML equivalent.
* - Incompatible (or unknown) units are set to @ref SENML_UNIT_NONE.
*
Expand All @@ -80,7 +80,7 @@ void phydat_to_senml_float(senml_value_t *senml, const phydat_t *phydat, const u
* - @ref UNIT_TIME is converted to @ref SENML_UNIT_SECOND.
* - @ref UNIT_BAR is converted to @ref SENML_UNIT_PASCAL.
* - @ref UNIT_GPM3 is converted to @ref SENML_UNIT_KILOGRAM_PER_CUBIC_METER.
* - @ref UNIT_GS is converted to @ref SENML_UNIT_TESLA.
* - @ref UNIT_GAUSS is converted to @ref SENML_UNIT_TESLA.
* - Compatible units are set to their SenML equivalent.
* - Incompatible (or unknown) units are set to @ref SENML_UNIT_NONE.
*
Expand Down
6 changes: 3 additions & 3 deletions sys/phydat/phydat_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
12 changes: 6 additions & 6 deletions sys/senml/phydat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions tests/phydat_dump/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ _phydat_test_t _test_vector[] =
{ .dim = 3, .dat = { .val = { 1234, 13456, -14567 }, .unit = UNIT_M, .scale = -3 } },
{ .dim = 1, .dat = { .val = { -12345 }, .unit = UNIT_M2, .scale = -5 } },
{ .dim = 1, .dat = { .val = { -12345 }, .unit = UNIT_M3, .scale = -6 } },
{ .dim = 3, .dat = { .val = { 12, 34, 5678}, .unit = UNIT_G, .scale = -2 } },
{ .dim = 3, .dat = { .val = { 12, 34, 5678}, .unit = UNIT_G_FORCE, .scale = -2 } },
{ .dim = 3, .dat = { .val = { 123, 345, 678 }, .unit = UNIT_DPS, .scale = -3 } },
{ .dim = 1, .dat = { .val = { 12345 }, .unit = UNIT_GR, .scale = -1 } },
{ .dim = 1, .dat = { .val = { 12345 }, .unit = UNIT_GRAM, .scale = -1 } },
{ .dim = 1, .dat = { .val = { 12345 }, .unit = UNIT_A, .scale = 3 } },
{ .dim = 1, .dat = { .val = { 12345 }, .unit = UNIT_V, .scale = 6 } },
{ .dim = 1, .dat = { .val = { 12345 }, .unit = UNIT_W, .scale = 7 } },
{ .dim = 1, .dat = { .val = { 12345 }, .unit = UNIT_GS, .scale = -1 } },
{ .dim = 1, .dat = { .val = { 12345 }, .unit = UNIT_GAUSS, .scale = -1 } },
{ .dim = 3, .dat = { .val = { 123, 456, 789 }, .unit = UNIT_T, .scale = -9 } },
{ .dim = 1, .dat = { .val = { 12345 }, .unit = UNIT_DBM, .scale = -3 } },
{ .dim = 1, .dat = { .val = { 12345 }, .unit = UNIT_COULOMB, .scale = 0 } },
Expand Down
8 changes: 4 additions & 4 deletions tests/phydat_dump/tests/01-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def testfunc(child):
child.expect(r'\t\[2\] -14567 mm\r\n')
child.expect(r'Data:\t -0.12345 m\^2\r\n')
child.expect(r'Data:\t -12345e-6 m\^3\r\n')
child.expect(r'Data:\t\[0\] 0.12 g\r\n')
child.expect(r'\t\[1\] 0.34 g\r\n')
child.expect(r'\t\[2\] 56.78 g\r\n')
child.expect(r'Data:\t\[0\] 0.12 gₙ\r\n')
child.expect(r'\t\[1\] 0.34 gₙ\r\n')
child.expect(r'\t\[2\] 56.78 gₙ\r\n')
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')
Expand Down
4 changes: 2 additions & 2 deletions tests/senml_phydat/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand All @@ -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),
}
Expand Down
10 changes: 5 additions & 5 deletions tests/unittests/tests-phydat/tests-phydat.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ static tdat_t data[] = {
},
{
.dim = 3,
.dat = { { 1032, 10, -509 }, UNIT_G, -3 },
.json = "{\"d\":[1.032,0.010,-0.509],\"u\":\"g\"}",
.dat = { { 1032, 10, -509 }, UNIT_G_FORCE, -3 },
.json = "{\"d\":[1.032,0.010,-0.509],\"u\":\"gₙ\"}",
},
{
.dim = 3,
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 3c287c0

Please sign in to comment.