simple CCS811 driver

Dependencies:   AMS_ENS210_temp_humid_sensor

Dependents:   TBSense2_Sensor_Demo

Fork of AMS_CCS811_gas_sensor by Marcus Lee

Revision:
0:5edbf3550350
Child:
1:acfca1d3256d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AMS_CCS811.h	Thu Jan 19 09:06:31 2017 +0000
@@ -0,0 +1,270 @@
+/**
+* @author Marcus Lee
+*
+* @section DESCRIPTION
+*    A library for the AMS CCS811 digital gas sensor.
+*
+*/
+#ifndef AMS_CCS811_H
+#define AMS_CCS811_H
+
+#include "mbed.h"
+
+/* Library defaults */
+#define CONFIG_OP_MODE      OP_MODES::TEN_SECOND    // Every 10 seconds
+#define CONFIG_INTR         0                       // Interupt off
+#define CONFIG_ADDR         0                       // ADDR n_wake_pin pulled low
+
+/* Library Constants */
+#define SLAVE_ADDR_RAW_H    0x5B
+#define SLAVE_ADDR_RAW_L    0x5A
+#define SLAVE_ADDR_RAW      slave_addr
+#define SLAVE_ADDR          SLAVE_ADDR_RAW << 1     // 0x86
+#define SLAVE_ADDR_W        SLAVE_ADDR
+#define SLAVE_ADDR_R        SLAVE_ADDR | 1          // 0x87
+
+#define SYS_MODE            0x01
+#define SYS_STATUS          0x00
+
+#define ALG_DATA            0x02
+#define ENV_DATA            0x05
+#define ERROR_ID            0xE0
+
+/** The AMS CCS811 class
+ */
+class AMS_CCS811
+{
+    public:
+        /** Sensor operation modes.
+         *
+         */
+        enum OP_MODES {
+            IDLE,           /**< Measurements disabled */
+            SECOND,         /**< Measurement every second */
+            TEN_SECOND,     /**< Measurement every 10 seconds */
+            SIXTY_SECOND,   /**< Measurement every 60 seconds */
+            CONSTANT        /**< Measurement every 250ms - Only raw data available */
+        };
+    
+        /** Data collection status.
+         *
+         */
+        enum DATA_STATUS {
+            DATA_NOT_READY, /**< No new data */
+            DATA_READY,     /**< New data */
+            ERROR,          /**< Error occurred */
+        };
+    
+        /** Create an AMS_CCS811 instance
+         *
+         * @param i2c           The I2C interface to use for communication
+         * @param n_wake_pin    Pin nWAKE is attached to
+         */
+        AMS_CCS811(I2C * i2c, PinName n_wake_pin);
+        
+        /** Create an AMS_CCS811 instance
+         *
+         * @param i2c           The I2C interface to use for communication
+         * @param n_wake_pin    Pin nWAKE is attached to
+         * @param ens210_i2c    The I2C interface for an attached AMS_ENS210
+         */
+        AMS_CCS811(I2C * i2c, PinName n_wake_pin, I2C * ens210_i2c);
+        
+        /** Destroy the AMS_CCS811 instance
+         */
+        ~AMS_CCS811();
+    
+        /** Initalise the sensor
+         *
+         * @return Intalisation success
+         */
+        bool init();
+    
+        /** Set the operation mode \n
+         * Note: \n When a sensor operating mode is changed to a new mode with\n
+         *          a lower sample rate (e.g.\ from SECOND to SIXTY_SECOND), it should be\n
+         *          placed in IDLE for at least 10 minutes before enabling the new mode.\ \n
+         *          When a sensor operating mode is changed to a new mode with a higher\n
+         *          sample rate (e.g.\ from SIXTY_SECOND to SECOND), there is no requirement\n
+         *          to wait before enabling the new mode.
+         *
+         * @param mode  OP_MODES mode to set
+         *
+         * @return Write success
+         */
+        bool mode(OP_MODES mode);
+    
+        /** Get the current power mode
+         *
+         * @return The current OP_MODES mode
+         */
+        OP_MODES mode();
+        
+        /** Set the ADDR mode \n
+         *
+         * @param high  True sets to high, false to low
+         *
+         * @return Write success
+         */
+        bool addr_mode(bool high);
+    
+        /** Get the the ADDR mode
+         *
+         * @return The current ADDR mode, true for high, false for low
+         */
+        bool addr_mode();
+        
+        /** Set the ADDR pin
+         *
+         * @param pin    Pin ADDR is attached to
+         *
+         * @return Write success
+         */
+        bool addr_pin(PinName pin);
+    
+        /** Get the the ADDR pin
+         *
+         * @return The addr pin
+         */
+        PinName addr_pin();
+        
+        /** Set the nWAKE pin
+         *
+         * @param pin    Pin nWAKE is attached to
+         *
+         * @return Write success
+         */
+        bool n_wake_pin(PinName pin);
+    
+        /** Get the the nWAKE pin
+         *
+         * @return The nWAKE pin
+         */
+        PinName n_wake_pin();
+        
+        /** Set the relative humidity (%) and temperature (C)
+         *  Use when AMS ENS210 is not linked
+         *
+         * @return Write success
+         */
+        bool env_data(float humid, float temp);
+    
+        /** Get the sensor collection state
+         *  Use when interupts are disabled
+         *
+         * @return Current collection state
+         */
+        DATA_STATUS has_new_data();
+    
+        /** Get the most recent CO2 measurement.
+         *  Must call has_new_data() first when when interupts are disabled otherwise the same data will be returned
+         *
+         * @return Most recent eCO2 measurement in ppm
+         */
+        uint16_t co2_read();
+    
+        /** Get the most recent TVOC measurement.
+         *  Must call has_new_data() first when when interupts are disabled otherwise the same data will be returned
+         *
+         * @return Most recent TVOC measurement in ppb
+         */
+        uint16_t tvoc_read();
+    
+        /** Get the most recent RAW data.
+         *  Must call has_new_data() first when when interupts are disabled otherwise the same data will be returned
+         *
+         * @return Most recent TVOC measurement in ppb
+         */
+        uint16_t raw_read();
+    
+        /** Get the last error.
+         *  Must call has_new_data() first when when interupts are disabled otherwise the same error will be returned
+         *
+         * @return Last error.
+         */
+        const char * last_error();
+    
+        /** Attach a function to be called when data is ready.
+         *  Calling this method enables interupts
+         *
+         * @param func_ptr  A pointer to the function to be called
+         * @param pin       Pin attached to nINT
+         *
+         * @return Attach success
+         */
+        bool attach(void (*func_ptr)(void), PinName pin) {
+            _isr_data_fp.attach(func_ptr);
+            _int_data_active = true;
+            _int_data(pin);
+            _int_data.fall(this, &AMS_CCS811::_isr_data());
+    
+            return enable_interupts(true);
+        }
+    
+        /** Attach a member function to be called when data is ready.
+         *  Calling this method enables interupts
+         *
+         * @param type_ptr  A pointer to the instance of the class
+         * @param mem_ptr   A pointer to the member function
+         * @param pin       Pin attached to nINT
+         *
+         * @return Attach success
+         */
+        template<typename T>
+        bool attach(T *type_ptr, void (T::*mem_ptr)(void), PinName pin) {
+            _isr_data_fp.attach(type_ptr, mem_ptr);
+            //_int_data_active = true;
+            //_int_data(pin);
+            //_int_data.fall(this, &AMS_CCS811::_isr_data());
+    
+            return enable_interupts(true);
+        }
+    
+        /** Get whether the sensor has collected new data.
+         *  Use when interupts are disabled
+         *
+         * @return Write success
+         */
+        bool enable_interupts(bool enable);
+        
+        /** Set the nINT pin
+         *
+         * @param pin    Pin nINT is attached to
+         *
+         * @return Write success
+         */
+        bool interrupt_pin(PinName pin);
+    
+        /** Get the the nINT pin
+         *
+         * @return The nINT pin
+         */
+        PinName interrupt_pin();
+    
+    
+    
+    
+    private:
+        I2C* _i2c;
+        bool _addr;
+        int slave_addr;
+        I2C* _ens210_i2c;
+        bool _ens210_enable;
+        OP_MODES _mode;
+        void set_defaults();
+        
+        DigitalIn _n_wake_in;
+        DigitalIn _addr_in;
+        
+    
+        FunctionPointer _isr_data_fp;
+        bool _int_data_active;
+        InterruptIn _int_data;
+        void _isr_data();
+    
+        bool write_config(bool mode = true, bool int_data = true, bool int_thresh = true);
+
+};
+
+
+#endif /* AMS_CCS811_H */
\ No newline at end of file