/*
 *
 *	https://www.ti.com/lit/ds/symlink/ads1015.pdf?ts=1616137488325&ref_url=https%253A%252F%252Fwww.google.com%252F
 *
 *
 *
 *
 *
 */

#ifndef _ADS1015_H_
#define _ADS1015_H_

#define ADS1015_I2C_ADRS 0x48
#define ADS1015_REG_CONVERSION 0	// Conversion Register
#define ADS1015_REG_CONFIG 1		// Config Register
#define ADS1015_REG_LOW_TRESHOLD 2	// Low Thresh Register
#define ADS1015_REG_HIGH_TRESHOLD 3	// Hight Thresh Register


#define ADS1015_CONFIG_RESET 0x8583 // Default mode.
#define ADS1015_OS_CONVERTING 0		// When reading: Device is currently performing a conversion 
#define ADS1015_OS_READY 1			// When reading: Device is not currently performing a conversion
#define ADS1015_OS_START_SINGLE 1	// Start a single conversion(when in power-down state)
#define ADS1015_OS_BIT_NO 15
#define ADS1015_OS_BIT_LEN 1

#define ADS1015_MUX_AIN0P_AIN1N 0	// AINP=AIN0 and AINN=AIN1
#define ADS1015_MUX_AIN0P_AIN3N 1	// AINP=AIN0 and AINN=AIN3
#define ADS1015_MUX_AIN1P_AIN3N 2	// AINP=AIN1 and AINN=AIN3 
#define ADS1015_MUX_AIN2P_AIN3N 3	// AINP=AIN2 and AINN=AIN3 
#define ADS1015_MUX_AIN0_GND 4		// AIN0 and AINN=GND
#define ADS1015_MUX_AIN1_GND 5		// AIN1 and AINN=GND
#define ADS1015_MUX_AIN2_GND 6		// AIN2 and AINN=GND
#define ADS1015_MUX_AIN3_GND 7		// AIN3 and AINN=GND
#define ADS1015_MUX_BIT_NO 12
#define ADS1015_MUX_BIT_LEN 3

// Do not applymorethan VDD+0.3 V to the analog inputs of the device
#define ADS1015_GAIN_6144 0			// +-6.144 V
#define ADS1015_GAIN_4096 1			// +-4.096 V
#define ADS1015_GAIN_2048 2			// +-2.048 V
#define ADS1015_GAIN_1024 3			// +-1.024 V
#define ADS1015_GAIN_512 4			// +-0.512 V
#define ADS1015_GAIN_256 5			// +-0.256 V
#define ADS1015_GAIN_BIT_NO 9
#define ADS1015_GAIN_BIT_LEN 3

#define ADS1015_MODE_CONTINIOUS 0
#define ADS1015_MODE_SINGLE 1
#define ADS1015_MODE_BIT_NO 8
#define ADS1015_MODE_BIT_LEN 1

#define ADS1015_DR_128_SPS 0	// 000 : 128 SPS
#define ADS1015_DR_250_SPS 1	// 001 : 250 SPS
#define ADS1015_DR_490_SPS 2	// 010 : 490 SPS
#define ADS1015_DR_920_SPS 3	// 011 : 920 SPS
#define ADS1015_DR_1600_SPS 4	// 100 : 1600 SPS(default)
#define ADS1015_DR_2400_SPS 5	// 101 : 2400 SPS
#define ADS1015_DR_3300_SPS 6	// 110 : 3300 SPS
#define ADS1015_DR_BIT_NO 5 
#define ADS1015_DR_BIT_LEN 3 

#define ADS1015_COMP_MODE_NORMAL 0	// Traditional comparator(default
#define ADS1015_COMP_MODE_WINDOW 1	// Window comparator
#define ADS1015_COMP_MODE_BIT_NO 4
#define ADS1015_COMP_MODE_BIT_LEN 1

#define ADS1015_COMP_POL_LOW 0	// Active low (default)
#define ADS1015_COMP_POL_HIGH 1	// Active high
#define ADS1015_COMP_POL_BIT_NO 3
#define ADS1015_COMP_POL_BIT_LEN 1

#define ADS1015_COMP_LAT_OFF 0	// Non latching comparator. The ALERT/RDY pin does not latch when asserted (default)
#define ADS1015_COMP_LAT_ON 1	// Latching comparator.The asserted ALERT/RDY pin remains latched until conversion data are read by the master or an appropriate SMBus alert response is sent by the master. The device responds with its address,and it is the lowest address currently asserting the ALERT/RDY bus line.
#define ADS1015_COMP_LAT_BIT_NO 2	
#define ADS1015_COMP_LAT_BIT_LEN 1	

#define ADS1015_COMP_QUE_1 0	// Assert after one conversion 
#define ADS1015_COMP_QUE_2 1	// Assert after two conversion 
#define ADS1015_COMP_QUE_3 2	// Assert after four conversion 
#define ADS1015_COMP_QUE_0 3	// Disable comparator and set ALERT/RDY pin to high-impedance (default)
#define ADS1015_COMP_QUE_BIT_NO 0	
#define ADS1015_COMP_QUE_BIT_LEN 2	



#include <stdint.h>
#include <iostream>
#include <unistd.h>
#include <math.h>
#include "../../periferals/i2c/i2c_ch1_pImpL.hpp"
#include "../../management/errorHandling.h"
#include "../../algorithms/bitgestion.h"

class Ads1015
{
	public: 
		Ads1015(i2c_ch1_pImpL* i2c,ErrorHandler* err);
		void setMultiplexer(uint16_t mode);
		void setGain(uint16_t gain); 
		void setOperationMode(uint8_t mode); 
		void setDataRate(uint8_t rate); 
		void setComparatorMode(uint8_t mode); 
		void setComparatorPolarity(uint8_t pol); 
		void setComparatorLatch(uint8_t latch); 
		void setComparatorQueue(uint8_t queue); 
		void startSingleConvertion(); 
		void pointToConvReg();
		uint16_t checkConversion();
		int16_t getConversion();
		uint16_t getConfig();
		void configMode();
		void writeConfig(); 

	private: 
		ErrorHandler* errorHandling;
		i2c_ch1_pImpL* i2c_ads1015;	
		uint8_t i2cWrite;
		uint8_t bitNo;
		uint8_t bitStart;
		uint8_t bitEnd;
		uint16_t Toset;
		uint16_t configuration; 
		uint16_t conversion; 
		uint16_t lowTreshold; 
		uint16_t highTreshlod;
}; 

#endif //_ADS1015_H_
