SparkFunCCS811.h (4671B)
1 /****************************************************************************** 2 SparkFunCCS811.h 3 CCS811 Arduino library 4 5 Marshall Taylor @ SparkFun Electronics 6 Nathan Seidle @ SparkFun Electronics 7 8 April 4, 2017 9 10 https://github.com/sparkfun/CCS811_Air_Quality_Breakout 11 https://github.com/sparkfun/SparkFun_CCS811_Arduino_Library 12 13 Resources: 14 Uses Wire.h for i2c operation 15 16 Development environment specifics: 17 Arduino IDE 1.8.1 18 19 This code is released under the [MIT License](http://opensource.org/licenses/MIT). 20 21 Please review the LICENSE.md file included with this example. If you have any questions 22 or concerns with licensing, please contact techsupport@sparkfun.com. 23 24 Distributed as-is; no warranty is given. 25 ******************************************************************************/ 26 27 #ifndef __CCS811_H__ 28 #define __CCS811_H__ 29 30 #include "stdint.h" 31 #include <Wire.h> 32 33 //Register addresses 34 #define CSS811_STATUS 0x00 35 #define CSS811_MEAS_MODE 0x01 36 #define CSS811_ALG_RESULT_DATA 0x02 37 #define CSS811_RAW_DATA 0x03 38 #define CSS811_ENV_DATA 0x05 39 #define CSS811_NTC 0x06 //NTC compensation no longer supported 40 #define CSS811_THRESHOLDS 0x10 41 #define CSS811_BASELINE 0x11 42 #define CSS811_HW_ID 0x20 43 #define CSS811_HW_VERSION 0x21 44 #define CSS811_FW_BOOT_VERSION 0x23 45 #define CSS811_FW_APP_VERSION 0x24 46 #define CSS811_ERROR_ID 0xE0 47 #define CSS811_APP_START 0xF4 48 #define CSS811_SW_RESET 0xFF 49 50 //This is the core operational class of the driver. 51 // CCS811Core contains only read and write operations towards the sensor. 52 // To use the higher level functions, use the class CCS811 which inherits 53 // this class. 54 55 class CCS811Core 56 { 57 public: 58 // Return values 59 typedef enum 60 { 61 CCS811_Stat_SUCCESS, 62 CCS811_Stat_ID_ERROR, 63 CCS811_Stat_I2C_ERROR, 64 CCS811_Stat_INTERNAL_ERROR, 65 CCS811_Stat_NUM, 66 CCS811_Stat_GENERIC_ERROR 67 //... 68 } CCS811_Status_e; 69 70 CCS811Core(uint8_t); 71 ~CCS811Core() = default; 72 73 CCS811_Status_e beginCore(TwoWire &wirePort); 74 75 //***Reading functions***// 76 77 //readRegister reads one 8-bit register 78 CCS811_Status_e readRegister(uint8_t offset, uint8_t *outputPointer); 79 //multiReadRegister takes a uint8 array address as input and performs 80 // a number of consecutive reads 81 CCS811_Status_e multiReadRegister(uint8_t offset, uint8_t *outputPointer, uint8_t length); 82 83 //***Writing functions***// 84 85 //Writes an 8-bit byte; 86 CCS811_Status_e writeRegister(uint8_t offset, uint8_t dataToWrite); 87 //multiWriteRegister takes a uint8 array address as input and performs 88 // a number of consecutive writes 89 CCS811_Status_e multiWriteRegister(uint8_t offset, uint8_t *inputPointer, uint8_t length); 90 91 protected: 92 //Variables 93 TwoWire *_i2cPort; //The generic connection to user's chosen I2C hardware 94 uint8_t I2CAddress; 95 }; 96 97 //This is the highest level class of the driver. 98 // 99 // class CCS811 inherits the CCS811Core and makes use of the beginCore() 100 //method through its own begin() method. It also contains user settings/values. 101 102 class CCS811 : public CCS811Core 103 { 104 public: 105 CCS811(uint8_t); 106 107 //Call to check for errors, start app, and set default mode 1 108 bool begin(TwoWire &wirePort = Wire); //Use the Wire hardware by default 109 CCS811_Status_e beginWithStatus(TwoWire &wirePort = Wire); //Use the Wire hardware by default 110 const char *statusString(CCS811_Status_e stat = CCS811_Stat_NUM); // Returns a human-readable status message. Defaults to status member, but prints string for supplied status if supplied 111 112 CCS811_Status_e readAlgorithmResults(void); 113 bool checkForStatusError(void); 114 bool dataAvailable(void); 115 bool appValid(void); 116 uint8_t getErrorRegister(void); 117 uint16_t getBaseline(void); 118 CCS811_Status_e setBaseline(uint16_t); 119 CCS811_Status_e enableInterrupts(void); 120 CCS811_Status_e disableInterrupts(void); 121 CCS811_Status_e setDriveMode(uint8_t mode); 122 CCS811_Status_e setEnvironmentalData(float relativeHumidity, float temperature); 123 void setRefResistance(float); //Unsupported feature. Refer to CPP file for more information. 124 CCS811_Status_e readNTC(void); //Unsupported feature. Refer to CPP file for more information. 125 uint16_t getTVOC(void); 126 uint16_t getCO2(void); 127 float getResistance(void); //Unsupported feature. Refer to CPP file for more information. 128 float getTemperature(void); //Unsupported feature. Refer to CPP file for more information. 129 130 private: 131 //These are the air quality values obtained from the sensor 132 float refResistance; //Unsupported feature. Refer to CPP file for more information. 133 float resistance; //Unsupported feature. Refer to CPP file for more information. 134 uint16_t tVOC; 135 uint16_t CO2; 136 uint16_t vrefCounts = 0; 137 uint16_t ntcCounts = 0; 138 float temperature; 139 }; 140 141 #endif // End of definition check