Change to comply with mbed os 5.1, as the attach function as used has been deprecated
Dependents: Barometer-Example LoRaWAN-demo-72-bootcamp
Fork of BMP085 by
BMP085 library supporting mbed os low power
Diff: BMP085.cpp
- Revision:
- 3:4622b90b4657
- Parent:
- 2:dcd90bee9ac2
- Child:
- 4:ed587a5331e4
--- a/BMP085.cpp Tue Jan 24 10:33:24 2017 +0000 +++ b/BMP085.cpp Mon Jan 30 21:35:24 2017 +0000 @@ -1,8 +1,11 @@ #include "BMP085.h" -void BMP085::init(void) { +extern Serial pc; + +void BMP085::init(void) +{ _callback.attach(this, &BMP085::_callback_handler); - + _cal_data.ac1 = read_cal_register(BMP085_CAL_AC1); _cal_data.ac2 = read_cal_register(BMP085_CAL_AC2); _cal_data.ac3 = read_cal_register(BMP085_CAL_AC3); @@ -16,67 +19,81 @@ _cal_data.md = read_cal_register(BMP085_CAL_MD); } -uint16_t BMP085::read_cal_register(BMP085_Register reg) { - _tx_buf[0] = reg; + +int BMP085::transfer(char *tx, int tx_len, char *rx, int rx_len) +{ + LowPowerTimeout delay; + + pc.printf("transfer "); _state = BMP085_BUSY; _i2c.transfer(BMP085_ADDR, (char*)_tx_buf, 1, (char*)_rx_buf, 2, _callback); - while(_state != BMP085_IDLE) { + delay.attach(callback(this, &BMP085::_callback_handler_timeout), 2.0); + while(_state == BMP085_BUSY) { sleep(); } + if (_state == BMP085_TIMEOUT) { + _i2c.abort_transfer(); + return -33; + } + pc.printf("completed "); + return 0; +} + +int BMP085::wait(float wait) +{ + LowPowerTimeout delay; + + pc.printf("bmpWait "); + _state = BMP085_BUSY; + delay.attach(callback(this, &BMP085::_callback_handler_timeout), wait); + while(_state == BMP085_BUSY) { + sleep(); + } + pc.printf("over "); + return 0; +} + + + +uint16_t BMP085::read_cal_register(BMP085_Register reg) +{ + pc.printf("\nread_cal_register "); + _tx_buf[0] = reg; + transfer((char*)_tx_buf, 1, (char*)_rx_buf, 2); return (_rx_buf[0] << 8) | _rx_buf[1]; } -uint16_t BMP085::read_uc_temperature(void) { - LowPowerTimeout delay; +uint16_t BMP085::read_uc_temperature(void) +{ + pc.printf("\read_uc_temperature "); _tx_buf[0] = BMP085_CTRL; _tx_buf[1] = BMP085_CMD_READ_TEMP; - _state = BMP085_BUSY; - _i2c.transfer(BMP085_ADDR, (char*)_tx_buf, 2, (char*)NULL, 0, _callback); - while(_state != BMP085_IDLE) { - sleep(); - } + transfer((char*)_tx_buf, 2, (char*)NULL, 0); - _state = BMP085_BUSY; - delay.attach(callback(this, &BMP085::_callback_handler_timeout), 0.01); - while(_state != BMP085_IDLE) { - sleep(); - } + wait(0.01); _tx_buf[0] = BMP085_DATA_TEMP; - _state = BMP085_BUSY; - _i2c.transfer(BMP085_ADDR, (char*)_tx_buf, 1, (char*)_rx_buf, 2, _callback); - while(_state != BMP085_IDLE) { - sleep(); - } + transfer((char*)_tx_buf, 1, (char*)_rx_buf, 2); return (_rx_buf[0] << 8) | _rx_buf[1]; } -uint32_t BMP085::read_uc_pressure(void) { - LowPowerTimeout delay; - _tx_buf[0] = BMP085_CTRL; +uint32_t BMP085::read_uc_pressure(void) +{ + pc.printf("\read_uc_pressure "); + _tx_buf[0] = BMP085_CTRL; _tx_buf[1] = BMP085_CMD_READ_PRESSURE + ((uint8_t)_mode << 6); - _state = BMP085_BUSY; - _i2c.transfer(BMP085_ADDR, (char*)_tx_buf, 2, (char*)NULL, 0, _callback); - while(_state != BMP085_IDLE) { - sleep(); - } + transfer((char*)_tx_buf, 2, (char*)NULL, 0); - _state = BMP085_BUSY; - delay.attach(callback(this, &BMP085::_callback_handler_timeout), 0.02); - while(_state != BMP085_IDLE) { - sleep(); - } + wait(0.02); _tx_buf[0] = BMP085_DATA_PRESSURE; - _state = BMP085_BUSY; - _i2c.transfer(BMP085_ADDR, (char*)_tx_buf, 1, (char*)_rx_buf, 3, _callback); - while(_state != BMP085_IDLE) { - sleep(); - } - return ((_rx_buf[0] << 16) | (_rx_buf[1] << 8) | _rx_buf[2]) >> (8 - (uint8_t)_mode); + transfer((char*)_tx_buf, 1, (char*)_rx_buf, 3); + + return ((_rx_buf[0] << 16) | (_rx_buf[1] << 8) | _rx_buf[2]) >> (8 - (uint8_t)_mode); } -void BMP085::measure() { +void BMP085::measure() +{ int t, p, ut, up, x1, x2, x3, b3, b5, b6; uint32_t b4, b7; @@ -87,9 +104,9 @@ b5 = x1 + x2; t = (b5 + 8) / (1 << 4); _temperature = (float)t / 10.0; - + up = read_uc_pressure(); - + b6 = b5 - 4000; x1 = (_cal_data.b2 * (b6 * b6 / (1 << 12))) / (1 << 11); x2 = _cal_data.ac2 * b6 / (1 << 11); @@ -112,10 +129,13 @@ _pressure = (float)p / 100.0; } -void BMP085::_callback_handler(int event) { +void BMP085::_callback_handler(int event) +{ _state = BMP085_IDLE; } -void BMP085::_callback_handler_timeout() { - _state = BMP085_IDLE; +void BMP085::_callback_handler_timeout() +{ + pc.printf("bmp timeout"); + _state = BMP085_TIMEOUT; }