library for AMS ENS210 temperature and humidity sensor
Dependents: rIoTwear-temp-humid
Diff: AMS_ENS210.h
- Revision:
- 0:7088b1bdc2e5
- Child:
- 1:94a79c88c105
diff -r 000000000000 -r 7088b1bdc2e5 AMS_ENS210.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AMS_ENS210.h Tue Jan 17 14:23:13 2017 +0000 @@ -0,0 +1,200 @@ +/** +* @author Marcus Lee +* +* @section DESCRIPTION +* A library for the AMS ENS210 tempuratire and relative humidy sensor. +* +*/ + + +#ifndef AMS_ENS210_H +#define AMS_ENS210_H + +#include "mbed.h" + + +/* Library defaults */ +#define CONFIG_TEMP_OP_MODE 1 +#define CONFIG_HUMID_OP_MODE 1 +#define CONFIG_POWER_MODE 1 + +/* Library Constants */ +#define SLAVE_ADDR_RAW 0x43 +#define SLAVE_ADDR SLAVE_ADDR_RAW << 1 // 0x86 +#define SLAVE_ADDR_W SLAVE_ADDR +#define SLAVE_ADDR_R SLAVE_ADDR | 1 // 0x87 + +#define SYS_CONFIG 0x10 +#define SYS_STATUS 0x11 + +#define SENS_OP_MODE 0x21 +#define SENS_START 0x22 +#define SENS_STOP 0x23 +#define SENS_STATUS 0x24 +#define SENS_TEMP 0x30 +#define SENS_HUMID 0x33 + +/** The AMS ENS210 class + */ +class AMS_ENS210 { + + public: + /** Create an AMS_ENS210 instance + * + * @param i2c The I2C interface to use for communication + */ + AMS_ENS210(I2C * i2c); + + /** Create an AMS_ENS210 instance + * + * @param i2c The I2C interface to use for communication + * @param temp_single_shot Set tempurature operation mode, true for single shot and false for continuious + * @param humid_single_shot Set humidity operation mode, true for single shot and false for continuious + */ + AMS_ENS210(I2C * i2c, bool temp_single_shot, bool humid_single_shot); + + /** Create an AMS_ENS210 instance + * + * @param i2c The I2C interface to use for communication + * @param temp_single_shot Set tempurature operation mode, true for single shot and false for continuious + * @param humid_single_shot Set humidity operation mode, true for single shot and false for continuious + * @param low_power Set power mode, true for low power/standby and false for active + */ + AMS_ENS210(I2C * i2c, bool temp_single_shot, bool humid_single_shot, bool low_power); + + /** Destroy the AMS_ENS210 instance + */ + ~AMS_ENS210(); + + /** Initalise the sensor + * + * @return Intalisation success + */ + bool init(); + + /** Software reset the sensor + * + * @return Reset success + */ + bool reset(); + + /** Set the power mode + * + * @param low_power True for low power/standby and false for active + * + * @return Write success + */ + bool power_mode(bool low_power); + + /** Get the current power mode + * + * @return The power mode, true for low power, false for active + */ + bool power_mode(); + + /** Set the tempurature operation mode + * + * @param single_shot True for single shot and false for continuious + * + * @return Write success + */ + bool temp_mode(bool single_shot); + + /** Get the current tempurature operation mode + * + * @return The tempurature operation mode, true for single shot, false for continuious + */ + bool temp_mode(); + + /** Set the humidity operation mode + * + * @param single_shot True for single shot and false for continuious + * + * @return Write success + */ + bool humid_mode(bool single_shot); + + /** Get the current humidity operation mode + * + * @return The humidity operation mode, true for single shot, false for continuious + */ + bool humid_mode(); + + /** Set the I2C interface + * + * @param i2c The I2C interface + */ + void i2c_interface(I2C * i2c); + + /** Start measurement collection + * + * @param temp Start the tempurature sensor + * @param humid Start the humidity sensor + * + * @return Write success + */ + bool start(bool temp = true, bool humid = true); + + /** Stop measurement collection + * + * @param temp Stop the tempurature sensor + * @param humid Stop the humidity sensor + * + * @return Write success + */ + bool stop(bool temp = true, bool humid = true); + + /** Get whether the sensor is measuring tempurature data + * + * @return Sensor activity, true for measureing, false for idle + */ + bool temp_is_measuring(); + + /** Get whether the sensor is measuring humidity data + * + * @return Sensor activity, true for measureing, false for idle + */ + bool humid_is_measuring(); + + /** Get whether the sensor has collected tempurature data + * + * @return Has data + */ + bool temp_has_data(); + + /** Get whether the sensor has collected humidity data + * + * @return Has data + */ + bool humid_has_data(); + + /** Get the most recent tempurature measurement, must call temp_has_data() first when in single shot mode otherwise the same data will be returned + * + * @return Most recent tempurature measurement as a 16 bits unsigned value in 1/64 Kelvin + */ + uint16_t temp_read(); + + /** Get the most recent humidty measurement, must call humid_has_data() first when in single shot mode otherwise the same data will be returned + * + * @return Most recent humidity measurement as a 16 bits unsigned value in 1/64 Kelvin + */ + uint16_t humid_read(); + + private: + I2C * _i2c; + bool _temp_mode; + bool _humid_mode; + bool _power_mode; + bool _reset; + uint16_t temp_reading; + uint16_t humid_reading; + + bool write_config(bool system = true, bool sensor = true); + const char * read_config(bool system = true, bool sensor = true); + int i2c_read(char reg_addr, char* output, int len); + + + +}; + +#endif /* AMS_ENS210_H */ \ No newline at end of file