From bfb9cb3a54ef3d88fe0c0ecc724974bd42618e16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sat, 16 Dec 2017 10:05:13 +0100 Subject: [PATCH] Properly name ADC channels --- code/espurna/sensors/EmonADC121Sensor.h | 8 +++--- code/espurna/sensors/EmonADS1115Sensor.h | 36 ++++++++++++------------ code/espurna/sensors/EmonAnalogSensor.h | 4 +-- code/espurna/sensors/EmonSensor.h | 6 ++-- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/code/espurna/sensors/EmonADC121Sensor.h b/code/espurna/sensors/EmonADC121Sensor.h index 8f6b031f..1395d6cf 100644 --- a/code/espurna/sensors/EmonADC121Sensor.h +++ b/code/espurna/sensors/EmonADC121Sensor.h @@ -106,24 +106,24 @@ class EmonADC121Sensor : public EmonSensor { protected: - unsigned int readADC(unsigned char port) { + unsigned int readADC(unsigned char channel) { unsigned int value; #if I2C_USE_BRZO uint8_t buffer[2]; buffer[0] = ADC121_REG_RESULT; - brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY); + brzo_i2c_start_transaction(channel, I2C_SCL_FREQUENCY); brzo_i2c_write(buffer, 1, false); brzo_i2c_read(buffer, 2, false); brzo_i2c_end_transaction(); value = (buffer[0] & 0x0F) << 8; value |= buffer[1]; #else - Wire.beginTransmission(port); + Wire.beginTransmission(channel); Wire.write(ADC121_REG_RESULT); Wire.endTransmission(); - Wire.requestFrom(port, (unsigned char) 2); + Wire.requestFrom(channel, (unsigned char) 2); value = (Wire.read() & 0x0F) << 8; value = value + Wire.read(); #endif diff --git a/code/espurna/sensors/EmonADS1115Sensor.h b/code/espurna/sensors/EmonADS1115Sensor.h index 38277d1a..889b0bac 100644 --- a/code/espurna/sensors/EmonADS1115Sensor.h +++ b/code/espurna/sensors/EmonADS1115Sensor.h @@ -84,7 +84,7 @@ #define ADS1015_REG_CONFIG_CQUE_NONE (0x0003) // Disable the comparator and put ALERT/RDY in high state (default) */ -#define EMON_ADS1115_PORTS 4 +#define EMON_ADS1115_CHANNELS 4 #define EMON_ADS1115_MAGNITUDES_PER_PORT 2 class EmonADS1115Sensor : public EmonSensor { @@ -126,7 +126,7 @@ class EmonADS1115Sensor : public EmonSensor { // Descriptive name of the slot # index String slot(unsigned char index) { char buffer[35]; - unsigned char port = getPort(index / EMON_ADS1115_MAGNITUDES_PER_PORT); + unsigned char port = getChannel(index / EMON_ADS1115_MAGNITUDES_PER_PORT); snprintf(buffer, sizeof(buffer), "EMON @ ADS1115 (A%d) @ I2C (0x%02X)", port, _address); return String(buffer); } @@ -135,7 +135,7 @@ class EmonADS1115Sensor : public EmonSensor { magnitude_t type(unsigned char index) { if (index < _count) { _error = SENSOR_ERROR_OK; - unsigned char port = getPort(index / EMON_ADS1115_MAGNITUDES_PER_PORT); + unsigned char port = getChannel(index / EMON_ADS1115_MAGNITUDES_PER_PORT); unsigned char magnitude = index % EMON_ADS1115_MAGNITUDES_PER_PORT; if (magnitude == 0) return MAGNITUDE_CURRENT; if (magnitude == 1) return MAGNITUDE_POWER_APPARENT; @@ -149,7 +149,7 @@ class EmonADS1115Sensor : public EmonSensor { void pre() { //static unsigned long last = 0; for (unsigned char index=0; index<_ports; index++) { - unsigned char port = getPort(index); + unsigned char port = getChannel(index); _current[port] = read(port); //if (last > 0) { // _delta[port] = _current[port] * _voltage * (millis() - last) / 1000; @@ -164,7 +164,7 @@ class EmonADS1115Sensor : public EmonSensor { if (index < _count) { _error = SENSOR_ERROR_OK; - unsigned char port = getPort(index / EMON_ADS1115_MAGNITUDES_PER_PORT); + unsigned char port = getChannel(index / EMON_ADS1115_MAGNITUDES_PER_PORT); unsigned char magnitude = index % EMON_ADS1115_MAGNITUDES_PER_PORT; if (magnitude == 0) return _current[port]; if (magnitude == 1) return _current[port] * _voltage; @@ -179,12 +179,12 @@ class EmonADS1115Sensor : public EmonSensor { protected: - unsigned char getPort(unsigned char index) { + unsigned char getChannel(unsigned char port) { unsigned char count = 0; unsigned char bit = 1; - for (unsigned char i=0; isetMultiplexer(port + 4); + unsigned int readADC(unsigned char channel) { + if (channel < EMON_ADS1115_CHANNELS) { + _ads->setMultiplexer(channel + 4); return _ads->getConversion(true); } return 0; } /* - unsigned int readADC(unsigned char port) { + unsigned int readADC(unsigned char channel) { - if (port > 3) return 0; - port = 3; + if (channel > 3) return 0; + channel = 3; unsigned int value; // Start with default values @@ -218,7 +218,7 @@ class EmonADS1115Sensor : public EmonSensor { config |= ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) config |= ADS1015_REG_CONFIG_OS_SINGLE; // Set 'start single-conversion' bit config |= EMON_ADS1115_GAIN; // Set PGA/voltage range - config |= ((port + 4) << 12); // Set single-ended input channel + config |= ((channel + 4) << 12); // Set single-ended input channel Serial.println(config); @@ -272,9 +272,9 @@ class EmonADS1115Sensor : public EmonSensor { unsigned char _address; unsigned char _mask; unsigned char _ports; - double _current[EMON_ADS1115_PORTS] = {0, 0, 0, 0}; - //unsigned long _energy[EMON_ADS1115_PORTS] = {0, 0, 0, 0}; - //unsigned long _delta[EMON_ADS1115_PORTS] = {0, 0, 0, 0}; + double _current[EMON_ADS1115_CHANNELS] = {0, 0, 0, 0}; + //unsigned long _energy[EMON_ADS1115_CHANNELS] = {0, 0, 0, 0}; + //unsigned long _delta[EMON_ADS1115_CHANNELS] = {0, 0, 0, 0}; }; diff --git a/code/espurna/sensors/EmonAnalogSensor.h b/code/espurna/sensors/EmonAnalogSensor.h index 3d0d2769..3e6e977a 100644 --- a/code/espurna/sensors/EmonAnalogSensor.h +++ b/code/espurna/sensors/EmonAnalogSensor.h @@ -78,8 +78,8 @@ class EmonAnalogSensor : public EmonSensor { protected: - unsigned int readADC(unsigned char port) { - return analogRead(port); + unsigned int readADC(unsigned char channel) { + return analogRead(channel); } unsigned char _gpio; diff --git a/code/espurna/sensors/EmonSensor.h b/code/espurna/sensors/EmonSensor.h index c2e4c80d..29912b0c 100644 --- a/code/espurna/sensors/EmonSensor.h +++ b/code/espurna/sensors/EmonSensor.h @@ -38,7 +38,7 @@ class EmonSensor : public BaseSensor { protected: - virtual unsigned int readADC(unsigned char port) {} + virtual unsigned int readADC(unsigned char channel) {} void calculateMultiplier() { unsigned int s = 1; @@ -52,7 +52,7 @@ class EmonSensor : public BaseSensor { } } - double read(unsigned char port) { + double read(unsigned char channel) { int sample; int max = 0; @@ -64,7 +64,7 @@ class EmonSensor : public BaseSensor { for (unsigned long i=0; i<_samples; i++) { // Read analog value - sample = readADC(port); + sample = readADC(channel); if (sample > max) max = sample; if (sample < min) min = sample;