I2C Temprature Sensor Progemm MPL3115A2
Dependents: I2C_Temprature_raspiboard
Fork of MPL3115A2 by
Altitude.h@4:c0ee6755d57f, 2017-06-20 (annotated)
- Committer:
- ODEM
- Date:
- Tue Jun 20 07:42:33 2017 +0000
- Revision:
- 4:c0ee6755d57f
- Parent:
- 3:7c7c1ea6fc33
I2C Temp,Pressure,
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sophtware | 0:beb43bc3d6d4 | 1 | /* |
sophtware | 0:beb43bc3d6d4 | 2 | MPL3115A2 Barometric Pressure and Tempurature Sensor Library |
sophtware | 0:beb43bc3d6d4 | 3 | By: Michael Lange |
sophtware | 0:beb43bc3d6d4 | 4 | Date: March 31, 2014 |
sophtware | 0:beb43bc3d6d4 | 5 | License: This code is public domain. |
sophtware | 0:beb43bc3d6d4 | 6 | |
sophtware | 0:beb43bc3d6d4 | 7 | This class encapsulates an altitude reading from the sensor. |
sophtware | 0:beb43bc3d6d4 | 8 | |
sophtware | 0:beb43bc3d6d4 | 9 | */ |
sophtware | 0:beb43bc3d6d4 | 10 | |
sophtware | 0:beb43bc3d6d4 | 11 | |
sophtware | 0:beb43bc3d6d4 | 12 | #ifndef ALTITUDE_H |
sophtware | 0:beb43bc3d6d4 | 13 | #define ALTITUDE_H |
sophtware | 0:beb43bc3d6d4 | 14 | |
sophtware | 0:beb43bc3d6d4 | 15 | #include "mbed.h" |
sophtware | 0:beb43bc3d6d4 | 16 | |
sophtware | 3:7c7c1ea6fc33 | 17 | //! Casting a float to a char truncates, therefore negative numbers become positive. |
sophtware | 3:7c7c1ea6fc33 | 18 | //! This will properly cast a float in the range -128 to 127 to a char. |
sophtware | 0:beb43bc3d6d4 | 19 | #define float_to_char(x) (((x)<0)?(-(char)(x)):((char)(x))) |
sophtware | 0:beb43bc3d6d4 | 20 | |
sophtware | 2:2ebc9c0d4a54 | 21 | //! Altitude provides a wrapper around altitude data coming from the sensor. The class handles |
sophtware | 2:2ebc9c0d4a54 | 22 | //! working with compressed data from the sensor and provides convenient functions for retreiving |
sophtware | 2:2ebc9c0d4a54 | 23 | //! the data in various units (with room to add more if needed). |
sophtware | 0:beb43bc3d6d4 | 24 | class Altitude |
sophtware | 0:beb43bc3d6d4 | 25 | { |
sophtware | 0:beb43bc3d6d4 | 26 | public: |
sophtware | 0:beb43bc3d6d4 | 27 | |
sophtware | 2:2ebc9c0d4a54 | 28 | //! The size of the compressed data buffer from the sensor. Used in an I2C read. |
sophtware | 0:beb43bc3d6d4 | 29 | static const int size = 3; |
sophtware | 2:2ebc9c0d4a54 | 30 | |
sophtware | 2:2ebc9c0d4a54 | 31 | //! The units we support converting the sensor data to. |
sophtware | 0:beb43bc3d6d4 | 32 | enum unitsType { METERS, FEET }; |
sophtware | 0:beb43bc3d6d4 | 33 | |
sophtware | 0:beb43bc3d6d4 | 34 | Altitude(); |
sophtware | 0:beb43bc3d6d4 | 35 | Altitude(float a, unitsType units = FEET); |
sophtware | 0:beb43bc3d6d4 | 36 | Altitude(const char* compressed); |
sophtware | 0:beb43bc3d6d4 | 37 | Altitude(const char msb, const char csb, const char lsb); |
sophtware | 0:beb43bc3d6d4 | 38 | |
sophtware | 2:2ebc9c0d4a54 | 39 | //! Allows using the object directly in an I2C read operation. |
sophtware | 0:beb43bc3d6d4 | 40 | operator char*(void) { return _compressed; } |
sophtware | 2:2ebc9c0d4a54 | 41 | //! Same as calling altitude with METERS as the parameter. |
sophtware | 0:beb43bc3d6d4 | 42 | operator float(void) { return _altitude; } |
sophtware | 0:beb43bc3d6d4 | 43 | |
sophtware | 2:2ebc9c0d4a54 | 44 | //! Returns the altitude in the units you specifiy, defaulting to FEET if none specified. |
sophtware | 0:beb43bc3d6d4 | 45 | float altitude(unitsType units = FEET); |
sophtware | 2:2ebc9c0d4a54 | 46 | //! Call to decompress the sensor data after an I2C read. |
sophtware | 0:beb43bc3d6d4 | 47 | void setAltitude(); |
sophtware | 0:beb43bc3d6d4 | 48 | void setAltitude(const char* compressed); |
sophtware | 0:beb43bc3d6d4 | 49 | void setAltitude(const char msb, const char csb, const char lsb); |
sophtware | 0:beb43bc3d6d4 | 50 | void setAltitude(float a, unitsType units = FEET); |
sophtware | 0:beb43bc3d6d4 | 51 | |
sophtware | 2:2ebc9c0d4a54 | 52 | //! Returns the altitude as a string in the units specified, defaulting to FEET if none specified. |
ODEM | 4:c0ee6755d57f | 53 | const char* print(unitsType units = METERS); |
sophtware | 0:beb43bc3d6d4 | 54 | |
sophtware | 2:2ebc9c0d4a54 | 55 | //! Converts meters to feet. |
sophtware | 0:beb43bc3d6d4 | 56 | static float MetersToFeet(float meters) { return meters * 3.28084; } |
sophtware | 2:2ebc9c0d4a54 | 57 | //! Converts feet to meters. |
sophtware | 0:beb43bc3d6d4 | 58 | static float FeetToMeters(float feet) { return feet / 3.28084; } |
sophtware | 0:beb43bc3d6d4 | 59 | |
sophtware | 0:beb43bc3d6d4 | 60 | private: |
sophtware | 0:beb43bc3d6d4 | 61 | float _altitude; |
sophtware | 0:beb43bc3d6d4 | 62 | char _compressed[3]; |
sophtware | 0:beb43bc3d6d4 | 63 | char _printBuffer[9]; |
sophtware | 0:beb43bc3d6d4 | 64 | }; |
sophtware | 0:beb43bc3d6d4 | 65 | |
sophtware | 0:beb43bc3d6d4 | 66 | #endif // ALTITUDE_H |