simple CCS811 driver

Dependencies:   AMS_ENS210_temp_humid_sensor

Dependents:   TBSense2_Sensor_Demo

Fork of AMS_CCS811_gas_sensor by Marcus Lee

Committer:
Steven Cooreman
Date:
Tue Mar 05 20:18:27 2019 +0100
Revision:
15:cf658680c53f
Parent:
8:58a36d9218be
Added FW v2.0.0 for CCS811 and introduced usage of mbed_trace

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