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 14:08:52 2017 +0000
Revision:
11:ef2d4258cd15
Parent:
10:225a71ec316c
Child:
12:45065badca0f
initialized all the pointers to NULL

Who changed what in which revision?

UserRevisionLine numberNew contents of line
UHSLMarcus 0:5edbf3550350 1
UHSLMarcus 2:e394671ef5f6 2 #include "AMS_CCS811.h"
UHSLMarcus 11:ef2d4258cd15 3 #include "messageParse.h"
UHSLMarcus 0:5edbf3550350 4
UHSLMarcus 11:ef2d4258cd15 5 AMS_CCS811::AMS_CCS811(I2C * i2c, PinName n_wake_pin) : _n_wake_out(), _addr_out(), _int_data(), _ens210(), i2c() {
UHSLMarcus 5:41e97348e9e7 6 _n_wake_out = new (std::nothrow) DigitalOut(n_wake_pin, 1);
UHSLMarcus 4:a6b8881eae87 7 _i2c = i2c;
UHSLMarcus 0:5edbf3550350 8 }
UHSLMarcus 0:5edbf3550350 9
UHSLMarcus 11:ef2d4258cd15 10 AMS_CCS811::AMS_CCS811(I2C * i2c, PinName n_wake_pin, I2C * ens210_i2c) : _n_wake_out(), _addr_out(), _int_data(), _ens210(), i2c() {
UHSLMarcus 5:41e97348e9e7 11 _n_wake_out = new (std::nothrow) DigitalOut(n_wake_pin, 1);
UHSLMarcus 0:5edbf3550350 12 _i2c = i2c;
UHSLMarcus 7:5c95614a61ee 13 ens210_i2c_interface(ens210_i2c);
UHSLMarcus 0:5edbf3550350 14 }
UHSLMarcus 0:5edbf3550350 15
UHSLMarcus 1:acfca1d3256d 16 AMS_CCS811::~AMS_CCS811() {
UHSLMarcus 1:acfca1d3256d 17 delete _n_wake_out;
UHSLMarcus 1:acfca1d3256d 18 delete _addr_out;
UHSLMarcus 1:acfca1d3256d 19 delete _int_data;
UHSLMarcus 7:5c95614a61ee 20 delete _ens210;
UHSLMarcus 1:acfca1d3256d 21 }
UHSLMarcus 1:acfca1d3256d 22
UHSLMarcus 1:acfca1d3256d 23 bool AMS_CCS811::init() {
UHSLMarcus 5:41e97348e9e7 24
UHSLMarcus 5:41e97348e9e7 25 bool success = false;
UHSLMarcus 5:41e97348e9e7 26
UHSLMarcus 5:41e97348e9e7 27 _init_errors();
UHSLMarcus 7:5c95614a61ee 28 _init_fractions();
UHSLMarcus 5:41e97348e9e7 29 set_defaults();
UHSLMarcus 5:41e97348e9e7 30
UHSLMarcus 8:58a36d9218be 31 temp_reading = 0;
UHSLMarcus 8:58a36d9218be 32 humid_reading = 0;
UHSLMarcus 8:58a36d9218be 33
UHSLMarcus 5:41e97348e9e7 34 if (_n_wake_out) {
UHSLMarcus 5:41e97348e9e7 35
UHSLMarcus 5:41e97348e9e7 36 int fw_mode = firmware_mode();
UHSLMarcus 5:41e97348e9e7 37
UHSLMarcus 5:41e97348e9e7 38 if (fw_mode == 1) {
UHSLMarcus 5:41e97348e9e7 39 enable_ens210(true);
UHSLMarcus 5:41e97348e9e7 40 success = write_config();
UHSLMarcus 5:41e97348e9e7 41
UHSLMarcus 5:41e97348e9e7 42 } else if (fw_mode == 0) { // is in boot mode, needs to be loaded into app mode
UHSLMarcus 5:41e97348e9e7 43 if (boot_app_start()) // if succesfully writes to app_start, retry init
UHSLMarcus 5:41e97348e9e7 44 init();
UHSLMarcus 5:41e97348e9e7 45 }
UHSLMarcus 5:41e97348e9e7 46 }
UHSLMarcus 5:41e97348e9e7 47 return success;
UHSLMarcus 4:a6b8881eae87 48 }
UHSLMarcus 4:a6b8881eae87 49
UHSLMarcus 4:a6b8881eae87 50 void AMS_CCS811::i2c_interface(I2C * i2c) {
UHSLMarcus 4:a6b8881eae87 51 _i2c = i2c;
UHSLMarcus 4:a6b8881eae87 52 }
UHSLMarcus 4:a6b8881eae87 53
UHSLMarcus 7:5c95614a61ee 54 bool AMS_CCS811::ens210_i2c_interface(I2C * i2c) {
UHSLMarcus 11:ef2d4258cd15 55
UHSLMarcus 7:5c95614a61ee 56 bool success;
UHSLMarcus 11:ef2d4258cd15 57
UHSLMarcus 7:5c95614a61ee 58 if (_ens210 == NULL) {
UHSLMarcus 7:5c95614a61ee 59 _ens210 = new (std::nothrow) AMS_ENS210(i2c, true, true);
UHSLMarcus 7:5c95614a61ee 60 if (_ens210 != NULL) {
UHSLMarcus 7:5c95614a61ee 61 if (_ens210->init()) {
UHSLMarcus 7:5c95614a61ee 62 success = _ens210->start();
UHSLMarcus 7:5c95614a61ee 63 }
UHSLMarcus 7:5c95614a61ee 64 }
UHSLMarcus 7:5c95614a61ee 65 } else {
UHSLMarcus 7:5c95614a61ee 66 _ens210->i2c_interface(i2c);
UHSLMarcus 7:5c95614a61ee 67 success = true;
UHSLMarcus 7:5c95614a61ee 68 }
UHSLMarcus 7:5c95614a61ee 69
UHSLMarcus 7:5c95614a61ee 70 if (!success) new_error(CCS811_LIB_ENS210_INIT_ID);
UHSLMarcus 7:5c95614a61ee 71
UHSLMarcus 7:5c95614a61ee 72 return success;
UHSLMarcus 4:a6b8881eae87 73 }
UHSLMarcus 4:a6b8881eae87 74
UHSLMarcus 4:a6b8881eae87 75 bool AMS_CCS811::enable_ens210(bool enable) {
UHSLMarcus 4:a6b8881eae87 76
UHSLMarcus 4:a6b8881eae87 77 _ens210_enabled = false;
UHSLMarcus 7:5c95614a61ee 78 if (_ens210 != NULL) {
UHSLMarcus 7:5c95614a61ee 79 if (_ens210->i2c_interface() != NULL) _ens210_enabled = enable;
UHSLMarcus 7:5c95614a61ee 80 }
UHSLMarcus 4:a6b8881eae87 81 update_ens210_timer();
UHSLMarcus 4:a6b8881eae87 82 return _ens210_enabled;
UHSLMarcus 4:a6b8881eae87 83 }
UHSLMarcus 4:a6b8881eae87 84
UHSLMarcus 4:a6b8881eae87 85 bool AMS_CCS811::ens210_is_enabled() {
UHSLMarcus 7:5c95614a61ee 86 enable_ens210(_ens210_enabled); // Make sure the state is representive
UHSLMarcus 4:a6b8881eae87 87 return _ens210_enabled;
UHSLMarcus 4:a6b8881eae87 88 }
UHSLMarcus 4:a6b8881eae87 89
UHSLMarcus 4:a6b8881eae87 90 void AMS_CCS811::ens210_poll_interval(int poll_ms) {
UHSLMarcus 4:a6b8881eae87 91 _ens210_poll_split = poll_ms;
UHSLMarcus 7:5c95614a61ee 92 enable_ens210(_ens210_enabled); // makes sure the state is representive, and will also update the timer
UHSLMarcus 4:a6b8881eae87 93 }
UHSLMarcus 4:a6b8881eae87 94
UHSLMarcus 4:a6b8881eae87 95 int AMS_CCS811::ens210_poll_interval() {
UHSLMarcus 4:a6b8881eae87 96 return _ens210_poll_split;
UHSLMarcus 1:acfca1d3256d 97 }
UHSLMarcus 1:acfca1d3256d 98
UHSLMarcus 5:41e97348e9e7 99 int AMS_CCS811::firmware_mode() {
UHSLMarcus 5:41e97348e9e7 100 int firmware_result = -1;
UHSLMarcus 5:41e97348e9e7 101
UHSLMarcus 6:22c0a7f2ece2 102 clear_errors();
UHSLMarcus 6:22c0a7f2ece2 103
UHSLMarcus 5:41e97348e9e7 104 read_byte_result read_result = read_status();
UHSLMarcus 5:41e97348e9e7 105 if (read_result.success) {
UHSLMarcus 5:41e97348e9e7 106 firmware_result = (read_result.byte >> 7) & 1;
UHSLMarcus 6:22c0a7f2ece2 107 }
UHSLMarcus 5:41e97348e9e7 108
UHSLMarcus 5:41e97348e9e7 109 return firmware_result;
UHSLMarcus 5:41e97348e9e7 110 }
UHSLMarcus 5:41e97348e9e7 111
UHSLMarcus 1:acfca1d3256d 112 bool AMS_CCS811::mode(OP_MODES mode) {
UHSLMarcus 6:22c0a7f2ece2 113 clear_errors();
UHSLMarcus 6:22c0a7f2ece2 114
UHSLMarcus 1:acfca1d3256d 115 OP_MODES old = _mode; // incase the write fails, to roll back
UHSLMarcus 1:acfca1d3256d 116 _mode = mode;
UHSLMarcus 1:acfca1d3256d 117
UHSLMarcus 1:acfca1d3256d 118 bool success = write_config();
UHSLMarcus 1:acfca1d3256d 119 if (!success)
UHSLMarcus 1:acfca1d3256d 120 _mode = old;
UHSLMarcus 1:acfca1d3256d 121
UHSLMarcus 1:acfca1d3256d 122 return success;
UHSLMarcus 1:acfca1d3256d 123 }
UHSLMarcus 1:acfca1d3256d 124
UHSLMarcus 1:acfca1d3256d 125 AMS_CCS811::OP_MODES AMS_CCS811::mode() {
UHSLMarcus 6:22c0a7f2ece2 126 clear_errors();
UHSLMarcus 6:22c0a7f2ece2 127
UHSLMarcus 5:41e97348e9e7 128 OP_MODES result = INVALID;
UHSLMarcus 1:acfca1d3256d 129
UHSLMarcus 5:41e97348e9e7 130 read_byte_result read_result = read_config();
UHSLMarcus 1:acfca1d3256d 131 if (read_result.success) {
UHSLMarcus 1:acfca1d3256d 132 int mode = (read_result.byte >> 4) & 0b111;
UHSLMarcus 2:e394671ef5f6 133 result = mode > 4 ? INVALID : (OP_MODES)mode;
UHSLMarcus 6:22c0a7f2ece2 134 }
UHSLMarcus 1:acfca1d3256d 135
UHSLMarcus 2:e394671ef5f6 136 return result;
UHSLMarcus 1:acfca1d3256d 137 }
UHSLMarcus 1:acfca1d3256d 138
UHSLMarcus 1:acfca1d3256d 139 bool AMS_CCS811::addr_mode(bool high) {
UHSLMarcus 1:acfca1d3256d 140 _addr_dir = high;
UHSLMarcus 5:41e97348e9e7 141 if (_addr_out != NULL) _addr_out->write(_addr_dir);
UHSLMarcus 1:acfca1d3256d 142
UHSLMarcus 2:e394671ef5f6 143 update_slave_addr();
UHSLMarcus 2:e394671ef5f6 144
UHSLMarcus 1:acfca1d3256d 145 return addr_mode() == high;
UHSLMarcus 1:acfca1d3256d 146 }
UHSLMarcus 1:acfca1d3256d 147
UHSLMarcus 1:acfca1d3256d 148 bool AMS_CCS811::addr_mode() {
UHSLMarcus 1:acfca1d3256d 149 if (_addr_out != NULL) {
UHSLMarcus 5:41e97348e9e7 150 _addr_dir = _addr_out->read();
UHSLMarcus 1:acfca1d3256d 151 }
UHSLMarcus 1:acfca1d3256d 152
UHSLMarcus 1:acfca1d3256d 153 return _addr_dir;
UHSLMarcus 1:acfca1d3256d 154 }
UHSLMarcus 1:acfca1d3256d 155
UHSLMarcus 1:acfca1d3256d 156 bool AMS_CCS811::addr_pin(PinName pin) {
UHSLMarcus 1:acfca1d3256d 157 _addr_out = _addr_out == NULL ? new (std::nothrow) DigitalOut(pin) : new (_addr_out) DigitalOut(pin);
UHSLMarcus 1:acfca1d3256d 158 addr_mode(_addr_dir);
UHSLMarcus 1:acfca1d3256d 159
UHSLMarcus 1:acfca1d3256d 160 return _addr_out != NULL;
UHSLMarcus 1:acfca1d3256d 161 }
UHSLMarcus 1:acfca1d3256d 162
UHSLMarcus 1:acfca1d3256d 163 bool AMS_CCS811::n_wake_pin(PinName pin) {
UHSLMarcus 1:acfca1d3256d 164 _n_wake_out = _n_wake_out == NULL ? new (std::nothrow) DigitalOut(pin) : new (_n_wake_out) DigitalOut(pin);
UHSLMarcus 1:acfca1d3256d 165 return _n_wake_out != NULL;
UHSLMarcus 1:acfca1d3256d 166 }
UHSLMarcus 1:acfca1d3256d 167
UHSLMarcus 2:e394671ef5f6 168 bool AMS_CCS811::env_data(float humid, float temp) {
UHSLMarcus 7:5c95614a61ee 169 char bytes[4];
UHSLMarcus 7:5c95614a61ee 170 if (humid > CCS811_MAX_HUMID) humid = CCS811_MAX_HUMID;
UHSLMarcus 7:5c95614a61ee 171 if (humid < 0) humid = 0;
UHSLMarcus 7:5c95614a61ee 172
UHSLMarcus 7:5c95614a61ee 173 temp += 25;
UHSLMarcus 7:5c95614a61ee 174 if (temp > CCS811_MAX_TEMP) humid = CCS811_MAX_TEMP;
UHSLMarcus 7:5c95614a61ee 175 if (temp < 0) temp = 0;
UHSLMarcus 7:5c95614a61ee 176
UHSLMarcus 7:5c95614a61ee 177 float_to_short(humid, bytes);
UHSLMarcus 7:5c95614a61ee 178 float_to_short(temp, bytes+2);
UHSLMarcus 7:5c95614a61ee 179
UHSLMarcus 7:5c95614a61ee 180 return i2c_write(ENV_DATA, bytes, 4) == 4;
UHSLMarcus 1:acfca1d3256d 181 }
UHSLMarcus 1:acfca1d3256d 182
UHSLMarcus 1:acfca1d3256d 183
UHSLMarcus 5:41e97348e9e7 184 int AMS_CCS811::has_new_data() {
UHSLMarcus 6:22c0a7f2ece2 185
UHSLMarcus 6:22c0a7f2ece2 186 clear_errors();
UHSLMarcus 6:22c0a7f2ece2 187
UHSLMarcus 5:41e97348e9e7 188 int result = -1;
UHSLMarcus 5:41e97348e9e7 189
UHSLMarcus 6:22c0a7f2ece2 190 char meas_mode[1];
UHSLMarcus 6:22c0a7f2ece2 191 if(i2c_read(MEAS_MODE, meas_mode, 1) == 1) { // one read here is quicker than calling read_config() twice
UHSLMarcus 6:22c0a7f2ece2 192
UHSLMarcus 6:22c0a7f2ece2 193 int curr_mode = (meas_mode[0] >> 4) & 0b111;
UHSLMarcus 6:22c0a7f2ece2 194 if (curr_mode < 5) {
UHSLMarcus 6:22c0a7f2ece2 195 if (curr_mode > 0) { // check for all valid modes other than idle
UHSLMarcus 6:22c0a7f2ece2 196 if (((meas_mode[0] >> 3) & 1) == 0) { // check if interrupts are disabled
UHSLMarcus 6:22c0a7f2ece2 197 char status[1];
UHSLMarcus 6:22c0a7f2ece2 198 if (i2c_read(STATUS, status, 1) == 1) // for some reason the status register in ALG_RESULT_DATA is not updated after reading data, however the STATUS register is
UHSLMarcus 6:22c0a7f2ece2 199 result = (status[0] >> 3) & 1;
UHSLMarcus 6:22c0a7f2ece2 200
UHSLMarcus 6:22c0a7f2ece2 201 } else result = 1;
UHSLMarcus 6:22c0a7f2ece2 202
UHSLMarcus 6:22c0a7f2ece2 203 if (result == 1)
UHSLMarcus 6:22c0a7f2ece2 204 if (i2c_read(ALG_RESULT_DATA, _alg_result_data, 8) != 8) result = -1;
UHSLMarcus 6:22c0a7f2ece2 205
UHSLMarcus 6:22c0a7f2ece2 206
UHSLMarcus 6:22c0a7f2ece2 207 } else result = 0; // return 0 when in idle
UHSLMarcus 6:22c0a7f2ece2 208 } else new_error(CCS811_LIB_INV_MODE_ID);
UHSLMarcus 5:41e97348e9e7 209 }
UHSLMarcus 5:41e97348e9e7 210
UHSLMarcus 5:41e97348e9e7 211 return result;
UHSLMarcus 1:acfca1d3256d 212 }
UHSLMarcus 1:acfca1d3256d 213
UHSLMarcus 1:acfca1d3256d 214 uint16_t AMS_CCS811::co2_read() {
UHSLMarcus 6:22c0a7f2ece2 215 return 0 | (_alg_result_data[0] << 8) | _alg_result_data[1];
UHSLMarcus 2:e394671ef5f6 216 }
UHSLMarcus 1:acfca1d3256d 217
UHSLMarcus 1:acfca1d3256d 218 uint16_t AMS_CCS811::tvoc_read() {
UHSLMarcus 6:22c0a7f2ece2 219 return 0 | (_alg_result_data[2] << 8) | _alg_result_data[3];
UHSLMarcus 1:acfca1d3256d 220 }
UHSLMarcus 1:acfca1d3256d 221
UHSLMarcus 1:acfca1d3256d 222 uint16_t AMS_CCS811::raw_read() {
UHSLMarcus 6:22c0a7f2ece2 223 return 0 | (_alg_result_data[6] << 8) | _alg_result_data[7];
UHSLMarcus 1:acfca1d3256d 224 }
UHSLMarcus 1:acfca1d3256d 225
UHSLMarcus 8:58a36d9218be 226 float AMS_CCS811::temp_read() {
UHSLMarcus 8:58a36d9218be 227 return temp_reading;
UHSLMarcus 8:58a36d9218be 228 }
UHSLMarcus 8:58a36d9218be 229
UHSLMarcus 8:58a36d9218be 230 float AMS_CCS811::humid_read() {
UHSLMarcus 8:58a36d9218be 231 return humid_reading;
UHSLMarcus 8:58a36d9218be 232 }
UHSLMarcus 8:58a36d9218be 233
UHSLMarcus 5:41e97348e9e7 234 bool AMS_CCS811::error_status() {
UHSLMarcus 5:41e97348e9e7 235 bool result = false;
UHSLMarcus 5:41e97348e9e7 236
UHSLMarcus 5:41e97348e9e7 237 read_byte_result read_result = read_status();
UHSLMarcus 5:41e97348e9e7 238 if (read_result.success) {
UHSLMarcus 5:41e97348e9e7 239 result = read_result.byte & 1;
UHSLMarcus 5:41e97348e9e7 240 }
UHSLMarcus 5:41e97348e9e7 241
UHSLMarcus 5:41e97348e9e7 242 result = result || (_error_count > 0);
UHSLMarcus 5:41e97348e9e7 243
UHSLMarcus 5:41e97348e9e7 244 return result;
UHSLMarcus 5:41e97348e9e7 245 }
UHSLMarcus 5:41e97348e9e7 246
UHSLMarcus 5:41e97348e9e7 247 AMS_CCS811::ccs811_errors AMS_CCS811::errors() {
UHSLMarcus 5:41e97348e9e7 248 ccs811_errors error_result;
UHSLMarcus 5:41e97348e9e7 249
UHSLMarcus 5:41e97348e9e7 250 char byte[1];
UHSLMarcus 5:41e97348e9e7 251 if (i2c_read(ERROR_ID, byte, 1) == 1) {
UHSLMarcus 5:41e97348e9e7 252 for(int i = 0; i < CCS811_ERR_NUM; i++) {
UHSLMarcus 5:41e97348e9e7 253 if ((byte[0] << i) & 1) {
UHSLMarcus 5:41e97348e9e7 254 error_result.codes[error_result.count++] = i;
UHSLMarcus 5:41e97348e9e7 255 }
UHSLMarcus 5:41e97348e9e7 256 }
UHSLMarcus 5:41e97348e9e7 257 }
UHSLMarcus 5:41e97348e9e7 258 for(int i = 0; i < CCS811_LIB_ERR_NUM; i++) {
UHSLMarcus 5:41e97348e9e7 259 if (_errors[i]) {
UHSLMarcus 5:41e97348e9e7 260 error_result.codes[error_result.count++] = i + CCS811_ERR_NUM;
UHSLMarcus 5:41e97348e9e7 261 }
UHSLMarcus 5:41e97348e9e7 262 }
UHSLMarcus 5:41e97348e9e7 263
UHSLMarcus 5:41e97348e9e7 264 return error_result;
UHSLMarcus 5:41e97348e9e7 265
UHSLMarcus 5:41e97348e9e7 266 }
UHSLMarcus 5:41e97348e9e7 267
UHSLMarcus 5:41e97348e9e7 268 const char * AMS_CCS811::error_string(int err_code){
UHSLMarcus 5:41e97348e9e7 269 static char result[255];
UHSLMarcus 5:41e97348e9e7 270 result[0] = 0;
UHSLMarcus 5:41e97348e9e7 271 if (err_code < CCS811_TOTAL_ERR_NUM && err_code > -1)
UHSLMarcus 5:41e97348e9e7 272 strcpy(result, _error_strings[err_code]);
UHSLMarcus 5:41e97348e9e7 273 else
UHSLMarcus 5:41e97348e9e7 274 sprintf(result, "Invalid Code: %d is out of range (0 - %d)", err_code, CCS811_TOTAL_ERR_NUM-1);
UHSLMarcus 5:41e97348e9e7 275
UHSLMarcus 5:41e97348e9e7 276 return result;
UHSLMarcus 1:acfca1d3256d 277 }
UHSLMarcus 1:acfca1d3256d 278
UHSLMarcus 1:acfca1d3256d 279 bool AMS_CCS811::enable_interupt(bool enable) {
UHSLMarcus 1:acfca1d3256d 280 bool old = _int_data_enabled; // incase the write fails, to roll back
UHSLMarcus 1:acfca1d3256d 281 _int_data_enabled = enable;
UHSLMarcus 1:acfca1d3256d 282
UHSLMarcus 1:acfca1d3256d 283 bool success = write_config();
UHSLMarcus 1:acfca1d3256d 284 if (!success)
UHSLMarcus 1:acfca1d3256d 285 _int_data_enabled = old;
UHSLMarcus 1:acfca1d3256d 286
UHSLMarcus 1:acfca1d3256d 287 return success;
UHSLMarcus 1:acfca1d3256d 288
UHSLMarcus 1:acfca1d3256d 289 }
UHSLMarcus 1:acfca1d3256d 290
UHSLMarcus 5:41e97348e9e7 291 int AMS_CCS811::interupt_enabled() {
UHSLMarcus 5:41e97348e9e7 292 int enabled = -1;
UHSLMarcus 1:acfca1d3256d 293
UHSLMarcus 5:41e97348e9e7 294 read_byte_result read_result = read_config();
UHSLMarcus 1:acfca1d3256d 295 if (read_result.success) {
UHSLMarcus 1:acfca1d3256d 296 enabled = (read_result.byte >> 3) & 1;
UHSLMarcus 6:22c0a7f2ece2 297 }
UHSLMarcus 1:acfca1d3256d 298
UHSLMarcus 2:e394671ef5f6 299 return enabled;
UHSLMarcus 1:acfca1d3256d 300 }
UHSLMarcus 1:acfca1d3256d 301
UHSLMarcus 1:acfca1d3256d 302 bool AMS_CCS811::interrupt_pin(PinName pin) {
UHSLMarcus 1:acfca1d3256d 303 bool success = false;
UHSLMarcus 1:acfca1d3256d 304
UHSLMarcus 2:e394671ef5f6 305 _int_data = _int_data == NULL ? new (std::nothrow) InterruptIn(pin) : new (_int_data) InterruptIn(pin);
UHSLMarcus 1:acfca1d3256d 306 if (_int_data != NULL) {
UHSLMarcus 2:e394671ef5f6 307 _int_data->fall(callback(this, &AMS_CCS811::_isr_data));
UHSLMarcus 1:acfca1d3256d 308 success = true;
UHSLMarcus 1:acfca1d3256d 309 }
UHSLMarcus 1:acfca1d3256d 310
UHSLMarcus 1:acfca1d3256d 311 return success;
UHSLMarcus 1:acfca1d3256d 312 }
UHSLMarcus 0:5edbf3550350 313
UHSLMarcus 0:5edbf3550350 314
UHSLMarcus 0:5edbf3550350 315
UHSLMarcus 1:acfca1d3256d 316
UHSLMarcus 1:acfca1d3256d 317 /** Private **/
UHSLMarcus 1:acfca1d3256d 318
UHSLMarcus 5:41e97348e9e7 319 void AMS_CCS811::set_defaults() {
UHSLMarcus 1:acfca1d3256d 320 if (_mode == NULL)
UHSLMarcus 1:acfca1d3256d 321 _mode = CONFIG_OP_MODE;
UHSLMarcus 1:acfca1d3256d 322 if (_addr_dir == NULL)
UHSLMarcus 1:acfca1d3256d 323 _addr_dir = CONFIG_ADDR_DIR;
UHSLMarcus 1:acfca1d3256d 324 if (_int_data_enabled == NULL)
UHSLMarcus 1:acfca1d3256d 325 _int_data_enabled = CONFIG_INTR;
UHSLMarcus 2:e394671ef5f6 326 if (_ens210_poll_split == NULL)
UHSLMarcus 2:e394671ef5f6 327 _ens210_poll_split = CONFIG_ENS210_POLL;
UHSLMarcus 2:e394671ef5f6 328
UHSLMarcus 5:41e97348e9e7 329 update_slave_addr();
UHSLMarcus 5:41e97348e9e7 330 }
UHSLMarcus 5:41e97348e9e7 331
UHSLMarcus 5:41e97348e9e7 332 void AMS_CCS811::_init_errors() {
UHSLMarcus 5:41e97348e9e7 333 clear_errors();
UHSLMarcus 5:41e97348e9e7 334 /* Sensor errors */
UHSLMarcus 5:41e97348e9e7 335 strcpy(_error_strings[0], CCS811_WRITE_REG_INVALID);
UHSLMarcus 5:41e97348e9e7 336 strcpy(_error_strings[1], CCS811_READ_REG_INVALID);
UHSLMarcus 5:41e97348e9e7 337 strcpy(_error_strings[2], CCS811_MEASMODE_INVALID);
UHSLMarcus 5:41e97348e9e7 338 strcpy(_error_strings[3], CCS811_MAX_RESISTANCE);
UHSLMarcus 5:41e97348e9e7 339 strcpy(_error_strings[4], CCS811_HEATER_FAULT);
UHSLMarcus 5:41e97348e9e7 340 strcpy(_error_strings[5], CCS811_HEATER_SUPPLY);
UHSLMarcus 5:41e97348e9e7 341 strcpy(_error_strings[6], CCS811_RESERVED);
UHSLMarcus 5:41e97348e9e7 342 strcpy(_error_strings[7], CCS811_RESERVED);
UHSLMarcus 5:41e97348e9e7 343 /* Library errors */
UHSLMarcus 5:41e97348e9e7 344 strcpy(_error_strings[CCS811_LIB_N_WAKE_ID+CCS811_ERR_NUM], CCS811_LIB_N_WAKE);
UHSLMarcus 5:41e97348e9e7 345 strcpy(_error_strings[CCS811_LIB_I2C_ID+CCS811_ERR_NUM], CCS811_LIB_I2C);
UHSLMarcus 5:41e97348e9e7 346 strcpy(_error_strings[CCS811_LIB_SLAVE_W_ID+CCS811_ERR_NUM], CCS811_LIB_SLAVE_W);
UHSLMarcus 5:41e97348e9e7 347 strcpy(_error_strings[CCS811_LIB_REG_ADDR_ID+CCS811_ERR_NUM], CCS811_LIB_REG_ADDR);
UHSLMarcus 5:41e97348e9e7 348 strcpy(_error_strings[CCS811_LIB_I2CWRITE_ID+CCS811_ERR_NUM], CCS811_LIB_I2CWRITE);
UHSLMarcus 5:41e97348e9e7 349 strcpy(_error_strings[CCS811_LIB_SLAVE_R_ID+CCS811_ERR_NUM], CCS811_LIB_SLAVE_R);
UHSLMarcus 6:22c0a7f2ece2 350 strcpy(_error_strings[CCS811_LIB_INV_MODE_ID+CCS811_ERR_NUM], CCS811_LIB_INV_MODE);
UHSLMarcus 7:5c95614a61ee 351 strcpy(_error_strings[CCS811_LIB_ENS210_INIT_ID+CCS811_ERR_NUM], CCS811_LIB_ENS210_INIT);
UHSLMarcus 5:41e97348e9e7 352 }
UHSLMarcus 5:41e97348e9e7 353
UHSLMarcus 5:41e97348e9e7 354 void AMS_CCS811::clear_errors() {
UHSLMarcus 5:41e97348e9e7 355 _error_count = 0;
UHSLMarcus 5:41e97348e9e7 356 for (int i = 0; i < CCS811_LIB_ERR_NUM; i++) {
UHSLMarcus 5:41e97348e9e7 357 _errors[i] = false;
UHSLMarcus 5:41e97348e9e7 358 }
UHSLMarcus 5:41e97348e9e7 359 }
UHSLMarcus 5:41e97348e9e7 360
UHSLMarcus 5:41e97348e9e7 361 void AMS_CCS811::new_error(int error_id) {
UHSLMarcus 5:41e97348e9e7 362 if (!_errors[error_id]) {
UHSLMarcus 5:41e97348e9e7 363 _errors[error_id] = true;
UHSLMarcus 5:41e97348e9e7 364 _error_count++;
UHSLMarcus 5:41e97348e9e7 365 }
UHSLMarcus 1:acfca1d3256d 366 }
UHSLMarcus 2:e394671ef5f6 367
UHSLMarcus 4:a6b8881eae87 368 void AMS_CCS811::update_ens210_timer() {
UHSLMarcus 4:a6b8881eae87 369 _ens210_poll_t.detach();
UHSLMarcus 4:a6b8881eae87 370 if (_ens210_enabled)
UHSLMarcus 4:a6b8881eae87 371 _ens210_poll_t.attach_us(callback(this, &AMS_CCS811::ens210_isr), _ens210_poll_split*1000);
UHSLMarcus 4:a6b8881eae87 372 }
UHSLMarcus 4:a6b8881eae87 373
UHSLMarcus 4:a6b8881eae87 374 void AMS_CCS811::ens210_isr() {
UHSLMarcus 8:58a36d9218be 375 temp_reading = ((float)_ens210->temp_read() / 64) - - 273.15;
UHSLMarcus 8:58a36d9218be 376 humid_reading = (float)_ens210->humid_read()/512;
UHSLMarcus 8:58a36d9218be 377 env_data(humid_reading, temp_reading);
UHSLMarcus 7:5c95614a61ee 378 }
UHSLMarcus 7:5c95614a61ee 379
UHSLMarcus 7:5c95614a61ee 380 void AMS_CCS811::_init_fractions() {
UHSLMarcus 7:5c95614a61ee 381
UHSLMarcus 7:5c95614a61ee 382 fractions[0] = 0.5;
UHSLMarcus 7:5c95614a61ee 383 fractions[1] = 0.25;
UHSLMarcus 7:5c95614a61ee 384 fractions[2] = 0.125;
UHSLMarcus 7:5c95614a61ee 385 fractions[3] = 0.0625;
UHSLMarcus 7:5c95614a61ee 386 fractions[4] = 0.03125;
UHSLMarcus 7:5c95614a61ee 387 fractions[5] = 0.015625;
UHSLMarcus 7:5c95614a61ee 388 fractions[6] = 0.0078125;
UHSLMarcus 7:5c95614a61ee 389 fractions[7] = 0.00390625;
UHSLMarcus 7:5c95614a61ee 390 fractions[8] = 0.001953125;
UHSLMarcus 7:5c95614a61ee 391
UHSLMarcus 7:5c95614a61ee 392 }
UHSLMarcus 7:5c95614a61ee 393
UHSLMarcus 7:5c95614a61ee 394 void AMS_CCS811::float_to_short(float in, char * output) {
UHSLMarcus 7:5c95614a61ee 395
UHSLMarcus 7:5c95614a61ee 396 uint8_t int_part = (uint8_t)in;
UHSLMarcus 7:5c95614a61ee 397 float dec_part = in - int_part;
UHSLMarcus 7:5c95614a61ee 398
UHSLMarcus 7:5c95614a61ee 399 uint16_t _short = 0;
UHSLMarcus 7:5c95614a61ee 400 for (int i = 0; i < 9; i++) {
UHSLMarcus 7:5c95614a61ee 401 if (dec_part == 0) break;
UHSLMarcus 7:5c95614a61ee 402 if (dec_part >= fractions[i]) {
UHSLMarcus 7:5c95614a61ee 403 dec_part -= fractions[i];
UHSLMarcus 7:5c95614a61ee 404 _short |= 256 >> i;
UHSLMarcus 10:225a71ec316c 405 }
UHSLMarcus 7:5c95614a61ee 406 }
UHSLMarcus 7:5c95614a61ee 407
UHSLMarcus 7:5c95614a61ee 408 _short |= int_part << 9;
UHSLMarcus 7:5c95614a61ee 409
UHSLMarcus 7:5c95614a61ee 410 output[0] = _short >> 8;
UHSLMarcus 7:5c95614a61ee 411 output[1] = _short;
UHSLMarcus 4:a6b8881eae87 412 }
UHSLMarcus 4:a6b8881eae87 413
UHSLMarcus 3:782a719f47a5 414 void AMS_CCS811::update_slave_addr() {
UHSLMarcus 5:41e97348e9e7 415 _slave_addr = addr_mode() ? CCS811_SLAVE_ADDR_RAW_H : CCS811_SLAVE_ADDR_RAW_L;
UHSLMarcus 2:e394671ef5f6 416 }
UHSLMarcus 1:acfca1d3256d 417
UHSLMarcus 1:acfca1d3256d 418 void AMS_CCS811::_isr_data() {
UHSLMarcus 6:22c0a7f2ece2 419 has_new_data(); // populate the data array
UHSLMarcus 3:782a719f47a5 420 _isr_data_fp.call();
UHSLMarcus 1:acfca1d3256d 421 }
UHSLMarcus 1:acfca1d3256d 422
UHSLMarcus 1:acfca1d3256d 423 bool AMS_CCS811::write_config() {
UHSLMarcus 3:782a719f47a5 424 char cmd[1] = {0 | (_int_data_enabled << 3) | (_mode << 4)};
UHSLMarcus 5:41e97348e9e7 425 return i2c_write(MEAS_MODE, cmd, 1) == 1;
UHSLMarcus 5:41e97348e9e7 426 }
UHSLMarcus 5:41e97348e9e7 427
UHSLMarcus 5:41e97348e9e7 428 AMS_CCS811::read_byte_result AMS_CCS811::read_config() {
UHSLMarcus 5:41e97348e9e7 429 read_byte_result result;
UHSLMarcus 5:41e97348e9e7 430 char byte[1];
UHSLMarcus 5:41e97348e9e7 431 if (i2c_read(MEAS_MODE, byte, 1) == 1) {
UHSLMarcus 5:41e97348e9e7 432 result.success = true;
UHSLMarcus 5:41e97348e9e7 433 result.byte = byte[0];
UHSLMarcus 5:41e97348e9e7 434 }
UHSLMarcus 5:41e97348e9e7 435 return result;
UHSLMarcus 1:acfca1d3256d 436 }
UHSLMarcus 1:acfca1d3256d 437
UHSLMarcus 5:41e97348e9e7 438 AMS_CCS811::read_byte_result AMS_CCS811::read_status() {
UHSLMarcus 5:41e97348e9e7 439 read_byte_result result;
UHSLMarcus 2:e394671ef5f6 440 char byte[1];
UHSLMarcus 5:41e97348e9e7 441 if (i2c_read(STATUS, byte, 1) == 1) {
UHSLMarcus 2:e394671ef5f6 442 result.success = true;
UHSLMarcus 5:41e97348e9e7 443 result.byte = byte[0];
UHSLMarcus 5:41e97348e9e7 444 }
UHSLMarcus 6:22c0a7f2ece2 445
UHSLMarcus 5:41e97348e9e7 446 return result;
UHSLMarcus 5:41e97348e9e7 447 }
UHSLMarcus 5:41e97348e9e7 448
UHSLMarcus 5:41e97348e9e7 449 bool AMS_CCS811::boot_app_start() {
UHSLMarcus 5:41e97348e9e7 450 bool success = false;
UHSLMarcus 5:41e97348e9e7 451
UHSLMarcus 5:41e97348e9e7 452 if (i2c_write(APP_START, NULL, 0) == 0) {
UHSLMarcus 5:41e97348e9e7 453 wait_ms(70);
UHSLMarcus 5:41e97348e9e7 454 success = true;
UHSLMarcus 2:e394671ef5f6 455 }
UHSLMarcus 2:e394671ef5f6 456
UHSLMarcus 5:41e97348e9e7 457 return success;
UHSLMarcus 2:e394671ef5f6 458 }
UHSLMarcus 2:e394671ef5f6 459
UHSLMarcus 2:e394671ef5f6 460 int AMS_CCS811::i2c_read(char reg_addr, char* output, int len) {
UHSLMarcus 2:e394671ef5f6 461
UHSLMarcus 2:e394671ef5f6 462 int read_count = 0;
UHSLMarcus 2:e394671ef5f6 463 if (_n_wake_out != NULL) { // check nWAKE pin is set
UHSLMarcus 5:41e97348e9e7 464 _n_wake_out->write(0); // Hold low
UHSLMarcus 5:41e97348e9e7 465 wait_us(CCS811_T_AWAKE); // tAWAKE time to allow sensor I2C to wake up
UHSLMarcus 2:e394671ef5f6 466 if (_i2c != NULL) { // check I2C interface is set
UHSLMarcus 2:e394671ef5f6 467 _i2c->start(); // send start condition for write
UHSLMarcus 5:41e97348e9e7 468 if(_i2c->write(CCS811_SLAVE_ADDR_W) == 1) { // write slave address with write bit
UHSLMarcus 2:e394671ef5f6 469 if(_i2c->write(reg_addr) == 1) { // write register address
UHSLMarcus 2:e394671ef5f6 470 _i2c->start(); // send another start condition for read
UHSLMarcus 5:41e97348e9e7 471 if(_i2c->write(CCS811_SLAVE_ADDR_R) == 1) { // write slave address with read bit
UHSLMarcus 2:e394671ef5f6 472 for (int i = 0; i < len; i++) { // read len bytes
UHSLMarcus 2:e394671ef5f6 473 output[i] = _i2c->read(i < len-1 ? 1 : 0); // ack all reads aside from the final one (i == len-1)
UHSLMarcus 2:e394671ef5f6 474 read_count++;
UHSLMarcus 2:e394671ef5f6 475 }
UHSLMarcus 5:41e97348e9e7 476 } else new_error(CCS811_LIB_SLAVE_R_ID);
UHSLMarcus 5:41e97348e9e7 477 } else new_error(CCS811_LIB_REG_ADDR_ID);
UHSLMarcus 5:41e97348e9e7 478 } else new_error(CCS811_LIB_SLAVE_W_ID);
UHSLMarcus 2:e394671ef5f6 479 _i2c->stop(); // send stop condition
UHSLMarcus 5:41e97348e9e7 480 } else new_error(CCS811_LIB_I2C_ID);
UHSLMarcus 5:41e97348e9e7 481 _n_wake_out->write(1); // Set back to high
UHSLMarcus 5:41e97348e9e7 482 wait_us(CCS811_T_DWAKE); // tDWAKE time to allow sensor I2C to sleep
UHSLMarcus 5:41e97348e9e7 483 } else new_error(CCS811_LIB_N_WAKE_ID);
UHSLMarcus 2:e394671ef5f6 484
UHSLMarcus 2:e394671ef5f6 485 return read_count;
UHSLMarcus 2:e394671ef5f6 486 }
UHSLMarcus 2:e394671ef5f6 487
UHSLMarcus 5:41e97348e9e7 488 int AMS_CCS811::i2c_write(char reg_addr, char* input, int len) {
UHSLMarcus 2:e394671ef5f6 489
UHSLMarcus 5:41e97348e9e7 490 int write_count = -1;
UHSLMarcus 2:e394671ef5f6 491 if (_n_wake_out != NULL) { // check nWAKE pin is set
UHSLMarcus 5:41e97348e9e7 492 _n_wake_out->write(0); // Hold low
UHSLMarcus 5:41e97348e9e7 493 wait_us(CCS811_T_AWAKE); // tAWAKE time to allow sensor I2C to wake up
UHSLMarcus 2:e394671ef5f6 494 if (_i2c != NULL) { // check I2C interface is set
UHSLMarcus 2:e394671ef5f6 495 _i2c->start(); // send start condition for write
UHSLMarcus 5:41e97348e9e7 496 if(_i2c->write(CCS811_SLAVE_ADDR_W) == 1) { // write slave address
UHSLMarcus 2:e394671ef5f6 497 if(_i2c->write(reg_addr) == 1) { // write register address
UHSLMarcus 5:41e97348e9e7 498 write_count = 0;
UHSLMarcus 2:e394671ef5f6 499 for (int i = 0; i < len; i++) { // write len bytes
UHSLMarcus 2:e394671ef5f6 500 if(_i2c->write(input[i]) == 1) write_count++; // write each byte, if successful increment count
UHSLMarcus 5:41e97348e9e7 501 else new_error(CCS811_LIB_I2CWRITE_ID);
UHSLMarcus 2:e394671ef5f6 502 }
UHSLMarcus 5:41e97348e9e7 503 } else new_error(CCS811_LIB_REG_ADDR_ID);
UHSLMarcus 5:41e97348e9e7 504 } else new_error(CCS811_LIB_SLAVE_W_ID);
UHSLMarcus 2:e394671ef5f6 505 _i2c->stop(); // send stop condition
UHSLMarcus 5:41e97348e9e7 506 } else new_error(CCS811_LIB_I2C_ID);
UHSLMarcus 5:41e97348e9e7 507 _n_wake_out->write(1); // set back to high
UHSLMarcus 5:41e97348e9e7 508 wait_us(CCS811_T_DWAKE); // tDWAKE time to allow sensor I2C to sleep
UHSLMarcus 5:41e97348e9e7 509 }else new_error(CCS811_LIB_N_WAKE_ID);
UHSLMarcus 2:e394671ef5f6 510
UHSLMarcus 2:e394671ef5f6 511 return write_count;
UHSLMarcus 1:acfca1d3256d 512 }