MPL3115A2_for_weather_shield

Dependents:   SPARKFUN_WEATHER_SHIELD

Fork of MPL3115A2 by Michael Lange

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MPL3115A2.cpp Source File

MPL3115A2.cpp

00001 #include "MPL3115A2.h"
00002 #include "mbed.h"
00003 
00004 #include "stdarg.h" // For debugOut use of the ... parameter and va_list
00005 
00006 MPL3115A2::MPL3115A2(I2C *i2c, Serial *pc) : _i2c(i2c), _debug(pc)
00007 {
00008 }
00009 
00010 //Barometer mode default
00011 void MPL3115A2::init()
00012 {
00013     setModeStandby();
00014     wait_ms(1);
00015     setModeBarometer();
00016     wait_ms(1);
00017     setOversampleRate(7);
00018     wait_ms(1);
00019     enableEventFlags();
00020     wait_ms(1);
00021     setModeActive();
00022     wait_ms(1);
00023 }
00024 
00025 // This method wait for a specified amount of time for one of the data
00026 // ready flags to be set. You need to pass in the correct data ready
00027 // mask for this to work. See page 22 of the datasheet.
00028 int MPL3115A2::dataReady(const char mask)
00029 {
00030     int attempts = 0;
00031  
00032     while ((i2cRead(STATUS) & mask) == 0)
00033     {
00034         attempts++;
00035         
00036         if(attempts > MAX_DATA_READY_ATTEMPTS) 
00037             return 0; // Failed
00038             
00039         wait_ms(1);
00040     }
00041     
00042     return 1; // Success
00043 }
00044 
00045 Altitude* MPL3115A2::readAltitude(Altitude* a)
00046 {
00047     // Force the sensor to take a new reading.
00048     toggleOneShot();
00049     
00050     // Wait for the data to be ready.
00051     if (!pressureDataReady())
00052     {
00053         debugOut("MPL3115A2::readAltitude: Sensor failed to generate an altitude reading in a reasonable time.\r\n");
00054         
00055         // We leave the altitude object as is if we encounter an error.
00056         return a;
00057     }
00058         
00059     // Get the new data from the sensor.
00060     _i2c->start();                              // Start
00061     if (_i2c->write(MPL3115A2_ADDRESS) != 1)    // A write to device
00062         debugOut("MPL3115A2::readAltitude: Sensor failed to respond to write request at address 0x%X\r\n", MPL3115A2_ADDRESS);
00063     
00064     if (_i2c->write(OUT_P_MSB) != 1)            // Register to read
00065         debugOut("MPL3115A2::readAltitude: Sensor at address 0x%X did not acknowledge register 0x%X\r\n", MPL3115A2_ADDRESS, OUT_P_MSB);
00066     
00067     // Write the data directly into our Altitude object. This object
00068     // takes care of converting the compressed data from the sensor, and
00069     // provides functions to get the data in various units. And it also
00070     // has a print function to output the data as a string.
00071     _i2c->read(MPL3115A2_ADDRESS, (*a), Altitude::size);
00072     a->setAltitude();
00073 
00074     return a;
00075 }
00076 
00077 // See readAltitude for comments about this function.
00078 Pressure* MPL3115A2::readPressure(Pressure* p)
00079 {
00080     toggleOneShot();
00081     
00082     if (!pressureDataReady())
00083     {
00084         debugOut("MPL3115A2::readPressure: Sensor failed to generate a pressure reading in a reasonable time.\r\n");
00085         return p;
00086     }
00087 
00088     _i2c->start(); 
00089     if (_i2c->write(MPL3115A2_ADDRESS) != 1)
00090         debugOut("MPL3115A2::readPressure: Sensor failed to respond to write request at address 0x%X\r\n", MPL3115A2_ADDRESS);
00091     
00092     if (_i2c->write(OUT_P_MSB) != 1) 
00093         debugOut("MPL3115A2::readPressure: Sensor at address 0x%X did not acknowledge register 0x%X\r\n", MPL3115A2_ADDRESS, OUT_P_MSB);
00094     
00095     _i2c->read(MPL3115A2_ADDRESS, (*p), Pressure::size);
00096     p->setPressure();
00097 
00098     return p;
00099 }
00100 
00101 // See readAltitude for comments about this function.
00102 Temperature* MPL3115A2::readTemperature(Temperature* t)
00103 {
00104     toggleOneShot();
00105 
00106     if (!temperatureDataReady())
00107     {
00108         debugOut("MPL3115A2::readTemperature: Sensor failed to generate a temperature reading in a reasonable time.\r\n");
00109         return t;
00110     }
00111 
00112     _i2c->start();  
00113     if (_i2c->write(MPL3115A2_ADDRESS) != 1)
00114         debugOut("MPL3115A2::readTemperature: Sensor failed to respond to write request at address 0x%X\r\n", MPL3115A2_ADDRESS);
00115     
00116     if (_i2c->write(OUT_T_MSB) != 1)        
00117         debugOut("MPL3115A2::readTemperature: Sensor at address 0x%X did not acknowledge register 0x%X\r\n", MPL3115A2_ADDRESS, OUT_P_MSB);
00118     
00119     _i2c->read(MPL3115A2_ADDRESS, (*t), Temperature::size);
00120     t->setTemperature();
00121     
00122     return t;
00123 }
00124 
00125 void MPL3115A2::setModeAltimeter()
00126 {
00127     setRegisterBit(CTRL_REG1, 0x80);    // Set ALT bit
00128 }
00129 
00130 void MPL3115A2::setModeBarometer()
00131 {
00132     clearRegisterBit(CTRL_REG1, 0x80);  // Clear ALT bit
00133 }
00134 
00135 void MPL3115A2::setModeStandby()
00136 {
00137     clearRegisterBit(CTRL_REG1, 0x01);  // Clear SBYB bit for Standby mode
00138 }
00139 void MPL3115A2::setModeActive()
00140 {
00141     setRegisterBit(CTRL_REG1, 0x01);    // Set SBYB bit for Active mode
00142 }
00143 
00144 void MPL3115A2::setOversampleRate(char sampleRate)
00145 {
00146     if(sampleRate > 7) 
00147         sampleRate = 7;                 // OS cannot be larger than 0b.0111
00148     
00149     sampleRate <<= 3;                   // Align it for the CTRL_REG1 register
00150   
00151     char temp = i2cRead(CTRL_REG1);     // Read current settings
00152     temp &= 0xC7;                       // Clear out old OS bits
00153     temp |= sampleRate;                 // Mask in new OS bits
00154     i2cWrite(CTRL_REG1, temp);
00155 }
00156 
00157 void MPL3115A2::enableEventFlags()
00158 {
00159     i2cWrite(PT_DATA_CFG, 0x07); // Enable all three pressure and temp event flags 
00160 }
00161 
00162 void MPL3115A2::toggleOneShot(void)
00163 {
00164     clearRegisterBit(CTRL_REG1, 0x02);  // Clear OST bit
00165     setRegisterBit(CTRL_REG1, 0x02);    // Set the OST bit.
00166 }
00167 
00168 void MPL3115A2::clearRegisterBit(const char regAddr, const char bitMask)
00169 {
00170     char temp = i2cRead(regAddr);   // Read the current register value
00171     temp &= ~bitMask;               // Clear the bit from the value
00172     i2cWrite(regAddr, temp);        // Write register value back
00173 }
00174 
00175 void MPL3115A2::setRegisterBit(const char regAddr, const char bitMask)
00176 {
00177     char temp = i2cRead(regAddr);   // Read the current register value
00178     temp |= bitMask;                // Set the bit in the value
00179     i2cWrite(regAddr, temp);        // Write register value back
00180 }
00181 
00182 char MPL3115A2::i2cRead(char regAddr)
00183 {
00184     _i2c->start();                              // Start
00185     if (_i2c->write(MPL3115A2_ADDRESS)!=1)      // A write to device
00186         debugOut("MPL3115A2::i2cRead: Sensor failed to respond to write request at address 0x%X\r\n", MPL3115A2_ADDRESS);
00187     
00188     if (_i2c->write(regAddr)!=1)                // Register to read
00189         debugOut("MPL3115A2::i2cRead: Sensor at address 0x%X did not acknowledge register 0x%X\r\n", MPL3115A2_ADDRESS, regAddr);
00190     
00191     _i2c->start();                  
00192     if (_i2c->write(MPL3115A2_ADDRESS|0x01)!=1) // Read from device
00193         debugOut("MPL3115A2::i2cRead: Sensor failed to respond to read request at address 0x%X\r\n", MPL3115A2_ADDRESS);
00194 
00195     char result = _i2c->read(READ_NAK);         // Read the data
00196     _i2c->stop();
00197     
00198     return result;  
00199 }
00200 
00201 void MPL3115A2::i2cWrite(char regAddr, char value)
00202 {
00203     char cmd[2];
00204     cmd[0] = regAddr;
00205     cmd[1] = value;
00206     if (_i2c->write(MPL3115A2_ADDRESS, cmd, 2) != 0)
00207         debugOut("MPL3115A2::i2cWrite: Failed writing value (%d, 0x%X) to register 0x%X\r\n", value, regAddr);    
00208 }
00209 
00210 void MPL3115A2::debugOut(const char * format, ...)
00211 {
00212     if (_debug == NULL)
00213         return;
00214         
00215     va_list arg;
00216     _debug->printf(format, arg);
00217 }