Actually works, and with mbed os 5 latest

Dependents:   CCS811-TEST Mbed-Connect-Cloud-Demo Mbed-Connect-Cloud-Demo HTTP-all-sensors ... more

Fork of CCS811 by MtM+

Files at this revision

API Documentation at this revision

Comitter:
andcor02
Date:
Thu Oct 19 11:24:59 2017 +0000
Parent:
1:4acc5b63a984
Commit message:
changed constructor, better compatibility

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
diff -r 4acc5b63a984 -r 64a96d555ef0 CCS811.cpp
--- a/CCS811.cpp	Wed Sep 13 15:31:53 2017 +0000
+++ b/CCS811.cpp	Thu Oct 19 11:24:59 2017 +0000
@@ -1,6 +1,6 @@
 #include "CCS811.h"
 
-CCS811::CCS811(I2C* i2c) : m_i2c(i2c), pc(USBTX, USBRX) {
+CCS811::CCS811(PinName sda, PinName scl) : _i2c(sda, scl) {
     
 }
 
@@ -17,8 +17,8 @@
       
     read[0] = CCS811_REG_STATUS; //0x00
     
-    m_i2c->write(CCS811_I2C_ADDR, read, 1);
-    m_i2c->read(CCS811_I2C_ADDR, hwv, 1);
+    _i2c.write(CCS811_I2C_ADDR, read, 1);
+    _i2c.read(CCS811_I2C_ADDR, hwv, 1);
     
     wait_us(50);
     
@@ -26,7 +26,7 @@
     
     wait_us(50);
     
-    m_i2c->write(CCS811_I2C_ADDR, send, 1);
+    _i2c.write(CCS811_I2C_ADDR, send, 1);
     
     wait_us(50);
 
@@ -34,8 +34,8 @@
     
     wait_us(50);
 
-    m_i2c->write(CCS811_I2C_ADDR, read, 1);
-    m_i2c->read(CCS811_I2C_ADDR, hwd, 1);
+    _i2c.write(CCS811_I2C_ADDR, read, 1);
+    _i2c.read(CCS811_I2C_ADDR, hwd, 1);
     
     wait_us(50);
 
@@ -43,7 +43,7 @@
     send[0] = CCS811_REG_MEAS_MODE; //0x01
     send[1] = CCS811_MEASUREMENT_MODE1; //0x10
    
-    m_i2c->write(CCS811_I2C_ADDR, send, 2);
+    _i2c.write(CCS811_I2C_ADDR, send, 2);
     wait_us(50);   
     
     wait_us(50);
@@ -54,8 +54,8 @@
     wait_us(50);
 
     
-    m_i2c->write(CCS811_I2C_ADDR, read, 1);
-    m_i2c->read(CCS811_I2C_ADDR, hwd, 1);
+    _i2c.write(CCS811_I2C_ADDR, read, 1);
+    _i2c.read(CCS811_I2C_ADDR, hwd, 1);
     
     wait_us(50);   
 }
@@ -67,12 +67,12 @@
     send[0] = CCS811_REG_MEAS_MODE;
     send[1] = mode;
     
-    m_i2c->write(CCS811_I2C_ADDR, send, 2);
+    _i2c.write(CCS811_I2C_ADDR, send, 2);
     
 //    send[0] = CCS811_REG_APP_START;
 //    send[1] = 0x00;
     
-//    m_i2c->write(CCS811_I2C_ADDR, send, 2);
+//    _i2c.write(CCS811_I2C_ADDR, send, 2);
     
     return 0;
 }
@@ -84,10 +84,10 @@
        
     read[0] = CCS811_REG_STATUS; //0x00
        
-    m_i2c->write(CCS811_I2C_ADDR, read, 1);
-    m_i2c->read(CCS811_I2C_ADDR, hwd, 1);
+    _i2c.write(CCS811_I2C_ADDR, read, 1);
+    _i2c.read(CCS811_I2C_ADDR, hwd, 1);
        
-    pc.printf("STATUS 0x%X\r\n", hwd[0]);
+    printf("STATUS 0x%X\r\n", hwd[0]);
     
     return 0;
 }
@@ -99,10 +99,10 @@
     
     read[0] = CCS811_REG_MEAS_MODE; //0x01
    
-    m_i2c->write(CCS811_I2C_ADDR, read, 1);
-    m_i2c->read(CCS811_I2C_ADDR, hwd, 1);
+    _i2c.write(CCS811_I2C_ADDR, read, 1);
+    _i2c.read(CCS811_I2C_ADDR, hwd, 1);
        
-    pc.printf("meas 0x%X\r\n", hwd[0]);
+    printf("meas 0x%X\r\n", hwd[0]);
     
     return 0;
 }
@@ -114,10 +114,10 @@
     
     read[0] = CCS811_REG_ERROR_ID; //0xE0
 
-    m_i2c->write(CCS811_I2C_ADDR, read, 1);
-    m_i2c->read(CCS811_I2C_ADDR, hwv, 1);
+    _i2c.write(CCS811_I2C_ADDR, read, 1);
+    _i2c.read(CCS811_I2C_ADDR, hwv, 1);
 
-    pc.printf("error 0x%X \r\n", hwv[0]);
+    printf("error 0x%X \r\n", hwv[0]);
     
     return 0;
 }
@@ -131,8 +131,8 @@
     char send[1];
 
     send[0] = CCS811_REG_ALG_RESULT_DATA;
-    m_i2c->write(CCS811_I2C_ADDR, send, 1, true);
-    m_i2c->read(CCS811_I2C_ADDR, recv, 8, false);
+    _i2c.write(CCS811_I2C_ADDR, send, 1, true);
+    _i2c.read(CCS811_I2C_ADDR, recv, 8, false);
     wait_ms(1);
 /*
     pc.printf("%X %X\r\n", recv[0], recv[1]);
@@ -157,8 +157,8 @@
     
     read[0] = CCS811_REG_HW_ID;
     
-    m_i2c->write(CCS811_I2C_ADDR, read, 1, false);
-    m_i2c->read(CCS811_I2C_ADDR, hid, 1, false);
+    _i2c.write(CCS811_I2C_ADDR, read, 1, false);
+    _i2c.read(CCS811_I2C_ADDR, hid, 1, false);
     
 //    pc.printf("%X\r\n", hid[0]);
     
@@ -177,7 +177,7 @@
      
     char rstCMD[5] = {CCS811_REG_SW_RESET, 0x11,0xE5,0x72,0x8A};
 
-    m_i2c->write(CCS811_I2C_ADDR, rstCMD, 5);
+    _i2c.write(CCS811_I2C_ADDR, rstCMD, 5);
     
     return false;
       
diff -r 4acc5b63a984 -r 64a96d555ef0 CCS811.h
--- a/CCS811.h	Wed Sep 13 15:31:53 2017 +0000
+++ b/CCS811.h	Thu Oct 19 11:24:59 2017 +0000
@@ -34,7 +34,7 @@
 
 class CCS811 {
     public:
-        CCS811(I2C* i2c);
+        CCS811(PinName sda, PinName scl);
         void init();
         int setMeasureMode(char mode);
         int readData(uint16_t *ECO2, uint16_t *TVOC);
@@ -44,8 +44,7 @@
         bool readmeas();
         bool readerror();
     protected:
-        I2C* m_i2c;
-        Serial pc;
+        I2C _i2c;
     private:
 };