Ian Hua / MPL3115A2

Fork of MPL3115A2 by Mark Randall

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MPL3115A2.cpp Source File

MPL3115A2.cpp

00001 #include "MPL3115A2.h"
00002 
00003 //MPL3115A2 Class Constructor
00004 /*
00005 MPL3115A2::MPL3115A2(int SlaveAddress, PinName pin1, PinName pin2, PinName pin3, PinName pin4) : _i2c(pin1,pin2), _pin1(pin3),_pin2(pin4)
00006 {
00007     _pin1.mode(PullUp);
00008     _pin2.mode(PullUp);
00009     SLAVE_ADDRESS = SlaveAddress;
00010 
00011 }
00012 */
00013 
00014 MPL3115A2::MPL3115A2(PinName pin1, PinName pin2): _i2c(pin1,pin2)
00015 {
00016     _i2c.frequency(400000);
00017 }
00018 
00019 int MPL3115A2::Write_Register (char regnum, char data)   //Used for writing data to the MPL3115A2 Registers
00020 {
00021     char Write_Data[2];
00022     Write_Data[0] = regnum;
00023     Write_Data[1] = data;
00024 
00025     return(_i2c.write(SLAVE_ADDRESS, Write_Data, 2));
00026 }
00027 
00028 // Initialize the MPL3115A2 in Altitude Interrupt Mode
00029 bool MPL3115A2::init(void)
00030 {
00031     int status = -1;
00032     status = Write_Register(0x26, 0xB9);//0xB8);
00033 
00034     if (!status)
00035         status = Write_Register(0x13,0x07);
00036 
00037     if (!status)
00038         status = Write_Register(0x29,0x80);
00039 
00040     /*
00041     if(status==0) {
00042         status = -1;
00043         status = Write_Register(0x2d,OFFSET);
00044     }
00045     */
00046 
00047     if (!status)
00048         status = Write_Register(0x28,0x11);
00049 
00050     if (!status)
00051         return true;
00052     else
00053         return false;
00054 }
00055 
00056 /*
00057 bool MPL3115A2::getInterrupt1(void)   //Return state of Int1 pin
00058 {
00059     return(_pin1.read());
00060 }
00061 
00062 bool MPL3115A2::getInterrupt2(void)   //Return state of Int2 pin
00063 {
00064     return(_pin2.read());
00065 }
00066 */
00067 
00068 
00069 int MPL3115A2::Read_Altitude_Data(void)     //Read Data Register from MPL3115A2 Sensor
00070 {
00071     char cmd[2] = {0x01};
00072     _i2c.write(SLAVE_ADDRESS, cmd, 1, true);
00073     
00074     return(_i2c.read(SLAVE_ADDRESS, _SensorData, 5));
00075 }
00076 
00077 // Return Altitude in Feet
00078 float MPL3115A2::Altitude_ft ()
00079 {
00080     if (!Read_Altitude_Data())
00081         return (((_SensorData[2]>>4)/128.0) + (_SensorData[0]*256+_SensorData[1])) * 3.28;
00082     else
00083         return -1;
00084 }
00085 
00086 // Return Altitude in meters
00087 float MPL3115A2::Altitude_m ()
00088 {
00089     if (!Read_Altitude_Data())
00090         return ((_SensorData[2]>>4)/128.0) + (_SensorData[0]*256+_SensorData[1]);
00091     else
00092         return -1;
00093 }
00094 
00095 // Return Temp in Degrees F
00096 float MPL3115A2::Temp_F ()
00097 {
00098     if (!Read_Altitude_Data())
00099         return ((_SensorData[4]>>4)/128.0 + (float)_SensorData[3])*1.8 + 32.0;
00100     else
00101         return -1;
00102 }
00103 
00104 // Return Temp in Degrees C
00105 float MPL3115A2::Temp_C ()
00106 {
00107     if (!Read_Altitude_Data())
00108         return (_SensorData[4]>>4)/128.0 + (float)_SensorData[3];
00109     else
00110         return -1;
00111 }