From e21848a029ddac59c635c588efe3b37488a79ed5 Mon Sep 17 00:00:00 2001 From: Tony DiCola Date: Thu, 12 Jun 2014 15:40:02 -0700 Subject: [PATCH] Move B5 calculation to private function and optimize with bit shifts. --- Adafruit_BMP085_U.cpp | 23 +++++++++++++++-------- Adafruit_BMP085_U.h | 3 ++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Adafruit_BMP085_U.cpp b/Adafruit_BMP085_U.cpp index 6eefb47..1122d12 100644 --- a/Adafruit_BMP085_U.cpp +++ b/Adafruit_BMP085_U.cpp @@ -212,6 +212,18 @@ static void readRawPressure(int32_t *pressure) #endif } +/**************************************************************************/ +/*! + @brief Compute B5 coefficient used in temperature & pressure calcs. +*/ +/**************************************************************************/ +int32_t Adafruit_BMP085_Unified::computeB5(int32_t ut) { + int32_t X1 = (ut - (int32_t)_bmp085_coeffs.ac6) * ((int32_t)_bmp085_coeffs.ac5) >> 15; + int32_t X2 = ((int32_t)_bmp085_coeffs.mc << 11) / (X1+(int32_t)_bmp085_coeffs.md); + return X1 + X2; +} + + /*************************************************************************** CONSTRUCTOR ***************************************************************************/ @@ -278,9 +290,7 @@ void Adafruit_BMP085_Unified::getPressure(float *pressure) readRawPressure(&up); /* Temperature compensation */ - x1 = (ut - (int32_t)(_bmp085_coeffs.ac6))*((int32_t)(_bmp085_coeffs.ac5))/pow(2,15); - x2 = ((int32_t)(_bmp085_coeffs.mc*pow(2,11)))/(x1+(int32_t)(_bmp085_coeffs.md)); - b5 = x1 + x2; + b5 = computeB5(ut); /* Pressure compensation */ b6 = b5 - 4000; @@ -333,11 +343,8 @@ void Adafruit_BMP085_Unified::getTemperature(float *temp) _bmp085_coeffs.md = 2868; #endif - // step 1 - X1 = (UT - (int32_t)_bmp085_coeffs.ac6) * ((int32_t)_bmp085_coeffs.ac5) / pow(2,15); - X2 = ((int32_t)_bmp085_coeffs.mc * pow(2,11)) / (X1+(int32_t)_bmp085_coeffs.md); - B5 = X1 + X2; - t = (B5+8)/pow(2,4); + B5 = computeB5(UT); + t = (B5+8) >> 4; t /= 10; *temp = t; diff --git a/Adafruit_BMP085_U.h b/Adafruit_BMP085_U.h index 259830d..386fbcc 100644 --- a/Adafruit_BMP085_U.h +++ b/Adafruit_BMP085_U.h @@ -110,7 +110,8 @@ class Adafruit_BMP085_Unified : public Adafruit_Sensor void getSensor(sensor_t*); private: - int32_t _sensorID; + int32_t computeB5(int32_t ut); + int32_t _sensorID; }; #endif