Arslan Test
Dependencies: AMS_ENS210_temp_humid_sensor
AMS_CCS811.cpp
- Committer:
- UHSLMarcus
- Date:
- 2017-01-19
- Revision:
- 3:782a719f47a5
- Parent:
- 2:e394671ef5f6
- Child:
- 4:a6b8881eae87
File content as of revision 3:782a719f47a5:
#include "AMS_CCS811.h" AMS_CCS811::AMS_CCS811(I2C * i2c, PinName n_wake_pin) { _n_wake_out = new (std::nothrow) DigitalOut(n_wake_pin); _i2c = i2c; set_defaults(); } AMS_CCS811::AMS_CCS811(I2C * i2c, PinName n_wake_pin, I2C * ens210_i2c) { _n_wake_out = new (std::nothrow) DigitalOut(n_wake_pin); _i2c = i2c; _ens210_i2c = ens210_i2c; set_defaults(); } AMS_CCS811::~AMS_CCS811() { delete _n_wake_out; delete _addr_out; delete _int_data; } bool AMS_CCS811::init() { return true; } bool AMS_CCS811::mode(OP_MODES mode) { OP_MODES old = _mode; // incase the write fails, to roll back _mode = mode; bool success = write_config(); if (!success) _mode = old; return success; } AMS_CCS811::OP_MODES AMS_CCS811::mode() { OP_MODES result = _mode; // rather not rely on this, but just incase the read fails read_config_result read_result = read_config(); if (read_result.success) { int mode = (read_result.byte >> 4) & 0b111; result = mode > 4 ? INVALID : (OP_MODES)mode; } // todo ... add a new "last error" here 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; } bool AMS_CCS811::addr_mode() { if (_addr_out != NULL) { _addr_dir = *_addr_out; } return _addr_dir; } bool AMS_CCS811::addr_pin(PinName pin) { _addr_out = _addr_out == NULL ? new (std::nothrow) DigitalOut(pin) : new (_addr_out) DigitalOut(pin); addr_mode(_addr_dir); return _addr_out != NULL; } bool AMS_CCS811::n_wake_pin(PinName pin) { _n_wake_out = _n_wake_out == NULL ? new (std::nothrow) DigitalOut(pin) : new (_n_wake_out) DigitalOut(pin); return _n_wake_out != NULL; } bool AMS_CCS811::env_data(float humid, float temp) { return true; } AMS_CCS811::DATA_STATUS AMS_CCS811::has_new_data() { return (DATA_STATUS) 0; } uint16_t AMS_CCS811::co2_read() { return 0; } uint16_t AMS_CCS811::tvoc_read() { return 0; } uint16_t AMS_CCS811::raw_read() { return 0; } const char * AMS_CCS811::last_error() { return ""; } bool AMS_CCS811::enable_interupt(bool enable) { bool old = _int_data_enabled; // incase the write fails, to roll back _int_data_enabled = enable; bool success = write_config(); if (!success) _int_data_enabled = old; return success; } bool AMS_CCS811::interupt_enabled() { bool enabled = _int_data_enabled; // rather not rely on this, but just incase the read fails read_config_result read_result = read_config(); if (read_result.success) { enabled = (read_result.byte >> 3) & 1; } // todo ... add a new "last error" here? or maybe the read method itself should set that. return enabled; } bool AMS_CCS811::interrupt_pin(PinName pin) { bool success = false; _int_data = _int_data == NULL ? new (std::nothrow) InterruptIn(pin) : new (_int_data) InterruptIn(pin); if (_int_data != NULL) { _int_data->fall(callback(this, &AMS_CCS811::_isr_data)); success = true; } return success; } /** Private **/ bool AMS_CCS811::set_defaults() { if (_mode == NULL) _mode = CONFIG_OP_MODE; if (_addr_dir == NULL) _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 AMS_CCS811::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() { char cmd[1] = {0 | (_int_data_enabled << 3) | (_mode << 4)}; return i2c_write(SYS_MODE, cmd, 1) == 1; } AMS_CCS811::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; }