Skip to content

Commit

Permalink
drivers/hih6130: avoid using floats
Browse files Browse the repository at this point in the history
  • Loading branch information
maribu committed Jun 8, 2023
1 parent d742513 commit bb366c4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 52 deletions.
29 changes: 15 additions & 14 deletions drivers/hih6130/hih6130.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 - 40000;
}

return status;
Expand Down
18 changes: 9 additions & 9 deletions drivers/include/hih6130.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion tests/drivers/hih6130/Makefile
Original file line number Diff line number Diff line change
@@ -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\)
Expand Down
3 changes: 2 additions & 1 deletion tests/drivers/hih6130/app.config.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# this file enables modules defined in Kconfig. Do not use this file for
# application configuration. This is only needed during migration.
CONFIG_MODULE_HIH6130=y
CONFIG_MODULE_XTIMER=y
CONFIG_MODULE_ZTIMER_MSEC=y
CONFIG_MODULE_FMT=y
53 changes: 26 additions & 27 deletions tests/drivers/hih6130/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,49 @@
#error "TEST_HIH6130_ADDR not defined"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#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;
Expand Down

0 comments on commit bb366c4

Please sign in to comment.