From e52254a653e513c0ca6a13523362521d53b38939 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Wed, 31 May 2023 19:32:49 +0200 Subject: [PATCH] drivers/hih6130: avoid using floats --- drivers/hih6130/hih6130.c | 29 ++++++++++--------- drivers/include/hih6130.h | 18 ++++++------ tests/drivers/hih6130/Makefile | 3 +- tests/drivers/hih6130/main.c | 53 +++++++++++++++++----------------- 4 files changed, 52 insertions(+), 51 deletions(-) diff --git a/drivers/hih6130/hih6130.c b/drivers/hih6130/hih6130.c index d44e4945efbc4..e5e92b5e66b05 100644 --- a/drivers/hih6130/hih6130.c +++ b/drivers/hih6130/hih6130.c @@ -93,14 +93,14 @@ static inline int hih6130_get_humidity_temperature_raw(const hih6130_t *dev, uin /* data is in big-endian format, with status bits in the first byte. */ switch (buf[0] & HIH6130_STATUS_MASK) { - case HIH6130_STATUS_OK: - status = 0; - break; - case HIH6130_STATUS_STALE_DATA: - status = 1; - break; - default: - return -2; + case HIH6130_STATUS_OK: + status = 0; + break; + case HIH6130_STATUS_STALE_DATA: + status = 1; + break; + default: + return -2; } *humidity_raw = ((buf[0] << 8) | buf[1]) & HIH6130_HUMIDITY_MASK; @@ -109,8 +109,8 @@ static inline int hih6130_get_humidity_temperature_raw(const hih6130_t *dev, uin return status; } -int hih6130_get_humidity_temperature_float(const hih6130_t *dev, - float *relative_humidity_percent, float *temperature_celsius) +int hih6130_get_humidity_temperature(const hih6130_t *dev, + int32_t *humidity_milli_percent, int32_t *temperature_milli_celsius) { uint16_t hum_raw, temp_raw; int status; @@ -127,11 +127,12 @@ int hih6130_get_humidity_temperature_float(const hih6130_t *dev, return -1; } - if (relative_humidity_percent != NULL) { - *relative_humidity_percent = hum_raw * (100.f / 16383.f); + if (humidity_milli_percent != NULL) { + *humidity_milli_percent= (int32_t)hum_raw * 100000 / 16383; } - if (temperature_celsius != NULL) { - *temperature_celsius = temp_raw * (165.f / 16383.f) - 40.f; + + if (temperature_milli_celsius != NULL) { + *temperature_milli_celsius = (int32_t)temp_raw * 165000 / 16383 - 40; } return status; diff --git a/drivers/include/hih6130.h b/drivers/include/hih6130.h index f3ae2b3bc264f..bce499e80887c 100644 --- a/drivers/include/hih6130.h +++ b/drivers/include/hih6130.h @@ -49,18 +49,18 @@ typedef struct { void hih6130_init(hih6130_t *dev, i2c_t i2c, uint8_t address); /** - * @brief Read humidity and temperature from sensor and convert to floating-point + * @brief Read humidity and temperature * - * @param[in] dev Sensor device descriptor - * @param[out] relative_humidity_percent Measured relative humidity in percent - * @param[out] temperature_celsius Measured temperature in degrees Celsius + * @param[in] dev Sensor device descriptor + * @param[out] humidity_milli_percent Relative humidity in E-03 % + * @param[out] temperature_milli_celsius Temperature in m°C * - * @return 0 on success - * @return -1 on error - * @return 1 if data is stale + * @retval 0 success + * @retval -1 error + * @retval 1 data is stale */ -int hih6130_get_humidity_temperature_float(const hih6130_t *dev, - float *relative_humidity_percent, float *temperature_celsius); +int hih6130_get_humidity_temperature(const hih6130_t *dev, + int32_t *humidity_milli_percent, int32_t *temperature_milli_celsius); #ifdef __cplusplus } diff --git a/tests/drivers/hih6130/Makefile b/tests/drivers/hih6130/Makefile index ee84e43622ba8..69ca8b4437043 100644 --- a/tests/drivers/hih6130/Makefile +++ b/tests/drivers/hih6130/Makefile @@ -1,7 +1,8 @@ include ../Makefile.drivers_common +USEMODULE += fmt USEMODULE += hih6130 -USEMODULE += xtimer +USEMODULE += ztimer_msec # set default device parameters in case they are undefined TEST_HIH6130_I2C ?= I2C_DEV\(0\) diff --git a/tests/drivers/hih6130/main.c b/tests/drivers/hih6130/main.c index 27ee8b2602b24..2c18337254aef 100644 --- a/tests/drivers/hih6130/main.c +++ b/tests/drivers/hih6130/main.c @@ -25,50 +25,49 @@ #error "TEST_HIH6130_ADDR not defined" #endif -#include -#include -#include - -#include "xtimer.h" +#include "fmt.h" #include "hih6130.h" +#include "ztimer.h" -#define SLEEP_USEC (100 * 1000U) +#define SLEEP_MSEC 100 int main(void) { hih6130_t dev; - puts("HIH6130 sensor driver test application\n"); + print_str("HIH6130 sensor driver test application\n"); - printf("Initializing HIH6130 sensor at I2C_%i, address 0x%02x... ", - TEST_HIH6130_I2C, TEST_HIH6130_ADDR); + print_str("Initializing HIH6130 sensor at I2C_"); + print_u32_dec(TEST_HIH6130_I2C); + print_str(", address 0x"); + print_u32_hex(TEST_HIH6130_ADDR); + print_str("...\n"); hih6130_init(&dev, TEST_HIH6130_I2C, TEST_HIH6130_ADDR); - puts("[OK]"); + print_str("[OK]\n"); while (1) { - float hum = 0.f; - float temp = 0.f; + int32_t hum, temp; int status; - float integral = 0.f; - float fractional; - xtimer_usleep(SLEEP_USEC); + ztimer_sleep(ZTIMER_MSEC, SLEEP_MSEC); - status = hih6130_get_humidity_temperature_float(&dev, &hum, &temp); + status = hih6130_get_humidity_temperature(&dev, &hum, &temp); if (status < 0) { - printf("Communication error: %d\n", status); + print_str("Communication error: "); + print_s32_dec(status); + print_str("\n"); continue; - } else if (status == 1) { - puts("Stale values"); } - /* Several platforms usually build with nano.specs, (without float printf) */ - /* Split value into two integer parts for printing. */ - fractional = modff(hum, &integral); - printf("humidity: %4d.%04u %%", - (int)integral, (unsigned int)abs((int)(fractional * 10000.f))); - fractional = modff(temp, &integral); - printf(" temperature: %4d.%04u C\n", - (int)integral, (unsigned int)abs((int)(fractional * 10000.f))); + else if (status == 1) { + print_str("Stale values\n"); + } + + print_str("humidity: "); + char buf[20]; + print(buf, fmt_s32_dfp(buf, hum, -3)); + print_str(" %, temperature: "); + print(buf, fmt_s32_dfp(buf, temp, -3)); + print_str(" °C\n"); } return 0;