MPL3115A2_for_weather_shield

Dependents:   SPARKFUN_WEATHER_SHIELD

Fork of MPL3115A2 by Michael Lange

Altitude.h

Committer:
sophtware
Date:
2014-04-01
Revision:
0:beb43bc3d6d4
Child:
2:2ebc9c0d4a54

File content as of revision 0:beb43bc3d6d4:

/*
    MPL3115A2 Barometric Pressure and Tempurature Sensor Library
    By: Michael Lange
    Date: March 31, 2014
    License: This code is public domain.
 
    This class encapsulates an altitude reading from the sensor.
 
 */
 
 
#ifndef ALTITUDE_H
#define ALTITUDE_H

#include "mbed.h"

// Casting truncates, therefore negative numbers become positive.
// This will only cast properly in the range -128 to 127.
#define float_to_char(x) (((x)<0)?(-(char)(x)):((char)(x)))

class Altitude
{
public:

    static const int size = 3;
    enum unitsType { METERS, FEET };

    Altitude();
    Altitude(float a, unitsType units = FEET);
    Altitude(const char* compressed);
    Altitude(const char msb, const char csb, const char lsb);

    operator char*(void) { return _compressed; }
    operator float(void) { return _altitude; }

    float altitude(unitsType units = FEET);
    void setAltitude();
    void setAltitude(const char* compressed);
    void setAltitude(const char msb, const char csb, const char lsb);
    void setAltitude(float a, unitsType units = FEET);

    const char* print(unitsType units = FEET);
    
    static float MetersToFeet(float meters) { return meters * 3.28084; }
    static float FeetToMeters(float feet) { return feet / 3.28084; }
    
private:
    float _altitude;
    char  _compressed[3];
    char  _printBuffer[9];
};

#endif // ALTITUDE_H