Arslan Test
Dependencies: AMS_ENS210_temp_humid_sensor
Diff: AMS_CCS811.cpp
- Revision:
- 2:e394671ef5f6
- Parent:
- 1:acfca1d3256d
- Child:
- 3:782a719f47a5
diff -r acfca1d3256d -r e394671ef5f6 AMS_CCS811.cpp --- a/AMS_CCS811.cpp Thu Jan 19 12:46:58 2017 +0000 +++ b/AMS_CCS811.cpp Thu Jan 19 13:23:21 2017 +0000 @@ -1,5 +1,5 @@ -#include "AMS_CSS811.h" +#include "AMS_CCS811.h" AMS_CCS811::AMS_CCS811(I2C * i2c, PinName n_wake_pin) { @@ -45,16 +45,18 @@ read_config_result read_result = read_config(); if (read_result.success) { int mode = (read_result.byte >> 4) & 0b111; - result = mode > 4 ? INVALID : mode; + result = mode > 4 ? INVALID : (OP_MODES)mode; } // todo ... add a new "last error" here - return mode; + return result; } bool AMS_CCS811::addr_mode(bool high) { _addr_dir = high; if (_addr_out != NULL) *_addr_out = _addr_dir; + update_slave_addr(); + return addr_mode() == high; } @@ -78,7 +80,7 @@ return _n_wake_out != NULL; } -bool eAMS_CCS811::env_data(float humid, float temp) { +bool AMS_CCS811::env_data(float humid, float temp) { return true; } @@ -89,6 +91,7 @@ uint16_t AMS_CCS811::co2_read() { return 0; +} uint16_t AMS_CCS811::tvoc_read() { return 0; @@ -122,15 +125,15 @@ enabled = (read_result.byte >> 3) & 1; } // todo ... add a new "last error" here? or maybe the read method itself should set that. - return mode; + return enabled; } bool AMS_CCS811::interrupt_pin(PinName pin) { bool success = false; - _int_data = _int_data == NULL ? new (std::nothrow) DigitalOut(pin) : new (_int_data) DigitalOut(pin); + _int_data = _int_data == NULL ? new (std::nothrow) InterruptIn(pin) : new (_int_data) InterruptIn(pin); if (_int_data != NULL) { - _int_data->fall(this, &AMS_CCS811::_isr_data); + _int_data->fall(callback(this, &AMS_CCS811::_isr_data)); success = true; } @@ -149,15 +152,78 @@ _addr_dir = CONFIG_ADDR_DIR; if (_int_data_enabled == NULL) _int_data_enabled = CONFIG_INTR; + if (_ens210_poll_split == NULL) + _ens210_poll_split = CONFIG_ENS210_POLL; + + update_slave_addr(); return write_config(); } + +void update_slave_addr() { + slave_addr = addr_mode() ? SLAVE_ADDR_RAW_H : SLAVE_ADDR_RAW_L; +} void AMS_CCS811::_isr_data() { + _isr_data_fp.call() } bool AMS_CCS811::write_config() { + cmd[1] = {0 | (_int_data_enabled << 3) | (_mode << 4)}; + return i2c_write(SYS_MODE, cmd, 1) == 1; } read_config_result AMS_CCS811::read_config() { + read_config_result result; + char byte[1]; + if (i2c_read(SYS_MODE, byte, 1) == 1) { + result.success = true; + result.byte = byte[1]; + } + + return result; +} + +int AMS_CCS811::i2c_read(char reg_addr, char* output, int len) { + + int read_count = 0; + if (_n_wake_out != NULL) { // check nWAKE pin is set + if (_i2c != NULL) { // check I2C interface is set + _i2c->start(); // send start condition for write + if(_i2c->write(SLAVE_ADDR_W) == 1) { // write slave address with write bit + if(_i2c->write(reg_addr) == 1) { // write register address + _i2c->start(); // send another start condition for read + if(_i2c->write(SLAVE_ADDR_R) == 1) { // write slave address with read bit + for (int i = 0; i < len; i++) { // read len bytes + output[i] = _i2c->read(i < len-1 ? 1 : 0); // ack all reads aside from the final one (i == len-1) + read_count++; + } + } + } + } + _i2c->stop(); // send stop condition + } + } + + return read_count; +} + +int AMS_CCS811::i2c_write(char reg_addr, char* input, int len) { // to do... error reporting + + int write_count = 0; + if (_n_wake_out != NULL) { // check nWAKE pin is set + if (_i2c != NULL) { // check I2C interface is set + _i2c->start(); // send start condition for write + if(_i2c->write(SLAVE_ADDR_W) == 1) { // write slave address + if(_i2c->write(reg_addr) == 1) { // write register address + for (int i = 0; i < len; i++) { // write len bytes + if(_i2c->write(input[i]) == 1) write_count++; // write each byte, if successful increment count + } + } + } + _i2c->stop(); // send stop condition + } + } + + return write_count; } \ No newline at end of file