Browse Source

Move I2C code in BaseSensor to I2CSensor

fastled
Xose Pérez 7 years ago
parent
commit
2bc8794919
9 changed files with 93 additions and 141 deletions
  1. +23
    -38
      code/espurna/sensors/BMX280Sensor.h
  2. +0
    -32
      code/espurna/sensors/BaseSensor.h
  3. +1
    -19
      code/espurna/sensors/EmonADC121Sensor.h
  4. +1
    -13
      code/espurna/sensors/EmonADS1X15Sensor.h
  5. +0
    -1
      code/espurna/sensors/EmonAnalogSensor.h
  6. +3
    -3
      code/espurna/sensors/EmonSensor.h
  7. +57
    -0
      code/espurna/sensors/I2CSensor.h
  8. +4
    -18
      code/espurna/sensors/SHT3XI2CSensor.h
  9. +4
    -17
      code/espurna/sensors/SI7021Sensor.h

+ 23
- 38
code/espurna/sensors/BMX280Sensor.h View File

@ -7,13 +7,13 @@
#pragma once
#include "Arduino.h"
#include "BaseSensor.h"
#include "I2CSensor.h"
#include <SparkFunBME280.h>
#define BMX280_CHIP_BMP280 0x58
#define BMX280_CHIP_BME280 0x60
class BMX280Sensor : public BaseSensor {
class BMX280Sensor : public I2CSensor {
public:
@ -23,20 +23,9 @@ class BMX280Sensor : public BaseSensor {
// Public
// ---------------------------------------------------------------------
BMX280Sensor(): BaseSensor() {
BMX280Sensor(): I2CSensor() {
_sensor_id = SENSOR_BMX280_ID;
}
void setAddress(unsigned char address) {
if (_address == address) return;
_address = address;
_dirty = true;
}
// ---------------------------------------------------------------------
unsigned char getAddress() {
return _address;
_bme = new BME280();
}
// ---------------------------------------------------------------------
@ -51,11 +40,11 @@ class BMX280Sensor : public BaseSensor {
_chip = 0;
// I2C auto-discover
_address = lock_i2c(_address, sizeof(BMX280Sensor::addresses), BMX280Sensor::addresses);
_address = _begin_i2c(_address, sizeof(BMX280Sensor::addresses), BMX280Sensor::addresses);
if (_address == 0) return;
// Init
init();
_init();
}
@ -108,14 +97,14 @@ class BMX280Sensor : public BaseSensor {
_error = SENSOR_ERROR_OK;
unsigned char i = 0;
#if BMX280_TEMPERATURE > 0
if (index == i++) return bme->readTempC();
if (index == i++) return _bme->readTempC();
#endif
#if BMX280_PRESSURE > 0
if (index == i++) return bme->readFloatPressure() / 100;
if (index == i++) return _bme->readFloatPressure() / 100;
#endif
#if BMX280_HUMIDITY > 0
if (_chip == BMX280_CHIP_BME280) {
if (index == i) return bme->readFloatHumidity();
if (index == i) return _bme->readFloatHumidity();
}
#endif
}
@ -164,29 +153,25 @@ class BMX280Sensor : public BaseSensor {
protected:
void init() {
// Destroy previous instance if any
if (bme) delete bme;
void _init() {
bme = new BME280();
bme->settings.commInterface = I2C_MODE;
bme->settings.I2CAddress = _address;
bme->settings.runMode = BMX280_MODE;
bme->settings.tStandby = 0;
bme->settings.filter = 0;
bme->settings.tempOverSample = BMX280_TEMPERATURE;
bme->settings.pressOverSample = BMX280_PRESSURE;
bme->settings.humidOverSample = BMX280_HUMIDITY;
_bme->settings.commInterface = I2C_MODE;
_bme->settings.I2CAddress = _address;
_bme->settings.runMode = BMX280_MODE;
_bme->settings.tStandby = 0;
_bme->settings.filter = 0;
_bme->settings.tempOverSample = BMX280_TEMPERATURE;
_bme->settings.pressOverSample = BMX280_PRESSURE;
_bme->settings.humidOverSample = BMX280_HUMIDITY;
// Fix when not measuring temperature, t_fine should have a sensible value
if (BMX280_TEMPERATURE == 0) bme->t_fine = 100000; // aprox 20ºC
if (BMX280_TEMPERATURE == 0) _bme->t_fine = 100000; // aprox 20ºC
// Make sure sensor had enough time to turn on. BMX280 requires 2ms to start up
delay(10);
// Check sensor correctly initialized
_chip = bme->begin();
_chip = _bme->begin();
if ((_chip != BMX280_CHIP_BME280) && (_chip != BMX280_CHIP_BMP280)) {
_chip = 0;
i2cReleaseLock(_address);
@ -237,9 +222,9 @@ class BMX280Sensor : public BaseSensor {
// We set the sensor in "forced mode" to force a reading.
// After the reading the sensor will go back to sleep mode.
uint8_t value = bme->readRegister(BME280_CTRL_MEAS_REG);
uint8_t value = _bme->readRegister(BME280_CTRL_MEAS_REG);
value = (value & 0xFC) + 0x01;
bme->writeRegister(BME280_CTRL_MEAS_REG, value);
_bme->writeRegister(BME280_CTRL_MEAS_REG, value);
delay(_measurement_delay);
@ -247,7 +232,7 @@ class BMX280Sensor : public BaseSensor {
// ---------------------------------------------------------------------
BME280 * bme;
BME280 * _bme = NULL;
unsigned char _chip;
unsigned long _measurement_delay;


+ 0
- 32
code/espurna/sensors/BaseSensor.h View File

@ -95,34 +95,6 @@ class BaseSensor {
// Sensor ID
unsigned char getID() { return _sensor_id; };
// Specific for I2C sensors
unsigned char lock_i2c(unsigned char address, size_t size, unsigned char * addresses) {
// Check if we should release a previously locked address
if (_previous_address != address) {
i2cReleaseLock(_previous_address);
}
// If we have already an address, check it is not locked
if (address && !i2cGetLock(address)) {
_error = SENSOR_ERROR_I2C;
// If we don't have an address...
} else {
// Trigger auto-discover
address = i2cFindAndLock(size, addresses);
// If still nothing exit with error
if (address == 0) _error = SENSOR_ERROR_I2C;
}
_previous_address = address;
return address;
}
// Return sensor status (true for ready)
bool status() { return _error == 0; }
@ -146,8 +118,4 @@ class BaseSensor {
bool _dirty = true;
unsigned char _count = 0;
// I2C
unsigned char _previous_address = 0;
unsigned char _address = 0;
};

+ 1
- 19
code/espurna/sensors/EmonADC121Sensor.h View File

@ -6,8 +6,6 @@
#pragma once
#include "Arduino.h"
#include "BaseSensor.h"
#include "EmonSensor.h"
#include "EmonAnalogSensor.h"
#if I2C_USE_BRZO
@ -43,20 +41,6 @@ class EmonADC121Sensor : public EmonAnalogSensor {
init();
}
// ---------------------------------------------------------------------
void setAddress(unsigned char address) {
if (_address == address) return;
_address = address;
_dirty = true;
}
// ---------------------------------------------------------------------
unsigned char getAddress() {
return _address;
}
// ---------------------------------------------------------------------
// Sensor API
// ---------------------------------------------------------------------
@ -69,7 +53,7 @@ class EmonADC121Sensor : public EmonAnalogSensor {
// Discover
unsigned char addresses[] = {0x50, 0x51, 0x52, 0x54, 0x55, 0x56, 0x58, 0x59, 0x5A};
_address = lock_i2c(_address, sizeof(addresses), addresses);
_address = _begin_i2c(_address, sizeof(addresses), addresses);
if (_address == 0) return;
// Init sensor
@ -154,6 +138,4 @@ class EmonADC121Sensor : public EmonAnalogSensor {
}
unsigned char _address = 0;
};

+ 1
- 13
code/espurna/sensors/EmonADS1X15Sensor.h View File

@ -6,7 +6,6 @@
#pragma once
#include "Arduino.h"
#include "BaseSensor.h"
#include "EmonSensor.h"
#if I2C_USE_BRZO
@ -112,12 +111,6 @@ class EmonADS1X15Sensor : public EmonSensor {
// ---------------------------------------------------------------------
void setAddress(unsigned char address) {
if (_address == address) return;
_address = address;
_dirty = true;
}
void setType(unsigned char type) {
if (_type == type) return;
_type = type;
@ -138,10 +131,6 @@ class EmonADS1X15Sensor : public EmonSensor {
// ---------------------------------------------------------------------
unsigned char getAddress() {
return _address;
}
unsigned char getType() {
return _type;
}
@ -166,7 +155,7 @@ class EmonADS1X15Sensor : public EmonSensor {
// Discover
unsigned char addresses[] = {0x48, 0x49, 0x4A, 0x4B};
_address = lock_i2c(_address, sizeof(addresses), addresses);
_address = _begin_i2c(_address, sizeof(addresses), addresses);
if (_address == 0) return;
// Calculate ports
@ -396,7 +385,6 @@ class EmonADS1X15Sensor : public EmonSensor {
}
unsigned char _address;
unsigned char _type = ADS1X15_CHIP_ADS1115;
unsigned char _mask = 0x0F;
unsigned int _gain = ADS1X15_REG_CONFIG_PGA_4_096V;


+ 0
- 1
code/espurna/sensors/EmonAnalogSensor.h View File

@ -6,7 +6,6 @@
#pragma once
#include "Arduino.h"
#include "BaseSensor.h"
#include "EmonSensor.h"
#define EMON_ANALOG_RESOLUTION 10


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

@ -6,9 +6,9 @@
#pragma once
#include "Arduino.h"
#include "BaseSensor.h"
#include "I2CSensor.h"
class EmonSensor : public BaseSensor {
class EmonSensor : public I2CSensor {
public:
@ -16,7 +16,7 @@ class EmonSensor : public BaseSensor {
// Public
// ---------------------------------------------------------------------
EmonSensor(): BaseSensor() {
EmonSensor(): I2CSensor() {
// Calculate # of magnitudes
#if EMON_REPORT_CURRENT


+ 57
- 0
code/espurna/sensors/I2CSensor.h View File

@ -0,0 +1,57 @@
// -----------------------------------------------------------------------------
// Abstract I2C sensor class (other sensor classes extend this class)
// Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
// -----------------------------------------------------------------------------
#pragma once
#include "BaseSensor.h"
class I2CSensor : public BaseSensor {
public:
void setAddress(unsigned char address) {
if (_address == address) return;
_address = address;
_dirty = true;
}
unsigned char getAddress() {
return _address;
}
protected:
// Specific for I2C sensors
unsigned char _begin_i2c(unsigned char address, size_t size, unsigned char * addresses) {
// Check if we should release a previously locked address
if (_previous_address != address) {
i2cReleaseLock(_previous_address);
}
// If we have already an address, check it is not locked
if (address && !i2cGetLock(address)) {
_error = SENSOR_ERROR_I2C;
// If we don't have an address...
} else {
// Trigger auto-discover
address = i2cFindAndLock(size, addresses);
// If still nothing exit with error
if (address == 0) _error = SENSOR_ERROR_I2C;
}
_previous_address = address;
return address;
}
unsigned char _previous_address = 0;
unsigned char _address = 0;
};

+ 4
- 18
code/espurna/sensors/SHT3XI2CSensor.h View File

@ -6,14 +6,14 @@
#pragma once
#include "Arduino.h"
#include "BaseSensor.h"
#include "I2CSensor.h"
#if I2C_USE_BRZO
#include <brzo_i2c.h>
#else
#include <Wire.h>
#endif
class SHT3XI2CSensor : public BaseSensor {
class SHT3XI2CSensor : public I2CSensor {
public:
@ -21,24 +21,10 @@ class SHT3XI2CSensor : public BaseSensor {
// Public
// ---------------------------------------------------------------------
SHT3XI2CSensor(): BaseSensor() {
SHT3XI2CSensor(): I2CSensor() {
_sensor_id = SENSOR_SHT3X_I2C_ID;
}
// ---------------------------------------------------------------------
void setAddress(unsigned char address) {
if (_address == address) return;
_address = address;
_dirty = true;
}
// ---------------------------------------------------------------------
unsigned char getAddress() {
return _address;
}
// ---------------------------------------------------------------------
// Sensor API
// ---------------------------------------------------------------------
@ -51,7 +37,7 @@ class SHT3XI2CSensor : public BaseSensor {
// I2C auto-discover
unsigned char addresses[] = {0x45};
_address = lock_i2c(_address, sizeof(addresses), addresses);
_address = _begin_i2c(_address, sizeof(addresses), addresses);
if (_address == 0) return;
}


+ 4
- 17
code/espurna/sensors/SI7021Sensor.h View File

@ -6,7 +6,7 @@
#pragma once
#include "Arduino.h"
#include "BaseSensor.h"
#include "I2CSensor.h"
#if I2C_USE_BRZO
#include <brzo_i2c.h>
#else
@ -23,7 +23,7 @@
#define SI7021_CMD_TMP_NOHOLD 0xF3
#define SI7021_CMD_HUM_NOHOLD 0xF5
class SI7021Sensor : public BaseSensor {
class SI7021Sensor : public I2CSensor {
public:
@ -31,23 +31,10 @@ class SI7021Sensor : public BaseSensor {
// Public
// ---------------------------------------------------------------------
SI7021Sensor(): BaseSensor() {
SI7021Sensor(): I2CSensor() {
_sensor_id = SENSOR_SI7021_ID;
}
// ---------------------------------------------------------------------
void setAddress(unsigned char address) {
if (_address != address) _dirty = true;
_address = address;
}
// ---------------------------------------------------------------------
unsigned char getAddress() {
return _address;
}
// ---------------------------------------------------------------------
// Sensor API
// ---------------------------------------------------------------------
@ -60,7 +47,7 @@ class SI7021Sensor : public BaseSensor {
// I2C auto-discover
unsigned char addresses[] = {0x40};
_address = lock_i2c(_address, sizeof(addresses), addresses);
_address = _begin_i2c(_address, sizeof(addresses), addresses);
if (_address == 0) return;
// Check device


Loading…
Cancel
Save