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

drivers/hdc1000: add caching capability for hdc1000 driver

This commit is contained in:
Hyungsin 2017-10-11 18:17:04 -07:00
parent 6404bb75cf
commit fd3f33880b
3 changed files with 54 additions and 2 deletions

View File

@ -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;
}

View File

@ -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));

View File

@ -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