From fd3f33880b42bb3901acbee83e7cb9b03598e0bd Mon Sep 17 00:00:00 2001 From: Hyungsin Date: Wed, 11 Oct 2017 18:17:04 -0700 Subject: [PATCH] drivers/hdc1000: add caching capability for hdc1000 driver --- drivers/hdc1000/hdc1000.c | 36 ++++++++++++++++++++++++++++++++++ drivers/hdc1000/hdc1000_saul.c | 4 ++-- drivers/include/hdc1000.h | 16 +++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/drivers/hdc1000/hdc1000.c b/drivers/hdc1000/hdc1000.c index eddd894b88..40eae9466e 100644 --- a/drivers/hdc1000/hdc1000.c +++ b/drivers/hdc1000/hdc1000.c @@ -33,6 +33,13 @@ #define I2C_SPEED I2C_SPEED_FAST +#ifndef HDC1000_RENEW_INTERVAL +#define HDC1000_RENEW_INTERVAL (1000000ul) +#endif + +static int16_t temp_cached, hum_cached; +static uint32_t last_read_time; + int hdc1000_init(hdc1000_t *dev, const hdc1000_params_t *params) { uint8_t reg[2]; @@ -72,6 +79,12 @@ int hdc1000_init(hdc1000_t *dev, const hdc1000_params_t *params) } i2c_release(dev->p.i2c); + /* initial read for caching operation */ + if (hdc1000_read(dev, &temp_cached, &hum_cached) != HDC1000_OK) { + return HDC1000_BUSERR; + } + last_read_time = xtimer_now_usec(); + /* all set */ return HDC1000_OK; } @@ -132,3 +145,26 @@ int hdc1000_read(const hdc1000_t *dev, int16_t *temp, int16_t *hum) xtimer_usleep(HDC1000_CONVERSION_TIME); return hdc1000_get_results(dev, temp, hum); } + + +int hdc1000_read_cached(const hdc1000_t *dev, int16_t *temp, int16_t *hum) +{ + uint32_t now = xtimer_now_usec(); + + /* check if readings are outdated */ + if (now - last_read_time > HDC1000_RENEW_INTERVAL) { + /* update last_read_time */ + if (hdc1000_read(dev, &temp_cached, &hum_cached) != HDC1000_OK) { + return HDC1000_BUSERR; + } + last_read_time = now; + } + + if (temp) { + *temp = temp_cached; + } + if (hum) { + *hum = hum_cached; + } + return HDC1000_OK; +} diff --git a/drivers/hdc1000/hdc1000_saul.c b/drivers/hdc1000/hdc1000_saul.c index 7971686fec..dca43277a9 100644 --- a/drivers/hdc1000/hdc1000_saul.c +++ b/drivers/hdc1000/hdc1000_saul.c @@ -25,7 +25,7 @@ static int read_temp(const void *dev, phydat_t *res) { - if (hdc1000_read((const hdc1000_t *)dev, &(res->val[0]), NULL) != HDC1000_OK) { + if (hdc1000_read_cached((const hdc1000_t *)dev, &(res->val[0]), NULL) != HDC1000_OK) { return -ECANCELED; } memset(&(res->val[1]), 0, 2 * sizeof(int16_t)); @@ -37,7 +37,7 @@ static int read_temp(const void *dev, phydat_t *res) static int read_hum(const void *dev, phydat_t *res) { - if (hdc1000_read((const hdc1000_t *)dev, NULL, &(res->val[0])) != HDC1000_OK) { + if (hdc1000_read_cached((const hdc1000_t *)dev, NULL, &(res->val[0])) != HDC1000_OK) { return -ECANCELED; } memset(&(res->val[1]), 0, 2 * sizeof(int16_t)); diff --git a/drivers/include/hdc1000.h b/drivers/include/hdc1000.h index 58addd354c..32644c14de 100644 --- a/drivers/include/hdc1000.h +++ b/drivers/include/hdc1000.h @@ -153,6 +153,22 @@ int hdc1000_get_results(const hdc1000_t *dev, int16_t *temp, int16_t *hum); */ int hdc1000_read(const hdc1000_t *dev, int16_t *temp, int16_t *hum); +/** + * @brief Extended read function including caching capability + * + * This function will return cached values if they are within the sampling + * period (HDC1000_RENEW_INTERVAL), or will trigger a new conversion, wait for + * the conversion to be finished and the get the results from the device. + * + * @param[in] dev device descriptor of sensor + * @param[out] temp temperature [in 100 * degree centigrade] + * @param[out] hum humidity [in 100 * percent relative] + * + * @return HDC1000_OK on success + * @return HDC1000_BUSERR on I2C communication failures + */ +int hdc1000_read_cached(const hdc1000_t *dev, int16_t *temp, int16_t *hum); + #ifdef __cplusplus } #endif