Demo program for the MPL3115A2 library.

Dependencies:   MPL3115A2 mbed

Example program for MPL3115 sensor. Added code for data acquisition using Interrupt.

main.cpp

Committer:
clemente
Date:
2013-05-30
Revision:
2:0b726b8c8ab9
Parent:
1:5ea71ebc0d82
Child:
3:e2a621ea6976

File content as of revision 2:0b726b8c8ab9:

#include "mbed.h"
#include "MPL3115A2.h"

#define MPL3115A2_I2C_ADDRESS (0x60<<1)

DigitalOut myled(LED1);
MPL3115A2 wigo_sensor1( PTE0, PTE1, MPL3115A2_I2C_ADDRESS);
Serial pc(USBTX, USBRX);

/* pos [0] = altimeter or pressure value */
/* pos [1] = temperature value */
float sensor_data[2];

void dataready( void);      // callback function for data streaming using Interrupt
void alttrigger( void);

int main() {
    
    pc.baud( 230400);
    pc.printf("MPL3115A2 Sensor. [%X]\r\n", wigo_sensor1.getDeviceID());
        
#if 1
    // ***** Data acquisition using Interrupt
    
    // Configure the sensor as Barometer.
    wigo_sensor1.Barometric_Mode();
    // Set callback function and over sampling value (see MPL3115A2.h for details)
    wigo_sensor1.DataReady( &dataready, OVERSAMPLE_RATIO_64);
    // just loop...
    while( 1)
    {
        wait( 1.0);
        pc.printf(".");
    }
#else
    // ***** Data acquisition using polling method
    
    // Configure the sensor as Barometer.
    unsigned int mode=1;
    
    // Set over sampling value (see MPL3115A2.h for details)
    wigo_sensor1.Oversample_Ratio( OVERSAMPLE_RATIO_64);
    // Configure the sensor as Barometer.
    wigo_sensor1.Barometric_Mode();

    while(1) {
        //
        if ( wigo_sensor1.isDataAvailable()) {
            wigo_sensor1.getAllData( &sensor_data[0]);
            if ( mode & 0x0001) {
                pc.printf("\tPressure: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]);
                wigo_sensor1.Altimeter_Mode();
            } else {
                pc.printf("\tAltitude: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]);
                wigo_sensor1.Barometric_Mode();            
            }
            mode++;
        }
        //
        wait( 0.001);
    }
#endif    
}

void dataready( void)
{
    wigo_sensor1.getAllData( &sensor_data[0]);
    pc.printf("\tPressure: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]);
}