h

Fork of MPL3115A2 by Michael Lange

Revision:
0:beb43bc3d6d4
Child:
2:2ebc9c0d4a54
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Altitude.h	Tue Apr 01 21:35:23 2014 +0000
@@ -0,0 +1,53 @@
+/*
+    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
\ No newline at end of file