Added CCS811 library.
This commit is contained in:
parent
f18edbb049
commit
c62d13149f
|
@ -50,6 +50,7 @@ set(C_LIBRARIES
|
|||
"lvgl"
|
||||
"st7789"
|
||||
"bme280"
|
||||
"ccs811"
|
||||
)
|
||||
|
||||
add_subdirectory(lib)
|
||||
|
|
|
@ -25,3 +25,11 @@ set(BME280_DHT_SOURCES
|
|||
|
||||
add_library(bme280 STATIC ${BME280_DHT_SOURCES})
|
||||
target_compile_definitions(bme280 PRIVATE ${BME280_DHT_DEFINES})
|
||||
|
||||
set(CCS811_TVOC_SOURCES
|
||||
"ccs811_tvoc/ccs811_tvoc.c"
|
||||
)
|
||||
|
||||
add_library(ccs811 STATIC ${CCS811_TVOC_SOURCES})
|
||||
target_compile_definitions(ccs811 PRIVATE ${CCS811_TVOC_DEFINES})
|
||||
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
#include "ccs811_tvoc.h"
|
||||
|
||||
ccs811_ret_t _ccs811_sw_reset(ccs811_t *ccs);
|
||||
ccs811_ret_t _ccs811_app_start(ccs811_t *ccs);
|
||||
ccs811_ret_t _ccs811_set_mode(ccs811_t *ccs, uint8_t mode);
|
||||
|
||||
|
||||
ccs811_ret_t ccs811_init(ccs811_t *ccs, uint8_t addr, void *arg) {
|
||||
if(_ccs811_sw_reset(ccs) == CCS_FAIL) return CCS_FAIL;
|
||||
ccs->cb.delay_ms_cb(ccs->user_data, 5);
|
||||
if(_ccs811_app_start(ccs) == CCS_FAIL) return CCS_FAIL;
|
||||
ccs->cb.delay_ms_cb(ccs->user_data, 5);
|
||||
if(_ccs811_set_mode(ccs, 0x01) == CCS_FAIL) return CCS_FAIL;
|
||||
return CCS_OK;
|
||||
}
|
||||
|
||||
ccs811_ret_t ccs811_set_env_data(ccs811_t *ccs, double temperature, double humidity) {
|
||||
uint16_t temp_in_512 = (uint16_t)((temperature + 25.0) * 512.0);
|
||||
uint16_t humid_in_512 = (uint16_t)(humidity * 512.0);
|
||||
uint8_t env_data[4] = {humid_in_512 >> 0x08, humid_in_512 & 0xFF, temp_in_512 >> 0x08, temp_in_512 & 0xFF};
|
||||
return ccs->cb.write_register_cb(ccs->user_data, 0x05, env_data, 0x04);
|
||||
}
|
||||
|
||||
ccs811_ret_t ccs811_read_baseline(ccs811_t *ccs, uint8_t *baseline) {
|
||||
ccs->cb.read_register_cb(ccs->user_data, 0x11, baseline, 0x02);
|
||||
return CCS_OK;
|
||||
}
|
||||
|
||||
ccs811_ret_t ccs811_store_baseline(ccs811_t *ccs, uint8_t *baseline) {
|
||||
ccs->cb.write_register_cb(ccs->user_data, 0x11, baseline, 0x02);
|
||||
return CCS_OK;
|
||||
}
|
||||
|
||||
ccs811_ret_t ccs811_measure(ccs811_t *ccs, ccs811_result_t *result) {
|
||||
uint8_t alg_result[8];
|
||||
uint8_t loop_count = 0;
|
||||
do {
|
||||
ccs->cb.read_register_cb(ccs->user_data, 0x02, alg_result, 0x08);
|
||||
ccs->cb.delay_ms_cb(ccs->user_data, 100);
|
||||
loop_count++;
|
||||
} while(((alg_result[4] & 0x08) == 0) && (loop_count < 12));
|
||||
result->eco2 = alg_result[0] << 0x08 | alg_result[1];
|
||||
result->tvoc = alg_result[2] << 0x08 | alg_result[3];
|
||||
return CCS_OK;
|
||||
}
|
||||
|
||||
ccs811_ret_t _ccs811_sw_reset(ccs811_t *ccs) {
|
||||
uint8_t magic[4] = {0x11, 0xE5, 0x72, 0x8A};
|
||||
return ccs->cb.write_register_cb(ccs->user_data, 0xFF, magic, 0x04);
|
||||
}
|
||||
|
||||
ccs811_ret_t _ccs811_app_start(ccs811_t *ccs) {
|
||||
uint8_t status;
|
||||
if(ccs->cb.read_register_cb(ccs->user_data, 0x00, &status, 0x01) == CCS_FAIL) {
|
||||
return CCS_FAIL;
|
||||
}
|
||||
if(status & 0x10 == 0) {
|
||||
return CCS_FAIL;
|
||||
}
|
||||
return ccs->cb.write_register_cb(ccs->user_data, 0xF4, (uint8_t *)0x00, 0x00);
|
||||
}
|
||||
|
||||
ccs811_ret_t _ccs811_set_mode(ccs811_t *ccs, uint8_t mode) {
|
||||
uint8_t drive_mode = mode << 0x04;
|
||||
return ccs->cb.write_register_cb(ccs->user_data, 0x01, &drive_mode, 0x01);
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
#ifndef __I2CDEV_CCS811_H
|
||||
#define __I2CDEV_CCS811_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define CCS_OK 0
|
||||
#define CCS_FAIL -1
|
||||
|
||||
typedef int ccs811_ret_t;
|
||||
|
||||
typedef struct {
|
||||
uint16_t eco2;
|
||||
uint16_t tvoc;
|
||||
} ccs811_result_t;
|
||||
|
||||
typedef struct {
|
||||
ccs811_ret_t (*set_nwake_cb)(void *handle, uint8_t is_awake);
|
||||
ccs811_ret_t (*delay_ms_cb)(void *handle, uint32_t msec);
|
||||
ccs811_ret_t (*read_register_cb)(void *handle, uint8_t reg, uint8_t *data, uint8_t len);
|
||||
ccs811_ret_t (*write_register_cb)(void *handle, uint8_t reg, uint8_t *data, uint8_t len);
|
||||
} ccs811_cb_t;
|
||||
|
||||
typedef struct {
|
||||
uint16_t baseline;
|
||||
void *user_data;
|
||||
ccs811_cb_t cb;
|
||||
} ccs811_t;
|
||||
|
||||
ccs811_ret_t ccs811_init(ccs811_t *ccs, uint8_t addr, void *arg);
|
||||
ccs811_ret_t ccs811_set_env_data(ccs811_t *ccs, double temperature, double humidity);
|
||||
ccs811_ret_t ccs811_read_baseline(ccs811_t *ccs, uint8_t *baseline);
|
||||
ccs811_ret_t ccs811_store_baseline(ccs811_t *ccs, uint8_t *baseline);
|
||||
ccs811_ret_t ccs811_measure(ccs811_t *ccs, ccs811_result_t *result);
|
||||
|
||||
#endif
|
|
@ -53,4 +53,4 @@ void *user_dht_task(void *arguments) {
|
|||
USER_LOG(USER_LOG_INFO, "BME: %.02f, %.02f, %.02f", res.temperature, res.humidity, res.pressure);
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue