A Library for the AMS ENS210 temperature and humidity sensor.

Dependents:   AMS_CCS811_gas_sensor AMS_CCS811_gas_sensor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AMS_ENS210.h Source File

AMS_ENS210.h

00001 /**
00002 * @author Marcus Lee
00003 *
00004 * @section DESCRIPTION
00005 *    A library for the AMS ENS210 tempuratire and relative humidy sensor.
00006 *
00007 */ 
00008 
00009 
00010 #ifndef AMS_ENS210_H
00011 #define AMS_ENS210_H
00012 
00013 #include "mbed.h"
00014 
00015 
00016 /* Library defaults */
00017 #define CONFIG_TEMP_OP_MODE     0                               // single shot
00018 #define CONFIG_HUMID_OP_MODE    0                               // single shot
00019 #define CONFIG_POWER_MODE       1                               // low power
00020 
00021 /* Library Constants */
00022 #define ENS210_SLAVE_ADDR_RAW   0x43
00023 #define ENS210_SLAVE_ADDR       ENS210_SLAVE_ADDR_RAW << 1     // 0x86
00024 #define ENS210_SLAVE_ADDR_W     ENS210_SLAVE_ADDR
00025 #define ENS210_SLAVE_ADDR_R     ENS210_SLAVE_ADDR | 1          // 0x87
00026 
00027 #define SYS_CONFIG              0x10
00028 #define SYS_STATUS              0x11
00029 
00030 #define SENS_OP_MODE            0x21
00031 #define SENS_START              0x22
00032 #define SENS_STOP               0x23
00033 #define SENS_STATUS             0x24
00034 #define SENS_TEMP               0x30
00035 #define SENS_HUMID              0x33
00036 
00037 /** The AMS ENS210 class
00038  */
00039 class AMS_ENS210 {
00040     
00041     public:
00042         /** Create an AMS_ENS210 instance
00043          * 
00044          * @param sda                   I2C SDA pin
00045          * @param scl                   I2C SCL pin
00046          */
00047         AMS_ENS210(PinName sda, PinName scl);
00048         
00049         /** Create an AMS_ENS210 instance
00050          * 
00051          * @param sda                   I2C SDA pin
00052          * @param scl                   I2C SCL pin
00053          * @param temp_continuous       Set tempurature operation mode, true for continuous and false for single shot
00054          * @param humid_continuous      Set humidity operation mode, true for continuous and false for single shot
00055          */
00056         AMS_ENS210(PinName sda, PinName scl, bool temp_continuous, bool humid_continuous);
00057         
00058         /** Create an AMS_ENS210 instance
00059          * 
00060          * @param sda                   I2C SDA pin
00061          * @param scl                   I2C SCL pin
00062          * @param temp_continuous       Set tempurature operation mode, true for continuous and false for single shot
00063          * @param humid_continuous      Set humidity operation mode, true for continuous and false for single shot
00064          * @param low_power             Set power mode, true for low power/standby and false for active
00065          */
00066         AMS_ENS210(PinName sda, PinName scl, bool temp_continuous, bool humid_continuous, bool low_power);
00067         
00068         /** Destroy the AMS_ENS210 instance
00069          */
00070         ~AMS_ENS210();
00071         
00072         /** Initalise the sensor
00073          *
00074          * @return Intalisation success
00075          */
00076         bool init();
00077         
00078         /** Software reset the sensor
00079          *
00080          * @return Reset success
00081          */
00082         bool reset();
00083         
00084         /** Set the power mode 
00085          * 
00086          * @param low_power     True for low power/standby and false for active
00087          *
00088          * @return Write success
00089          */
00090         bool low_power_mode(bool low_power);
00091         
00092         /** Get the current power mode
00093          *
00094          * @return The power mode, true for low power, false for active
00095          */
00096         bool low_power_mode();
00097         
00098         /** Get whether the sensor is in the active state or not
00099          *
00100          * @return The active state, true for active, false for inactive
00101          */
00102         bool is_active();
00103         
00104         /** Set the tempurature operation mode 
00105          * 
00106          * @param single_shot      True for continuous and false for single shot
00107          *
00108          * @return Write success
00109          */
00110         bool temp_continuous_mode(bool continuous);
00111         
00112         /** Get the current tempurature operation mode
00113          *
00114          * @return Write success
00115          */
00116         bool temp_continuous_mode();
00117         
00118         /** Set the humidity operation mode 
00119          * 
00120          * @param single_shot      True for continuous and false for single shot
00121          *
00122          * @return Write success
00123          */
00124         bool humid_continuous_mode(bool continuous);
00125         
00126         /** Get the current humidity operation mode
00127          *
00128          * @return Write success
00129          */
00130         bool humid_continuous_mode();
00131         
00132         /** Set the I2C interface
00133          * 
00134          * @param i2c   The I2C interface
00135          */
00136         void i2c_interface(I2C * i2c);
00137         
00138         /** Get the I2C interface
00139          * 
00140          * @return The I2C interface
00141          */
00142         I2C* i2c_interface();
00143         
00144         /** Start measurement collection
00145          *
00146          * @param temp      Start the tempurature sensor
00147          * @param humid     Start the humidity sensor
00148          *
00149          * @return Write success
00150          */
00151         bool start(bool temp = true, bool humid = true);
00152         
00153         /** Stop measurement collection
00154          *
00155          * @param temp      Stop the tempurature sensor
00156          * @param humid     Stop the humidity sensor
00157          *
00158          * @return Write success
00159          */
00160         bool stop(bool temp = true, bool humid = true);
00161         
00162         /** Get whether the sensor is measuring tempurature data
00163          *
00164          * @return Sensor activity, true for measureing, false for idle
00165          */
00166         bool temp_is_measuring();
00167         
00168         /** Get whether the sensor is measuring humidity data
00169          *
00170          * @return Sensor activity, true for measureing, false for idle
00171          */
00172         bool humid_is_measuring();
00173         
00174         /** Get whether the sensor has collected tempurature data
00175          *
00176          * @return Has data
00177          */
00178         bool temp_has_data();
00179         
00180         /** Get whether the sensor has collected humidity data
00181          *
00182          * @return Has data
00183          */
00184         bool humid_has_data();
00185         
00186         /** Get the most recent tempurature measurement, must call temp_has_data() first when in single shot mode otherwise the same data will be returned 
00187          *
00188          * @return Most recent tempurature measurement as a 16 bits unsigned value in 1/64 Kelvin
00189          */
00190         uint16_t temp_read();
00191         
00192         /** Get the most recent humidty measurement, must call humid_has_data() first when in single shot mode otherwise the same data will be returned 
00193          *
00194          * @return Most recent humidity measurement as a 16 bits unsigned value in 1/64 Kelvin
00195          */
00196         uint16_t humid_read();
00197         
00198     private:
00199         I2C _i2c;
00200         bool _temp_mode;
00201         bool _humid_mode;
00202         bool _power_mode;
00203         bool _reset;
00204         uint16_t temp_reading;
00205         uint16_t humid_reading;
00206         
00207         bool write_config(bool system = true, bool sensor =  true);
00208         const char * read_config(bool system = true, bool sensor =  true);
00209         int i2c_read(char reg_addr, char* output, int len);
00210         int i2c_write(char reg_addr, char* input, int len);
00211         
00212         
00213         
00214 };
00215 
00216 #endif /* AMS_ENS210_H */