From ed922856667e40d0b7f663cea2eb849401bd1eef Mon Sep 17 00:00:00 2001 From: Clemens Arth Date: Wed, 8 Jul 2020 12:48:55 +0200 Subject: [PATCH] Renamed STATUS to MPLSTATUS because of incompatibility with ESP8266 internal libraries --- src/SparkFunMPL3115A2.cpp | 43 +++++++++++++++++++-------------------- src/SparkFunMPL3115A2.h | 18 ++++++++-------- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/SparkFunMPL3115A2.cpp b/src/SparkFunMPL3115A2.cpp index 68f6d63..be620ec 100644 --- a/src/SparkFunMPL3115A2.cpp +++ b/src/SparkFunMPL3115A2.cpp @@ -6,15 +6,15 @@ License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). This library allows an Arduino to read from the MPL3115A2 low-cost high-precision pressure sensor. - + If you have feature suggestions or need support please use the github support page: https://github.com/sparkfun/MPL3115A2_Breakout Hardware Setup: The MPL3115A2 lives on the I2C bus. Attach the SDA pin to A4, SCL to A5. Use inline 10k resistors - if you have a 5V board. If you are using the SparkFun breakout board you *do not* need 4.7k pull-up resistors + if you have a 5V board. If you are using the SparkFun breakout board you *do not* need 4.7k pull-up resistors on the bus (they are built-in). - + Link to the breakout board product: - + Software: .begin() Gets sensor on the I2C bus. .readAltitude() Returns float with meters above sealevel. Ex: 1638.94 @@ -28,9 +28,9 @@ .setModeActive() Start taking measurements! .setOversampleRate(byte) Sets the # of samples from 1 to 128. See datasheet. .enableEventFlags() Sets the fundamental event flags. Required during setup. - + Updated by PaulZC: October 19th, 2019 - + */ #include @@ -61,7 +61,7 @@ float MPL3115A2::readAltitude() //Wait for PDR bit, indicates we have new pressure data int counter = 0; - while( (IIC_Read(STATUS) & (1<<1)) == 0) + while( (IIC_Read(MPLSTATUS) & (1<<1)) == 0) { if(++counter > 600) return(-999); //Error out after max of 512ms for a read delay(1); @@ -82,8 +82,8 @@ float MPL3115A2::readAltitude() // The least significant bytes l_altitude and l_temp are 4-bit, // fractional values, so you must cast the calulation in (float), - // shift the value over 4 spots to the right and divide by 16 (since - // there are 16 values in 4-bits). + // shift the value over 4 spots to the right and divide by 16 (since + // there are 16 values in 4-bits). float tempcsb = (lsb>>4)/16.0; float altitude = (float)( (msb << 8) | csb) + tempcsb; @@ -103,11 +103,11 @@ float MPL3115A2::readAltitudeFt() float MPL3115A2::readPressure() { //Check PDR bit, if it's not set then toggle OST - if(IIC_Read(STATUS) & (1<<2) == 0) toggleOneShot(); //Toggle the OST bit causing the sensor to immediately take another reading + if(IIC_Read(MPLSTATUS) & (1<<2) == 0) toggleOneShot(); //Toggle the OST bit causing the sensor to immediately take another reading //Wait for PDR bit, indicates we have new pressure data int counter = 0; - while(IIC_Read(STATUS) & (1<<2) == 0) + while(IIC_Read(MPLSTATUS) & (1<<2) == 0) { if(++counter > 600) return(-999); //Error out after max of 512ms for a read delay(1); @@ -125,7 +125,7 @@ float MPL3115A2::readPressure() msb = _i2cPort->read(); csb = _i2cPort->read(); lsb = _i2cPort->read(); - + toggleOneShot(); //Toggle the OST bit causing the sensor to immediately take another reading // Pressure comes back as a left shifted 20 bit number @@ -143,11 +143,11 @@ float MPL3115A2::readPressure() float MPL3115A2::readTemp() { - if(IIC_Read(STATUS) & (1<<1) == 0) toggleOneShot(); //Toggle the OST bit causing the sensor to immediately take another reading + if(IIC_Read(MPLSTATUS) & (1<<1) == 0) toggleOneShot(); //Toggle the OST bit causing the sensor to immediately take another reading //Wait for TDR bit, indicates we have new temp data int counter = 0; - while( (IIC_Read(STATUS) & (1<<1)) == 0) + while( (IIC_Read(MPLSTATUS) & (1<<1)) == 0) { if(++counter > 600) return(-999); //Error out after max of 512ms for a read delay(1); @@ -176,20 +176,20 @@ float MPL3115A2::readTemp() { foo = ~((msb << 8) + lsb) + 1; //2’s complement msb = foo >> 8; - lsb = foo & 0x00F0; + lsb = foo & 0x00F0; negSign = true; } // The least significant bytes l_altitude and l_temp are 4-bit, // fractional values, so you must cast the calulation in (float), - // shift the value over 4 spots to the right and divide by 16 (since - // there are 16 values in 4-bits). + // shift the value over 4 spots to the right and divide by 16 (since + // there are 16 values in 4-bits). float templsb = (lsb>>4)/16.0; //temp, fraction of a degree float temperature = (float)(msb + templsb); if (negSign) temperature = 0 - temperature; - + return(temperature); } @@ -236,14 +236,14 @@ void MPL3115A2::setModeActive() } //Call with a rate from 0 to 7. See page 33 for table of ratios. -//Sets the over sample rate. Datasheet calls for 128 but you can set it +//Sets the over sample rate. Datasheet calls for 128 but you can set it //from 1 to 128 samples. The higher the oversample rate the greater //the time between data samples. void MPL3115A2::setOversampleRate(byte sampleRate) { if(sampleRate > 7) sampleRate = 7; //OS cannot be larger than 0b.0111 sampleRate <<= 3; //Align it for the CTRL_REG1 register - + byte tempSetting = IIC_Read(CTRL_REG1); //Read current settings tempSetting &= 0xc7; // B11000111; //Clear out old OS bits tempSetting |= sampleRate; //Mask in new OS bits @@ -254,7 +254,7 @@ void MPL3115A2::setOversampleRate(byte sampleRate) //test against them. This is recommended in datasheet during setup. void MPL3115A2::enableEventFlags() { - IIC_Write(PT_DATA_CFG, 0x07); // Enable all three pressure and temp event flags + IIC_Write(PT_DATA_CFG, 0x07); // Enable all three pressure and temp event flags } //Clears then sets the OST bit which causes the sensor to immediately take another reading @@ -290,4 +290,3 @@ void MPL3115A2::IIC_Write(byte regAddr, byte value) _i2cPort->write(value); _i2cPort->endTransmission(true); } - diff --git a/src/SparkFunMPL3115A2.h b/src/SparkFunMPL3115A2.h index ec86dac..89f9a20 100644 --- a/src/SparkFunMPL3115A2.h +++ b/src/SparkFunMPL3115A2.h @@ -1,18 +1,18 @@ -/* +/* MPL3115A2 Barometric Pressure Sensor Library By: Nathan Seidle SparkFun Electronics Date: September 24th, 2013 License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). - + Get pressure, altitude and temperature from the MPL3115A2 sensor. - + Updated by PaulZC: October 19th, 2019 - + */ - -#ifndef _SPARKFUN_MPL3115A2_H_ -#define _SPARKFUN_MPL3115A2_H_ + +#ifndef _SPARKFUN_MPL3115A2_H_ +#define _SPARKFUN_MPL3115A2_H_ #include @@ -21,7 +21,7 @@ // Define MPL3115A2 registers enum mpl3115a2_regs { - STATUS = 0x00, + MPLSTATUS = 0x00, OUT_P_MSB = 0x01, OUT_P_CSB = 0x02, OUT_P_LSB = 0x03, @@ -66,7 +66,7 @@ enum mpl3115a2_regs CTRL_REG5 = 0x2A, OFF_P = 0x2B, OFF_T = 0x2C, - OFF_H = 0x2D + OFF_H = 0x2D };