1 #ifndef LIBPROPELLER_MS5611_H_
2 #define LIBPROPELLER_MS5611_H_
5 #include "libpropeller/i2c/i2c.h"
11 #include "libpropeller/stopwatch/stopwatch.h"
62 if (status_ ==
false) {
66 static const char kPROMRead [] = {
83 for (
int i = 0; i < 6; i++) {
85 bus_->
Put(device_address, kPROMRead[i + 1]);
86 bus_->
Get(device_address, data, 2);
87 C[i] = data[0] << 8 | data[1];
90 SetC(C[0], C[1], C[2], C[3], C[4], C[5]);
92 convertingTemperature_ =
true;
93 bus_->
Put(device_address, kConvertD2OSR4096);
124 bus_->
Put(device_address, kADCRead);
126 bus_->
Get(device_address, data, 3);
127 int reading = ExpandReading(data);
133 if (convertingTemperature_) {
136 bus_->
Put(device_address, kConvertD1OSR4096);
138 convertingTemperature_ =
false;
143 bus_->
Put(device_address, kConvertD2OSR4096);
145 convertingTemperature_ =
true;
165 void Get(
int & tPressure,
int & tTemperature,
166 const bool calibrationCalculation =
true) {
168 if (calibrationCalculation ==
true) {
172 tPressure = pressure_;
173 tTemperature = temperature_;
188 status_ = bus_->
Ping(device_address);
199 return bus_->
Put(device_address, kReset);
212 void SetC(
const int newC1,
const int newC2,
const int newC3,
213 const int newC4,
const int newC5,
const int newC6) {
214 C1_ = ((int64_t) newC1) << 15;
215 C2_ = ((int64_t) newC2) << 16;
216 C3_ = (int64_t) newC3;
217 C4_ = (int64_t) newC4;
232 void GetC(
int & oldC1,
int & oldC2,
int & oldC3,
233 int & oldC4,
int & oldC5,
int & oldC6) {
234 oldC1 = (int) (C1_ >> 15);
235 oldC2 = (int) (C2_ >> 16);
247 void TEST_SetD(
const int newD1,
const int newD2) {
253 void Calculate(
void) {
256 temperature_ = 2000 + ((dT * C6_) >> 23);
262 if (temperature_ < 2000) {
266 T2 = (dT64 * dT64) >> 31;
267 OFF2 = (5 * (temperature_ - 2000) * (temperature_ - 2000)) >> 1;
270 if (temperature_ < -1500) {
271 OFF2 = OFF2 + (7 * (temperature_ + 1500) * (temperature_ + 1500));
272 SENS2 = SENS2 + ((11 * (temperature_ + 1500) * (temperature_ + 1500)) >> 1);
277 int64_t
OFF = C2_ + ((C4_ * dT) >> 7);
278 int64_t SENS = C1_ + ((C3_ * dT) >> 8);
280 temperature_ = temperature_ - T2;
284 pressure_ = (int) ((((((int64_t) D1_) * SENS) >> 21) -
OFF) >> 15);
287 int ExpandReading(
const char data[])
const {
289 return data[0] << 16 | data[1] << 8 | data[2];
296 int64_t C1_, C2_, C3_, C4_;
300 int temperature_, pressure_;
303 bool convertingTemperature_;
309 const static char kConvertD1OSR4096 = 0x48;
310 const static char kConvertD2OSR4096 = 0x58;
311 const static char kADCRead = 0x00;
312 const static char kReset = 0b00011110;
314 unsigned char device_address;
317 if (address == LSB_0) {
318 device_address = 0b11101100;
319 }
else if (address == LSB_1) {
320 device_address = 0b11101110;
326 friend class ::UnityTests;
332 #endif // LIBPROPELLER_MS5611_H_