Library for the AMS CC811 digitial gas sensor

Dependencies:   AMS_ENS210_temp_humid_sensor

AMS_CCS811.cpp

Committer:
UHSLMarcus
Date:
2017-01-19
Revision:
1:acfca1d3256d
Parent:
0:5edbf3550350
Child:
2:e394671ef5f6

File content as of revision 1:acfca1d3256d:


#include "AMS_CSS811.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() {
    
}

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 : mode;
    } // todo ... add a new "last error" here
    
    return mode;
}

bool AMS_CCS811::addr_mode(bool high) { 
    _addr_dir = high;
    if (_addr_out != NULL) *_addr_out = _addr_dir;
    
    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 eAMS_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 mode;
}

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);
    if (_int_data != NULL) {
        _int_data->fall(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;
        
    return write_config();
}
        
void AMS_CCS811::_isr_data() {
}

bool AMS_CCS811::write_config() {
}

read_config_result AMS_CCS811::read_config() {
}