1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers/hih6130: avoid using floats

This commit is contained in:
Marian Buschsieweke 2023-05-31 19:32:49 +02:00
parent d742513b62
commit bb366c476a
No known key found for this signature in database
GPG Key ID: CB8E3238CE715A94
5 changed files with 54 additions and 52 deletions

View File

@ -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. */ /* data is in big-endian format, with status bits in the first byte. */
switch (buf[0] & HIH6130_STATUS_MASK) { switch (buf[0] & HIH6130_STATUS_MASK) {
case HIH6130_STATUS_OK: case HIH6130_STATUS_OK:
status = 0; status = 0;
break; break;
case HIH6130_STATUS_STALE_DATA: case HIH6130_STATUS_STALE_DATA:
status = 1; status = 1;
break; break;
default: default:
return -2; return -2;
} }
*humidity_raw = ((buf[0] << 8) | buf[1]) & HIH6130_HUMIDITY_MASK; *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; return status;
} }
int hih6130_get_humidity_temperature_float(const hih6130_t *dev, int hih6130_get_humidity_temperature(const hih6130_t *dev,
float *relative_humidity_percent, float *temperature_celsius) int32_t *humidity_milli_percent, int32_t *temperature_milli_celsius)
{ {
uint16_t hum_raw, temp_raw; uint16_t hum_raw, temp_raw;
int status; int status;
@ -127,11 +127,12 @@ int hih6130_get_humidity_temperature_float(const hih6130_t *dev,
return -1; return -1;
} }
if (relative_humidity_percent != NULL) { if (humidity_milli_percent != NULL) {
*relative_humidity_percent = hum_raw * (100.f / 16383.f); *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; return status;

View File

@ -49,18 +49,18 @@ typedef struct {
void hih6130_init(hih6130_t *dev, i2c_t i2c, uint8_t address); 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[in] dev Sensor device descriptor
* @param[out] relative_humidity_percent Measured relative humidity in percent * @param[out] humidity_milli_percent Relative humidity in E-03 %
* @param[out] temperature_celsius Measured temperature in degrees Celsius * @param[out] temperature_milli_celsius Temperature in m°C
* *
* @return 0 on success * @retval 0 success
* @return -1 on error * @retval -1 error
* @return 1 if data is stale * @retval 1 data is stale
*/ */
int hih6130_get_humidity_temperature_float(const hih6130_t *dev, int hih6130_get_humidity_temperature(const hih6130_t *dev,
float *relative_humidity_percent, float *temperature_celsius); int32_t *humidity_milli_percent, int32_t *temperature_milli_celsius);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1,7 +1,8 @@
include ../Makefile.drivers_common include ../Makefile.drivers_common
USEMODULE += fmt
USEMODULE += hih6130 USEMODULE += hih6130
USEMODULE += xtimer USEMODULE += ztimer_msec
# set default device parameters in case they are undefined # set default device parameters in case they are undefined
TEST_HIH6130_I2C ?= I2C_DEV\(0\) TEST_HIH6130_I2C ?= I2C_DEV\(0\)

View File

@ -1,4 +1,5 @@
# this file enables modules defined in Kconfig. Do not use this file for # this file enables modules defined in Kconfig. Do not use this file for
# application configuration. This is only needed during migration. # application configuration. This is only needed during migration.
CONFIG_MODULE_HIH6130=y CONFIG_MODULE_HIH6130=y
CONFIG_MODULE_XTIMER=y CONFIG_MODULE_ZTIMER_MSEC=y
CONFIG_MODULE_FMT=y

View File

@ -25,50 +25,49 @@
#error "TEST_HIH6130_ADDR not defined" #error "TEST_HIH6130_ADDR not defined"
#endif #endif
#include <stdio.h> #include "fmt.h"
#include <stdlib.h>
#include <math.h>
#include "xtimer.h"
#include "hih6130.h" #include "hih6130.h"
#include "ztimer.h"
#define SLEEP_USEC (100 * 1000U) #define SLEEP_MSEC 100
int main(void) int main(void)
{ {
hih6130_t dev; 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... ", print_str("Initializing HIH6130 sensor at I2C_");
TEST_HIH6130_I2C, TEST_HIH6130_ADDR); 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); hih6130_init(&dev, TEST_HIH6130_I2C, TEST_HIH6130_ADDR);
puts("[OK]"); print_str("[OK]\n");
while (1) { while (1) {
float hum = 0.f; int32_t hum, temp;
float temp = 0.f;
int status; 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) { if (status < 0) {
printf("Communication error: %d\n", status); print_str("Communication error: ");
print_s32_dec(status);
print_str("\n");
continue; continue;
} else if (status == 1) {
puts("Stale values");
} }
/* Several platforms usually build with nano.specs, (without float printf) */ else if (status == 1) {
/* Split value into two integer parts for printing. */ print_str("Stale values\n");
fractional = modff(hum, &integral); }
printf("humidity: %4d.%04u %%",
(int)integral, (unsigned int)abs((int)(fractional * 10000.f))); print_str("humidity: ");
fractional = modff(temp, &integral); char buf[20];
printf(" temperature: %4d.%04u C\n", print(buf, fmt_s32_dfp(buf, hum, -3));
(int)integral, (unsigned int)abs((int)(fractional * 10000.f))); print_str(" %, temperature: ");
print(buf, fmt_s32_dfp(buf, temp, -3));
print_str(" °C\n");
} }
return 0; return 0;