From 2bc879491951d5c5c031c92e06509c40debaaaa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sat, 23 Dec 2017 13:38:56 +0100 Subject: [PATCH] Move I2C code in BaseSensor to I2CSensor --- code/espurna/sensors/BMX280Sensor.h | 61 +++++++++--------------- code/espurna/sensors/BaseSensor.h | 32 ------------- code/espurna/sensors/EmonADC121Sensor.h | 20 +------- code/espurna/sensors/EmonADS1X15Sensor.h | 14 +----- code/espurna/sensors/EmonAnalogSensor.h | 1 - code/espurna/sensors/EmonSensor.h | 6 +-- code/espurna/sensors/I2CSensor.h | 57 ++++++++++++++++++++++ code/espurna/sensors/SHT3XI2CSensor.h | 22 ++------- code/espurna/sensors/SI7021Sensor.h | 21 ++------ 9 files changed, 93 insertions(+), 141 deletions(-) create mode 100644 code/espurna/sensors/I2CSensor.h diff --git a/code/espurna/sensors/BMX280Sensor.h b/code/espurna/sensors/BMX280Sensor.h index 32d59890..80b15dbb 100644 --- a/code/espurna/sensors/BMX280Sensor.h +++ b/code/espurna/sensors/BMX280Sensor.h @@ -7,13 +7,13 @@ #pragma once #include "Arduino.h" -#include "BaseSensor.h" +#include "I2CSensor.h" #include #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; diff --git a/code/espurna/sensors/BaseSensor.h b/code/espurna/sensors/BaseSensor.h index 30c069c0..b9ea6d46 100644 --- a/code/espurna/sensors/BaseSensor.h +++ b/code/espurna/sensors/BaseSensor.h @@ -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; - }; diff --git a/code/espurna/sensors/EmonADC121Sensor.h b/code/espurna/sensors/EmonADC121Sensor.h index 5e0da6fb..1d0a5133 100644 --- a/code/espurna/sensors/EmonADC121Sensor.h +++ b/code/espurna/sensors/EmonADC121Sensor.h @@ -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; - }; diff --git a/code/espurna/sensors/EmonADS1X15Sensor.h b/code/espurna/sensors/EmonADS1X15Sensor.h index a0b59e24..c156b320 100644 --- a/code/espurna/sensors/EmonADS1X15Sensor.h +++ b/code/espurna/sensors/EmonADS1X15Sensor.h @@ -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; diff --git a/code/espurna/sensors/EmonAnalogSensor.h b/code/espurna/sensors/EmonAnalogSensor.h index e781648f..88cb9b67 100644 --- a/code/espurna/sensors/EmonAnalogSensor.h +++ b/code/espurna/sensors/EmonAnalogSensor.h @@ -6,7 +6,6 @@ #pragma once #include "Arduino.h" -#include "BaseSensor.h" #include "EmonSensor.h" #define EMON_ANALOG_RESOLUTION 10 diff --git a/code/espurna/sensors/EmonSensor.h b/code/espurna/sensors/EmonSensor.h index 508a0a8b..a2e6d4fb 100644 --- a/code/espurna/sensors/EmonSensor.h +++ b/code/espurna/sensors/EmonSensor.h @@ -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 diff --git a/code/espurna/sensors/I2CSensor.h b/code/espurna/sensors/I2CSensor.h new file mode 100644 index 00000000..a3c18a9c --- /dev/null +++ b/code/espurna/sensors/I2CSensor.h @@ -0,0 +1,57 @@ +// ----------------------------------------------------------------------------- +// Abstract I2C sensor class (other sensor classes extend this class) +// Copyright (C) 2017 by Xose Pérez +// ----------------------------------------------------------------------------- + +#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; + +}; diff --git a/code/espurna/sensors/SHT3XI2CSensor.h b/code/espurna/sensors/SHT3XI2CSensor.h index b76a54ae..24fdf867 100644 --- a/code/espurna/sensors/SHT3XI2CSensor.h +++ b/code/espurna/sensors/SHT3XI2CSensor.h @@ -6,14 +6,14 @@ #pragma once #include "Arduino.h" -#include "BaseSensor.h" +#include "I2CSensor.h" #if I2C_USE_BRZO #include #else #include #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; } diff --git a/code/espurna/sensors/SI7021Sensor.h b/code/espurna/sensors/SI7021Sensor.h index f95009a7..f0da7b66 100644 --- a/code/espurna/sensors/SI7021Sensor.h +++ b/code/espurna/sensors/SI7021Sensor.h @@ -6,7 +6,7 @@ #pragma once #include "Arduino.h" -#include "BaseSensor.h" +#include "I2CSensor.h" #if I2C_USE_BRZO #include #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