Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 3:423f2830684e, committed 2022-01-03
- Comitter:
- rollman
- Date:
- Mon Jan 03 02:40:16 2022 +0000
- Parent:
- 2:ec392b3a49f2
- Commit message:
- initial commit;
Changed in this revision
| CCS811.cpp | Show annotated file Show diff for this revision Revisions of this file |
| CCS811.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/CCS811.cpp Thu Mar 05 08:27:24 2020 +0000
+++ b/CCS811.cpp Mon Jan 03 02:40:16 2022 +0000
@@ -155,6 +155,37 @@
return i2c_buf[0];
}
+int CCS811::setEnvironment(float celsius, float humidity){
+ uint8_t humh = convertCCSFixedPointUpper(humidity);
+ uint8_t huml = convertCCSFixedPointLower(humidity);
+ uint8_t tmph = convertCCSFixedPointUpper(celsius+25);
+ uint8_t tmpl = convertCCSFixedPointLower(celsius+25);
+
+ {
+ char i2c_buf[5];
+ i2c_buf[0] = CCS811_REG_ENV_DATA;
+ i2c_buf[1] = humh;
+ i2c_buf[2] = huml;
+ i2c_buf[3] = tmph;
+ i2c_buf[4] = tmpl;
+ m_i2c.write(CCS811_I2C_ADDR, i2c_buf, 5);
+ }
+
+ int status = readStatus();
+ int err;
+ if((status & 1) != 0) {
+ char i2c_buf[2];
+
+ i2c_buf[0] = CCS811_REG_ERROR_ID;
+ m_i2c.write(CCS811_I2C_ADDR, i2c_buf, 1);
+ m_i2c.read(CCS811_I2C_ADDR, i2c_buf, 1);
+ err = i2c_buf[0];
+ return err;
+ }else{
+ return 0;
+ }
+}
+
int CCS811::readStatus(){
char i2c_buf[2];
@@ -164,6 +195,30 @@
return i2c_buf[0];
}
+uint8_t CCS811::convertCCSFixedPointUpper(float x){
+ uint8_t upper = int(x) << 2;
+ float frac = x - int(x);
+ if(frac * 2 >= 1.0){
+ upper = upper | 1;
+ }
+ return upper;
+}
+
+uint8_t CCS811::convertCCSFixedPointLower(float x){
+ uint8_t lower = 0;
+ float frac = x - int(x);
+ frac *= 2;
+ if(frac >= 1.0) frac -= 1;
+ for(int i = 7; i >= 0; i--) {
+ frac *= 2;
+ if(frac >= 1.0) {
+ lower = (lower | (1 << i));
+ frac -= 1.0;
+ }
+ }
+ return lower;
+}
+
void printerr(Serial& m_pc, int errid){
if (errid == CCS811_ERR_HWID){
m_pc.printf("CCS811_ERR_HWID\r\n");
--- a/CCS811.h Thu Mar 05 08:27:24 2020 +0000
+++ b/CCS811.h Mon Jan 03 02:40:16 2022 +0000
@@ -51,6 +51,7 @@
CCS811(I2C& i2c, Serial &pc);
int init();
int setMeasureMode(char mode);
+ int setEnvironment(float celsius, float humidity);
int readData(uint16_t *ECO2, uint16_t *TVOC);
bool checkHW();
bool softRest();
@@ -60,6 +61,8 @@
I2C m_i2c;
Serial m_pc;
private:
+ uint8_t convertCCSFixedPointUpper(float x);
+ uint8_t convertCCSFixedPointLower(float x);
};
void printerr(Serial& m_pc, int errid);