From cf27d853cb223d2cdea21cd6711fa4f4e73022e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sun, 17 Dec 2017 17:37:04 +0100 Subject: [PATCH] Remove code for I2Cdevlib-ADS1115 --- code/espurna/config/prototypes.h | 3 - code/espurna/config/sensors.h | 4 +- code/espurna/sensors/EmonADS1X15Sensor.h | 140 +++++++++-------------- code/platformio.ini | 1 - 4 files changed, 55 insertions(+), 93 deletions(-) diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index 4ace7b9c..135814c2 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -78,9 +78,6 @@ template bool idbSend(const char * topic, unsigned char id, T payloa #if DS18B20_SUPPORT #include #endif -#if EMON_ADS1X15_SUPPORT & EMON_ADS1X15_USE_I2CDEVLIB -#include -#endif // ----------------------------------------------------------------------------- // Utils diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index be574c8b..2f564410 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -186,11 +186,9 @@ //-------------------------------------------------------------------------------- #ifndef EMON_ADS1X15_SUPPORT -#define EMON_ADS1X15_SUPPORT 1 // Do not build support by default +#define EMON_ADS1X15_SUPPORT 0 // Do not build support by default #endif -#define EMON_ADS1X15_USE_I2CDEVLIB 0 // Use I2CDevLib - #define EMON_ADS1X15_ADS1115 1 // 0 for ADS10115, 1 for ADS1115 #define EMON_ADS1X15_PORT_MASK 0x08 // A0=1 A1=2 A2=4 A4=8 #define EMON_ADS1X15_I2C_ADDRESS 0x48 // I2C address of the ADS1115 diff --git a/code/espurna/sensors/EmonADS1X15Sensor.h b/code/espurna/sensors/EmonADS1X15Sensor.h index e68b9c31..4c766bad 100644 --- a/code/espurna/sensors/EmonADS1X15Sensor.h +++ b/code/espurna/sensors/EmonADS1X15Sensor.h @@ -8,14 +8,10 @@ #include "BaseSensor.h" #include "EmonSensor.h" -#if EMON_ADS1X15_USE_I2CDEVLIB - #include +#if I2C_USE_BRZO + #include #else - #if I2C_USE_BRZO - #include - #else - #include - #endif + #include #endif #define ADS1015_CONVERSIONDELAY (1) @@ -112,9 +108,6 @@ class EmonADS1X15Sensor : public EmonSensor { } _count = _ports * EMON_ADS1X15_MAGNITUDES_PER_PORT; - // initialize - init(); - // warmup warmup(); @@ -206,80 +199,62 @@ class EmonADS1X15Sensor : public EmonSensor { // I2C //---------------------------------------------------------------------- - void init() { - #if EMON_ADS1X15_USE_I2CDEVLIB - _ads = new ADS1115(_address); - _ads->initialize(); - _ads->setMode(ADS1115_MODE_CONTINUOUS); - _ads->setRate(ADS1115_RATE_860); - _ads->setGain(ADS1115_PGA_4P096); - #endif - } - - #if EMON_ADS1X15_USE_I2CDEVLIB == 0 - - void setConfigRegistry(unsigned char channel, bool continuous, bool start) { + void setConfigRegistry(unsigned char channel, bool continuous, bool start) { + + // Start with default values + uint16_t config = 0; + config |= ADS1X15_REG_CONFIG_PGA_4_096V; // Set PGA/voltage range (0x0200) + config |= ADS1X15_REG_CONFIG_DR_MASK; // Always at max speed (0x00E0) + //config |= ADS1X15_REG_CONFIG_CMODE_TRAD; // Traditional comparator (default val) (0x0000) + //config |= ADS1X15_REG_CONFIG_CPOL_ACTVLOW; // Alert/Rdy active low (default val) (0x0000) + //config |= ADS1X15_REG_CONFIG_CLAT_NONLAT; // Non-latching (default val) (0x0000) + config |= ADS1X15_REG_CONFIG_CQUE_NONE; // Disable the comparator (default val) (0x0003) + if (start) { + config |= ADS1X15_REG_CONFIG_OS_SINGLE; // Start a single-conversion (0x8000) + } + if (continuous) { + //config |= ADS1X15_REG_CONFIG_MODE_CONTIN; // Continuous mode (default) (0x0000) + } else { + config |= ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode (0x0100) + } + config |= ((channel + 4) << 12); // Set single-ended input channel (0x4000 - 0x7000) - // Start with default values - uint16_t config = 0; - config |= ADS1X15_REG_CONFIG_PGA_4_096V; // Set PGA/voltage range (0x0200) - config |= ADS1X15_REG_CONFIG_DR_MASK; // Always at max speed (0x00E0) - //config |= ADS1X15_REG_CONFIG_CMODE_TRAD; // Traditional comparator (default val) (0x0000) - //config |= ADS1X15_REG_CONFIG_CPOL_ACTVLOW; // Alert/Rdy active low (default val) (0x0000) - //config |= ADS1X15_REG_CONFIG_CLAT_NONLAT; // Non-latching (default val) (0x0000) - config |= ADS1X15_REG_CONFIG_CQUE_NONE; // Disable the comparator (default val) (0x0003) - if (start) { - config |= ADS1X15_REG_CONFIG_OS_SINGLE; // Start a single-conversion (0x8000) - } - if (continuous) { - //config |= ADS1X15_REG_CONFIG_MODE_CONTIN; // Continuous mode (default) (0x0000) - } else { - config |= ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode (0x0100) - } - config |= ((channel + 4) << 12); // Set single-ended input channel (0x4000 - 0x7000) - - #if EMON_DEBUG - Serial.printf("[EMON] ADS1X115 Config Registry: %04X\n", config); - #endif - - // Write config register to the ADC - #if I2C_USE_BRZO - uint8_t buffer[3]; - buffer[0] = ADS1X15_REG_POINTER_CONFIG; - buffer[1] = config >> 8; - buffer[2] = config & 0xFF; - brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY); - brzo_i2c_write(buffer, 3, false); - brzo_i2c_end_transaction(); - #else - Wire.beginTransmission(_address); - Wire.write((uint8_t) ADS1X15_REG_POINTER_CONFIG); - Wire.write((uint8_t) (config >> 8)); - Wire.write((uint8_t) (config & 0xFF)); - Wire.endTransmission(); - #endif + #if EMON_DEBUG + Serial.printf("[EMON] ADS1X115 Config Registry: %04X\n", config); + #endif - } + // Write config register to the ADC + #if I2C_USE_BRZO + uint8_t buffer[3]; + buffer[0] = ADS1X15_REG_POINTER_CONFIG; + buffer[1] = config >> 8; + buffer[2] = config & 0xFF; + brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY); + brzo_i2c_write(buffer, 3, false); + brzo_i2c_end_transaction(); + #else + Wire.beginTransmission(_address); + Wire.write((uint8_t) ADS1X15_REG_POINTER_CONFIG); + Wire.write((uint8_t) (config >> 8)); + Wire.write((uint8_t) (config & 0xFF)); + Wire.endTransmission(); + #endif - #endif + } double getCurrent(unsigned char channel) { - #if EMON_ADS1X15_USE_I2CDEVLIB - _ads->setMultiplexer(channel + 4); - #else - // Force stop by setting single mode and back to continuous - static unsigned char previous = 9; - if (previous != channel) { - setConfigRegistry(channel, true, false); - setConfigRegistry(channel, false, false); - setConfigRegistry(channel, false, true); - delay(10); - readADC(channel); - previous = channel; - } - setConfigRegistry(channel, true, true); - #endif + // Force stop by setting single mode and back to continuous + static unsigned char previous = 9; + if (previous != channel) { + setConfigRegistry(channel, true, false); + setConfigRegistry(channel, false, false); + setConfigRegistry(channel, false, true); + delay(10); + readADC(channel); + previous = channel; + } + setConfigRegistry(channel, true, true); return read(channel, _pivot[channel]); @@ -289,10 +264,7 @@ class EmonADS1X15Sensor : public EmonSensor { unsigned int value = 0; - #if EMON_ADS1X15_USE_I2CDEVLIB - value = _ads->getConversion(); - - #elif I2C_USE_BRZO + #if I2C_USE_BRZO uint8_t buffer[3]; buffer[0] = ADS1X15_REG_POINTER_CONVERT; brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY); @@ -319,10 +291,6 @@ class EmonADS1X15Sensor : public EmonSensor { } - #if EMON_ADS1X15_USE_I2CDEVLIB - ADS1115 * _ads; - #endif - bool _is_ads1115 = true; unsigned char _address; unsigned char _mask; diff --git a/code/platformio.ini b/code/platformio.ini index 530ff851..0153bb84 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -21,7 +21,6 @@ lib_deps = NtpClientLib OneWire Brzo I2C - I2Cdevlib-ADS1115 EspSoftwareSerial https://bitbucket.org/xoseperez/justwifi.git#1.1.4 https://bitbucket.org/xoseperez/hlw8012.git#1.1.0