/*
	BMP280.h
	Bosch BMP280 pressure sensor library for the Arduino microcontroller.
	This library uses I2C connection.
	Uses floating-point equations from BMP280 datasheet.
	modified by mhafuzul islam
	version 1.01		 16/9/2014 initial version
*/

#ifndef BMP280_h
#define BMP280_h
//
#include "Arduino.h"
//
#define BMP280_I2CADDRESS_0x76 0x76 // 7-bit address
#define BMP280_I2CADDRESS_0x77 0x77 // 7-bit address
//
#define	BMP280_REG_CONTROL 0xF4
#define	BMP280_REG_RESULT_PRESSURE 0xF7			// 0xF7(msb) , 0xF8(lsb) , 0xF9(xlsb) : stores the pressure data.
#define BMP280_REG_RESULT_TEMPRERATURE 0xFA		// 0xFA(msb) , 0xFB(lsb) , 0xFC(xlsb) : stores the temperature data.
//
#define	BMP280_COMMAND_TEMPERATURE 0x2E
#define	BMP280_COMMAND_PRESSURE0 0x25  			 
#define	BMP280_COMMAND_PRESSURE1 0x29  			
#define	BMP280_COMMAND_PRESSURE2 0x2D    
#define	BMP280_COMMAND_PRESSURE3 0x31    
#define	BMP280_COMMAND_PRESSURE4 0x5D    
#define	BMP280_COMMAND_OVERSAMPLING_MAX 0xF5
//
class CBMP280
{
	private:
	byte I2CAddress;
	char ReadCalibration();
		// Retrieve calibration data from device:
		// The BMP280 includes factory calibration data stored on the device.
		// Each device has different numbers, these must be retrieved and
		// used in the calculations when taking measurements.
	//			
	char ReadInt(char address, double &value);
		// read an signed int (16 bits) from a BMP280 register
		// address: BMP280 register address
		// value: external signed int for returned value (16 bits)
		// returns 1 for success, 0 for fail, with result in value
	//			
	char ReadUInt(char address, double &value);
		// read an unsigned int (16 bits) from a BMP280 register
		// address: BMP280 register address
		// value: external unsigned int for returned value (16 bits)
		// returns 1 for success, 0 for fail, with result in value
	//			
	char ReadBytes(unsigned char *values, char length);
		// read a number of bytes from a BMP280 register
		// values: array of char with register address in first location [0]
		// length: number of bytes to read back
		// returns 1 for success, 0 for fail, with read bytes in values[] array
	//					
	char WriteBytes(unsigned char *values, char length);
		// write a number of bytes to a BMP280 register (and consecutive subsequent registers)
		// values: array of char with register address in first location [0]
		// length: number of bytes to write
		// returns 1 for success, 0 for fail
	//				
	char GetUnPT(double &uP, double &uT);	
		//get uncalibrated UP and UT value.
	//			
	double dig_T1, dig_T2 , dig_T3 , dig_T4 , dig_P1, dig_P2 , dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9; 
	short FOversampling, FOversampling_t;
	double t_fine;
	char FError;
	public:
	CBMP280(const uint8_t i2caddress = BMP280_I2CADDRESS_0x76);
	char Open();
	char Open(int sdaPin, int sclPin);
		// call pressure.begin() to initialize BMP280 before use
		// returns 1 if success, 0 if failure (i2C connection problem.)
			
	short GetOversampling(void);
	char  SetOversampling(short oss);
	
	char StartMeasurement(void);
		// command BMP280 to start a pressure measurement
		// oversampling: 0 - 3 for oversampling value
		// returns (number of ms to wait) for success, 0 for fail
	
	char CalculateTemperature(double &T, double &uT);
		// calculation the true temperature from the given uncalibrated Temperature 
		
	char CalculatePressure(double &P, double uP);
		//calculation for measuring pressure.
		
	double GetSealevel(double P, double A);
		// convert absolute pressure to sea-level pressure 
		// P: absolute pressure (mbar)
		// A: current altitude (meters)
		// returns sealevel pressure in mbar

	double GetAltitude(double P, double P0);
		// convert absolute pressure to altitude (given baseline pressure; sea-level, runway, etc.)
		// P: absolute pressure (mbar)
		// P0: fixed baseline pressure (mbar)
		// returns signed altitude in meters

	char GetError(void);
		// If any library command fails, you can retrieve an extended
		// error code using this command. Errors are from the wire library: 
		// 0 = Success
		// 1 = Data too long to fit in transmit buffer
		// 2 = Received NACK on transmit of address
		// 3 = Received NACK on transmit of data
		// 4 = Other error
		
	char GetTemperaturePressure(double& T,double& P);
};


#endif
