Browse Source

Properly name ADC channels

fastled
Xose Pérez 7 years ago
parent
commit
bfb9cb3a54
4 changed files with 27 additions and 27 deletions
  1. +4
    -4
      code/espurna/sensors/EmonADC121Sensor.h
  2. +18
    -18
      code/espurna/sensors/EmonADS1115Sensor.h
  3. +2
    -2
      code/espurna/sensors/EmonAnalogSensor.h
  4. +3
    -3
      code/espurna/sensors/EmonSensor.h

+ 4
- 4
code/espurna/sensors/EmonADC121Sensor.h View File

@ -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


+ 18
- 18
code/espurna/sensors/EmonADS1115Sensor.h View File

@ -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; i<EMON_ADS1115_PORTS; i++) {
for (unsigned char channel=0; channel<EMON_ADS1115_CHANNELS; channel++) {
if ((_mask & bit) == bit) {
if (count == index) return i;
if (count == port) return channel;
++count;
}
bit <<= 1;
@ -192,19 +192,19 @@ class EmonADS1115Sensor : public EmonSensor {
return 0;
}
unsigned int readADC(unsigned char port) {
if (port < EMON_ADS1115_PORTS) {
_ads->setMultiplexer(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};
};

+ 2
- 2
code/espurna/sensors/EmonAnalogSensor.h View File

@ -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;


+ 3
- 3
code/espurna/sensors/EmonSensor.h View File

@ -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;


Loading…
Cancel
Save