diff options
author | Steven <steven.vasilogianis@gmail.com> | 2021-10-05 21:24:08 -0400 |
---|---|---|
committer | Steven <steven.vasilogianis@gmail.com> | 2021-10-05 22:50:39 -0400 |
commit | 7ab9abd11e3ebe94f307b64728738b0789f97719 (patch) | |
tree | d157fa272450cc0236562ca7bfadbd72b6f8cfe2 /src/main/CCS811.cpp | |
parent | 0848a13c38d2f54c91a1e98e114fd49e3f4c8a70 (diff) | |
parent | a6ac5a6e71fa4feadc60b9932eb898d59ca2f337 (diff) |
WIP on master: 0848a13 Add photoresistor support
Diffstat (limited to 'src/main/CCS811.cpp')
-rw-r--r-- | src/main/CCS811.cpp | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/src/main/CCS811.cpp b/src/main/CCS811.cpp new file mode 100644 index 0000000..05b71c0 --- /dev/null +++ b/src/main/CCS811.cpp | |||
@@ -0,0 +1,154 @@ | |||
1 | #include "CCS811.h" | ||
2 | |||
3 | int 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 | |||
25 | void CCS811::softReset(){ | ||
26 | uint8_t value[4] = {0x11, 0xE5, 0x72, 0x8A}; | ||
27 | writeReg(CCS811_REG_SW_RESET, value, 4); | ||
28 | } | ||
29 | |||
30 | bool 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 | |||
41 | uint16_t CCS811::readBaseLine(){ | ||
42 | uint8_t buffer[2]; | ||
43 | readReg(CCS811_REG_BASELINE, buffer, 2); | ||
44 | return buffer[0]<<8|buffer[1]; | ||
45 | } | ||
46 | |||
47 | void 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 | |||
55 | void 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 | |||
61 | void 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 | |||
67 | uint8_t CCS811::getMeasurementMode(){ | ||
68 | uint8_t meas[1] = {0}; | ||
69 | readReg(CCS811_REG_MEAS_MODE, meas, 1); | ||
70 | return meas[0]; | ||
71 | } | ||
72 | |||
73 | void 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 | |||
86 | uint16_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 | |||
93 | uint16_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 | |||
100 | void 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 | |||
120 | void 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(®, 1); | ||
128 | |||
129 | for(uint16_t i = 0; i < size; i++){ | ||
130 | _pWire->write(_pBuf[i]); | ||
131 | } | ||
132 | _pWire->endTransmission(); | ||
133 | } | ||
134 | |||
135 | uint8_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(®, 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 | } | ||