A Library for the AMS ENS210 temperature and humidity sensor.

Dependents:   AMS_CCS811_gas_sensor

Committer:
UHSLMarcus
Date:
Wed Mar 08 09:44:13 2017 +0000
Revision:
8:1a7d241afbcb
Parent:
6:475b764b720d
.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
UHSLMarcus 0:7088b1bdc2e5 1 /**
UHSLMarcus 0:7088b1bdc2e5 2 * @author Marcus Lee
UHSLMarcus 0:7088b1bdc2e5 3 *
UHSLMarcus 0:7088b1bdc2e5 4 * @section DESCRIPTION
UHSLMarcus 0:7088b1bdc2e5 5 * A library for the AMS ENS210 tempuratire and relative humidy sensor.
UHSLMarcus 0:7088b1bdc2e5 6 *
UHSLMarcus 0:7088b1bdc2e5 7 */
UHSLMarcus 0:7088b1bdc2e5 8
UHSLMarcus 0:7088b1bdc2e5 9
UHSLMarcus 0:7088b1bdc2e5 10 #ifndef AMS_ENS210_H
UHSLMarcus 0:7088b1bdc2e5 11 #define AMS_ENS210_H
UHSLMarcus 0:7088b1bdc2e5 12
UHSLMarcus 0:7088b1bdc2e5 13 #include "mbed.h"
UHSLMarcus 0:7088b1bdc2e5 14
UHSLMarcus 0:7088b1bdc2e5 15
UHSLMarcus 0:7088b1bdc2e5 16 /* Library defaults */
UHSLMarcus 5:22b8ef3a65e1 17 #define CONFIG_TEMP_OP_MODE 0 // single shot
UHSLMarcus 5:22b8ef3a65e1 18 #define CONFIG_HUMID_OP_MODE 0 // single shot
UHSLMarcus 5:22b8ef3a65e1 19 #define CONFIG_POWER_MODE 1 // low power
UHSLMarcus 0:7088b1bdc2e5 20
UHSLMarcus 0:7088b1bdc2e5 21 /* Library Constants */
UHSLMarcus 5:22b8ef3a65e1 22 #define ENS210_SLAVE_ADDR_RAW 0x43
UHSLMarcus 5:22b8ef3a65e1 23 #define ENS210_SLAVE_ADDR ENS210_SLAVE_ADDR_RAW << 1 // 0x86
UHSLMarcus 5:22b8ef3a65e1 24 #define ENS210_SLAVE_ADDR_W ENS210_SLAVE_ADDR
UHSLMarcus 5:22b8ef3a65e1 25 #define ENS210_SLAVE_ADDR_R ENS210_SLAVE_ADDR | 1 // 0x87
UHSLMarcus 0:7088b1bdc2e5 26
UHSLMarcus 5:22b8ef3a65e1 27 #define SYS_CONFIG 0x10
UHSLMarcus 5:22b8ef3a65e1 28 #define SYS_STATUS 0x11
UHSLMarcus 0:7088b1bdc2e5 29
UHSLMarcus 5:22b8ef3a65e1 30 #define SENS_OP_MODE 0x21
UHSLMarcus 5:22b8ef3a65e1 31 #define SENS_START 0x22
UHSLMarcus 5:22b8ef3a65e1 32 #define SENS_STOP 0x23
UHSLMarcus 5:22b8ef3a65e1 33 #define SENS_STATUS 0x24
UHSLMarcus 5:22b8ef3a65e1 34 #define SENS_TEMP 0x30
UHSLMarcus 5:22b8ef3a65e1 35 #define SENS_HUMID 0x33
UHSLMarcus 0:7088b1bdc2e5 36
UHSLMarcus 0:7088b1bdc2e5 37 /** The AMS ENS210 class
UHSLMarcus 0:7088b1bdc2e5 38 */
UHSLMarcus 0:7088b1bdc2e5 39 class AMS_ENS210 {
UHSLMarcus 0:7088b1bdc2e5 40
UHSLMarcus 0:7088b1bdc2e5 41 public:
UHSLMarcus 0:7088b1bdc2e5 42 /** Create an AMS_ENS210 instance
UHSLMarcus 0:7088b1bdc2e5 43 *
UHSLMarcus 8:1a7d241afbcb 44 * @param sda I2C SDA pin
UHSLMarcus 8:1a7d241afbcb 45 * @param scl I2C SCL pin
UHSLMarcus 0:7088b1bdc2e5 46 */
UHSLMarcus 8:1a7d241afbcb 47 AMS_ENS210(PinName sda, PinName scl);
UHSLMarcus 0:7088b1bdc2e5 48
UHSLMarcus 0:7088b1bdc2e5 49 /** Create an AMS_ENS210 instance
UHSLMarcus 0:7088b1bdc2e5 50 *
UHSLMarcus 8:1a7d241afbcb 51 * @param sda I2C SDA pin
UHSLMarcus 8:1a7d241afbcb 52 * @param scl I2C SCL pin
UHSLMarcus 3:3b427231e5a7 53 * @param temp_continuous Set tempurature operation mode, true for continuous and false for single shot
UHSLMarcus 3:3b427231e5a7 54 * @param humid_continuous Set humidity operation mode, true for continuous and false for single shot
UHSLMarcus 0:7088b1bdc2e5 55 */
UHSLMarcus 8:1a7d241afbcb 56 AMS_ENS210(PinName sda, PinName scl, bool temp_continuous, bool humid_continuous);
UHSLMarcus 0:7088b1bdc2e5 57
UHSLMarcus 0:7088b1bdc2e5 58 /** Create an AMS_ENS210 instance
UHSLMarcus 0:7088b1bdc2e5 59 *
UHSLMarcus 8:1a7d241afbcb 60 * @param sda I2C SDA pin
UHSLMarcus 8:1a7d241afbcb 61 * @param scl I2C SCL pin
UHSLMarcus 3:3b427231e5a7 62 * @param temp_continuous Set tempurature operation mode, true for continuous and false for single shot
UHSLMarcus 3:3b427231e5a7 63 * @param humid_continuous Set humidity operation mode, true for continuous and false for single shot
UHSLMarcus 0:7088b1bdc2e5 64 * @param low_power Set power mode, true for low power/standby and false for active
UHSLMarcus 0:7088b1bdc2e5 65 */
UHSLMarcus 8:1a7d241afbcb 66 AMS_ENS210(PinName sda, PinName scl, bool temp_continuous, bool humid_continuous, bool low_power);
UHSLMarcus 0:7088b1bdc2e5 67
UHSLMarcus 0:7088b1bdc2e5 68 /** Destroy the AMS_ENS210 instance
UHSLMarcus 0:7088b1bdc2e5 69 */
UHSLMarcus 0:7088b1bdc2e5 70 ~AMS_ENS210();
UHSLMarcus 0:7088b1bdc2e5 71
UHSLMarcus 0:7088b1bdc2e5 72 /** Initalise the sensor
UHSLMarcus 0:7088b1bdc2e5 73 *
UHSLMarcus 0:7088b1bdc2e5 74 * @return Intalisation success
UHSLMarcus 0:7088b1bdc2e5 75 */
UHSLMarcus 0:7088b1bdc2e5 76 bool init();
UHSLMarcus 0:7088b1bdc2e5 77
UHSLMarcus 0:7088b1bdc2e5 78 /** Software reset the sensor
UHSLMarcus 0:7088b1bdc2e5 79 *
UHSLMarcus 0:7088b1bdc2e5 80 * @return Reset success
UHSLMarcus 0:7088b1bdc2e5 81 */
UHSLMarcus 0:7088b1bdc2e5 82 bool reset();
UHSLMarcus 0:7088b1bdc2e5 83
UHSLMarcus 0:7088b1bdc2e5 84 /** Set the power mode
UHSLMarcus 0:7088b1bdc2e5 85 *
UHSLMarcus 0:7088b1bdc2e5 86 * @param low_power True for low power/standby and false for active
UHSLMarcus 0:7088b1bdc2e5 87 *
UHSLMarcus 0:7088b1bdc2e5 88 * @return Write success
UHSLMarcus 0:7088b1bdc2e5 89 */
UHSLMarcus 3:3b427231e5a7 90 bool low_power_mode(bool low_power);
UHSLMarcus 0:7088b1bdc2e5 91
UHSLMarcus 0:7088b1bdc2e5 92 /** Get the current power mode
UHSLMarcus 0:7088b1bdc2e5 93 *
UHSLMarcus 0:7088b1bdc2e5 94 * @return The power mode, true for low power, false for active
UHSLMarcus 0:7088b1bdc2e5 95 */
UHSLMarcus 3:3b427231e5a7 96 bool low_power_mode();
UHSLMarcus 3:3b427231e5a7 97
UHSLMarcus 3:3b427231e5a7 98 /** Get whether the sensor is in the active state or not
UHSLMarcus 3:3b427231e5a7 99 *
UHSLMarcus 3:3b427231e5a7 100 * @return The active state, true for active, false for inactive
UHSLMarcus 3:3b427231e5a7 101 */
UHSLMarcus 3:3b427231e5a7 102 bool is_active();
UHSLMarcus 0:7088b1bdc2e5 103
UHSLMarcus 0:7088b1bdc2e5 104 /** Set the tempurature operation mode
UHSLMarcus 0:7088b1bdc2e5 105 *
UHSLMarcus 3:3b427231e5a7 106 * @param single_shot True for continuous and false for single shot
UHSLMarcus 0:7088b1bdc2e5 107 *
UHSLMarcus 0:7088b1bdc2e5 108 * @return Write success
UHSLMarcus 0:7088b1bdc2e5 109 */
UHSLMarcus 3:3b427231e5a7 110 bool temp_continuous_mode(bool continuous);
UHSLMarcus 0:7088b1bdc2e5 111
UHSLMarcus 0:7088b1bdc2e5 112 /** Get the current tempurature operation mode
UHSLMarcus 0:7088b1bdc2e5 113 *
UHSLMarcus 3:3b427231e5a7 114 * @return Write success
UHSLMarcus 0:7088b1bdc2e5 115 */
UHSLMarcus 3:3b427231e5a7 116 bool temp_continuous_mode();
UHSLMarcus 0:7088b1bdc2e5 117
UHSLMarcus 0:7088b1bdc2e5 118 /** Set the humidity operation mode
UHSLMarcus 0:7088b1bdc2e5 119 *
UHSLMarcus 3:3b427231e5a7 120 * @param single_shot True for continuous and false for single shot
UHSLMarcus 0:7088b1bdc2e5 121 *
UHSLMarcus 0:7088b1bdc2e5 122 * @return Write success
UHSLMarcus 0:7088b1bdc2e5 123 */
UHSLMarcus 3:3b427231e5a7 124 bool humid_continuous_mode(bool continuous);
UHSLMarcus 0:7088b1bdc2e5 125
UHSLMarcus 0:7088b1bdc2e5 126 /** Get the current humidity operation mode
UHSLMarcus 0:7088b1bdc2e5 127 *
UHSLMarcus 3:3b427231e5a7 128 * @return Write success
UHSLMarcus 0:7088b1bdc2e5 129 */
UHSLMarcus 3:3b427231e5a7 130 bool humid_continuous_mode();
UHSLMarcus 0:7088b1bdc2e5 131
UHSLMarcus 0:7088b1bdc2e5 132 /** Set the I2C interface
UHSLMarcus 0:7088b1bdc2e5 133 *
UHSLMarcus 0:7088b1bdc2e5 134 * @param i2c The I2C interface
UHSLMarcus 0:7088b1bdc2e5 135 */
UHSLMarcus 0:7088b1bdc2e5 136 void i2c_interface(I2C * i2c);
UHSLMarcus 0:7088b1bdc2e5 137
UHSLMarcus 6:475b764b720d 138 /** Get the I2C interface
UHSLMarcus 6:475b764b720d 139 *
UHSLMarcus 6:475b764b720d 140 * @return The I2C interface
UHSLMarcus 6:475b764b720d 141 */
UHSLMarcus 6:475b764b720d 142 I2C* i2c_interface();
UHSLMarcus 6:475b764b720d 143
UHSLMarcus 0:7088b1bdc2e5 144 /** Start measurement collection
UHSLMarcus 0:7088b1bdc2e5 145 *
UHSLMarcus 0:7088b1bdc2e5 146 * @param temp Start the tempurature sensor
UHSLMarcus 0:7088b1bdc2e5 147 * @param humid Start the humidity sensor
UHSLMarcus 0:7088b1bdc2e5 148 *
UHSLMarcus 0:7088b1bdc2e5 149 * @return Write success
UHSLMarcus 0:7088b1bdc2e5 150 */
UHSLMarcus 0:7088b1bdc2e5 151 bool start(bool temp = true, bool humid = true);
UHSLMarcus 0:7088b1bdc2e5 152
UHSLMarcus 0:7088b1bdc2e5 153 /** Stop measurement collection
UHSLMarcus 0:7088b1bdc2e5 154 *
UHSLMarcus 0:7088b1bdc2e5 155 * @param temp Stop the tempurature sensor
UHSLMarcus 0:7088b1bdc2e5 156 * @param humid Stop the humidity sensor
UHSLMarcus 0:7088b1bdc2e5 157 *
UHSLMarcus 0:7088b1bdc2e5 158 * @return Write success
UHSLMarcus 0:7088b1bdc2e5 159 */
UHSLMarcus 0:7088b1bdc2e5 160 bool stop(bool temp = true, bool humid = true);
UHSLMarcus 0:7088b1bdc2e5 161
UHSLMarcus 0:7088b1bdc2e5 162 /** Get whether the sensor is measuring tempurature data
UHSLMarcus 0:7088b1bdc2e5 163 *
UHSLMarcus 0:7088b1bdc2e5 164 * @return Sensor activity, true for measureing, false for idle
UHSLMarcus 0:7088b1bdc2e5 165 */
UHSLMarcus 0:7088b1bdc2e5 166 bool temp_is_measuring();
UHSLMarcus 0:7088b1bdc2e5 167
UHSLMarcus 0:7088b1bdc2e5 168 /** Get whether the sensor is measuring humidity data
UHSLMarcus 0:7088b1bdc2e5 169 *
UHSLMarcus 0:7088b1bdc2e5 170 * @return Sensor activity, true for measureing, false for idle
UHSLMarcus 0:7088b1bdc2e5 171 */
UHSLMarcus 0:7088b1bdc2e5 172 bool humid_is_measuring();
UHSLMarcus 0:7088b1bdc2e5 173
UHSLMarcus 0:7088b1bdc2e5 174 /** Get whether the sensor has collected tempurature data
UHSLMarcus 0:7088b1bdc2e5 175 *
UHSLMarcus 0:7088b1bdc2e5 176 * @return Has data
UHSLMarcus 0:7088b1bdc2e5 177 */
UHSLMarcus 0:7088b1bdc2e5 178 bool temp_has_data();
UHSLMarcus 0:7088b1bdc2e5 179
UHSLMarcus 0:7088b1bdc2e5 180 /** Get whether the sensor has collected humidity data
UHSLMarcus 0:7088b1bdc2e5 181 *
UHSLMarcus 0:7088b1bdc2e5 182 * @return Has data
UHSLMarcus 0:7088b1bdc2e5 183 */
UHSLMarcus 0:7088b1bdc2e5 184 bool humid_has_data();
UHSLMarcus 0:7088b1bdc2e5 185
UHSLMarcus 0:7088b1bdc2e5 186 /** Get the most recent tempurature measurement, must call temp_has_data() first when in single shot mode otherwise the same data will be returned
UHSLMarcus 0:7088b1bdc2e5 187 *
UHSLMarcus 0:7088b1bdc2e5 188 * @return Most recent tempurature measurement as a 16 bits unsigned value in 1/64 Kelvin
UHSLMarcus 0:7088b1bdc2e5 189 */
UHSLMarcus 0:7088b1bdc2e5 190 uint16_t temp_read();
UHSLMarcus 0:7088b1bdc2e5 191
UHSLMarcus 0:7088b1bdc2e5 192 /** Get the most recent humidty measurement, must call humid_has_data() first when in single shot mode otherwise the same data will be returned
UHSLMarcus 0:7088b1bdc2e5 193 *
UHSLMarcus 0:7088b1bdc2e5 194 * @return Most recent humidity measurement as a 16 bits unsigned value in 1/64 Kelvin
UHSLMarcus 0:7088b1bdc2e5 195 */
UHSLMarcus 0:7088b1bdc2e5 196 uint16_t humid_read();
UHSLMarcus 0:7088b1bdc2e5 197
UHSLMarcus 0:7088b1bdc2e5 198 private:
UHSLMarcus 8:1a7d241afbcb 199 I2C _i2c;
UHSLMarcus 0:7088b1bdc2e5 200 bool _temp_mode;
UHSLMarcus 0:7088b1bdc2e5 201 bool _humid_mode;
UHSLMarcus 0:7088b1bdc2e5 202 bool _power_mode;
UHSLMarcus 3:3b427231e5a7 203 bool _reset;
UHSLMarcus 3:3b427231e5a7 204 uint16_t temp_reading;
UHSLMarcus 3:3b427231e5a7 205 uint16_t humid_reading;
UHSLMarcus 0:7088b1bdc2e5 206
UHSLMarcus 0:7088b1bdc2e5 207 bool write_config(bool system = true, bool sensor = true);
UHSLMarcus 0:7088b1bdc2e5 208 const char * read_config(bool system = true, bool sensor = true);
UHSLMarcus 0:7088b1bdc2e5 209 int i2c_read(char reg_addr, char* output, int len);
UHSLMarcus 3:3b427231e5a7 210 int i2c_write(char reg_addr, char* input, int len);
UHSLMarcus 0:7088b1bdc2e5 211
UHSLMarcus 0:7088b1bdc2e5 212
UHSLMarcus 0:7088b1bdc2e5 213
UHSLMarcus 0:7088b1bdc2e5 214 };
UHSLMarcus 0:7088b1bdc2e5 215
UHSLMarcus 0:7088b1bdc2e5 216 #endif /* AMS_ENS210_H */