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