simple CCS811 driver

Dependencies:   AMS_ENS210_temp_humid_sensor

Dependents:   TBSense2_Sensor_Demo

Fork of AMS_CCS811_gas_sensor by Marcus Lee

Committer:
UHSLMarcus
Date:
Thu Jan 19 12:46:58 2017 +0000
Revision:
1:acfca1d3256d
Parent:
0:5edbf3550350
Child:
2:e394671ef5f6
simple members implimented

Who changed what in which revision?

UserRevisionLine numberNew contents of line
UHSLMarcus 0:5edbf3550350 1 /**
UHSLMarcus 0:5edbf3550350 2 * @author Marcus Lee
UHSLMarcus 0:5edbf3550350 3 *
UHSLMarcus 0:5edbf3550350 4 * @section DESCRIPTION
UHSLMarcus 0:5edbf3550350 5 * A library for the AMS CCS811 digital gas sensor.
UHSLMarcus 0:5edbf3550350 6 *
UHSLMarcus 0:5edbf3550350 7 */
UHSLMarcus 0:5edbf3550350 8 #ifndef AMS_CCS811_H
UHSLMarcus 0:5edbf3550350 9 #define AMS_CCS811_H
UHSLMarcus 0:5edbf3550350 10
UHSLMarcus 0:5edbf3550350 11 #include "mbed.h"
UHSLMarcus 0:5edbf3550350 12
UHSLMarcus 0:5edbf3550350 13 /* Library defaults */
UHSLMarcus 1:acfca1d3256d 14 #define CONFIG_OP_MODE TEN_SECOND // Every 10 seconds
UHSLMarcus 0:5edbf3550350 15 #define CONFIG_INTR 0 // Interupt off
UHSLMarcus 1:acfca1d3256d 16 #define CONFIG_ADDR_DIR 0 // ADDR n_wake_pin pulled low
UHSLMarcus 0:5edbf3550350 17
UHSLMarcus 0:5edbf3550350 18 /* Library Constants */
UHSLMarcus 0:5edbf3550350 19 #define SLAVE_ADDR_RAW_H 0x5B
UHSLMarcus 0:5edbf3550350 20 #define SLAVE_ADDR_RAW_L 0x5A
UHSLMarcus 0:5edbf3550350 21 #define SLAVE_ADDR_RAW slave_addr
UHSLMarcus 0:5edbf3550350 22 #define SLAVE_ADDR SLAVE_ADDR_RAW << 1 // 0x86
UHSLMarcus 0:5edbf3550350 23 #define SLAVE_ADDR_W SLAVE_ADDR
UHSLMarcus 0:5edbf3550350 24 #define SLAVE_ADDR_R SLAVE_ADDR | 1 // 0x87
UHSLMarcus 0:5edbf3550350 25
UHSLMarcus 0:5edbf3550350 26 #define SYS_MODE 0x01
UHSLMarcus 0:5edbf3550350 27 #define SYS_STATUS 0x00
UHSLMarcus 0:5edbf3550350 28
UHSLMarcus 0:5edbf3550350 29 #define ALG_DATA 0x02
UHSLMarcus 0:5edbf3550350 30 #define ENV_DATA 0x05
UHSLMarcus 0:5edbf3550350 31 #define ERROR_ID 0xE0
UHSLMarcus 0:5edbf3550350 32
UHSLMarcus 0:5edbf3550350 33 /** The AMS CCS811 class
UHSLMarcus 0:5edbf3550350 34 */
UHSLMarcus 0:5edbf3550350 35 class AMS_CCS811
UHSLMarcus 0:5edbf3550350 36 {
UHSLMarcus 0:5edbf3550350 37 public:
UHSLMarcus 0:5edbf3550350 38 /** Sensor operation modes.
UHSLMarcus 0:5edbf3550350 39 *
UHSLMarcus 0:5edbf3550350 40 */
UHSLMarcus 0:5edbf3550350 41 enum OP_MODES {
UHSLMarcus 0:5edbf3550350 42 IDLE, /**< Measurements disabled */
UHSLMarcus 0:5edbf3550350 43 SECOND, /**< Measurement every second */
UHSLMarcus 0:5edbf3550350 44 TEN_SECOND, /**< Measurement every 10 seconds */
UHSLMarcus 0:5edbf3550350 45 SIXTY_SECOND, /**< Measurement every 60 seconds */
UHSLMarcus 0:5edbf3550350 46 CONSTANT /**< Measurement every 250ms - Only raw data available */
UHSLMarcus 0:5edbf3550350 47 };
UHSLMarcus 0:5edbf3550350 48
UHSLMarcus 0:5edbf3550350 49 /** Data collection status.
UHSLMarcus 0:5edbf3550350 50 *
UHSLMarcus 0:5edbf3550350 51 */
UHSLMarcus 0:5edbf3550350 52 enum DATA_STATUS {
UHSLMarcus 0:5edbf3550350 53 DATA_NOT_READY, /**< No new data */
UHSLMarcus 0:5edbf3550350 54 DATA_READY, /**< New data */
UHSLMarcus 0:5edbf3550350 55 ERROR, /**< Error occurred */
UHSLMarcus 0:5edbf3550350 56 };
UHSLMarcus 0:5edbf3550350 57
UHSLMarcus 0:5edbf3550350 58 /** Create an AMS_CCS811 instance
UHSLMarcus 0:5edbf3550350 59 *
UHSLMarcus 0:5edbf3550350 60 * @param i2c The I2C interface to use for communication
UHSLMarcus 0:5edbf3550350 61 * @param n_wake_pin Pin nWAKE is attached to
UHSLMarcus 0:5edbf3550350 62 */
UHSLMarcus 0:5edbf3550350 63 AMS_CCS811(I2C * i2c, PinName n_wake_pin);
UHSLMarcus 0:5edbf3550350 64
UHSLMarcus 0:5edbf3550350 65 /** Create an AMS_CCS811 instance
UHSLMarcus 0:5edbf3550350 66 *
UHSLMarcus 0:5edbf3550350 67 * @param i2c The I2C interface to use for communication
UHSLMarcus 0:5edbf3550350 68 * @param n_wake_pin Pin nWAKE is attached to
UHSLMarcus 0:5edbf3550350 69 * @param ens210_i2c The I2C interface for an attached AMS_ENS210
UHSLMarcus 0:5edbf3550350 70 */
UHSLMarcus 0:5edbf3550350 71 AMS_CCS811(I2C * i2c, PinName n_wake_pin, I2C * ens210_i2c);
UHSLMarcus 0:5edbf3550350 72
UHSLMarcus 0:5edbf3550350 73 /** Destroy the AMS_CCS811 instance
UHSLMarcus 0:5edbf3550350 74 */
UHSLMarcus 0:5edbf3550350 75 ~AMS_CCS811();
UHSLMarcus 0:5edbf3550350 76
UHSLMarcus 0:5edbf3550350 77 /** Initalise the sensor
UHSLMarcus 0:5edbf3550350 78 *
UHSLMarcus 0:5edbf3550350 79 * @return Intalisation success
UHSLMarcus 0:5edbf3550350 80 */
UHSLMarcus 0:5edbf3550350 81 bool init();
UHSLMarcus 0:5edbf3550350 82
UHSLMarcus 0:5edbf3550350 83 /** Set the operation mode \n
UHSLMarcus 0:5edbf3550350 84 * Note: \n When a sensor operating mode is changed to a new mode with\n
UHSLMarcus 0:5edbf3550350 85 * a lower sample rate (e.g.\ from SECOND to SIXTY_SECOND), it should be\n
UHSLMarcus 0:5edbf3550350 86 * placed in IDLE for at least 10 minutes before enabling the new mode.\ \n
UHSLMarcus 0:5edbf3550350 87 * When a sensor operating mode is changed to a new mode with a higher\n
UHSLMarcus 0:5edbf3550350 88 * sample rate (e.g.\ from SIXTY_SECOND to SECOND), there is no requirement\n
UHSLMarcus 0:5edbf3550350 89 * to wait before enabling the new mode.
UHSLMarcus 0:5edbf3550350 90 *
UHSLMarcus 0:5edbf3550350 91 * @param mode OP_MODES mode to set
UHSLMarcus 0:5edbf3550350 92 *
UHSLMarcus 0:5edbf3550350 93 * @return Write success
UHSLMarcus 0:5edbf3550350 94 */
UHSLMarcus 0:5edbf3550350 95 bool mode(OP_MODES mode);
UHSLMarcus 0:5edbf3550350 96
UHSLMarcus 0:5edbf3550350 97 /** Get the current power mode
UHSLMarcus 0:5edbf3550350 98 *
UHSLMarcus 0:5edbf3550350 99 * @return The current OP_MODES mode
UHSLMarcus 0:5edbf3550350 100 */
UHSLMarcus 0:5edbf3550350 101 OP_MODES mode();
UHSLMarcus 0:5edbf3550350 102
UHSLMarcus 0:5edbf3550350 103 /** Set the ADDR mode \n
UHSLMarcus 0:5edbf3550350 104 *
UHSLMarcus 0:5edbf3550350 105 * @param high True sets to high, false to low
UHSLMarcus 0:5edbf3550350 106 *
UHSLMarcus 0:5edbf3550350 107 * @return Write success
UHSLMarcus 0:5edbf3550350 108 */
UHSLMarcus 0:5edbf3550350 109 bool addr_mode(bool high);
UHSLMarcus 0:5edbf3550350 110
UHSLMarcus 0:5edbf3550350 111 /** Get the the ADDR mode
UHSLMarcus 0:5edbf3550350 112 *
UHSLMarcus 0:5edbf3550350 113 * @return The current ADDR mode, true for high, false for low
UHSLMarcus 0:5edbf3550350 114 */
UHSLMarcus 0:5edbf3550350 115 bool addr_mode();
UHSLMarcus 0:5edbf3550350 116
UHSLMarcus 0:5edbf3550350 117 /** Set the ADDR pin
UHSLMarcus 0:5edbf3550350 118 *
UHSLMarcus 0:5edbf3550350 119 * @param pin Pin ADDR is attached to
UHSLMarcus 0:5edbf3550350 120 *
UHSLMarcus 0:5edbf3550350 121 * @return Write success
UHSLMarcus 0:5edbf3550350 122 */
UHSLMarcus 0:5edbf3550350 123 bool addr_pin(PinName pin);
UHSLMarcus 0:5edbf3550350 124
UHSLMarcus 0:5edbf3550350 125 /** Get the the ADDR pin
UHSLMarcus 0:5edbf3550350 126 *
UHSLMarcus 0:5edbf3550350 127 * @return The addr pin
UHSLMarcus 0:5edbf3550350 128 */
UHSLMarcus 0:5edbf3550350 129 PinName addr_pin();
UHSLMarcus 0:5edbf3550350 130
UHSLMarcus 0:5edbf3550350 131 /** Set the nWAKE pin
UHSLMarcus 0:5edbf3550350 132 *
UHSLMarcus 0:5edbf3550350 133 * @param pin Pin nWAKE is attached to
UHSLMarcus 0:5edbf3550350 134 *
UHSLMarcus 0:5edbf3550350 135 * @return Write success
UHSLMarcus 0:5edbf3550350 136 */
UHSLMarcus 0:5edbf3550350 137 bool n_wake_pin(PinName pin);
UHSLMarcus 0:5edbf3550350 138
UHSLMarcus 0:5edbf3550350 139 /** Get the the nWAKE pin
UHSLMarcus 0:5edbf3550350 140 *
UHSLMarcus 0:5edbf3550350 141 * @return The nWAKE pin
UHSLMarcus 0:5edbf3550350 142 */
UHSLMarcus 0:5edbf3550350 143 PinName n_wake_pin();
UHSLMarcus 0:5edbf3550350 144
UHSLMarcus 0:5edbf3550350 145 /** Set the relative humidity (%) and temperature (C)
UHSLMarcus 0:5edbf3550350 146 * Use when AMS ENS210 is not linked
UHSLMarcus 0:5edbf3550350 147 *
UHSLMarcus 0:5edbf3550350 148 * @return Write success
UHSLMarcus 0:5edbf3550350 149 */
UHSLMarcus 0:5edbf3550350 150 bool env_data(float humid, float temp);
UHSLMarcus 0:5edbf3550350 151
UHSLMarcus 0:5edbf3550350 152 /** Get the sensor collection state
UHSLMarcus 0:5edbf3550350 153 * Use when interupts are disabled
UHSLMarcus 0:5edbf3550350 154 *
UHSLMarcus 0:5edbf3550350 155 * @return Current collection state
UHSLMarcus 0:5edbf3550350 156 */
UHSLMarcus 0:5edbf3550350 157 DATA_STATUS has_new_data();
UHSLMarcus 0:5edbf3550350 158
UHSLMarcus 0:5edbf3550350 159 /** Get the most recent CO2 measurement.
UHSLMarcus 0:5edbf3550350 160 * Must call has_new_data() first when when interupts are disabled otherwise the same data will be returned
UHSLMarcus 0:5edbf3550350 161 *
UHSLMarcus 0:5edbf3550350 162 * @return Most recent eCO2 measurement in ppm
UHSLMarcus 0:5edbf3550350 163 */
UHSLMarcus 0:5edbf3550350 164 uint16_t co2_read();
UHSLMarcus 0:5edbf3550350 165
UHSLMarcus 0:5edbf3550350 166 /** Get the most recent TVOC measurement.
UHSLMarcus 0:5edbf3550350 167 * Must call has_new_data() first when when interupts are disabled otherwise the same data will be returned
UHSLMarcus 0:5edbf3550350 168 *
UHSLMarcus 0:5edbf3550350 169 * @return Most recent TVOC measurement in ppb
UHSLMarcus 0:5edbf3550350 170 */
UHSLMarcus 0:5edbf3550350 171 uint16_t tvoc_read();
UHSLMarcus 0:5edbf3550350 172
UHSLMarcus 0:5edbf3550350 173 /** Get the most recent RAW data.
UHSLMarcus 0:5edbf3550350 174 * Must call has_new_data() first when when interupts are disabled otherwise the same data will be returned
UHSLMarcus 0:5edbf3550350 175 *
UHSLMarcus 0:5edbf3550350 176 * @return Most recent TVOC measurement in ppb
UHSLMarcus 0:5edbf3550350 177 */
UHSLMarcus 0:5edbf3550350 178 uint16_t raw_read();
UHSLMarcus 0:5edbf3550350 179
UHSLMarcus 0:5edbf3550350 180 /** Get the last error.
UHSLMarcus 0:5edbf3550350 181 * Must call has_new_data() first when when interupts are disabled otherwise the same error will be returned
UHSLMarcus 0:5edbf3550350 182 *
UHSLMarcus 0:5edbf3550350 183 * @return Last error.
UHSLMarcus 0:5edbf3550350 184 */
UHSLMarcus 0:5edbf3550350 185 const char * last_error();
UHSLMarcus 0:5edbf3550350 186
UHSLMarcus 0:5edbf3550350 187 /** Attach a function to be called when data is ready.
UHSLMarcus 0:5edbf3550350 188 * Calling this method enables interupts
UHSLMarcus 0:5edbf3550350 189 *
UHSLMarcus 0:5edbf3550350 190 * @param func_ptr A pointer to the function to be called
UHSLMarcus 0:5edbf3550350 191 * @param pin Pin attached to nINT
UHSLMarcus 0:5edbf3550350 192 *
UHSLMarcus 0:5edbf3550350 193 * @return Attach success
UHSLMarcus 0:5edbf3550350 194 */
UHSLMarcus 0:5edbf3550350 195 bool attach(void (*func_ptr)(void), PinName pin) {
UHSLMarcus 0:5edbf3550350 196 _isr_data_fp.attach(func_ptr);
UHSLMarcus 0:5edbf3550350 197 _int_data_active = true;
UHSLMarcus 0:5edbf3550350 198 _int_data(pin);
UHSLMarcus 0:5edbf3550350 199 _int_data.fall(this, &AMS_CCS811::_isr_data());
UHSLMarcus 0:5edbf3550350 200
UHSLMarcus 1:acfca1d3256d 201 return enable_interupt(true);
UHSLMarcus 0:5edbf3550350 202 }
UHSLMarcus 0:5edbf3550350 203
UHSLMarcus 0:5edbf3550350 204 /** Attach a member function to be called when data is ready.
UHSLMarcus 0:5edbf3550350 205 * Calling this method enables interupts
UHSLMarcus 0:5edbf3550350 206 *
UHSLMarcus 0:5edbf3550350 207 * @param type_ptr A pointer to the instance of the class
UHSLMarcus 0:5edbf3550350 208 * @param mem_ptr A pointer to the member function
UHSLMarcus 0:5edbf3550350 209 * @param pin Pin attached to nINT
UHSLMarcus 0:5edbf3550350 210 *
UHSLMarcus 0:5edbf3550350 211 * @return Attach success
UHSLMarcus 0:5edbf3550350 212 */
UHSLMarcus 0:5edbf3550350 213 template<typename T>
UHSLMarcus 0:5edbf3550350 214 bool attach(T *type_ptr, void (T::*mem_ptr)(void), PinName pin) {
UHSLMarcus 0:5edbf3550350 215 _isr_data_fp.attach(type_ptr, mem_ptr);
UHSLMarcus 0:5edbf3550350 216 //_int_data_active = true;
UHSLMarcus 0:5edbf3550350 217 //_int_data(pin);
UHSLMarcus 0:5edbf3550350 218 //_int_data.fall(this, &AMS_CCS811::_isr_data());
UHSLMarcus 0:5edbf3550350 219
UHSLMarcus 1:acfca1d3256d 220 return enable_interupt(true);
UHSLMarcus 0:5edbf3550350 221 }
UHSLMarcus 0:5edbf3550350 222
UHSLMarcus 1:acfca1d3256d 223 /** Set whether the data ready interupt is enabled.
UHSLMarcus 1:acfca1d3256d 224 *
UHSLMarcus 1:acfca1d3256d 225 * @param enabled True for enabled, false for disabled
UHSLMarcus 0:5edbf3550350 226 *
UHSLMarcus 0:5edbf3550350 227 * @return Write success
UHSLMarcus 0:5edbf3550350 228 */
UHSLMarcus 1:acfca1d3256d 229 bool enable_interupt(bool enable);
UHSLMarcus 1:acfca1d3256d 230
UHSLMarcus 1:acfca1d3256d 231 /** Get whether the data ready interupt is enabled.
UHSLMarcus 1:acfca1d3256d 232 *
UHSLMarcus 1:acfca1d3256d 233 *
UHSLMarcus 1:acfca1d3256d 234 * @return True for enabled, false for disabled
UHSLMarcus 1:acfca1d3256d 235 */
UHSLMarcus 1:acfca1d3256d 236 bool interupt_enabled();
UHSLMarcus 0:5edbf3550350 237
UHSLMarcus 0:5edbf3550350 238 /** Set the nINT pin
UHSLMarcus 0:5edbf3550350 239 *
UHSLMarcus 0:5edbf3550350 240 * @param pin Pin nINT is attached to
UHSLMarcus 0:5edbf3550350 241 *
UHSLMarcus 0:5edbf3550350 242 * @return Write success
UHSLMarcus 0:5edbf3550350 243 */
UHSLMarcus 0:5edbf3550350 244 bool interrupt_pin(PinName pin);
UHSLMarcus 0:5edbf3550350 245
UHSLMarcus 0:5edbf3550350 246 /** Get the the nINT pin
UHSLMarcus 0:5edbf3550350 247 *
UHSLMarcus 0:5edbf3550350 248 * @return The nINT pin
UHSLMarcus 0:5edbf3550350 249 */
UHSLMarcus 0:5edbf3550350 250 PinName interrupt_pin();
UHSLMarcus 0:5edbf3550350 251
UHSLMarcus 0:5edbf3550350 252
UHSLMarcus 0:5edbf3550350 253
UHSLMarcus 0:5edbf3550350 254
UHSLMarcus 0:5edbf3550350 255 private:
UHSLMarcus 0:5edbf3550350 256 I2C* _i2c;
UHSLMarcus 1:acfca1d3256d 257 bool _addr_dir;
UHSLMarcus 0:5edbf3550350 258 int slave_addr;
UHSLMarcus 0:5edbf3550350 259 I2C* _ens210_i2c;
UHSLMarcus 0:5edbf3550350 260 bool _ens210_enable;
UHSLMarcus 0:5edbf3550350 261 OP_MODES _mode;
UHSLMarcus 1:acfca1d3256d 262 bool set_defaults();
UHSLMarcus 0:5edbf3550350 263
UHSLMarcus 1:acfca1d3256d 264 DigitalOut *_n_wake_out;
UHSLMarcus 1:acfca1d3256d 265 DigitalOut *_addr_out;
UHSLMarcus 0:5edbf3550350 266
UHSLMarcus 0:5edbf3550350 267
UHSLMarcus 0:5edbf3550350 268 FunctionPointer _isr_data_fp;
UHSLMarcus 1:acfca1d3256d 269 bool _int_data_enabled;
UHSLMarcus 1:acfca1d3256d 270 InterruptIn *_int_data;
UHSLMarcus 0:5edbf3550350 271 void _isr_data();
UHSLMarcus 0:5edbf3550350 272
UHSLMarcus 1:acfca1d3256d 273 bool write_config();
UHSLMarcus 1:acfca1d3256d 274
UHSLMarcus 1:acfca1d3256d 275 struct read_config_result {
UHSLMarcus 1:acfca1d3256d 276 bool success;
UHSLMarcus 1:acfca1d3256d 277 uint8_t byte;
UHSLMarcus 1:acfca1d3256d 278 }
UHSLMarcus 1:acfca1d3256d 279 read_config_result read_config();
UHSLMarcus 0:5edbf3550350 280
UHSLMarcus 0:5edbf3550350 281 };
UHSLMarcus 0:5edbf3550350 282
UHSLMarcus 0:5edbf3550350 283
UHSLMarcus 0:5edbf3550350 284 #endif /* AMS_CCS811_H */