summaryrefslogtreecommitdiff
path: root/CCS811.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'CCS811.cpp')
-rw-r--r--CCS811.cpp154
1 files changed, 154 insertions, 0 deletions
diff --git a/CCS811.cpp b/CCS811.cpp
new file mode 100644
index 0000000..05b71c0
--- /dev/null
+++ b/CCS811.cpp
@@ -0,0 +1,154 @@
1#include "CCS811.h"
2
3int CCS811::begin(void)
4{
5 uint8_t id=0;
6 Wire.begin();
7 softReset();
8 delay(100);
9 if(readReg(CCS811_REG_HW_ID,&id,1) != 1){DBG("");
10 DBG("bus data access error");DBG("");
11 return ERR_DATA_BUS;DBG("");
12 }
13
14 DBG("real sensor id=");DBG(id);
15 if(id != CCS811_HW_ID){DBG("");
16 delay(1);
17 return ERR_IC_VERSION;
18 }
19 writeReg(CCS811_BOOTLOADER_APP_START, NULL, 0);
20 setMeasurementMode(0,0,eMode4);
21 setInTempHum(25, 50);
22 return ERR_OK;
23}
24
25void CCS811::softReset(){
26 uint8_t value[4] = {0x11, 0xE5, 0x72, 0x8A};
27 writeReg(CCS811_REG_SW_RESET, value, 4);
28}
29
30bool CCS811::checkDataReady()
31{
32 int8_t status[1] = {0};
33 readReg(CCS811_REG_STATUS, status, 1);
34 DBG(status[0],HEX);
35 if(!((status[0] >> 3) & 0x01))
36 return false;
37 else
38 return true;
39}
40
41uint16_t CCS811::readBaseLine(){
42 uint8_t buffer[2];
43 readReg(CCS811_REG_BASELINE, buffer, 2);
44 return buffer[0]<<8|buffer[1];
45}
46
47void CCS811::writeBaseLine(uint16_t baseLine){
48 uint8_t buffer[2];
49
50 buffer[0] = baseLine>>8;
51 buffer[1] = baseLine;
52 writeReg(CCS811_REG_BASELINE, buffer, 2);
53}
54
55void CCS811::setMeasurementMode(uint8_t thresh, uint8_t interrupt, eDRIVE_MODE_t mode){
56 uint8_t measurement[1] = {0};
57 measurement[0] = (thresh << 2) | (interrupt << 3) | (mode << 4);
58 writeReg(CCS811_REG_MEAS_MODE, measurement, 1);
59}
60
61void CCS811::setMeasCycle(eCycle_t cycle){
62 uint8_t measurement[1] = {0};
63 measurement[0] = cycle << 4;
64 writeReg(CCS811_REG_MEAS_MODE, measurement, 1);
65}
66
67uint8_t CCS811::getMeasurementMode(){
68 uint8_t meas[1] = {0};
69 readReg(CCS811_REG_MEAS_MODE, meas, 1);
70 return meas[0];
71}
72
73void CCS811::setThresholds(uint16_t lowToMed, uint16_t medToHigh)
74{
75 uint8_t buffer[] = {(uint8_t)((lowToMed >> 8) & 0xF),
76 (uint8_t)(lowToMed & 0xF),
77 (uint8_t)((medToHigh >> 8) & 0xF),
78 (uint8_t)(medToHigh & 0xF)};
79
80 writeReg(CCS811_REG_THRESHOLDS, buffer, 5);
81 uint8_t buf[1];
82 readReg(CCS811_REG_THRESHOLDS, buf, 1);
83 Serial.println(buf[0],HEX);
84}
85
86uint16_t CCS811::getCO2PPM(){
87 uint8_t buffer[8];
88 readReg(CCS811_REG_ALG_RESULT_DATA, buffer, 8);
89 eCO2 = (((uint16_t)buffer[0] << 8) | (uint16_t)buffer[1]);
90 return eCO2;
91}
92
93uint16_t CCS811::getTVOCPPB(){
94 uint8_t buffer[8];
95 readReg(CCS811_REG_ALG_RESULT_DATA, buffer, 8);
96 eTVOC = (((uint16_t)buffer[2] << 8) | (uint16_t)buffer[3]);
97 return eTVOC;
98}
99
100void CCS811::setInTempHum(float temperature, float humidity) // compensate for temperature and relative humidity
101{
102 int _temp, _rh;
103 if(temperature>0)
104 _temp = (int)temperature + 0.5; // this will round off the floating point to the nearest integer value
105 else if(temperature<0) // account for negative temperatures
106 _temp = (int)temperature - 0.5;
107 _temp = _temp + 25; // temperature high byte is stored as T+25°C in the sensor's memory so the value of byte is positive
108 _rh = (int)humidity + 0.5; // this will round off the floating point to the nearest integer value
109
110 uint8_t envData[4];
111
112 envData[0] = _rh << 1; // shift the binary number to left by 1. This is stored as a 7-bit value
113 envData[1] = 0; // most significant fractional bit. Using 0 here - gives us accuracy of +/-1%. Current firmware (2016) only supports fractional increments of 0.5
114 envData[2] = _temp << 1;
115 envData[3] = 0;
116
117 writeReg(CCS811_REG_ENV_DATA, &envData, 4);
118}
119
120void CCS811::writeReg(uint8_t reg, const void* pBuf, size_t size)
121{
122 if(pBuf == NULL){
123 DBG("pBuf ERROR!! : null pointer");
124 }
125 uint8_t * _pBuf = (uint8_t *)pBuf;
126 _pWire->beginTransmission(_deviceAddr);
127 _pWire->write(&reg, 1);
128
129 for(uint16_t i = 0; i < size; i++){
130 _pWire->write(_pBuf[i]);
131 }
132 _pWire->endTransmission();
133}
134
135uint8_t CCS811::readReg(uint8_t reg, const void* pBuf, size_t size)
136{
137 if(pBuf == NULL){
138 DBG("pBuf ERROR!! : null pointer");
139 }
140 uint8_t * _pBuf = (uint8_t *)pBuf;
141 _pWire->beginTransmission(_deviceAddr);
142 _pWire->write(&reg, 1);
143
144 if( _pWire->endTransmission() != 0){
145 return 0;
146 }
147
148 _pWire->requestFrom(_deviceAddr, (uint8_t) size);
149 for(uint16_t i = 0; i < size; i++){
150 _pBuf[i] = _pWire->read();
151 }
152 _pWire->endTransmission();
153 return size;
154}