Implemented error handling and performance improvements

This commit is contained in:
2021-07-13 19:14:34 +02:00
parent 531b734469
commit 862f0474ec
2 changed files with 196 additions and 98 deletions

View File

@@ -38,6 +38,14 @@
#include <Wire.h> #include <Wire.h>
#endif #endif
//#define DEBUG_OUTPUT
#ifdef DEBUG_OUTPUT
#include <esp_log.h>
#define DBGPRNT(format, ...) ESP_LOGW("BMP085", format, ##__VA_ARGS__)
#else
#define DBGPRNT(format, ...)
#endif
#include <limits.h> #include <limits.h>
#include <math.h> #include <math.h>
@@ -52,6 +60,14 @@ static uint8_t _bmp085Mode;
#define BMP085_USE_DATASHEET_VALS \ #define BMP085_USE_DATASHEET_VALS \
(0) //!< Set to 1 for sanity check, when true, will use values from datasheet (0) //!< Set to 1 for sanity check, when true, will use values from datasheet
#if ARDUINO >= 100
#define WIRE_WRITE Wire.write
#define WIRE_READ Wire.read
#else
#define WIRE_WRITE Wire.send
#define WIRE_READ Wire.receive
#endif
/*************************************************************************** /***************************************************************************
PRIVATE FUNCTIONS PRIVATE FUNCTIONS
***************************************************************************/ ***************************************************************************/
@@ -61,16 +77,15 @@ static uint8_t _bmp085Mode;
@brief Writes an 8 bit value over I2C @brief Writes an 8 bit value over I2C
*/ */
/**************************************************************************/ /**************************************************************************/
static void writeCommand(byte reg, byte value) { static bool writeCommand(byte reg, byte value) {
bool succ{true};
Wire.beginTransmission((uint8_t)BMP085_ADDRESS); Wire.beginTransmission((uint8_t)BMP085_ADDRESS);
#if ARDUINO >= 100 if (WIRE_WRITE((uint8_t)reg) != 1) { DBGPRNT("fail"); succ = false; }
Wire.write((uint8_t)reg); if (WIRE_WRITE((uint8_t)value) != 1) { DBGPRNT("fail"); succ = false; }
Wire.write((uint8_t)value); if (const auto result = Wire.endTransmission(); result != I2C_ERROR_OK) { DBGPRNT("fail %hhu", result); succ = false; }
#else
Wire.send(reg); return succ;
Wire.send(value);
#endif
Wire.endTransmission();
} }
/**************************************************************************/ /**************************************************************************/
@@ -78,21 +93,21 @@ static void writeCommand(byte reg, byte value) {
@brief Reads an 8 bit value over I2C @brief Reads an 8 bit value over I2C
*/ */
/**************************************************************************/ /**************************************************************************/
static void read8(byte reg, uint8_t *value) { static bool read8(byte reg, uint8_t *value) {
bool succ{true};
Wire.beginTransmission((uint8_t)BMP085_ADDRESS); Wire.beginTransmission((uint8_t)BMP085_ADDRESS);
#if ARDUINO >= 100 if (WIRE_WRITE((uint8_t)reg) != 1) { DBGPRNT("fail"); succ = false; }
Wire.write((uint8_t)reg); if (const auto result = Wire.endTransmission(); result != I2C_ERROR_OK) { DBGPRNT("fail %hhu", result); succ = false; }
#else
Wire.send(reg); if (const auto result = Wire.requestFrom((uint8_t)BMP085_ADDRESS, (byte)1); result != 1) { DBGPRNT("fail %hhu", result); succ = false; }
#endif if (const auto result = WIRE_READ(); result == -1) { DBGPRNT("fail %i", result); succ = false; }
Wire.endTransmission(); else *value = result;
Wire.requestFrom((uint8_t)BMP085_ADDRESS, (byte)1);
#if ARDUINO >= 100 //if (const auto result = Wire.endTransmission(); result != I2C_ERROR_OK) { DBGPRNT("fail %hhu", result); succ = false; }
*value = Wire.read();
#else
*value = Wire.receive();
#endif
Wire.endTransmission(); Wire.endTransmission();
return succ;
} }
/**************************************************************************/ /**************************************************************************/
@@ -100,21 +115,24 @@ static void read8(byte reg, uint8_t *value) {
@brief Reads a 16 bit value over I2C @brief Reads a 16 bit value over I2C
*/ */
/**************************************************************************/ /**************************************************************************/
static void read16(byte reg, uint16_t *value) { static bool read16(byte reg, uint16_t *value) {
bool succ{true};
Wire.beginTransmission((uint8_t)BMP085_ADDRESS); Wire.beginTransmission((uint8_t)BMP085_ADDRESS);
#if ARDUINO >= 100 if (WIRE_WRITE((uint8_t)reg) != 1) { DBGPRNT("fail"); succ = false; }
Wire.write((uint8_t)reg); if (const auto result = Wire.endTransmission(); result != I2C_ERROR_OK) { DBGPRNT("fail %hhu", result); succ = false; }
#else
Wire.send(reg); if (const auto result = Wire.requestFrom((uint8_t)BMP085_ADDRESS, (byte)2); result != 2) { DBGPRNT("fail %hhu", result); succ = false; }
#endif const auto result0 = WIRE_READ();
Wire.endTransmission(); const auto result1 = WIRE_READ();
Wire.requestFrom((uint8_t)BMP085_ADDRESS, (byte)2); if (result0 == -1) { DBGPRNT("fail %i", result0); succ = false; }
#if ARDUINO >= 100 else if (result1 == -1) { DBGPRNT("fail %i", result1); succ = false; }
*value = (Wire.read() << 8) | Wire.read(); else { *value = (result0 << 8) | result1; }
#else
*value = (Wire.receive() << 8) | Wire.receive(); //if (const auto result = Wire.endTransmission(); result != I2C_ERROR_OK) { DBGPRNT("fail %hhu", result); succ = false; }
#endif
Wire.endTransmission(); Wire.endTransmission();
return succ;
} }
/**************************************************************************/ /**************************************************************************/
@@ -122,10 +140,12 @@ static void read16(byte reg, uint16_t *value) {
@brief Reads a signed 16 bit value over I2C @brief Reads a signed 16 bit value over I2C
*/ */
/**************************************************************************/ /**************************************************************************/
static void readS16(byte reg, int16_t *value) { static bool readS16(byte reg, int16_t *value) {
bool succ{true};
uint16_t i; uint16_t i;
read16(reg, &i); if (!read16(reg, &i)) { DBGPRNT("fail"); succ = false; }
*value = (int16_t)i; else *value = (int16_t)i;
return succ;
} }
/**************************************************************************/ /**************************************************************************/
@@ -133,7 +153,8 @@ static void readS16(byte reg, int16_t *value) {
@brief Reads the factory-set coefficients @brief Reads the factory-set coefficients
*/ */
/**************************************************************************/ /**************************************************************************/
static void readCoefficients(void) { static bool readCoefficients(void) {
bool succ{true};
#if BMP085_USE_DATASHEET_VALS #if BMP085_USE_DATASHEET_VALS
_bmp085_coeffs.ac1 = 408; _bmp085_coeffs.ac1 = 408;
_bmp085_coeffs.ac2 = -72; _bmp085_coeffs.ac2 = -72;
@@ -148,18 +169,19 @@ static void readCoefficients(void) {
_bmp085_coeffs.md = 2868; _bmp085_coeffs.md = 2868;
_bmp085Mode = 0; _bmp085Mode = 0;
#else #else
readS16(BMP085_REGISTER_CAL_AC1, &_bmp085_coeffs.ac1); if (!readS16(BMP085_REGISTER_CAL_AC1, &_bmp085_coeffs.ac1)) { DBGPRNT("fail"); succ = false; }
readS16(BMP085_REGISTER_CAL_AC2, &_bmp085_coeffs.ac2); if (!readS16(BMP085_REGISTER_CAL_AC2, &_bmp085_coeffs.ac2)) { DBGPRNT("fail"); succ = false; }
readS16(BMP085_REGISTER_CAL_AC3, &_bmp085_coeffs.ac3); if (!readS16(BMP085_REGISTER_CAL_AC3, &_bmp085_coeffs.ac3)) { DBGPRNT("fail"); succ = false; }
read16(BMP085_REGISTER_CAL_AC4, &_bmp085_coeffs.ac4); if (!read16(BMP085_REGISTER_CAL_AC4, &_bmp085_coeffs.ac4)) { DBGPRNT("fail"); succ = false; }
read16(BMP085_REGISTER_CAL_AC5, &_bmp085_coeffs.ac5); if (!read16(BMP085_REGISTER_CAL_AC5, &_bmp085_coeffs.ac5)) { DBGPRNT("fail"); succ = false; }
read16(BMP085_REGISTER_CAL_AC6, &_bmp085_coeffs.ac6); if (!read16(BMP085_REGISTER_CAL_AC6, &_bmp085_coeffs.ac6)) { DBGPRNT("fail"); succ = false; }
readS16(BMP085_REGISTER_CAL_B1, &_bmp085_coeffs.b1); if (!readS16(BMP085_REGISTER_CAL_B1, &_bmp085_coeffs.b1)) { DBGPRNT("fail"); succ = false; }
readS16(BMP085_REGISTER_CAL_B2, &_bmp085_coeffs.b2); if (!readS16(BMP085_REGISTER_CAL_B2, &_bmp085_coeffs.b2)) { DBGPRNT("fail"); succ = false; }
readS16(BMP085_REGISTER_CAL_MB, &_bmp085_coeffs.mb); if (!readS16(BMP085_REGISTER_CAL_MB, &_bmp085_coeffs.mb)) { DBGPRNT("fail"); succ = false; }
readS16(BMP085_REGISTER_CAL_MC, &_bmp085_coeffs.mc); if (!readS16(BMP085_REGISTER_CAL_MC, &_bmp085_coeffs.mc)) { DBGPRNT("fail"); succ = false; }
readS16(BMP085_REGISTER_CAL_MD, &_bmp085_coeffs.md); if (!readS16(BMP085_REGISTER_CAL_MD, &_bmp085_coeffs.md)) { DBGPRNT("fail"); succ = false; }
#endif #endif
return succ;
} }
/**************************************************************************/ /**************************************************************************/
@@ -167,15 +189,19 @@ static void readCoefficients(void) {
*/ */
/**************************************************************************/ /**************************************************************************/
static void readRawTemperature(int32_t *temperature) { static std::optional<int32_t> readRawTemperature() {
#if BMP085_USE_DATASHEET_VALS #if BMP085_USE_DATASHEET_VALS
*temperature = 27898; return 27898;
#else #else
uint16_t t; bool succ{true};
writeCommand(BMP085_REGISTER_CONTROL, BMP085_REGISTER_READTEMPCMD); if (!writeCommand(BMP085_REGISTER_CONTROL, BMP085_REGISTER_READTEMPCMD)) { DBGPRNT("fail"); succ = false; }
delay(5); delay(5);
read16(BMP085_REGISTER_TEMPDATA, &t); uint16_t t;
*temperature = t; if (!read16(BMP085_REGISTER_TEMPDATA, &t)) { DBGPRNT("fail"); succ = false; }
if (succ)
return t;
else
return std::nullopt;
#endif #endif
} }
@@ -184,16 +210,14 @@ static void readRawTemperature(int32_t *temperature) {
*/ */
/**************************************************************************/ /**************************************************************************/
static void readRawPressure(int32_t *pressure) { static std::optional<int32_t> readRawPressure() {
#if BMP085_USE_DATASHEET_VALS #if BMP085_USE_DATASHEET_VALS
*pressure = 23843; return 23843;
#else #else
uint8_t p8; bool succ{true};
uint16_t p16;
int32_t p32;
writeCommand(BMP085_REGISTER_CONTROL, if (!writeCommand(BMP085_REGISTER_CONTROL,
BMP085_REGISTER_READPRESSURECMD + (_bmp085Mode << 6)); BMP085_REGISTER_READPRESSURECMD + (_bmp085Mode << 6))) { DBGPRNT("fail"); succ = false; }
switch (_bmp085Mode) { switch (_bmp085Mode) {
case BMP085_MODE_ULTRALOWPOWER: case BMP085_MODE_ULTRALOWPOWER:
delay(5); delay(5);
@@ -210,13 +234,25 @@ static void readRawPressure(int32_t *pressure) {
break; break;
} }
read16(BMP085_REGISTER_PRESSUREDATA, &p16); int32_t p32{};
p32 = (uint32_t)p16 << 8;
read8(BMP085_REGISTER_PRESSUREDATA + 2, &p8);
p32 += p8;
p32 >>= (8 - _bmp085Mode);
*pressure = p32; {
uint16_t p16;
if (!read16(BMP085_REGISTER_PRESSUREDATA, &p16)) { DBGPRNT("fail"); succ = false; }
else if (succ) p32 += (uint32_t)p16 << 8;
}
{
uint8_t p8;
if (!read8(BMP085_REGISTER_PRESSUREDATA + 2, &p8)) { DBGPRNT("fail"); succ = false; }
else if (succ) p32 += p8;
}
if (succ) {
p32 >>= (8 - _bmp085Mode);
return p32;
} else
return std::nullopt;
#endif #endif
} }
@@ -258,7 +294,8 @@ Adafruit_BMP085_Unified::Adafruit_BMP085_Unified(int32_t sensorID) {
bool Adafruit_BMP085_Unified::begin(bool skipWireBegin, bmp085_mode_t mode) { bool Adafruit_BMP085_Unified::begin(bool skipWireBegin, bmp085_mode_t mode) {
// Enable I2C // Enable I2C
if (!skipWireBegin) if (!skipWireBegin)
Wire.begin(); if (!Wire.begin())
{ DBGPRNT("fail"); return false; }
/* Mode boundary check */ /* Mode boundary check */
if ((mode > BMP085_MODE_ULTRAHIGHRES) || (mode < 0)) { if ((mode > BMP085_MODE_ULTRAHIGHRES) || (mode < 0)) {
@@ -267,16 +304,14 @@ bool Adafruit_BMP085_Unified::begin(bool skipWireBegin, bmp085_mode_t mode) {
/* Make sure we have the right device */ /* Make sure we have the right device */
uint8_t id; uint8_t id;
read8(BMP085_REGISTER_CHIPID, &id); if (!read8(BMP085_REGISTER_CHIPID, &id)) { DBGPRNT("fail"); return false; }
if (id != 0x55) { if (id != 0x55) { DBGPRNT("fail"); return false; }
return false;
}
/* Set the mode indicator */ /* Set the mode indicator */
_bmp085Mode = mode; _bmp085Mode = mode;
/* Coefficients need to be read once */ /* Coefficients need to be read once */
readCoefficients(); if (!readCoefficients()) { DBGPRNT("fail"); return false; }
return true; return true;
} }
@@ -286,14 +321,33 @@ bool Adafruit_BMP085_Unified::begin(bool skipWireBegin, bmp085_mode_t mode) {
@brief Gets the compensated pressure level in kPa @brief Gets the compensated pressure level in kPa
*/ */
/**************************************************************************/ /**************************************************************************/
void Adafruit_BMP085_Unified::getPressure(float *pressure) { std::optional<float> Adafruit_BMP085_Unified::getPressure() {
int32_t ut = 0, up = 0, compp = 0; int32_t ut;
int32_t x1, x2, b5, b6, x3, b3, p;
uint32_t b4, b7;
/* Get the raw pressure and temperature values */ /* Get the raw pressure and temperature values */
readRawTemperature(&ut); if (const auto rawTemp = readRawTemperature())
readRawPressure(&up); ut = *rawTemp;
else
{ DBGPRNT("fail"); return std::nullopt; }
int32_t up;
if (const auto rawPress = readRawPressure())
up = *rawPress;
else
{ DBGPRNT("fail"); return std::nullopt; }
return getPressure(ut, up);
}
/**************************************************************************/
/*!
@brief Gets the compensated pressure level in kPa
*/
/**************************************************************************/
float Adafruit_BMP085_Unified::getPressure(int32_t ut, int32_t up) {
int32_t x1, x2, b5, b6, x3, b3, p;
uint32_t b4, b7;
/* Temperature compensation */ /* Temperature compensation */
b5 = computeB5(ut); b5 = computeB5(ut);
@@ -319,10 +373,32 @@ void Adafruit_BMP085_Unified::getPressure(float *pressure) {
x1 = (p >> 8) * (p >> 8); x1 = (p >> 8) * (p >> 8);
x1 = (x1 * 3038) >> 16; x1 = (x1 * 3038) >> 16;
x2 = (-7357 * p) >> 16; x2 = (-7357 * p) >> 16;
compp = p + ((x1 + x2 + 3791) >> 4); float compp = p + ((x1 + x2 + 3791) >> 4);
/* Assign compensated pressure value */ /* Assign compensated pressure value */
*pressure = compp; return compp / 100.f;
}
std::optional<Adafruit_BMP085_Unified::TemperatureAndPressure> Adafruit_BMP085_Unified::getTemperatureAndPressure() {
int32_t ut;
/* Get the raw pressure and temperature values */
if (const auto rawTemp = readRawTemperature())
ut = *rawTemp;
else
{ DBGPRNT("fail"); return std::nullopt; }
int32_t up;
if (const auto rawPress = readRawPressure())
up = *rawPress;
else
{ DBGPRNT("fail"); return std::nullopt; }
return TemperatureAndPressure {
.temperature = getTemperature(ut),
.pressure = getPressure(ut, up)
};
} }
/**************************************************************************/ /**************************************************************************/
@@ -330,12 +406,7 @@ void Adafruit_BMP085_Unified::getPressure(float *pressure) {
@brief Reads the temperatures in degrees Celsius @brief Reads the temperatures in degrees Celsius
*/ */
/**************************************************************************/ /**************************************************************************/
void Adafruit_BMP085_Unified::getTemperature(float *temp) { std::optional<float> Adafruit_BMP085_Unified::getTemperature() {
int32_t UT, B5; // following ds convention
float t;
readRawTemperature(&UT);
#if BMP085_USE_DATASHEET_VALS #if BMP085_USE_DATASHEET_VALS
// use datasheet numbers! // use datasheet numbers!
UT = 27898; UT = 27898;
@@ -343,13 +414,31 @@ void Adafruit_BMP085_Unified::getTemperature(float *temp) {
_bmp085_coeffs.ac5 = 32757; _bmp085_coeffs.ac5 = 32757;
_bmp085_coeffs.mc = -8711; _bmp085_coeffs.mc = -8711;
_bmp085_coeffs.md = 2868; _bmp085_coeffs.md = 2868;
#else
int32_t UT;
if (const auto rawTemp = readRawTemperature())
UT = *rawTemp;
else
{ DBGPRNT("fail"); return std::nullopt; }
#endif #endif
return getTemperature(UT);
}
/**************************************************************************/
/*!
@brief Reads the temperatures in degrees Celsius
*/
/**************************************************************************/
float Adafruit_BMP085_Unified::getTemperature(int32_t UT) {
int32_t B5; // following ds convention
float t;
B5 = computeB5(UT); B5 = computeB5(UT);
t = (B5 + 8) >> 4; t = (B5 + 8) >> 4;
t /= 10; t /= 10;
*temp = t; return t;
} }
/**************************************************************************/ /**************************************************************************/
@@ -466,18 +555,17 @@ sensor_t Adafruit_BMP085_Unified::getSensor() {
*/ */
/**************************************************************************/ /**************************************************************************/
std::optional<sensors_event_t> Adafruit_BMP085_Unified::getEvent() { std::optional<sensors_event_t> Adafruit_BMP085_Unified::getEvent() {
const auto pressure = getPressure();
if (!pressure)
return std::nullopt;
sensors_event_t event; sensors_event_t event;
event.version = sizeof(sensors_event_t); event.version = sizeof(sensors_event_t);
event.sensor_id = _sensorID; event.sensor_id = _sensorID;
event.type = SENSOR_TYPE_PRESSURE; event.type = SENSOR_TYPE_PRESSURE;
event.timestamp = espchrono::millis_clock::now(); event.timestamp = espchrono::millis_clock::now();
event.pressure = *pressure;
{
float pressure_kPa;
getPressure(&pressure_kPa);
event.pressure = pressure_kPa / 100.0F;
}
return event; return event;
} }

View File

@@ -99,13 +99,23 @@ public:
* @param temp Temperature * @param temp Temperature
* @return Returns the temperature * @return Returns the temperature
*/ */
void getTemperature(float *temp); std::optional<float> getTemperature();
float getTemperature(int32_t UT);
/*! /*!
* @brief Gets the pressure over I2C from the BMP085 * @brief Gets the pressure over I2C from the BMP085
* @param pressure Pressure * @param pressure Pressure
* @return Returns the pressure * @return Returns the pressure
*/ */
void getPressure(float *pressure); std::optional<float> getPressure();
float getPressure(int32_t ut, int32_t up);
struct TemperatureAndPressure { float temperature, pressure; };
std::optional<TemperatureAndPressure> getTemperatureAndPressure();
/*! /*!
* @brief Calculates absolute pressure * @brief Calculates absolute pressure
* @param seaLevel Pressure at sea level * @param seaLevel Pressure at sea level