38 lines
1.2 KiB
C
38 lines
1.2 KiB
C
#include "co2_level.h"
|
|
|
|
static const struct device* dev = DEVICE_DT_GET_ONE(ams_ccs811);
|
|
|
|
const int CO2_LEVEL_EMPTY_ROOM = 400; // [ppm]
|
|
|
|
enum error_code co2_lvl_init(){
|
|
enum error_code ret = init_failed;
|
|
if(device_is_ready(dev)){
|
|
ret = success;
|
|
}else{}
|
|
return ret;
|
|
}
|
|
|
|
enum error_code co2_lvl_get_value(int* holder){
|
|
struct sensor_value temp, humidity, co2;
|
|
enum error_code ret = read_failed;
|
|
int temp_value, humidity_value;
|
|
if( (success == thermo_get_value(&temp_value)) && (success == hygro_get_value(&humidity_value)) ){
|
|
// temperature conversion from deci, no function in the API
|
|
temp.val1 = temp_value/10;
|
|
temp.val2 = temp_value%10;
|
|
// humidity conversion is straight away
|
|
humidity.val1 = humidity_value;
|
|
if(
|
|
// co2 measurement requires temperature and humidity
|
|
(0 == ccs811_envdata_update(dev, &temp, &humidity)) &&
|
|
// fetch is required to update sensor read data
|
|
(0 == sensor_sample_fetch(dev)) &&
|
|
(0 == sensor_channel_get(dev, SENSOR_CHAN_CO2, &co2))
|
|
){
|
|
*holder = co2.val1; // taking only the integer part
|
|
ret = success;
|
|
}else{}
|
|
}else{}
|
|
return success;
|
|
}
|