Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-blocking drop-in replacement for read() function #28

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
40 changes: 22 additions & 18 deletions examples/MS5837_Example/MS5837_Example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,25 @@ void loop() {
// Update pressure and temperature readings
sensor.read();

Serial.print("Pressure: ");
Serial.print(sensor.pressure());
Serial.println(" mbar");

Serial.print("Temperature: ");
Serial.print(sensor.temperature());
Serial.println(" deg C");

Serial.print("Depth: ");
Serial.print(sensor.depth());
Serial.println(" m");

Serial.print("Altitude: ");
Serial.print(sensor.altitude());
Serial.println(" m above mean sea level");

delay(1000);
}
static uint32_t lastPrintTime = 0;

if(millis() - lastPrintTime > 1000) {
Serial.print("Pressure: ");
Serial.print(sensor.pressure());
Serial.println(" mbar");

Serial.print("Temperature: ");
Serial.print(sensor.temperature());
Serial.println(" deg C");

Serial.print("Depth: ");
Serial.print(sensor.depth());
Serial.println(" m");

Serial.print("Altitude: ");
Serial.print(sensor.altitude());
Serial.println(" m above mean sea level\n");

lastPrintTime = millis();
}
}
80 changes: 48 additions & 32 deletions src/MS5837.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,47 +96,63 @@ void MS5837::setFluidDensity(float density) {
}

void MS5837::read() {
static uint32_t pressReadStartTime = 0;
static uint32_t tempReadStartTime = 0;

static bool pressReadNext = true;

//Check that _i2cPort is not NULL (i.e. has the user forgoten to call .init or .begin?)
if (_i2cPort == NULL)
{
return;
}

//if 20ms have passed since read AND the next reading should be a D1 (pressure) reading, read.
if(millis() - pressReadStartTime > 20 && pressReadNext) {
//get requested D1 numbers
_i2cPort->beginTransmission(MS5837_ADDR);
_i2cPort->write(MS5837_ADC_READ);
_i2cPort->endTransmission();

// Request D1 conversion
_i2cPort->beginTransmission(MS5837_ADDR);
_i2cPort->write(MS5837_CONVERT_D1_8192);
_i2cPort->endTransmission();

delay(20); // Max conversion time per datasheet

_i2cPort->beginTransmission(MS5837_ADDR);
_i2cPort->write(MS5837_ADC_READ);
_i2cPort->endTransmission();

_i2cPort->requestFrom(MS5837_ADDR,3);
D1_pres = 0;
D1_pres = _i2cPort->read();
D1_pres = (D1_pres << 8) | _i2cPort->read();
D1_pres = (D1_pres << 8) | _i2cPort->read();

// Request D2 conversion
_i2cPort->beginTransmission(MS5837_ADDR);
_i2cPort->write(MS5837_CONVERT_D2_8192);
_i2cPort->endTransmission();

delay(20); // Max conversion time per datasheet
_i2cPort->requestFrom(MS5837_ADDR,3);
D1_pres = 0;
D1_pres = _i2cPort->read();
D1_pres = (D1_pres << 8) | _i2cPort->read();
D1_pres = (D1_pres << 8) | _i2cPort->read();

_i2cPort->beginTransmission(MS5837_ADDR);
_i2cPort->write(MS5837_ADC_READ);
_i2cPort->endTransmission();
// Request D2 conversion
_i2cPort->beginTransmission(MS5837_ADDR);
_i2cPort->write(MS5837_CONVERT_D2_8192);
_i2cPort->endTransmission();

tempReadStartTime = millis(); //start a timer for the temperature read
pressReadNext = false; //ensure the next reading is a temperautre reading

calculate(); //calculate only gets run when necessary
}

if(millis() - tempReadStartTime > 20 && !pressReadNext) {
//get requested D2 numbers
_i2cPort->beginTransmission(MS5837_ADDR);
_i2cPort->write(MS5837_ADC_READ);
_i2cPort->endTransmission();

_i2cPort->requestFrom(MS5837_ADDR,3);
D2_temp = 0;
D2_temp = _i2cPort->read();
D2_temp = (D2_temp << 8) | _i2cPort->read();
D2_temp = (D2_temp << 8) | _i2cPort->read();
_i2cPort->requestFrom(MS5837_ADDR,3);
D2_temp = 0;
D2_temp = _i2cPort->read();
D2_temp = (D2_temp << 8) | _i2cPort->read();
D2_temp = (D2_temp << 8) | _i2cPort->read();

calculate();
// Request D1 conversion
_i2cPort->beginTransmission(MS5837_ADDR);
_i2cPort->write(MS5837_CONVERT_D1_8192);
_i2cPort->endTransmission();

pressReadStartTime = millis();
pressReadNext = true;

calculate();
}
}

void MS5837::calculate() {
Expand Down