From 17fd502cb5bec26d095f5a2772fe8691522b554a Mon Sep 17 00:00:00 2001 From: Henry Bruce Date: Fri, 8 Jan 2016 16:41:38 -0800 Subject: [PATCH] ads1x15: ads1015 module now implements IADC. Removed ads1015_iadc. Signed-off-by: Henry Bruce Signed-off-by: Abhishek Malik --- examples/c++/adc-sensor.cxx | 4 +- src/ads1x15/CMakeLists.txt | 4 +- src/ads1x15/ads1015.cxx | 71 ++++++++++++++++++++- src/ads1x15/ads1015.h | 61 ++++++++++++++++-- src/ads1x15/ads1015_iadc.cxx | 90 -------------------------- src/ads1x15/ads1015_iadc.h | 119 ----------------------------------- src/upm/iADC.h | 1 - 7 files changed, 130 insertions(+), 220 deletions(-) delete mode 100644 src/ads1x15/ads1015_iadc.cxx delete mode 100644 src/ads1x15/ads1015_iadc.h diff --git a/examples/c++/adc-sensor.cxx b/examples/c++/adc-sensor.cxx index adb11c32..1ebe7cb1 100644 --- a/examples/c++/adc-sensor.cxx +++ b/examples/c++/adc-sensor.cxx @@ -24,7 +24,7 @@ #include #include -#include "ads1015_iadc.h" +#include "ads1015.h" #include "mraa/gpio.hpp" #define EDISON_I2C_BUS 1 @@ -42,7 +42,7 @@ upm::IADC* getADC() { upm::IADC* adc = NULL; try { - adc = new upm::ADS1015_IADC(EDISON_I2C_BUS); + adc = new upm::ADS1015(EDISON_I2C_BUS); mraa::Gpio gpio(EDISON_GPIO_SI7005_CS); gpio.dir(mraa::DIR_OUT_HIGH); return adc; diff --git a/src/ads1x15/CMakeLists.txt b/src/ads1x15/CMakeLists.txt index b05a6537..67c2e857 100644 --- a/src/ads1x15/CMakeLists.txt +++ b/src/ads1x15/CMakeLists.txt @@ -1,5 +1,5 @@ set (libname "ads1x15") set (libdescription "analog to digital converter") -set (module_src ${libname}.cxx ads1115.cxx ads1015.cxx ads1015_iadc.cxx) -set (module_h ${libname}.h ads1115.h ads1015.h ads1015_iadc.h) +set (module_src ${libname}.cxx ads1115.cxx ads1015.cxx) +set (module_h ${libname}.h ads1115.h ads1015.h) upm_module_init() \ No newline at end of file diff --git a/src/ads1x15/ads1015.cxx b/src/ads1x15/ads1015.cxx index 1beef3dc..a2c83266 100644 --- a/src/ads1x15/ads1015.cxx +++ b/src/ads1x15/ads1015.cxx @@ -30,15 +30,68 @@ ADS1015::setSPS(ADSSAMPLERATE rate){ updateConfigRegister((m_config_reg & ~ADS1X15_DR_MASK) | rate); } -ADS1015::ADS1015(int bus, uint8_t address) : ADS1X15(bus, address) { +ADS1015::ADS1015(int bus, uint8_t address, float vref) : ADS1X15(bus, address) { m_name = "ADS1015"; m_conversionDelay = ADS1015_CONVERSIONDELAY; m_bitShift = 4; ADS1X15::getCurrentConfig(); + if (vref < 0.0 || vref > 6.144) + UPM_THROW("vref out of range"); + else if (vref > 4.096) + setGain(GAIN_TWOTHIRDS); + else if (vref > 2.048) + setGain(GAIN_ONE); + else if (vref > 1.024) + setGain(GAIN_TWO); + else if (vref > 0.512) + setGain(GAIN_FOUR); + else if (vref > 0.256) + setGain(GAIN_EIGHT); + else + setGain(GAIN_SIXTEEN); } ADS1015::~ADS1015(){}; +bool +ADS1015::isConfigured() { + return true; +} + +const char* +ADS1015::getModuleName() { + return m_name.c_str(); +} + + +unsigned int +ADS1015::getNumInputs() { + return 4; +} + +unsigned int +ADS1015::getResolutionInBits() { + return 12; +} + +uint16_t +ADS1015::getRawValue(unsigned int input) { + ADS1X15::ADSMUXMODE mode = getMuxMode(input); + updateConfigRegister((m_config_reg & ~ADS1X15_MUX_MASK) | mode, true); + usleep(m_conversionDelay); + uint16_t value = i2c->readWordReg(ADS1X15_REG_POINTER_CONVERT); + value = value >> m_bitShift; + return swapWord(value); +} + +float +ADS1015::getVoltage(unsigned int input) { + ADSMUXMODE mode = getMuxMode(input); + return getSample(mode); +} + + + //Private functions float ADS1015::getMultiplier(void){ @@ -99,4 +152,20 @@ ADS1015::setDelay(){ } } +ADS1X15::ADSMUXMODE +ADS1015::getMuxMode(unsigned int input) { + ADS1X15::ADSMUXMODE mode; + switch (input) { + case 0: + return SINGLE_0; + case 1: + return SINGLE_1; + case 2: + return SINGLE_2; + case 3: + return SINGLE_3; + default: + UPM_THROW("Invalid input"); + } +} diff --git a/src/ads1x15/ads1015.h b/src/ads1x15/ads1015.h index 6d0fd9fe..5e823c5f 100644 --- a/src/ads1x15/ads1015.h +++ b/src/ads1x15/ads1015.h @@ -26,8 +26,9 @@ #pragma once #include "ads1x15.h" +#include "upm/iADC.h" - +#define ADS1015_VREF 2.048 /*========================================================================= CONVERSION DELAY (in microS) @@ -82,7 +83,7 @@ namespace upm { * @image html ads1015.jpg * @snippet ads1x15.cxx Interesting */ - class ADS1015 : public ADS1X15 { + class ADS1015 : public ADS1X15, public IADC { public: @@ -111,12 +112,16 @@ namespace upm { /** - * ADS1X15 constructor + * ADS1015 constructor + * + * This constructor includes a vref parameter that can be used + * to set gain so it matches full scale value of input * * @param bus i2c bus the sensor is attached to. - * @param address. Device address. Default is 0x48. + * @param address. Optional device address. Default is 0x48. + * @param vref. Optional reference (i.e. half full swing) voltage. Default is 2.048V */ - ADS1015 (int bus, uint8_t address = 0x48); + ADS1015 (int bus, uint8_t address = 0x48, float vref = ADS1015_VREF); /** * ADS1X15 destructor @@ -132,9 +137,55 @@ namespace upm { */ void setSPS(ADSSAMPLERATE rate = SPS_1600); + /** + * Get number of inputs + * + * @return number of inputs + */ + unsigned int getNumInputs(); + + /** + * Read current value for current single ended analogue input + * + * @return current conversion value + */ + uint16_t getRawValue(unsigned int input); + + /** + * Read current voltage for current single ended analogue input + * + * @return current voltage + */ + float getVoltage(unsigned int input); + + /** + * Read current voltage for current single ended analogue input + * + * @return current voltage + */ + unsigned int getResolutionInBits(); + + /** + * Returns whether the sensor is detected and correctly configured. + * + * @return true if is detected and correctly configured, otherwise false + */ + bool isConfigured(); + + /** + * Returns module name + * + * @return modulename as const char* + */ + const char* getModuleName(); + + protected: float getMultiplier(void); void setDelay(void); + private: + ADS1X15::ADSMUXMODE getMuxMode(unsigned int input); + }; } diff --git a/src/ads1x15/ads1015_iadc.cxx b/src/ads1x15/ads1015_iadc.cxx deleted file mode 100644 index 327355b2..00000000 --- a/src/ads1x15/ads1015_iadc.cxx +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Author: Henry Bruce - * Copyright (c) 2015 Intel Corporation. - * Based on work by Marc Graham - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include "ads1015_iadc.h" - -using namespace upm; - - -ADS1015_IADC::ADS1015_IADC(int bus, uint8_t address, float vref) : ADS1015(bus, address) -{ - m_vref = vref; -} - -ADS1015_IADC::~ADS1015_IADC() -{ -} - -unsigned int ADS1015_IADC::getNumInputs() -{ - return 4; -} - -float ADS1015_IADC::getReferenceVoltage() -{ - return m_vref; -} - -unsigned int ADS1015_IADC::getResolutionInBits() -{ - return 12; -} - -uint16_t -ADS1015_IADC::getRawValue(unsigned int input) -{ - ADS1X15::ADSMUXMODE mode = getMuxMode(input); - updateConfigRegister((m_config_reg & ~ADS1X15_MUX_MASK) | mode, true); - usleep(m_conversionDelay); - uint16_t value = i2c->readWordReg(ADS1X15_REG_POINTER_CONVERT); - value = value >> m_bitShift; - return swapWord(value); -} - -float -ADS1015_IADC::getVoltage(unsigned int input) -{ - ADSMUXMODE mode = getMuxMode(input); - return getSample(mode); -} - - -ADS1X15::ADSMUXMODE -ADS1015_IADC::getMuxMode(unsigned int input) -{ - ADS1X15::ADSMUXMODE mode; - switch (input) { - case 0: - return SINGLE_0; - case 1: - return SINGLE_1; - case 2: - return SINGLE_2; - case 3: - return SINGLE_3; - default: - UPM_THROW("Invalid input"); - } -} diff --git a/src/ads1x15/ads1015_iadc.h b/src/ads1x15/ads1015_iadc.h deleted file mode 100644 index bcd5fca9..00000000 --- a/src/ads1x15/ads1015_iadc.h +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Author: Henry Bruce - * Copyright (c) 2015 Intel Corporation. - * Based on work by Marc Graham - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -/*=========================================================================*/ - -#pragma once - -#include "ads1015.h" -#include "upm/iADC.h" - -#define ADS1015_VREF 3.0 - -namespace upm { - /** - * @library ads1x15 - * @sensor ADS1115 - * @comname ADS1115 ADC - * @type electric - * @man ti adafruit - * @con i2c - * @web web http://www.ti.com/lit/ds/symlink/ads1015.pdf - * - * @brief API for ADS1015 - * - * The ADS1013, ADS1014, and ADS1015 are precision analog-to-digital converters (ADCs) with 12 bits of resolution - * offered in an ultra-small, leadless QFN-10 package or an MSOP-10 package. The ADS1013/4/5 are designed with - * precision, power, and ease of implementation in mind. The ADS1013/4/5 feature an onboard reference and oscillator. - * Data is transferred via an I2C-compatible serial interface; four I2C slave addresses can be selected. The ADS1013/4/5 - * operate from a single power supply ranging from 2.0V to 5.5V. - * The ADS1013/4/5 can perform conversions at rates up to 3300 samples per second (SPS). An onboard PGA is available - * on the ADS1014 and ADS1015 that offers input ranges from the supply to as low as ±256mV, allowing both large and small - * signals to be measured with high resolution. The ADS1015 also features an input multiplexer (MUX) that provides two - * differential or four single-ended inputs. - * The ADS1013/4/5 operate either in continuous conversion mode or a single-shot mode that automatically powers down - * after a conversion and greatly reduces current consumption during idle periods. The ADS1013/4/5 are specified from - * –40°C to +125°C. - * - * Tested with Adafriut ADS1015 board: https://www.adafruit.com/products/1083 - * - * @image html ads1015.jpg - * @snippet ads1x15.cxx Interesting - */ - class ADS1015_IADC : public ADS1015, public IADC { - - public: - - /** - * ADS1015_IADC constructor - * - * @param bus i2c bus the sensor is attached to. - * @param address. Device address. Default is 0x48. - * @param vref reference voltage for this sensor; default is 3.0 - */ - ADS1015_IADC (int bus, uint8_t address = 0x48, float vref = ADS1015_VREF); - - /** - * ADS1015_IADC destructor - */ - ~ADS1015_IADC (); - - /** - * Get number of inputs - * - * @return number of inputs - */ - unsigned int getNumInputs(); - - /** - * Read current value for current single ended analogue input - * - * @return current conversion value - */ - uint16_t getRawValue(unsigned int input); - - /** - * Read current voltage for current single ended analogue input - * - * @return current voltage - */ - float getVoltage(unsigned int input); - - float getReferenceVoltage(); - - unsigned int getResolutionInBits(); - - /** - * Returns whether the sensor is configured. - */ - bool isConfigured() { return true; } - - const char* getModuleName() { return m_name.c_str(); } - - private: - ADS1X15::ADSMUXMODE getMuxMode(unsigned int input); - float m_vref; - }; -} diff --git a/src/upm/iADC.h b/src/upm/iADC.h index b4217425..fbb32f2b 100644 --- a/src/upm/iADC.h +++ b/src/upm/iADC.h @@ -38,7 +38,6 @@ namespace upm class IADC : public IModuleStatus { public: - virtual float getReferenceVoltage() = 0; virtual unsigned int getResolutionInBits() = 0; virtual unsigned int getNumInputs() = 0; virtual uint16_t getRawValue(unsigned int input) = 0;