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:
Tue Jan 24 11:21:43 2017 +0000
Revision:
8:58a36d9218be
Parent:
7:5c95614a61ee
Child:
15:cf658680c53f
added facility to query the current temp and humid measurement from an attached ENS210

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