Objet permettant de manipuler 2 ads1115 comme 1 seul afin d'avoir 8 entrées analogiques

This commit is contained in:
anschrammh 2020-01-09 23:19:47 +01:00
parent 1bd7c088a5
commit a0bf6de46d
2 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,85 @@
#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;
}

24
lib/MeasureUnit/Ads1115.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef ADS1115_H
#define ADS1115_H
#include "Adc.h"
#include <Wire.h>
#include <Adafruit_ADS1015.h>
class Ads1115 : public Adc
{
public:
Ads1115();
~Ads1115();
virtual void begin();
virtual int32_t sampleValue(int16_t channel, boolean sgl = true);
virtual int32_t sampleValue();
virtual double sampleVoltage(int16_t channel, boolean sgl = true);
virtual double sampleVoltage();
protected:
private:
uint16_t getReading(int16_t channel = -1, boolean sgl = true);
Adafruit_ADS1115 ads1, ads2;
};
#endif //ADS1115_H