simple CCS811 driver

Dependencies:   AMS_ENS210_temp_humid_sensor

Dependents:   TBSense2_Sensor_Demo

Fork of AMS_CCS811_gas_sensor by Marcus Lee

Revision:
7:5c95614a61ee
Parent:
6:22c0a7f2ece2
Child:
8:58a36d9218be
--- a/AMS_CCS811.h	Mon Jan 23 14:27:57 2017 +0000
+++ b/AMS_CCS811.h	Tue Jan 24 10:47:38 2017 +0000
@@ -9,11 +9,7 @@
 #define AMS_CCS811_H
 
 #include "mbed.h"
-
-#include "serialBuffer.h"
-using utils::SerialBuffer;
-extern SerialBuffer USBserialComms;
-const char *short_to_binary(uint16_t in);
+#include "AMS_ENS210.h"
 
 /* Library defaults */
 #define CONFIG_OP_MODE              TEN_SECOND              // Every 10 seconds
@@ -42,6 +38,9 @@
 #define CCS811_T_AWAKE              55                      // us - time taken for sensor I2C to become active
 #define CCS811_T_DWAKE              25                      // us - time taken for sensor I2C to become inactive
 
+#define CCS811_MAX_HUMID            127.998046875           // maxmium value that can be represented in the register
+#define CCS811_MAX_TEMP             102.998046875           // maxmium value that can be represented in the register
+
 /* Error Codes */
 #define CCS811_NO_ERROR             "No Error";
 /* Sensor Errors */
@@ -54,7 +53,7 @@
 #define CCS811_HEATER_SUPPLY        "The Heater voltage is not being applied correctly"
 #define CCS811_RESERVED             "Reserved for Future Use"
 /* Library Errors */
-#define CCS811_LIB_ERR_NUM          7
+#define CCS811_LIB_ERR_NUM          9
 #define CCS811_LIB_N_WAKE_ID        0
 #define CCS811_LIB_N_WAKE           "nWAKE pin not set"
 #define CCS811_LIB_I2C_ID           1
@@ -69,6 +68,10 @@
 #define CCS811_LIB_SLAVE_R          "Failed to write slave read address"
 #define CCS811_LIB_INV_MODE_ID      6
 #define CCS811_LIB_INV_MODE         "Invalid operation mode"
+#define CCS811_LIB_ENS210_INIT_ID   7
+#define CCS811_LIB_ENS210_INIT      "Failed to create new AMS_ENS210 object"
+#define CCS811_LIB_ENS210_POLL_ID   7
+#define CCS811_LIB_ENS210_POLL      "AMS_ENS210 poll error"
 
 #define CCS811_TOTAL_ERR_NUM        CCS811_ERR_NUM+CCS811_LIB_ERR_NUM
 
@@ -136,8 +139,9 @@
          *
          * @param i2c  The I2C interface for an attached AMS_ENS210
          *
+         * @return Success
          */
-        void ens210_i2c_interface(I2C * i2c);
+        bool ens210_i2c_interface(I2C * i2c);
         
         /** Set whether the attached AMS_ENS210 is enabled.
          *  If an I2C interface is not set for the ENS210, calling this method will have no effect.
@@ -238,13 +242,15 @@
          */
         PinName n_wake_pin();
         
-        /** Set the relative humidity (%) and temperature (C)
-         *  Use when AMS ENS210 is not linked
+        /** Set the relative humidity (%) and temperature (C).\ \n
+         *  Use when AMS ENS210 is not linked.\ \n
+         *  Humidity values are clipped between 0 and CCS811_MAX_HUMID.\ \n
+         *  Temperature values are clipped between -25 and CCS811_MAX_TEMP.
          *
          * @return Write success
          */
         bool env_data(float humid, float temp);
-    
+        
         /** Get the sensor collection state
          *  Use when interrupts are disabled.
          *
@@ -360,18 +366,22 @@
     
     private:
         I2C* _i2c;
-        I2C* _ens210_i2c;
         
         bool _addr_dir;
         int _slave_addr;
         void update_slave_addr();
         
+        AMS_ENS210 *_ens210;
         bool _ens210_enabled;
         int _ens210_poll_split;
         void update_ens210_timer();
         Ticker _ens210_poll_t;
         void ens210_isr();
         
+        float fractions[9];
+        void _init_fractions();
+        void float_to_short(float in, char * output);
+        
         OP_MODES _mode;
         
         void set_defaults();