projet3_temperature/lib/MeasureUnit/Ads1115.cpp

86 lines
1.5 KiB
C++

#include "Ads1115.h"
#define ADS_DEBUG
Ads1115::Ads1115() : ads1(0x48), ads2(0x49)
{
#ifdef ADS_DEBUG
Serial.println("Ads1115 constructor called");
#endif
//We set the adcs up:
ads1.setGain(GAIN_ONE);
ads2.setGain(GAIN_ONE);
}
Ads1115::~Ads1115()
{
}
void Ads1115::begin()
{
#ifdef ADS_DEBUG
Serial.println("Ads1115 : begin");
#endif
ads1.begin();
ads2.begin();
}
int32_t Ads1115::sampleValue(int16_t channel, boolean sgl)
{
int64_t total(0);
for(int i(0); i < getAdcSetting().getMeasureIteration(); i++)
{
delay(getAdcSetting().getDelayBetweenIteration());
total += getReading(channel, sgl);
}
//We divide
total /= getAdcSetting().getMeasureIteration();
//We return
return total;
}
int32_t Ads1115::sampleValue()
{
int64_t total(0);
for(int i(0); i < getAdcSetting().getMeasureIteration(); i++)
{
delay(getAdcSetting().getDelayBetweenIteration());
total += getReading();
}
//We divide
total /= getAdcSetting().getMeasureIteration();
//We return
return total;
}
double Ads1115::sampleVoltage(int16_t channel, boolean sgl)
{
return sampleValue(channel, sgl)*0.125;
}
double Ads1115::sampleVoltage()
{
return sampleValue()*0.125;
}
uint16_t Ads1115::getReading(int16_t channel, boolean sgl)
{
int16_t chan(channel == -1 ? _lastChannel : channel);
_lastChannel = chan > 8 ? 0 : chan;
if(chan < 4)
{
return ads1.readADC_SingleEnded(chan);
}
else if(chan < 8)
{
return ads2.readADC_SingleEnded(chan - 4);
}
else return 0;
}