From a0bf6de46dd9c6e1350b92842de81aad3020816f Mon Sep 17 00:00:00 2001 From: anschrammh Date: Thu, 9 Jan 2020 23:19:47 +0100 Subject: [PATCH] =?UTF-8?q?Objet=20permettant=20de=20manipuler=202=20ads11?= =?UTF-8?q?15=20comme=201=20seul=20afin=20d'avoir=208=20entr=C3=A9es=20ana?= =?UTF-8?q?logiques?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/MeasureUnit/Ads1115.cpp | 85 +++++++++++++++++++++++++++++++++++++ lib/MeasureUnit/Ads1115.h | 24 +++++++++++ 2 files changed, 109 insertions(+) create mode 100644 lib/MeasureUnit/Ads1115.cpp create mode 100644 lib/MeasureUnit/Ads1115.h diff --git a/lib/MeasureUnit/Ads1115.cpp b/lib/MeasureUnit/Ads1115.cpp new file mode 100644 index 0000000..18fbec2 --- /dev/null +++ b/lib/MeasureUnit/Ads1115.cpp @@ -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; +} diff --git a/lib/MeasureUnit/Ads1115.h b/lib/MeasureUnit/Ads1115.h new file mode 100644 index 0000000..9132b4f --- /dev/null +++ b/lib/MeasureUnit/Ads1115.h @@ -0,0 +1,24 @@ +#ifndef ADS1115_H +#define ADS1115_H +#include "Adc.h" +#include +#include + +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