drivers: add driver for CCS811 gas sensor

This commit is contained in:
Schorcht 2018-10-01 09:04:31 +02:00 committed by Alexandre Abadie
parent 74e1ab220c
commit 98af7d3d62

View File

@ -23,45 +23,44 @@
#include "ccs811.h" #include "ccs811.h"
static uint16_t _iaq_tvoc = 0; static bool _data_ready = false;
static uint16_t _iaq_eco2 = 0;
static int read(const ccs811_t *dev) static int read(const ccs811_t *dev, uint16_t *iaq_tvoc, uint16_t *iaq_eco2)
{ {
/* check whether new data can be read */ if (!_data_ready) {
int res = ccs811_data_ready((ccs811_t *)dev); /* if no data were ready yet, test for new data */
if (ccs811_data_ready((ccs811_t *)dev) == CCS811_OK) {
if (res != CCS811_OK) { _data_ready = true;
return res; }
else {
return -ECANCELED;
}
} }
/* read new data and save them to local storage */ int res = ccs811_read_iaq((ccs811_t *)dev, iaq_tvoc, iaq_eco2, NULL, NULL);
return ccs811_read_iaq((ccs811_t *)dev, &_iaq_tvoc, &_iaq_eco2, 0, 0);
/* in case of CCS811_ERROR_NO_NEW_DATA last valid data are returned */
return (res == CCS811_OK ||
res == -CCS811_ERROR_NO_NEW_DATA) ? 0 : -ECANCELED;
} }
static int read_tvoc(const void *dev, phydat_t *res) static int read_tvoc(const void *dev, phydat_t *res)
{ {
/* read new data if available */ if (read(dev, (uint16_t*)&res->val[0], NULL) != 0) {
read(dev); return -ECANCELED;
}
/* fill data from local storage */
res->val[0] = _iaq_tvoc;
res->unit = UNIT_PPB; res->unit = UNIT_PPB;
res->scale = 0; res->scale = 0;
return 1; return 1;
} }
static int read_eco2(const void *dev, phydat_t *res) static int read_eco2(const void *dev, phydat_t *res)
{ {
/* read new data if available */ if (read(dev, NULL, (uint16_t*)&res->val[0]) != 0) {
read(dev); return -ECANCELED;
}
/* fill data from local storage */
res->val[0] = _iaq_eco2;
res->unit = UNIT_PPM; res->unit = UNIT_PPM;
res->scale = 0; res->scale = 0;
return 1; return 1;
} }