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:
Fri Jan 20 14:34:41 2017 +0000
Revision:
5:41e97348e9e7
Parent:
4:a6b8881eae87
Child:
6:22c0a7f2ece2
Implimented error methods and starting data collection. Added a few extra methods to read the status and boot into app mode from boot mode

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 5:41e97348e9e7 13 #include "serialBuffer.h"
UHSLMarcus 5:41e97348e9e7 14 using utils::SerialBuffer;
UHSLMarcus 5:41e97348e9e7 15 extern SerialBuffer USBserialComms;
UHSLMarcus 5:41e97348e9e7 16
UHSLMarcus 0:5edbf3550350 17 /* Library defaults */
UHSLMarcus 5:41e97348e9e7 18 #define CONFIG_OP_MODE TEN_SECOND // Every 10 seconds
UHSLMarcus 5:41e97348e9e7 19 #define CONFIG_INTR 0 // Interupt off
UHSLMarcus 5:41e97348e9e7 20 #define CONFIG_ADDR_DIR 0 // ADDR n_wake_pin pulled low
UHSLMarcus 5:41e97348e9e7 21 #define CONFIG_ENS210_POLL 3000 // ENS210 is polled every 3 seconds
UHSLMarcus 0:5edbf3550350 22
UHSLMarcus 0:5edbf3550350 23 /* Library Constants */
UHSLMarcus 5:41e97348e9e7 24 #define CCS811_SLAVE_ADDR_RAW_H 0x5B
UHSLMarcus 5:41e97348e9e7 25 #define CCS811_SLAVE_ADDR_RAW_L 0x5A
UHSLMarcus 5:41e97348e9e7 26 #define CCS811_SLAVE_ADDR_RAW _slave_addr
UHSLMarcus 5:41e97348e9e7 27 #define CCS811_SLAVE_ADDR CCS811_SLAVE_ADDR_RAW << 1
UHSLMarcus 5:41e97348e9e7 28 #define CCS811_SLAVE_ADDR_W CCS811_SLAVE_ADDR
UHSLMarcus 5:41e97348e9e7 29 #define CCS811_SLAVE_ADDR_R CCS811_SLAVE_ADDR | 1
UHSLMarcus 5:41e97348e9e7 30
UHSLMarcus 5:41e97348e9e7 31 #define MEAS_MODE 0x01
UHSLMarcus 5:41e97348e9e7 32 #define STATUS 0x00
UHSLMarcus 5:41e97348e9e7 33
UHSLMarcus 5:41e97348e9e7 34 #define ALG_RESULT_DATA 0x02
UHSLMarcus 5:41e97348e9e7 35 #define ENV_DATA 0x05
UHSLMarcus 5:41e97348e9e7 36 #define ERROR_ID 0xE0
UHSLMarcus 5:41e97348e9e7 37
UHSLMarcus 5:41e97348e9e7 38 #define APP_START 0xF4
UHSLMarcus 5:41e97348e9e7 39
UHSLMarcus 5:41e97348e9e7 40 #define CCS811_T_AWAKE 55 // us - time taken for sensor I2C to become active
UHSLMarcus 5:41e97348e9e7 41 #define CCS811_T_DWAKE 25 // us - time taken for sensor I2C to become inactive
UHSLMarcus 0:5edbf3550350 42
UHSLMarcus 5:41e97348e9e7 43 /* Error Codes */
UHSLMarcus 5:41e97348e9e7 44 #define CCS811_NO_ERROR "No Error";
UHSLMarcus 5:41e97348e9e7 45 /* Sensor Errors */
UHSLMarcus 5:41e97348e9e7 46 #define CCS811_ERR_NUM 8
UHSLMarcus 5:41e97348e9e7 47 #define CCS811_WRITE_REG_INVALID "The CCS811 received an I2C write request addressed to this station but with invalid register address ID"
UHSLMarcus 5:41e97348e9e7 48 #define CCS811_READ_REG_INVALID "The CCS811 received an I2C read request to a mailbox ID that is invalid"
UHSLMarcus 5:41e97348e9e7 49 #define CCS811_MEASMODE_INVALID "The CCS811 received an I2C request to write an unsupported mode to MEAS_MODE"
UHSLMarcus 5:41e97348e9e7 50 #define CCS811_MAX_RESISTANCE "The sensor resistance measurement has reached or exceeded the maximum range"
UHSLMarcus 5:41e97348e9e7 51 #define CCS811_HEATER_FAULT "The Heater current in the CCS811 is not in range"
UHSLMarcus 5:41e97348e9e7 52 #define CCS811_HEATER_SUPPLY "The Heater voltage is not being applied correctly"
UHSLMarcus 5:41e97348e9e7 53 #define CCS811_RESERVED "Reserved for Future Use"
UHSLMarcus 5:41e97348e9e7 54 /* Library Errors */
UHSLMarcus 5:41e97348e9e7 55 #define CCS811_LIB_ERR_NUM 6
UHSLMarcus 5:41e97348e9e7 56 #define CCS811_LIB_N_WAKE_ID 0
UHSLMarcus 5:41e97348e9e7 57 #define CCS811_LIB_N_WAKE "nWAKE pin not set"
UHSLMarcus 5:41e97348e9e7 58 #define CCS811_LIB_I2C_ID 1
UHSLMarcus 5:41e97348e9e7 59 #define CCS811_LIB_I2C "I2C interface is NULL"
UHSLMarcus 5:41e97348e9e7 60 #define CCS811_LIB_SLAVE_W_ID 2
UHSLMarcus 5:41e97348e9e7 61 #define CCS811_LIB_SLAVE_W "Invaid slave write address"
UHSLMarcus 5:41e97348e9e7 62 #define CCS811_LIB_REG_ADDR_ID 3
UHSLMarcus 5:41e97348e9e7 63 #define CCS811_LIB_REG_ADDR "Failed to write register address"
UHSLMarcus 5:41e97348e9e7 64 #define CCS811_LIB_I2CWRITE_ID 4
UHSLMarcus 5:41e97348e9e7 65 #define CCS811_LIB_I2CWRITE "Failed to write byte"
UHSLMarcus 5:41e97348e9e7 66 #define CCS811_LIB_SLAVE_R_ID 5
UHSLMarcus 5:41e97348e9e7 67 #define CCS811_LIB_SLAVE_R "Invaid slave read address"
UHSLMarcus 0:5edbf3550350 68
UHSLMarcus 5:41e97348e9e7 69 #define CCS811_TOTAL_ERR_NUM CCS811_ERR_NUM+CCS811_LIB_ERR_NUM
UHSLMarcus 5:41e97348e9e7 70
UHSLMarcus 0:5edbf3550350 71
UHSLMarcus 0:5edbf3550350 72 /** The AMS CCS811 class
UHSLMarcus 0:5edbf3550350 73 */
UHSLMarcus 0:5edbf3550350 74 class AMS_CCS811
UHSLMarcus 0:5edbf3550350 75 {
UHSLMarcus 0:5edbf3550350 76 public:
UHSLMarcus 0:5edbf3550350 77 /** Sensor operation modes.
UHSLMarcus 0:5edbf3550350 78 *
UHSLMarcus 0:5edbf3550350 79 */
UHSLMarcus 0:5edbf3550350 80 enum OP_MODES {
UHSLMarcus 0:5edbf3550350 81 IDLE, /**< Measurements disabled */
UHSLMarcus 0:5edbf3550350 82 SECOND, /**< Measurement every second */
UHSLMarcus 0:5edbf3550350 83 TEN_SECOND, /**< Measurement every 10 seconds */
UHSLMarcus 0:5edbf3550350 84 SIXTY_SECOND, /**< Measurement every 60 seconds */
UHSLMarcus 2:e394671ef5f6 85 CONSTANT, /**< Measurement every 250ms - Only raw data available */
UHSLMarcus 5:41e97348e9e7 86 INVALID /**< Invalid bit configuration/Error Occured */
UHSLMarcus 0:5edbf3550350 87 };
UHSLMarcus 0:5edbf3550350 88
UHSLMarcus 5:41e97348e9e7 89 /** Holds error information.
UHSLMarcus 0:5edbf3550350 90 *
UHSLMarcus 0:5edbf3550350 91 */
UHSLMarcus 5:41e97348e9e7 92 struct ccs811_errors {
UHSLMarcus 5:41e97348e9e7 93 int count; /**< Number of total errors */
UHSLMarcus 5:41e97348e9e7 94 int codes[CCS811_TOTAL_ERR_NUM]; /**< Array of active error codes */
UHSLMarcus 5:41e97348e9e7 95
UHSLMarcus 5:41e97348e9e7 96 ccs811_errors() : count(0) {}
UHSLMarcus 0:5edbf3550350 97 };
UHSLMarcus 0:5edbf3550350 98
UHSLMarcus 0:5edbf3550350 99 /** Create an AMS_CCS811 instance
UHSLMarcus 0:5edbf3550350 100 *
UHSLMarcus 0:5edbf3550350 101 * @param i2c The I2C interface to use for communication
UHSLMarcus 0:5edbf3550350 102 * @param n_wake_pin Pin nWAKE is attached to
UHSLMarcus 0:5edbf3550350 103 */
UHSLMarcus 0:5edbf3550350 104 AMS_CCS811(I2C * i2c, PinName n_wake_pin);
UHSLMarcus 0:5edbf3550350 105
UHSLMarcus 0:5edbf3550350 106 /** Create an AMS_CCS811 instance
UHSLMarcus 0:5edbf3550350 107 *
UHSLMarcus 0:5edbf3550350 108 * @param i2c The I2C interface to use for communication
UHSLMarcus 0:5edbf3550350 109 * @param n_wake_pin Pin nWAKE is attached to
UHSLMarcus 0:5edbf3550350 110 * @param ens210_i2c The I2C interface for an attached AMS_ENS210
UHSLMarcus 0:5edbf3550350 111 */
UHSLMarcus 0:5edbf3550350 112 AMS_CCS811(I2C * i2c, PinName n_wake_pin, I2C * ens210_i2c);
UHSLMarcus 0:5edbf3550350 113
UHSLMarcus 0:5edbf3550350 114 /** Destroy the AMS_CCS811 instance
UHSLMarcus 0:5edbf3550350 115 */
UHSLMarcus 0:5edbf3550350 116 ~AMS_CCS811();
UHSLMarcus 0:5edbf3550350 117
UHSLMarcus 0:5edbf3550350 118 /** Initalise the sensor
UHSLMarcus 0:5edbf3550350 119 *
UHSLMarcus 0:5edbf3550350 120 * @return Intalisation success
UHSLMarcus 0:5edbf3550350 121 */
UHSLMarcus 0:5edbf3550350 122 bool init();
UHSLMarcus 4:a6b8881eae87 123
UHSLMarcus 4:a6b8881eae87 124 /** Set the I2C interface
UHSLMarcus 4:a6b8881eae87 125 *
UHSLMarcus 4:a6b8881eae87 126 * @param i2c The I2C interface to use for communication
UHSLMarcus 4:a6b8881eae87 127 *
UHSLMarcus 4:a6b8881eae87 128 * @return Write success
UHSLMarcus 4:a6b8881eae87 129 */
UHSLMarcus 4:a6b8881eae87 130 void i2c_interface(I2C * i2c);
UHSLMarcus 4:a6b8881eae87 131
UHSLMarcus 4:a6b8881eae87 132 /** Set the ENS210 I2C interface
UHSLMarcus 4:a6b8881eae87 133 *
UHSLMarcus 4:a6b8881eae87 134 * @param i2c The I2C interface for an attached AMS_ENS210
UHSLMarcus 4:a6b8881eae87 135 *
UHSLMarcus 4:a6b8881eae87 136 * @return Write success
UHSLMarcus 4:a6b8881eae87 137 */
UHSLMarcus 4:a6b8881eae87 138 void ens210_i2c_interface(I2C * i2c);
UHSLMarcus 4:a6b8881eae87 139
UHSLMarcus 4:a6b8881eae87 140 /** Set whether the attached AMS_ENS210 is enabled.
UHSLMarcus 4:a6b8881eae87 141 * If an I2C interface is not set for the ENS210, calling this method will have no effect.
UHSLMarcus 4:a6b8881eae87 142 *
UHSLMarcus 4:a6b8881eae87 143 * @param enabled True for enabled, false for disabled
UHSLMarcus 4:a6b8881eae87 144 *
UHSLMarcus 4:a6b8881eae87 145 * @return enabled True for enabled, false for disabled
UHSLMarcus 4:a6b8881eae87 146 */
UHSLMarcus 4:a6b8881eae87 147 bool enable_ens210(bool enable);
UHSLMarcus 4:a6b8881eae87 148
UHSLMarcus 4:a6b8881eae87 149 /** Get whether the attached AMS_ENS210 is enabled.
UHSLMarcus 4:a6b8881eae87 150 *
UHSLMarcus 4:a6b8881eae87 151 * @return enabled True for enabled, false for disabled
UHSLMarcus 4:a6b8881eae87 152 *
UHSLMarcus 4:a6b8881eae87 153 */
UHSLMarcus 4:a6b8881eae87 154 bool ens210_is_enabled();
UHSLMarcus 4:a6b8881eae87 155
UHSLMarcus 4:a6b8881eae87 156 /** Set the AMS_ENS210 poll interval
UHSLMarcus 4:a6b8881eae87 157 *
UHSLMarcus 4:a6b8881eae87 158 * @param poll_ms Poll interval in ms
UHSLMarcus 4:a6b8881eae87 159 *
UHSLMarcus 4:a6b8881eae87 160 */
UHSLMarcus 4:a6b8881eae87 161 void ens210_poll_interval(int poll_ms);
UHSLMarcus 4:a6b8881eae87 162
UHSLMarcus 4:a6b8881eae87 163 /** Get the AMS_ENS210 poll interval
UHSLMarcus 4:a6b8881eae87 164 *
UHSLMarcus 4:a6b8881eae87 165 * @return The poll interval in ms
UHSLMarcus 4:a6b8881eae87 166 */
UHSLMarcus 4:a6b8881eae87 167 int ens210_poll_interval();
UHSLMarcus 5:41e97348e9e7 168
UHSLMarcus 5:41e97348e9e7 169 /** Get the current firmware mode
UHSLMarcus 5:41e97348e9e7 170 *
UHSLMarcus 5:41e97348e9e7 171 * @return 1 application mode, 0 for boot mode, -1 for error
UHSLMarcus 5:41e97348e9e7 172 */
UHSLMarcus 5:41e97348e9e7 173 int firmware_mode();
UHSLMarcus 0:5edbf3550350 174
UHSLMarcus 0:5edbf3550350 175 /** Set the operation mode \n
UHSLMarcus 0:5edbf3550350 176 * Note: \n When a sensor operating mode is changed to a new mode with\n
UHSLMarcus 0:5edbf3550350 177 * a lower sample rate (e.g.\ from SECOND to SIXTY_SECOND), it should be\n
UHSLMarcus 0:5edbf3550350 178 * placed in IDLE for at least 10 minutes before enabling the new mode.\ \n
UHSLMarcus 0:5edbf3550350 179 * When a sensor operating mode is changed to a new mode with a higher\n
UHSLMarcus 0:5edbf3550350 180 * sample rate (e.g.\ from SIXTY_SECOND to SECOND), there is no requirement\n
UHSLMarcus 0:5edbf3550350 181 * to wait before enabling the new mode.
UHSLMarcus 0:5edbf3550350 182 *
UHSLMarcus 0:5edbf3550350 183 * @param mode OP_MODES mode to set
UHSLMarcus 0:5edbf3550350 184 *
UHSLMarcus 0:5edbf3550350 185 * @return Write success
UHSLMarcus 0:5edbf3550350 186 */
UHSLMarcus 0:5edbf3550350 187 bool mode(OP_MODES mode);
UHSLMarcus 0:5edbf3550350 188
UHSLMarcus 0:5edbf3550350 189 /** Get the current power mode
UHSLMarcus 0:5edbf3550350 190 *
UHSLMarcus 0:5edbf3550350 191 * @return The current OP_MODES mode
UHSLMarcus 0:5edbf3550350 192 */
UHSLMarcus 2:e394671ef5f6 193 AMS_CCS811::OP_MODES mode();
UHSLMarcus 0:5edbf3550350 194
UHSLMarcus 0:5edbf3550350 195 /** Set the ADDR mode \n
UHSLMarcus 0:5edbf3550350 196 *
UHSLMarcus 0:5edbf3550350 197 * @param high True sets to high, false to low
UHSLMarcus 0:5edbf3550350 198 *
UHSLMarcus 0:5edbf3550350 199 * @return Write success
UHSLMarcus 0:5edbf3550350 200 */
UHSLMarcus 0:5edbf3550350 201 bool addr_mode(bool high);
UHSLMarcus 0:5edbf3550350 202
UHSLMarcus 0:5edbf3550350 203 /** Get the the ADDR mode
UHSLMarcus 0:5edbf3550350 204 *
UHSLMarcus 0:5edbf3550350 205 * @return The current ADDR mode, true for high, false for low
UHSLMarcus 0:5edbf3550350 206 */
UHSLMarcus 0:5edbf3550350 207 bool addr_mode();
UHSLMarcus 0:5edbf3550350 208
UHSLMarcus 0:5edbf3550350 209 /** Set the ADDR pin
UHSLMarcus 0:5edbf3550350 210 *
UHSLMarcus 0:5edbf3550350 211 * @param pin Pin ADDR is attached to
UHSLMarcus 0:5edbf3550350 212 *
UHSLMarcus 0:5edbf3550350 213 * @return Write success
UHSLMarcus 0:5edbf3550350 214 */
UHSLMarcus 0:5edbf3550350 215 bool addr_pin(PinName pin);
UHSLMarcus 0:5edbf3550350 216
UHSLMarcus 0:5edbf3550350 217 /** Get the the ADDR pin
UHSLMarcus 0:5edbf3550350 218 *
UHSLMarcus 0:5edbf3550350 219 * @return The addr pin
UHSLMarcus 0:5edbf3550350 220 */
UHSLMarcus 0:5edbf3550350 221 PinName addr_pin();
UHSLMarcus 0:5edbf3550350 222
UHSLMarcus 0:5edbf3550350 223 /** Set the nWAKE pin
UHSLMarcus 0:5edbf3550350 224 *
UHSLMarcus 0:5edbf3550350 225 * @param pin Pin nWAKE is attached to
UHSLMarcus 0:5edbf3550350 226 *
UHSLMarcus 0:5edbf3550350 227 * @return Write success
UHSLMarcus 0:5edbf3550350 228 */
UHSLMarcus 0:5edbf3550350 229 bool n_wake_pin(PinName pin);
UHSLMarcus 0:5edbf3550350 230
UHSLMarcus 0:5edbf3550350 231 /** Get the the nWAKE pin
UHSLMarcus 0:5edbf3550350 232 *
UHSLMarcus 0:5edbf3550350 233 * @return The nWAKE pin
UHSLMarcus 0:5edbf3550350 234 */
UHSLMarcus 0:5edbf3550350 235 PinName n_wake_pin();
UHSLMarcus 0:5edbf3550350 236
UHSLMarcus 0:5edbf3550350 237 /** Set the relative humidity (%) and temperature (C)
UHSLMarcus 0:5edbf3550350 238 * Use when AMS ENS210 is not linked
UHSLMarcus 0:5edbf3550350 239 *
UHSLMarcus 0:5edbf3550350 240 * @return Write success
UHSLMarcus 0:5edbf3550350 241 */
UHSLMarcus 0:5edbf3550350 242 bool env_data(float humid, float temp);
UHSLMarcus 0:5edbf3550350 243
UHSLMarcus 0:5edbf3550350 244 /** Get the sensor collection state
UHSLMarcus 0:5edbf3550350 245 * Use when interupts are disabled
UHSLMarcus 0:5edbf3550350 246 *
UHSLMarcus 5:41e97348e9e7 247 * @return Current collection state, 1 for new data ready, 0 for data not ready and -1 for error
UHSLMarcus 0:5edbf3550350 248 */
UHSLMarcus 5:41e97348e9e7 249 int has_new_data();
UHSLMarcus 0:5edbf3550350 250
UHSLMarcus 0:5edbf3550350 251 /** Get the most recent CO2 measurement.
UHSLMarcus 0:5edbf3550350 252 * Must call has_new_data() first when when interupts are disabled otherwise the same data will be returned
UHSLMarcus 0:5edbf3550350 253 *
UHSLMarcus 0:5edbf3550350 254 * @return Most recent eCO2 measurement in ppm
UHSLMarcus 0:5edbf3550350 255 */
UHSLMarcus 0:5edbf3550350 256 uint16_t co2_read();
UHSLMarcus 0:5edbf3550350 257
UHSLMarcus 0:5edbf3550350 258 /** Get the most recent TVOC measurement.
UHSLMarcus 0:5edbf3550350 259 * Must call has_new_data() first when when interupts are disabled otherwise the same data will be returned
UHSLMarcus 0:5edbf3550350 260 *
UHSLMarcus 0:5edbf3550350 261 * @return Most recent TVOC measurement in ppb
UHSLMarcus 0:5edbf3550350 262 */
UHSLMarcus 0:5edbf3550350 263 uint16_t tvoc_read();
UHSLMarcus 0:5edbf3550350 264
UHSLMarcus 0:5edbf3550350 265 /** Get the most recent RAW data.
UHSLMarcus 0:5edbf3550350 266 * Must call has_new_data() first when when interupts are disabled otherwise the same data will be returned
UHSLMarcus 0:5edbf3550350 267 *
UHSLMarcus 0:5edbf3550350 268 * @return Most recent TVOC measurement in ppb
UHSLMarcus 0:5edbf3550350 269 */
UHSLMarcus 0:5edbf3550350 270 uint16_t raw_read();
UHSLMarcus 5:41e97348e9e7 271
UHSLMarcus 5:41e97348e9e7 272 /** Get current error status
UHSLMarcus 5:41e97348e9e7 273 *
UHSLMarcus 5:41e97348e9e7 274 * @return True when error has occured, false when no error has occured
UHSLMarcus 5:41e97348e9e7 275 */
UHSLMarcus 5:41e97348e9e7 276 bool error_status();
UHSLMarcus 5:41e97348e9e7 277
UHSLMarcus 5:41e97348e9e7 278 /** Get the latest errors.
UHSLMarcus 0:5edbf3550350 279 *
UHSLMarcus 5:41e97348e9e7 280 * @return Latest errors.
UHSLMarcus 0:5edbf3550350 281 */
UHSLMarcus 5:41e97348e9e7 282 ccs811_errors errors();
UHSLMarcus 5:41e97348e9e7 283
UHSLMarcus 5:41e97348e9e7 284 /** Get the error string.
UHSLMarcus 5:41e97348e9e7 285 *
UHSLMarcus 5:41e97348e9e7 286 * @param err_code Error code to be translated
UHSLMarcus 5:41e97348e9e7 287 *
UHSLMarcus 5:41e97348e9e7 288 * @return Error String.
UHSLMarcus 5:41e97348e9e7 289 */
UHSLMarcus 5:41e97348e9e7 290 const char * error_string(int err_code);
UHSLMarcus 0:5edbf3550350 291
UHSLMarcus 0:5edbf3550350 292 /** Attach a function to be called when data is ready.
UHSLMarcus 0:5edbf3550350 293 * Calling this method enables interupts
UHSLMarcus 0:5edbf3550350 294 *
UHSLMarcus 0:5edbf3550350 295 * @param func_ptr A pointer to the function to be called
UHSLMarcus 0:5edbf3550350 296 * @param pin Pin attached to nINT
UHSLMarcus 0:5edbf3550350 297 *
UHSLMarcus 0:5edbf3550350 298 * @return Attach success
UHSLMarcus 0:5edbf3550350 299 */
UHSLMarcus 0:5edbf3550350 300 bool attach(void (*func_ptr)(void), PinName pin) {
UHSLMarcus 0:5edbf3550350 301 _isr_data_fp.attach(func_ptr);
UHSLMarcus 2:e394671ef5f6 302 interrupt_pin(pin);
UHSLMarcus 1:acfca1d3256d 303 return enable_interupt(true);
UHSLMarcus 0:5edbf3550350 304 }
UHSLMarcus 0:5edbf3550350 305
UHSLMarcus 0:5edbf3550350 306 /** Attach a member function to be called when data is ready.
UHSLMarcus 0:5edbf3550350 307 * Calling this method enables interupts
UHSLMarcus 0:5edbf3550350 308 *
UHSLMarcus 0:5edbf3550350 309 * @param type_ptr A pointer to the instance of the class
UHSLMarcus 0:5edbf3550350 310 * @param mem_ptr A pointer to the member function
UHSLMarcus 0:5edbf3550350 311 * @param pin Pin attached to nINT
UHSLMarcus 0:5edbf3550350 312 *
UHSLMarcus 0:5edbf3550350 313 * @return Attach success
UHSLMarcus 0:5edbf3550350 314 */
UHSLMarcus 0:5edbf3550350 315 template<typename T>
UHSLMarcus 0:5edbf3550350 316 bool attach(T *type_ptr, void (T::*mem_ptr)(void), PinName pin) {
UHSLMarcus 2:e394671ef5f6 317 _isr_data_fp.attach(callback(type_ptr, mem_ptr));
UHSLMarcus 2:e394671ef5f6 318 interrupt_pin(pin);
UHSLMarcus 1:acfca1d3256d 319 return enable_interupt(true);
UHSLMarcus 0:5edbf3550350 320 }
UHSLMarcus 0:5edbf3550350 321
UHSLMarcus 1:acfca1d3256d 322 /** Set whether the data ready interupt is enabled.
UHSLMarcus 1:acfca1d3256d 323 *
UHSLMarcus 1:acfca1d3256d 324 * @param enabled True for enabled, false for disabled
UHSLMarcus 0:5edbf3550350 325 *
UHSLMarcus 0:5edbf3550350 326 * @return Write success
UHSLMarcus 0:5edbf3550350 327 */
UHSLMarcus 1:acfca1d3256d 328 bool enable_interupt(bool enable);
UHSLMarcus 1:acfca1d3256d 329
UHSLMarcus 1:acfca1d3256d 330 /** Get whether the data ready interupt is enabled.
UHSLMarcus 1:acfca1d3256d 331 *
UHSLMarcus 1:acfca1d3256d 332 *
UHSLMarcus 5:41e97348e9e7 333 * @return 1 for enabled, 0 for disabled, -1 for error
UHSLMarcus 1:acfca1d3256d 334 */
UHSLMarcus 5:41e97348e9e7 335 int interupt_enabled();
UHSLMarcus 0:5edbf3550350 336
UHSLMarcus 0:5edbf3550350 337 /** Set the nINT pin
UHSLMarcus 0:5edbf3550350 338 *
UHSLMarcus 0:5edbf3550350 339 * @param pin Pin nINT is attached to
UHSLMarcus 0:5edbf3550350 340 *
UHSLMarcus 0:5edbf3550350 341 * @return Write success
UHSLMarcus 0:5edbf3550350 342 */
UHSLMarcus 0:5edbf3550350 343 bool interrupt_pin(PinName pin);
UHSLMarcus 0:5edbf3550350 344
UHSLMarcus 0:5edbf3550350 345 /** Get the the nINT pin
UHSLMarcus 0:5edbf3550350 346 *
UHSLMarcus 0:5edbf3550350 347 * @return The nINT pin
UHSLMarcus 0:5edbf3550350 348 */
UHSLMarcus 0:5edbf3550350 349 PinName interrupt_pin();
UHSLMarcus 0:5edbf3550350 350
UHSLMarcus 0:5edbf3550350 351
UHSLMarcus 0:5edbf3550350 352
UHSLMarcus 0:5edbf3550350 353
UHSLMarcus 0:5edbf3550350 354 private:
UHSLMarcus 0:5edbf3550350 355 I2C* _i2c;
UHSLMarcus 2:e394671ef5f6 356 I2C* _ens210_i2c;
UHSLMarcus 2:e394671ef5f6 357
UHSLMarcus 1:acfca1d3256d 358 bool _addr_dir;
UHSLMarcus 5:41e97348e9e7 359 int _slave_addr;
UHSLMarcus 2:e394671ef5f6 360 void update_slave_addr();
UHSLMarcus 2:e394671ef5f6 361
UHSLMarcus 4:a6b8881eae87 362 bool _ens210_enabled;
UHSLMarcus 2:e394671ef5f6 363 int _ens210_poll_split;
UHSLMarcus 4:a6b8881eae87 364 void update_ens210_timer();
UHSLMarcus 4:a6b8881eae87 365 Ticker _ens210_poll_t;
UHSLMarcus 4:a6b8881eae87 366 void ens210_isr();
UHSLMarcus 2:e394671ef5f6 367
UHSLMarcus 0:5edbf3550350 368 OP_MODES _mode;
UHSLMarcus 2:e394671ef5f6 369
UHSLMarcus 5:41e97348e9e7 370 void set_defaults();
UHSLMarcus 5:41e97348e9e7 371
UHSLMarcus 5:41e97348e9e7 372 bool _errors[CCS811_LIB_ERR_NUM];
UHSLMarcus 5:41e97348e9e7 373 int _error_count;
UHSLMarcus 5:41e97348e9e7 374 char _error_strings[CCS811_TOTAL_ERR_NUM][255];
UHSLMarcus 5:41e97348e9e7 375 void _init_errors();
UHSLMarcus 5:41e97348e9e7 376 void clear_errors();
UHSLMarcus 5:41e97348e9e7 377 void new_error(int error_id);
UHSLMarcus 0:5edbf3550350 378
UHSLMarcus 1:acfca1d3256d 379 DigitalOut *_n_wake_out;
UHSLMarcus 1:acfca1d3256d 380 DigitalOut *_addr_out;
UHSLMarcus 0:5edbf3550350 381
UHSLMarcus 5:41e97348e9e7 382 char _alg_result_data[8];
UHSLMarcus 5:41e97348e9e7 383
UHSLMarcus 0:5edbf3550350 384 FunctionPointer _isr_data_fp;
UHSLMarcus 1:acfca1d3256d 385 bool _int_data_enabled;
UHSLMarcus 1:acfca1d3256d 386 InterruptIn *_int_data;
UHSLMarcus 0:5edbf3550350 387 void _isr_data();
UHSLMarcus 0:5edbf3550350 388
UHSLMarcus 1:acfca1d3256d 389 bool write_config();
UHSLMarcus 1:acfca1d3256d 390
UHSLMarcus 5:41e97348e9e7 391 struct read_byte_result {
UHSLMarcus 1:acfca1d3256d 392 bool success;
UHSLMarcus 1:acfca1d3256d 393 uint8_t byte;
UHSLMarcus 5:41e97348e9e7 394 read_byte_result() : success(false), byte(0) {}
UHSLMarcus 2:e394671ef5f6 395 };
UHSLMarcus 5:41e97348e9e7 396 read_byte_result read_config();
UHSLMarcus 5:41e97348e9e7 397 read_byte_result read_status();
UHSLMarcus 5:41e97348e9e7 398
UHSLMarcus 5:41e97348e9e7 399 bool boot_app_start();
UHSLMarcus 2:e394671ef5f6 400
UHSLMarcus 2:e394671ef5f6 401 int i2c_read(char reg_addr, char* output, int len);
UHSLMarcus 2:e394671ef5f6 402 int i2c_write(char reg_addr, char* input, int len);
UHSLMarcus 0:5edbf3550350 403
UHSLMarcus 0:5edbf3550350 404 };
UHSLMarcus 0:5edbf3550350 405
UHSLMarcus 0:5edbf3550350 406
UHSLMarcus 0:5edbf3550350 407 #endif /* AMS_CCS811_H */