Modified to run on Renesas GR Peach board

Dependencies:   EthernetInterface HTTP-Server Webpage mbed-rpc mbed-src

Fork of HTTPServer_echoback by Takuya Urakawa

Committer:
webOnBoard
Date:
Wed Oct 07 20:42:51 2015 +0000
Revision:
16:5d102be2566c
web based AHRS demo Renesas GR Peach board; Using HTTP Server/RPC Adafruit 10DOF

Who changed what in which revision?

UserRevisionLine numberNew contents of line
webOnBoard 16:5d102be2566c 1 /***************************************************************************
webOnBoard 16:5d102be2566c 2 This is a library for the BMP085 pressure sensor
webOnBoard 16:5d102be2566c 3
webOnBoard 16:5d102be2566c 4 Designed specifically to work with the Adafruit BMP085 or BMP180 Breakout
webOnBoard 16:5d102be2566c 5 ----> http://www.adafruit.com/products/391
webOnBoard 16:5d102be2566c 6 ----> http://www.adafruit.com/products/1603
webOnBoard 16:5d102be2566c 7
webOnBoard 16:5d102be2566c 8 These displays use I2C to communicate, 2 pins are required to interface.
webOnBoard 16:5d102be2566c 9
webOnBoard 16:5d102be2566c 10 Adafruit invests time and resources providing this open source code,
webOnBoard 16:5d102be2566c 11 please support Adafruit andopen-source hardware by purchasing products
webOnBoard 16:5d102be2566c 12 from Adafruit!
webOnBoard 16:5d102be2566c 13
webOnBoard 16:5d102be2566c 14 Written by Kevin Townsend for Adafruit Industries.
webOnBoard 16:5d102be2566c 15 BSD license, all text above must be included in any redistribution
webOnBoard 16:5d102be2566c 16 ***************************************************************************/
webOnBoard 16:5d102be2566c 17
webOnBoard 16:5d102be2566c 18 //#include "mbed.h"
webOnBoard 16:5d102be2566c 19 //#include "I2C.h"
webOnBoard 16:5d102be2566c 20 #include "Adafruit_BMP085_U.h"
webOnBoard 16:5d102be2566c 21
webOnBoard 16:5d102be2566c 22 static bmp085_calib_data _bmp085_coeffs; // Last read accelerometer data will be available here
webOnBoard 16:5d102be2566c 23 static uint8_t _bmp085Mode;
webOnBoard 16:5d102be2566c 24
webOnBoard 16:5d102be2566c 25 #define BMP085_USE_DATASHEET_VALS (0) /* Set to 1 for sanity check */
webOnBoard 16:5d102be2566c 26 typedef uint8_t byte;
webOnBoard 16:5d102be2566c 27
webOnBoard 16:5d102be2566c 28 //extern i2c;
webOnBoard 16:5d102be2566c 29 //extern I2C* i2c;
webOnBoard 16:5d102be2566c 30 //extern mbed::I2C* i2c;
webOnBoard 16:5d102be2566c 31 //extern mbed::I2C* i2c(I2C_SDA, I2C_SCL);
webOnBoard 16:5d102be2566c 32 //extern I2C i2c();
webOnBoard 16:5d102be2566c 33 //extern i2c(I2C_SDA, I2C_SCL);
webOnBoard 16:5d102be2566c 34 //extern int i2c(I2C_SDA, I2C_SCL);
webOnBoard 16:5d102be2566c 35 //extern I2C i2c(I2C_SDA, I2C_SCL); // sda, scl
webOnBoard 16:5d102be2566c 36
webOnBoard 16:5d102be2566c 37 /***************************************************************************
webOnBoard 16:5d102be2566c 38 PRIVATE FUNCTIONS
webOnBoard 16:5d102be2566c 39 ***************************************************************************/
webOnBoard 16:5d102be2566c 40
webOnBoard 16:5d102be2566c 41 /**************************************************************************/
webOnBoard 16:5d102be2566c 42 /*!
webOnBoard 16:5d102be2566c 43 @brief Writes an 8 bit value over I2C
webOnBoard 16:5d102be2566c 44 */
webOnBoard 16:5d102be2566c 45 /**************************************************************************/
webOnBoard 16:5d102be2566c 46 static void writeCommand(byte reg, byte value)
webOnBoard 16:5d102be2566c 47 {
webOnBoard 16:5d102be2566c 48 char data_write[2];
webOnBoard 16:5d102be2566c 49
webOnBoard 16:5d102be2566c 50 data_write[0] = reg;
webOnBoard 16:5d102be2566c 51 data_write[1] = value;
webOnBoard 16:5d102be2566c 52 int status = i2c->write(BMP085_ADDRESS, data_write, 2, 0);
webOnBoard 16:5d102be2566c 53
webOnBoard 16:5d102be2566c 54 }
webOnBoard 16:5d102be2566c 55
webOnBoard 16:5d102be2566c 56 /**************************************************************************/
webOnBoard 16:5d102be2566c 57 /*!
webOnBoard 16:5d102be2566c 58 @brief Reads an 8 bit value over I2C
webOnBoard 16:5d102be2566c 59 */
webOnBoard 16:5d102be2566c 60 /**************************************************************************/
webOnBoard 16:5d102be2566c 61 static void read8(byte reg, uint8_t *value)
webOnBoard 16:5d102be2566c 62 {
webOnBoard 16:5d102be2566c 63 char data_write[2];
webOnBoard 16:5d102be2566c 64 char data_read[2];
webOnBoard 16:5d102be2566c 65
webOnBoard 16:5d102be2566c 66 // Read register
webOnBoard 16:5d102be2566c 67 data_write[0] = reg;
webOnBoard 16:5d102be2566c 68 i2c->write(BMP085_ADDRESS, data_write, 1, 1); // no stop
webOnBoard 16:5d102be2566c 69 i2c->read(BMP085_ADDRESS, data_read, 2, 0);
webOnBoard 16:5d102be2566c 70
webOnBoard 16:5d102be2566c 71 }
webOnBoard 16:5d102be2566c 72
webOnBoard 16:5d102be2566c 73 /**************************************************************************/
webOnBoard 16:5d102be2566c 74 /*!
webOnBoard 16:5d102be2566c 75 @brief Reads a 16 bit value over I2C
webOnBoard 16:5d102be2566c 76 */
webOnBoard 16:5d102be2566c 77 /**************************************************************************/
webOnBoard 16:5d102be2566c 78 static void read16(byte reg, uint16_t *value)
webOnBoard 16:5d102be2566c 79 {
webOnBoard 16:5d102be2566c 80 char data_write[2];
webOnBoard 16:5d102be2566c 81 char data_read[2];
webOnBoard 16:5d102be2566c 82 //uint8_t data_read[2];
webOnBoard 16:5d102be2566c 83
webOnBoard 16:5d102be2566c 84 //Sample_RIIC_Read( (uint8_t)BMP085_ADDRESS,reg,1, data_read);
webOnBoard 16:5d102be2566c 85 data_write[0] = reg;
webOnBoard 16:5d102be2566c 86 i2c->write(BMP085_ADDRESS, data_write, 1, 1); // no stop
webOnBoard 16:5d102be2566c 87 i2c->read(BMP085_ADDRESS, data_read, 2, 0);
webOnBoard 16:5d102be2566c 88
webOnBoard 16:5d102be2566c 89 *value = (((uint16_t)data_read[0]) << 8) | ((uint16_t)data_read[1]);
webOnBoard 16:5d102be2566c 90
webOnBoard 16:5d102be2566c 91 }
webOnBoard 16:5d102be2566c 92
webOnBoard 16:5d102be2566c 93 /**************************************************************************/
webOnBoard 16:5d102be2566c 94 /*!
webOnBoard 16:5d102be2566c 95 @brief Reads a signed 16 bit value over I2C
webOnBoard 16:5d102be2566c 96 */
webOnBoard 16:5d102be2566c 97 /**************************************************************************/
webOnBoard 16:5d102be2566c 98 static void readS16(byte reg, int16_t *value)
webOnBoard 16:5d102be2566c 99 {
webOnBoard 16:5d102be2566c 100 uint16_t i;
webOnBoard 16:5d102be2566c 101 read16(reg, &i);
webOnBoard 16:5d102be2566c 102 *value = (int16_t)i;
webOnBoard 16:5d102be2566c 103 }
webOnBoard 16:5d102be2566c 104
webOnBoard 16:5d102be2566c 105 /**************************************************************************/
webOnBoard 16:5d102be2566c 106 /*!
webOnBoard 16:5d102be2566c 107 @brief Reads the factory-set coefficients
webOnBoard 16:5d102be2566c 108 */
webOnBoard 16:5d102be2566c 109 /**************************************************************************/
webOnBoard 16:5d102be2566c 110 static void readCoefficients(void)
webOnBoard 16:5d102be2566c 111 {
webOnBoard 16:5d102be2566c 112 #if BMP085_USE_DATASHEET_VALS
webOnBoard 16:5d102be2566c 113 _bmp085_coeffs.ac1 = 408;
webOnBoard 16:5d102be2566c 114 _bmp085_coeffs.ac2 = -72;
webOnBoard 16:5d102be2566c 115 _bmp085_coeffs.ac3 = -14383;
webOnBoard 16:5d102be2566c 116 _bmp085_coeffs.ac4 = 32741;
webOnBoard 16:5d102be2566c 117 _bmp085_coeffs.ac5 = 32757;
webOnBoard 16:5d102be2566c 118 _bmp085_coeffs.ac6 = 23153;
webOnBoard 16:5d102be2566c 119 _bmp085_coeffs.b1 = 6190;
webOnBoard 16:5d102be2566c 120 _bmp085_coeffs.b2 = 4;
webOnBoard 16:5d102be2566c 121 _bmp085_coeffs.mb = -32768;
webOnBoard 16:5d102be2566c 122 _bmp085_coeffs.mc = -8711;
webOnBoard 16:5d102be2566c 123 _bmp085_coeffs.md = 2868;
webOnBoard 16:5d102be2566c 124 _bmp085Mode = 0;
webOnBoard 16:5d102be2566c 125 #else
webOnBoard 16:5d102be2566c 126 readS16(BMP085_REGISTER_CAL_AC1, &_bmp085_coeffs.ac1);
webOnBoard 16:5d102be2566c 127 readS16(BMP085_REGISTER_CAL_AC2, &_bmp085_coeffs.ac2);
webOnBoard 16:5d102be2566c 128 readS16(BMP085_REGISTER_CAL_AC3, &_bmp085_coeffs.ac3);
webOnBoard 16:5d102be2566c 129 read16(BMP085_REGISTER_CAL_AC4, &_bmp085_coeffs.ac4);
webOnBoard 16:5d102be2566c 130 read16(BMP085_REGISTER_CAL_AC5, &_bmp085_coeffs.ac5);
webOnBoard 16:5d102be2566c 131 read16(BMP085_REGISTER_CAL_AC6, &_bmp085_coeffs.ac6);
webOnBoard 16:5d102be2566c 132 readS16(BMP085_REGISTER_CAL_B1, &_bmp085_coeffs.b1);
webOnBoard 16:5d102be2566c 133 readS16(BMP085_REGISTER_CAL_B2, &_bmp085_coeffs.b2);
webOnBoard 16:5d102be2566c 134 readS16(BMP085_REGISTER_CAL_MB, &_bmp085_coeffs.mb);
webOnBoard 16:5d102be2566c 135 readS16(BMP085_REGISTER_CAL_MC, &_bmp085_coeffs.mc);
webOnBoard 16:5d102be2566c 136 readS16(BMP085_REGISTER_CAL_MD, &_bmp085_coeffs.md);
webOnBoard 16:5d102be2566c 137 #endif
webOnBoard 16:5d102be2566c 138 }
webOnBoard 16:5d102be2566c 139
webOnBoard 16:5d102be2566c 140 /**************************************************************************/
webOnBoard 16:5d102be2566c 141 /*!
webOnBoard 16:5d102be2566c 142
webOnBoard 16:5d102be2566c 143 */
webOnBoard 16:5d102be2566c 144 /**************************************************************************/
webOnBoard 16:5d102be2566c 145 static void readRawTemperature(int32_t *temperature)
webOnBoard 16:5d102be2566c 146 {
webOnBoard 16:5d102be2566c 147 #if BMP085_USE_DATASHEET_VALS
webOnBoard 16:5d102be2566c 148 *temperature = 27898;
webOnBoard 16:5d102be2566c 149 #else
webOnBoard 16:5d102be2566c 150 uint16_t t;
webOnBoard 16:5d102be2566c 151 writeCommand(BMP085_REGISTER_CONTROL, BMP085_REGISTER_READTEMPCMD);
webOnBoard 16:5d102be2566c 152 wait(5);//delay(5);
webOnBoard 16:5d102be2566c 153 read16(BMP085_REGISTER_TEMPDATA, &t);
webOnBoard 16:5d102be2566c 154 *temperature = t;
webOnBoard 16:5d102be2566c 155 #endif
webOnBoard 16:5d102be2566c 156 }
webOnBoard 16:5d102be2566c 157
webOnBoard 16:5d102be2566c 158 /**************************************************************************/
webOnBoard 16:5d102be2566c 159 /*!
webOnBoard 16:5d102be2566c 160
webOnBoard 16:5d102be2566c 161 */
webOnBoard 16:5d102be2566c 162 /**************************************************************************/
webOnBoard 16:5d102be2566c 163 static void readRawPressure(int32_t *pressure)
webOnBoard 16:5d102be2566c 164 {
webOnBoard 16:5d102be2566c 165 #if BMP085_USE_DATASHEET_VALS
webOnBoard 16:5d102be2566c 166 *pressure = 23843;
webOnBoard 16:5d102be2566c 167 #else
webOnBoard 16:5d102be2566c 168 uint8_t p8;
webOnBoard 16:5d102be2566c 169 uint16_t p16;
webOnBoard 16:5d102be2566c 170 int32_t p32;
webOnBoard 16:5d102be2566c 171
webOnBoard 16:5d102be2566c 172 writeCommand(BMP085_REGISTER_CONTROL, BMP085_REGISTER_READPRESSURECMD + (_bmp085Mode << 6));
webOnBoard 16:5d102be2566c 173 switch(_bmp085Mode)
webOnBoard 16:5d102be2566c 174 {
webOnBoard 16:5d102be2566c 175 case BMP085_MODE_ULTRALOWPOWER:
webOnBoard 16:5d102be2566c 176 wait(5);//delay(5);
webOnBoard 16:5d102be2566c 177 break;
webOnBoard 16:5d102be2566c 178 case BMP085_MODE_STANDARD:
webOnBoard 16:5d102be2566c 179 wait(8);//delay(8);
webOnBoard 16:5d102be2566c 180 break;
webOnBoard 16:5d102be2566c 181 case BMP085_MODE_HIGHRES:
webOnBoard 16:5d102be2566c 182 wait(14);//delay(14);
webOnBoard 16:5d102be2566c 183 break;
webOnBoard 16:5d102be2566c 184 case BMP085_MODE_ULTRAHIGHRES:
webOnBoard 16:5d102be2566c 185 default:
webOnBoard 16:5d102be2566c 186 wait(26);//delay(26);
webOnBoard 16:5d102be2566c 187 break;
webOnBoard 16:5d102be2566c 188 }
webOnBoard 16:5d102be2566c 189
webOnBoard 16:5d102be2566c 190 read16(BMP085_REGISTER_PRESSUREDATA, &p16);
webOnBoard 16:5d102be2566c 191 p32 = (uint32_t)p16 << 8;
webOnBoard 16:5d102be2566c 192 read8(BMP085_REGISTER_PRESSUREDATA+2, &p8);
webOnBoard 16:5d102be2566c 193 p32 += p8;
webOnBoard 16:5d102be2566c 194 p32 >>= (8 - _bmp085Mode);
webOnBoard 16:5d102be2566c 195
webOnBoard 16:5d102be2566c 196 *pressure = p32;
webOnBoard 16:5d102be2566c 197 #endif
webOnBoard 16:5d102be2566c 198 }
webOnBoard 16:5d102be2566c 199
webOnBoard 16:5d102be2566c 200 /**************************************************************************/
webOnBoard 16:5d102be2566c 201 /*!
webOnBoard 16:5d102be2566c 202 @brief Compute B5 coefficient used in temperature & pressure calcs.
webOnBoard 16:5d102be2566c 203 */
webOnBoard 16:5d102be2566c 204 /**************************************************************************/
webOnBoard 16:5d102be2566c 205 int32_t Adafruit_BMP085_Unified::computeB5(int32_t ut) {
webOnBoard 16:5d102be2566c 206 int32_t X1 = (ut - (int32_t)_bmp085_coeffs.ac6) * ((int32_t)_bmp085_coeffs.ac5) >> 15;
webOnBoard 16:5d102be2566c 207 int32_t X2 = ((int32_t)_bmp085_coeffs.mc << 11) / (X1+(int32_t)_bmp085_coeffs.md);
webOnBoard 16:5d102be2566c 208 return X1 + X2;
webOnBoard 16:5d102be2566c 209 }
webOnBoard 16:5d102be2566c 210
webOnBoard 16:5d102be2566c 211
webOnBoard 16:5d102be2566c 212 /***************************************************************************
webOnBoard 16:5d102be2566c 213 CONSTRUCTOR
webOnBoard 16:5d102be2566c 214 ***************************************************************************/
webOnBoard 16:5d102be2566c 215
webOnBoard 16:5d102be2566c 216 /**************************************************************************/
webOnBoard 16:5d102be2566c 217 /*!
webOnBoard 16:5d102be2566c 218 @brief Instantiates a new Adafruit_BMP085_Unified class
webOnBoard 16:5d102be2566c 219 */
webOnBoard 16:5d102be2566c 220 /**************************************************************************/
webOnBoard 16:5d102be2566c 221 Adafruit_BMP085_Unified::Adafruit_BMP085_Unified(int32_t sensorID) {
webOnBoard 16:5d102be2566c 222 _sensorID = sensorID;
webOnBoard 16:5d102be2566c 223 }
webOnBoard 16:5d102be2566c 224
webOnBoard 16:5d102be2566c 225 /***************************************************************************
webOnBoard 16:5d102be2566c 226 PUBLIC FUNCTIONS
webOnBoard 16:5d102be2566c 227 ***************************************************************************/
webOnBoard 16:5d102be2566c 228
webOnBoard 16:5d102be2566c 229 /**************************************************************************/
webOnBoard 16:5d102be2566c 230 /*!
webOnBoard 16:5d102be2566c 231 @brief Setups the HW
webOnBoard 16:5d102be2566c 232 */
webOnBoard 16:5d102be2566c 233 /**************************************************************************/
webOnBoard 16:5d102be2566c 234 bool Adafruit_BMP085_Unified::begin(bmp085_mode_t mode)
webOnBoard 16:5d102be2566c 235 {
webOnBoard 16:5d102be2566c 236 // Enable I2C
webOnBoard 16:5d102be2566c 237 //Wire.begin();
webOnBoard 16:5d102be2566c 238
webOnBoard 16:5d102be2566c 239 /* Mode boundary check */
webOnBoard 16:5d102be2566c 240 if ((mode > BMP085_MODE_ULTRAHIGHRES) || (mode < 0))
webOnBoard 16:5d102be2566c 241 {
webOnBoard 16:5d102be2566c 242 mode = BMP085_MODE_ULTRAHIGHRES;
webOnBoard 16:5d102be2566c 243 }
webOnBoard 16:5d102be2566c 244
webOnBoard 16:5d102be2566c 245 /* Make sure we have the right device */
webOnBoard 16:5d102be2566c 246 uint8_t id;
webOnBoard 16:5d102be2566c 247 read8(BMP085_REGISTER_CHIPID, &id);
webOnBoard 16:5d102be2566c 248 if(id != 0x55)
webOnBoard 16:5d102be2566c 249 {
webOnBoard 16:5d102be2566c 250 return false;
webOnBoard 16:5d102be2566c 251 }
webOnBoard 16:5d102be2566c 252
webOnBoard 16:5d102be2566c 253 /* Set the mode indicator */
webOnBoard 16:5d102be2566c 254 _bmp085Mode = mode;
webOnBoard 16:5d102be2566c 255
webOnBoard 16:5d102be2566c 256 /* Coefficients need to be read once */
webOnBoard 16:5d102be2566c 257 readCoefficients();
webOnBoard 16:5d102be2566c 258
webOnBoard 16:5d102be2566c 259 return true;
webOnBoard 16:5d102be2566c 260 }
webOnBoard 16:5d102be2566c 261
webOnBoard 16:5d102be2566c 262 /**************************************************************************/
webOnBoard 16:5d102be2566c 263 /*!
webOnBoard 16:5d102be2566c 264 @brief Gets the compensated pressure level in kPa
webOnBoard 16:5d102be2566c 265 */
webOnBoard 16:5d102be2566c 266 /**************************************************************************/
webOnBoard 16:5d102be2566c 267 void Adafruit_BMP085_Unified::getPressure(float *pressure)
webOnBoard 16:5d102be2566c 268 {
webOnBoard 16:5d102be2566c 269 int32_t ut = 0, up = 0, compp = 0;
webOnBoard 16:5d102be2566c 270 int32_t x1, x2, b5, b6, x3, b3, p;
webOnBoard 16:5d102be2566c 271 uint32_t b4, b7;
webOnBoard 16:5d102be2566c 272
webOnBoard 16:5d102be2566c 273 /* Get the raw pressure and temperature values */
webOnBoard 16:5d102be2566c 274 readRawTemperature(&ut);
webOnBoard 16:5d102be2566c 275 readRawPressure(&up);
webOnBoard 16:5d102be2566c 276
webOnBoard 16:5d102be2566c 277 /* Temperature compensation */
webOnBoard 16:5d102be2566c 278 b5 = computeB5(ut);
webOnBoard 16:5d102be2566c 279
webOnBoard 16:5d102be2566c 280 /* Pressure compensation */
webOnBoard 16:5d102be2566c 281 b6 = b5 - 4000;
webOnBoard 16:5d102be2566c 282 x1 = (_bmp085_coeffs.b2 * ((b6 * b6) >> 12)) >> 11;
webOnBoard 16:5d102be2566c 283 x2 = (_bmp085_coeffs.ac2 * b6) >> 11;
webOnBoard 16:5d102be2566c 284 x3 = x1 + x2;
webOnBoard 16:5d102be2566c 285 b3 = (((((int32_t) _bmp085_coeffs.ac1) * 4 + x3) << _bmp085Mode) + 2) >> 2;
webOnBoard 16:5d102be2566c 286 x1 = (_bmp085_coeffs.ac3 * b6) >> 13;
webOnBoard 16:5d102be2566c 287 x2 = (_bmp085_coeffs.b1 * ((b6 * b6) >> 12)) >> 16;
webOnBoard 16:5d102be2566c 288 x3 = ((x1 + x2) + 2) >> 2;
webOnBoard 16:5d102be2566c 289 b4 = (_bmp085_coeffs.ac4 * (uint32_t) (x3 + 32768)) >> 15;
webOnBoard 16:5d102be2566c 290 b7 = ((uint32_t) (up - b3) * (50000 >> _bmp085Mode));
webOnBoard 16:5d102be2566c 291
webOnBoard 16:5d102be2566c 292 if (b7 < 0x80000000)
webOnBoard 16:5d102be2566c 293 {
webOnBoard 16:5d102be2566c 294 p = (b7 << 1) / b4;
webOnBoard 16:5d102be2566c 295 }
webOnBoard 16:5d102be2566c 296 else
webOnBoard 16:5d102be2566c 297 {
webOnBoard 16:5d102be2566c 298 p = (b7 / b4) << 1;
webOnBoard 16:5d102be2566c 299 }
webOnBoard 16:5d102be2566c 300
webOnBoard 16:5d102be2566c 301 x1 = (p >> 8) * (p >> 8);
webOnBoard 16:5d102be2566c 302 x1 = (x1 * 3038) >> 16;
webOnBoard 16:5d102be2566c 303 x2 = (-7357 * p) >> 16;
webOnBoard 16:5d102be2566c 304 compp = p + ((x1 + x2 + 3791) >> 4);
webOnBoard 16:5d102be2566c 305
webOnBoard 16:5d102be2566c 306 /* Assign compensated pressure value */
webOnBoard 16:5d102be2566c 307 *pressure = compp;
webOnBoard 16:5d102be2566c 308 }
webOnBoard 16:5d102be2566c 309
webOnBoard 16:5d102be2566c 310 /**************************************************************************/
webOnBoard 16:5d102be2566c 311 /*!
webOnBoard 16:5d102be2566c 312 @brief Reads the temperatures in degrees Celsius
webOnBoard 16:5d102be2566c 313 */
webOnBoard 16:5d102be2566c 314 /**************************************************************************/
webOnBoard 16:5d102be2566c 315 void Adafruit_BMP085_Unified::getTemperature(float *temp)
webOnBoard 16:5d102be2566c 316 {
webOnBoard 16:5d102be2566c 317 int32_t UT, X1, X2, B5; // following ds convention
webOnBoard 16:5d102be2566c 318 float t;
webOnBoard 16:5d102be2566c 319
webOnBoard 16:5d102be2566c 320 readRawTemperature(&UT);
webOnBoard 16:5d102be2566c 321
webOnBoard 16:5d102be2566c 322 #if BMP085_USE_DATASHEET_VALS
webOnBoard 16:5d102be2566c 323 // use datasheet numbers!
webOnBoard 16:5d102be2566c 324 UT = 27898;
webOnBoard 16:5d102be2566c 325 _bmp085_coeffs.ac6 = 23153;
webOnBoard 16:5d102be2566c 326 _bmp085_coeffs.ac5 = 32757;
webOnBoard 16:5d102be2566c 327 _bmp085_coeffs.mc = -8711;
webOnBoard 16:5d102be2566c 328 _bmp085_coeffs.md = 2868;
webOnBoard 16:5d102be2566c 329 #endif
webOnBoard 16:5d102be2566c 330
webOnBoard 16:5d102be2566c 331 B5 = computeB5(UT);
webOnBoard 16:5d102be2566c 332 t = (B5+8) >> 4;
webOnBoard 16:5d102be2566c 333 t /= 10;
webOnBoard 16:5d102be2566c 334
webOnBoard 16:5d102be2566c 335 *temp = t;
webOnBoard 16:5d102be2566c 336 }
webOnBoard 16:5d102be2566c 337
webOnBoard 16:5d102be2566c 338 /**************************************************************************/
webOnBoard 16:5d102be2566c 339 /*!
webOnBoard 16:5d102be2566c 340 Calculates the altitude (in meters) from the specified atmospheric
webOnBoard 16:5d102be2566c 341 pressure (in hPa), and sea-level pressure (in hPa).
webOnBoard 16:5d102be2566c 342
webOnBoard 16:5d102be2566c 343 @param seaLevel Sea-level pressure in hPa
webOnBoard 16:5d102be2566c 344 @param atmospheric Atmospheric pressure in hPa
webOnBoard 16:5d102be2566c 345 */
webOnBoard 16:5d102be2566c 346 /**************************************************************************/
webOnBoard 16:5d102be2566c 347 float Adafruit_BMP085_Unified::pressureToAltitude(float seaLevel, float atmospheric)
webOnBoard 16:5d102be2566c 348 {
webOnBoard 16:5d102be2566c 349 // Equation taken from BMP180 datasheet (page 16):
webOnBoard 16:5d102be2566c 350 // http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
webOnBoard 16:5d102be2566c 351
webOnBoard 16:5d102be2566c 352 // Note that using the equation from wikipedia can give bad results
webOnBoard 16:5d102be2566c 353 // at high altitude. See this thread for more information:
webOnBoard 16:5d102be2566c 354 // http://forums.adafruit.com/viewtopic.php?f=22&t=58064
webOnBoard 16:5d102be2566c 355 float y = 0.1903;
webOnBoard 16:5d102be2566c 356 float x;
webOnBoard 16:5d102be2566c 357 x = atmospheric / seaLevel;
webOnBoard 16:5d102be2566c 358
webOnBoard 16:5d102be2566c 359 return 44330.0 * (1.0 - pow(x, y)); // 0.1903));
webOnBoard 16:5d102be2566c 360 }
webOnBoard 16:5d102be2566c 361
webOnBoard 16:5d102be2566c 362 /**************************************************************************/
webOnBoard 16:5d102be2566c 363 /*!
webOnBoard 16:5d102be2566c 364 Calculates the altitude (in meters) from the specified atmospheric
webOnBoard 16:5d102be2566c 365 pressure (in hPa), and sea-level pressure (in hPa). Note that this
webOnBoard 16:5d102be2566c 366 function just calls the overload of pressureToAltitude which takes
webOnBoard 16:5d102be2566c 367 seaLevel and atmospheric pressure--temperature is ignored. The original
webOnBoard 16:5d102be2566c 368 implementation of this function was based on calculations from Wikipedia
webOnBoard 16:5d102be2566c 369 which are not accurate at higher altitudes. To keep compatibility with
webOnBoard 16:5d102be2566c 370 old code this function remains with the same interface, but it calls the
webOnBoard 16:5d102be2566c 371 more accurate calculation.
webOnBoard 16:5d102be2566c 372
webOnBoard 16:5d102be2566c 373 @param seaLevel Sea-level pressure in hPa
webOnBoard 16:5d102be2566c 374 @param atmospheric Atmospheric pressure in hPa
webOnBoard 16:5d102be2566c 375 @param temp Temperature in degrees Celsius
webOnBoard 16:5d102be2566c 376 */
webOnBoard 16:5d102be2566c 377 /**************************************************************************/
webOnBoard 16:5d102be2566c 378 float Adafruit_BMP085_Unified::pressureToAltitude(float seaLevel, float atmospheric, float temp)
webOnBoard 16:5d102be2566c 379 {
webOnBoard 16:5d102be2566c 380 return pressureToAltitude(seaLevel, atmospheric);
webOnBoard 16:5d102be2566c 381 }
webOnBoard 16:5d102be2566c 382
webOnBoard 16:5d102be2566c 383 /**************************************************************************/
webOnBoard 16:5d102be2566c 384 /*!
webOnBoard 16:5d102be2566c 385 Calculates the pressure at sea level (in hPa) from the specified altitude
webOnBoard 16:5d102be2566c 386 (in meters), and atmospheric pressure (in hPa).
webOnBoard 16:5d102be2566c 387
webOnBoard 16:5d102be2566c 388 @param altitude Altitude in meters
webOnBoard 16:5d102be2566c 389 @param atmospheric Atmospheric pressure in hPa
webOnBoard 16:5d102be2566c 390 */
webOnBoard 16:5d102be2566c 391 /**************************************************************************/
webOnBoard 16:5d102be2566c 392 float Adafruit_BMP085_Unified::seaLevelForAltitude(float altitude, float atmospheric)
webOnBoard 16:5d102be2566c 393 {
webOnBoard 16:5d102be2566c 394 // Equation taken from BMP180 datasheet (page 17):
webOnBoard 16:5d102be2566c 395 // http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
webOnBoard 16:5d102be2566c 396
webOnBoard 16:5d102be2566c 397 // Note that using the equation from wikipedia can give bad results
webOnBoard 16:5d102be2566c 398 // at high altitude. See this thread for more information:
webOnBoard 16:5d102be2566c 399 // http://forums.adafruit.com/viewtopic.php?f=22&t=58064
webOnBoard 16:5d102be2566c 400
webOnBoard 16:5d102be2566c 401 return atmospheric / pow(1.0 - (altitude/44330.0), 5.255);
webOnBoard 16:5d102be2566c 402 }
webOnBoard 16:5d102be2566c 403
webOnBoard 16:5d102be2566c 404 /**************************************************************************/
webOnBoard 16:5d102be2566c 405 /*!
webOnBoard 16:5d102be2566c 406 Calculates the pressure at sea level (in hPa) from the specified altitude
webOnBoard 16:5d102be2566c 407 (in meters), and atmospheric pressure (in hPa). Note that this
webOnBoard 16:5d102be2566c 408 function just calls the overload of seaLevelForAltitude which takes
webOnBoard 16:5d102be2566c 409 altitude and atmospheric pressure--temperature is ignored. The original
webOnBoard 16:5d102be2566c 410 implementation of this function was based on calculations from Wikipedia
webOnBoard 16:5d102be2566c 411 which are not accurate at higher altitudes. To keep compatibility with
webOnBoard 16:5d102be2566c 412 old code this function remains with the same interface, but it calls the
webOnBoard 16:5d102be2566c 413 more accurate calculation.
webOnBoard 16:5d102be2566c 414
webOnBoard 16:5d102be2566c 415 @param altitude Altitude in meters
webOnBoard 16:5d102be2566c 416 @param atmospheric Atmospheric pressure in hPa
webOnBoard 16:5d102be2566c 417 @param temp Temperature in degrees Celsius
webOnBoard 16:5d102be2566c 418 */
webOnBoard 16:5d102be2566c 419 /**************************************************************************/
webOnBoard 16:5d102be2566c 420 float Adafruit_BMP085_Unified::seaLevelForAltitude(float altitude, float atmospheric, float temp)
webOnBoard 16:5d102be2566c 421 {
webOnBoard 16:5d102be2566c 422 return seaLevelForAltitude(altitude, atmospheric);
webOnBoard 16:5d102be2566c 423 }
webOnBoard 16:5d102be2566c 424
webOnBoard 16:5d102be2566c 425 /**************************************************************************/
webOnBoard 16:5d102be2566c 426 /*!
webOnBoard 16:5d102be2566c 427 @brief Provides the sensor_t data for this sensor
webOnBoard 16:5d102be2566c 428 */
webOnBoard 16:5d102be2566c 429 /**************************************************************************/
webOnBoard 16:5d102be2566c 430 void Adafruit_BMP085_Unified::getSensor(sensor_t *sensor)
webOnBoard 16:5d102be2566c 431 {
webOnBoard 16:5d102be2566c 432 /* Clear the sensor_t object */
webOnBoard 16:5d102be2566c 433 memset(sensor, 0, sizeof(sensor_t));
webOnBoard 16:5d102be2566c 434
webOnBoard 16:5d102be2566c 435 /* Insert the sensor name in the fixed length char array */
webOnBoard 16:5d102be2566c 436 strncpy (sensor->name, "BMP085", sizeof(sensor->name) - 1);
webOnBoard 16:5d102be2566c 437 sensor->name[sizeof(sensor->name)- 1] = 0;
webOnBoard 16:5d102be2566c 438 sensor->version = 1;
webOnBoard 16:5d102be2566c 439 sensor->sensor_id = _sensorID;
webOnBoard 16:5d102be2566c 440 sensor->type = SENSOR_TYPE_PRESSURE;
webOnBoard 16:5d102be2566c 441 sensor->min_delay = 0;
webOnBoard 16:5d102be2566c 442 sensor->max_value = 1100.0F; // 300..1100 hPa
webOnBoard 16:5d102be2566c 443 sensor->min_value = 300.0F;
webOnBoard 16:5d102be2566c 444 sensor->resolution = 0.01F; // Datasheet states 0.01 hPa resolution
webOnBoard 16:5d102be2566c 445 }
webOnBoard 16:5d102be2566c 446
webOnBoard 16:5d102be2566c 447 /**************************************************************************/
webOnBoard 16:5d102be2566c 448 /*!
webOnBoard 16:5d102be2566c 449 @brief Reads the sensor and returns the data as a sensors_event_t
webOnBoard 16:5d102be2566c 450 */
webOnBoard 16:5d102be2566c 451 /**************************************************************************/
webOnBoard 16:5d102be2566c 452 bool Adafruit_BMP085_Unified::getEvent(sensors_event_t *event)
webOnBoard 16:5d102be2566c 453 {
webOnBoard 16:5d102be2566c 454 float pressure_kPa;
webOnBoard 16:5d102be2566c 455
webOnBoard 16:5d102be2566c 456 /* Clear the event */
webOnBoard 16:5d102be2566c 457 memset(event, 0, sizeof(sensors_event_t));
webOnBoard 16:5d102be2566c 458
webOnBoard 16:5d102be2566c 459 event->version = sizeof(sensors_event_t);
webOnBoard 16:5d102be2566c 460 event->sensor_id = _sensorID;
webOnBoard 16:5d102be2566c 461 event->type = SENSOR_TYPE_PRESSURE;
webOnBoard 16:5d102be2566c 462 event->timestamp = 0;
webOnBoard 16:5d102be2566c 463 getPressure(&pressure_kPa);
webOnBoard 16:5d102be2566c 464 event->pressure = pressure_kPa / 100.0F;
webOnBoard 16:5d102be2566c 465
webOnBoard 16:5d102be2566c 466 return true;
webOnBoard 16:5d102be2566c 467 }