#include "ads1050.h"

Ads1015::Ads1015(i2c_ch1_pImpL* i2c,ErrorHandler* err)
{
	i2c_ads1015 = i2c; 
	errorHandling = err; 
	i2cWrite = 1;
	configuration = ADS1015_CONFIG_RESET; 
	writeConfig(); 
	configuration = 0; 
	errorHandling->addNewError(-1,__FILE__,"This mutpilexer mode does not Exist",KILL);
}

void Ads1015::writeConfig()
{
	i2c_ads1015->writeWordWithReg(ADS1015_I2C_ADRS,ADS1015_REG_CONFIG,configuration);
}

uint16_t Ads1015::getConfig()
{
	configuration = i2c_ads1015->readWord(ADS1015_I2C_ADRS, ADS1015_REG_CONFIG);
	return configuration; 
}
void Ads1015::setMultiplexer(uint16_t mode)
{
	if(mode <= ADS1015_MUX_AIN3_GND)
	{
		bitStart = ADS1015_MUX_BIT_NO; 
		bitEnd = bitStart + ADS1015_MUX_BIT_LEN -1;
		set_bits_range_uint16(&configuration,bitStart,bitEnd,mode);
	}
	else
	{
		errorHandling->handleError(-1,__FILE__);
	}
}

void Ads1015::setGain(uint16_t gain)
{
	if(gain <= ADS1015_GAIN_256)
	{
		bitStart = ADS1015_GAIN_BIT_NO; 
		bitEnd = bitStart + ADS1015_GAIN_BIT_LEN -1;
		set_bits_range_uint16(&configuration,bitStart,bitEnd,gain);
	}
}
 
void Ads1015::setOperationMode(uint8_t mode)
{
	if(mode <= ADS1015_MODE_SINGLE)
	{
		bitNo = ADS1015_MODE_BIT_NO;
		changeBitUint16(&configuration,bitNo,mode);
	}
}
 
void Ads1015::setDataRate(uint8_t rate)
{
	
}
 
void Ads1015::setComparatorMode(uint8_t mode)
{
	
}
 
void Ads1015::setComparatorPolarity(uint8_t pol)
{
	
}
 
void Ads1015::setComparatorLatch(uint8_t latch)
{
	
}
 
void Ads1015::setComparatorQueue(uint8_t queue)
{
	
}

void Ads1015::pointToConvReg()
{
	i2c_ads1015 -> writeByte(ADS1015_I2C_ADRS,ADS1015_REG_CONVERSION);
}
 
uint16_t Ads1015::checkConversion()
{
	bitNo = ADS1015_OS_CONVERTING; 
	getConfig();
	return getBitUint16(&configuration,bitNo);
}


int16_t Ads1015::getConversion()
{
	conversion = i2c_ads1015 -> readWord(ADS1015_I2C_ADRS,ADS1015_REG_CONVERSION)>>4;
	if (conversion > 0x07FF) {
      // negative number - extend the sign to 16th bit
      conversion |= 0xF000;
    }
    return (int16_t)conversion;
}

void Ads1015::startSingleConvertion()
{
	bitNo = ADS1015_OS_BIT_NO; 
	getConfig();
	setBitUint16(&configuration,bitNo);
}
