Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: WiFiDipCortexSensor
Fork of MPL3115A2 by
Revision 0:beb43bc3d6d4, committed 2014-04-01
- Comitter:
- sophtware
- Date:
- Tue Apr 01 21:35:23 2014 +0000
- Child:
- 1:a011ae93a350
- Commit message:
- Initial Checkin
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Altitude.cpp Tue Apr 01 21:35:23 2014 +0000
@@ -0,0 +1,64 @@
+#include "Altitude.h"
+#include "mbed.h"
+
+Altitude::Altitude()
+{
+ _altitude = 0.0;
+
+ _compressed[0] = 0;
+ _compressed[1] = 0;
+ _compressed[2] = 0;
+}
+
+Altitude::Altitude(float a, unitsType units)
+{
+ setAltitude(a, units);
+}
+
+Altitude::Altitude(const char* compressed)
+{
+ setAltitude(compressed);
+}
+
+Altitude::Altitude(const char msb, const char csb, const char lsb)
+{
+ setAltitude(msb, csb, lsb);
+}
+
+void Altitude::setAltitude()
+{
+ setAltitude(_compressed[0], _compressed[1], _compressed[2]);
+}
+
+void Altitude::setAltitude(float a, unitsType units)
+{
+ // TODO:
+}
+
+void Altitude::setAltitude(const char* compressed)
+{
+ setAltitude(compressed[0], compressed[1], compressed[2]);
+}
+
+void Altitude::setAltitude(const char msb, const char csb, const char lsb)
+{
+ float tempcsb = (lsb>>4) / 16.0;
+ _altitude = (float)((msb << 8) | csb) + tempcsb;
+}
+
+float Altitude::altitude(unitsType units)
+{
+ switch (units)
+ {
+ case FEET:
+ return MetersToFeet(_altitude);
+ }
+
+ return _altitude;
+}
+
+const char* Altitude::print(unitsType units)
+{
+ sprintf(_printBuffer, "%.0f", altitude(units));
+ return _printBuffer;
+}
--- /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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MPL3115A2.cpp Tue Apr 01 21:35:23 2014 +0000
@@ -0,0 +1,219 @@
+#include "MPL3115A2.h"
+#include "mbed.h"
+
+#include "stdarg.h" // For debugOut use of the ... parameter and va_list
+
+MPL3115A2::MPL3115A2(I2C *i2c, Serial *pc) : _i2c(i2c), _debug(pc)
+{
+}
+
+// By default I set the sensor to altimeter mode. I inserted a 1ms pause
+// between each call to allow logic capture if needed. This give a small
+// gap between captures on the bus to make working with the data easier.
+void MPL3115A2::init()
+{
+ setModeStandby();
+ wait_ms(1);
+ setModeAltimeter();
+ wait_ms(1);
+ setOversampleRate(7);
+ wait_ms(1);
+ enableEventFlags();
+ wait_ms(1);
+ setModeActive();
+ wait_ms(1);
+}
+
+// This method wait for a specified amount of time for one of the data
+// ready flags to be set. You need to pass in the correct data ready
+// mask for this to work. See page 22 of the datasheet.
+int MPL3115A2::dataReady(const char mask)
+{
+ int attempts = 0;
+
+ while ((i2cRead(STATUS) & mask) == 0)
+ {
+ attempts++;
+
+ if(attempts > MAX_DATA_READY_ATTEMPTS)
+ return 0; // Failed
+
+ wait_ms(1);
+ }
+
+ return 1; // Success
+}
+
+Altitude* MPL3115A2::readAltitude(Altitude* a)
+{
+ // Force the sensor to take a new reading.
+ toggleOneShot();
+
+ // Wait for the data to be ready.
+ if (!pressureDataReady())
+ {
+ debugOut("MPL3115A2::readAltitude: Sensor failed to generate an altitude reading in a reasonable time.\r\n");
+
+ // We leave the altitude object as is if we encounter an error.
+ return a;
+ }
+
+ // Get the new data from the sensor.
+ _i2c->start(); // Start
+ if (_i2c->write(MPL3115A2_ADDRESS) != 1) // A write to device
+ debugOut("MPL3115A2::readAltitude: Sensor failed to respond to write request at address 0x%X\r\n", MPL3115A2_ADDRESS);
+
+ if (_i2c->write(OUT_P_MSB) != 1) // Register to read
+ debugOut("MPL3115A2::readAltitude: Sensor at address 0x%X did not acknowledge register 0x%X\r\n", MPL3115A2_ADDRESS, OUT_P_MSB);
+
+ // Write the data directly into our Altitude object. This object
+ // takes care of converting the compressed data from the sensor, and
+ // provides functions to get the data in various units. And it also
+ // has a print function to output the data as a string.
+ _i2c->read(MPL3115A2_ADDRESS, (*a), Altitude::size);
+ a->setAltitude();
+
+ return a;
+}
+
+// See readAltitude for comments about this function.
+Pressure* MPL3115A2::readPressure(Pressure* p)
+{
+ toggleOneShot();
+
+ if (!pressureDataReady())
+ {
+ debugOut("MPL3115A2::readPressure: Sensor failed to generate a pressure reading in a reasonable time.\r\n");
+ return p;
+ }
+
+ _i2c->start();
+ if (_i2c->write(MPL3115A2_ADDRESS) != 1)
+ debugOut("MPL3115A2::readPressure: Sensor failed to respond to write request at address 0x%X\r\n", MPL3115A2_ADDRESS);
+
+ if (_i2c->write(OUT_P_MSB) != 1)
+ debugOut("MPL3115A2::readPressure: Sensor at address 0x%X did not acknowledge register 0x%X\r\n", MPL3115A2_ADDRESS, OUT_P_MSB);
+
+ _i2c->read(MPL3115A2_ADDRESS, (*p), Pressure::size);
+ p->setPressure();
+
+ return p;
+}
+
+// See readAltitude for comments about this function.
+Temperature* MPL3115A2::readTemperature(Temperature* t)
+{
+ toggleOneShot();
+
+ if (!temperatureDataReady())
+ {
+ debugOut("MPL3115A2::readTemperature: Sensor failed to generate a temperature reading in a reasonable time.\r\n");
+ return t;
+ }
+
+ _i2c->start();
+ if (_i2c->write(MPL3115A2_ADDRESS) != 1)
+ debugOut("MPL3115A2::readTemperature: Sensor failed to respond to write request at address 0x%X\r\n", MPL3115A2_ADDRESS);
+
+ if (_i2c->write(OUT_T_MSB) != 1)
+ debugOut("MPL3115A2::readTemperature: Sensor at address 0x%X did not acknowledge register 0x%X\r\n", MPL3115A2_ADDRESS, OUT_P_MSB);
+
+ _i2c->read(MPL3115A2_ADDRESS, (*t), Temperature::size);
+ t->setTemperature();
+
+ return t;
+}
+
+void MPL3115A2::setModeAltimeter()
+{
+ setRegisterBit(CTRL_REG1, 0x80); // Set ALT bit
+}
+
+void MPL3115A2::setModeBarometer()
+{
+ clearRegisterBit(CTRL_REG1, 0x80); // Clear ALT bit
+}
+
+void MPL3115A2::setModeStandby()
+{
+ clearRegisterBit(CTRL_REG1, 0x01); // Clear SBYB bit for Standby mode
+}
+void MPL3115A2::setModeActive()
+{
+ setRegisterBit(CTRL_REG1, 0x01); // Set SBYB bit for Active mode
+}
+
+void MPL3115A2::setOversampleRate(char sampleRate)
+{
+ if(sampleRate > 7)
+ sampleRate = 7; // OS cannot be larger than 0b.0111
+
+ sampleRate <<= 3; // Align it for the CTRL_REG1 register
+
+ char temp = i2cRead(CTRL_REG1); // Read current settings
+ temp &= 0xC7; // Clear out old OS bits
+ temp |= sampleRate; // Mask in new OS bits
+ i2cWrite(CTRL_REG1, temp);
+}
+
+void MPL3115A2::enableEventFlags()
+{
+ i2cWrite(PT_DATA_CFG, 0x07); // Enable all three pressure and temp event flags
+}
+
+void MPL3115A2::toggleOneShot(void)
+{
+ clearRegisterBit(CTRL_REG1, 0x02); // Clear OST bit
+ setRegisterBit(CTRL_REG1, 0x02); // Set the OST bit.
+}
+
+void MPL3115A2::clearRegisterBit(const char regAddr, const char bitMask)
+{
+ char temp = i2cRead(regAddr); // Read the current register value
+ temp &= ~bitMask; // Clear the bit from the value
+ i2cWrite(regAddr, temp); // Write register value back
+}
+
+void MPL3115A2::setRegisterBit(const char regAddr, const char bitMask)
+{
+ char temp = i2cRead(regAddr); // Read the current register value
+ temp |= bitMask; // Set the bit in the value
+ i2cWrite(regAddr, temp); // Write register value back
+}
+
+char MPL3115A2::i2cRead(char regAddr)
+{
+ _i2c->start(); // Start
+ if (_i2c->write(MPL3115A2_ADDRESS)!=1) // A write to device
+ debugOut("MPL3115A2::i2cRead: Sensor failed to respond to write request at address 0x%X\r\n", MPL3115A2_ADDRESS);
+
+ if (_i2c->write(regAddr)!=1) // Register to read
+ debugOut("MPL3115A2::i2cRead: Sensor at address 0x%X did not acknowledge register 0x%X\r\n", MPL3115A2_ADDRESS, regAddr);
+
+ _i2c->start();
+ if (_i2c->write(MPL3115A2_ADDRESS|0x01)!=1) // Read from device
+ debugOut("MPL3115A2::i2cRead: Sensor failed to respond to read request at address 0x%X\r\n", MPL3115A2_ADDRESS);
+
+ char result = _i2c->read(READ_NAK); // Read the data
+ _i2c->stop();
+
+ return result;
+}
+
+void MPL3115A2::i2cWrite(char regAddr, char value)
+{
+ char cmd[2];
+ cmd[0] = regAddr;
+ cmd[1] = value;
+ if (_i2c->write(MPL3115A2_ADDRESS, cmd, 2) != 0)
+ debugOut("MPL3115A2::i2cWrite: Failed writing value (%d, 0x%X) to register 0x%X\r\n", value, regAddr);
+}
+
+void MPL3115A2::debugOut(const char * format, ...)
+{
+ if (_debug == NULL)
+ return;
+
+ va_list arg;
+ _debug->printf(format, arg);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MPL3115A2.h Tue Apr 01 21:35:23 2014 +0000
@@ -0,0 +1,172 @@
+/*
+ MPL3115A2 Barometric Pressure and Tempurature Sensor Library
+ By: Michael Lange
+ Date: March 31, 2014
+ License: This code is public domain.
+
+ This class wraps the functions of the MPL3115A2 sensor into
+ a usable class that exposes most functions to your project.
+ Functions that are not exposed can easily be added using the
+ existing functions in this library. Specifically, I did not
+ add any funtions to handle FIFO logging or the use of either
+ of the pin interrupts. This should not be too difficult to
+ add if you need those features.
+
+ The motivation here was to get a set of support classes
+ together that supported the chip and could be expanded on.
+ With this library you can extract all relevant data from the
+ sensor.
+
+ Be sure to download the DATASHEET and the App Note AN4519.
+
+ This libraray was created using the mbed NXP LPC11U24. Pins
+ p27 and p28 were used for the I2C functions. Be sure to install
+ 1K pull-up resistors on both lines. Also, if you're not using
+ the SparkFun breakout board, be sure to use the right caps on
+ the power pin. If you don't, the jitter can cause problems.
+
+ This library was inspired by the similar library available for
+ the Arduino written by Nathan Seidle at SparkFun. I copied
+ some of the number crunching routines and tried to follow his
+ style of coding for the library. That way users of Arduinos
+ cound step into this library a little easier.
+
+ */
+
+
+#ifndef MPL3115A2_H
+#define MPL3115A2_H
+
+#include "mbed.h"
+
+#include "Altitude.h" // Our classes to handle compressed data from the sensor.
+#include "Temperature.h"
+#include "Pressure.h"
+
+#define MPL3115A2_ADDRESS 0xC0 // Shifted 7-bit I2C address for sensor
+
+#define READ_ACK 1 // For mbed I2C Read method.
+#define READ_NAK 0
+
+#define MAX_DATA_READY_ATTEMPTS 512 // How many times we loop waiting for data before giving up.
+
+// DEFINE |REGISTER |RESET |RESET |TYPE |AUTO-INC |NAME/COMMENT
+// | | |STBY2ACT | |ADDRESS |
+#define STATUS 0x00 // | 0x00 | Yes | R | 0x01 | Sensor Status Register (Alias for DR_STATUS or F_STATUS)
+#define OUT_P_MSB 0x01 // | 0x00 | Yes | R | 0x02 | 0x01 | Pressure Data Out MSB (Bits 12-19 of 20-bit real-time Pressure sample | Root pointer t oPressure and Tempurature FIFO data)
+#define OUT_P_CSB 0x02 // | 0x00 | Yes | R | 0x03 | Pressure Data out CSB (Bits 0-3 of 20-bit real-time Pressure sample)
+#define OUT_P_LSB 0x03 // | 0x00 | | | 0x |
+#define OUT_T_MSB 0x04 // | 0x00 | | | 0x |
+#define OUT_T_LSB 0x05 // | 0x00 | | | 0x |
+#define DR_STATUS 0x06 // | 0x00 | | | 0x |
+#define OUT_P_DELTA_MSB 0x07 // | 0x00 | | | 0x |
+#define OUT_P_DELTA_CSB 0x08 // | 0x00 | | | 0x |
+#define OUT_P_DELTA_LSB 0x09 // | 0x00 | | | 0x |
+#define OUT_T_DELTA_MSB 0x0A // | 0x00 | | | 0x |
+#define OUT_T_DELTA_LSB 0x0B // | 0x00 | | | 0x |
+#define WHO_AM_I 0x0C // | 0xC4 | | | 0x |
+#define F_STATUS 0x0D // | 0x00 | | | 0x |
+#define F_DATA 0x0E // | 0x00 | | | 0x |
+#define F_SETUP 0x0F // | 0x00 | | | 0x |
+#define TIME_DLY 0x10 // | 0x00 | | | 0x |
+#define SYSMOD 0x11 // | 0x00 | | | 0x |
+#define INT_SOURCE 0x12 // | 0x00 | | | 0x |
+#define PT_DATA_CFG 0x13 // | 0x00 | | | 0x |
+#define BAR_IN_MSB 0x14 // | 0xC5 | | | 0x |
+#define BAR_IN_LSB 0x15 // | 0xE7 | | | 0x |
+#define P_TGT_MSB 0x16 // | 0x00 | | | 0x |
+#define P_TGT_LSB 0x17 // | 0x00 | | | 0x |
+#define T_TGT 0x18 // | 0x00 | | | 0x |
+#define P_WND_MSB 0x19 // | 0x00 | | | 0x |
+#define P_WND_LSB 0x1A // | 0x00 | | | 0x |
+#define T_WND 0x1B // | 0x00 | | | 0x |
+#define P_MIN_MSB 0x1C // | 0x00 | | | 0x |
+#define P_MIN_CSB 0x1D // | 0x00 | | | 0x |
+#define P_MIN_LSB 0x1E // | 0x00 | | | 0x |
+#define T_MIN_MSB 0x1F // | 0x00 | | | 0x |
+#define T_MIN_LSB 0x20 // | 0x00 | | | 0x |
+#define P_MAX_MSB 0x21 // | 0x00 | | | 0x |
+#define P_MAX_CSB 0x22 // | 0x00 | | | 0x |
+#define P_MAX_LSB 0x23 // | 0x00 | | | 0x |
+#define T_MAX_MSB 0x24 // | 0x00 | | | 0x |
+#define T_MAX_LSB 0x25 // | 0x00 | | | 0x |
+#define CTRL_REG1 0x26 // | 0x00 | | | 0x |
+#define CTRL_REG2 0x27 // | 0x00 | | | 0x |
+#define CTRL_REG3 0x28 // | 0x00 | | | 0x |
+#define CTRL_REG4 0x29 // | 0x00 | | | 0x |
+#define CTRL_REG5 0x2A // | 0x00 | | | 0x |
+#define OFF_P 0x2B // | 0x00 | | | 0x |
+#define OFF_T 0x2C // | 0x00 | | | 0x |
+#define OFF_H 0x2D // | 0x00 | | | 0x |
+
+class MPL3115A2
+{
+public:
+ MPL3115A2(I2C *i2c, Serial *pc = NULL);
+
+ // Call this method in main to initialize the sensor.
+ void init();
+
+ // Returns the fixed device ID number (usually equal to 0xC4).
+ char whoAmI() { return i2cRead(WHO_AM_I); }
+
+ Altitude* readAltitude(Altitude* a); // Returns the altitude object with altitude
+ Pressure* readPressure(Pressure* p); // Returns the pressure object with barometric pressure
+ Temperature* readTemperature(Temperature* t); // Returns the temperature object with temperature
+
+ // Use these methods to set the sensor's offsets to increase its accuracy. You can generally
+ // find your current altitude with a smart phone or GPS. Same goes for the temperature. For
+ // the current pressure where you are, you may need an accurate weather station. Getting the
+ // pressure from the web for your area is generally not close enough to help with calibration.
+ // You may need to play with the setting to achieve good accuracy. I found the offset steps
+ // were not 100% accurate to the datasheet and had to adjust accordingly.
+ char offsetAltitude() { return i2cRead(OFF_H); }
+ void setOffsetAltitude(const char offset) { i2cWrite(OFF_H, offset); } // -128 to 127 meters
+ char offsetPressure() { return i2cRead(OFF_P); }
+ void setOffsetPressure(const char offset) { i2cWrite(OFF_P, offset); } // 4 Pa per LSB
+ char offsetTemperature() { return i2cRead(OFF_T); }
+ void setOffsetTemperature(const char offset) { i2cWrite(OFF_T, offset); } // 0.0625ºC per LSB
+
+ void setModeStandby(); // Puts the sensor into Standby mode. Required when using methods below.
+ void setModeActive(); // Activates the sensor to start taking measurements.
+
+ // When calling any of these methods, be sure to put the sensor in standby mode first.
+ void setModeBarometer(); // Puts the sensor into barometric mode
+ void setModeAltimeter(); // Puts the sensor into altimeter mode
+ void setOversampleRate(char); // Sets the number of samples from 1 to 128.
+ void enableEventFlags(); // Sets the all event flags.
+
+private:
+ I2C *_i2c; // The I2C object we use to communicate with the sensor. It is not part
+ // of the class so that it can be shared with other peripherals on the
+ // bus.
+ Serial *_debug; // Set this in the constructor if you want the class to output debug messages.
+ // If you need to pair down your code, you can remove this and all the
+ // references to it in the code.
+
+ // Debug method that mimics the printf function, but will output nothing if _debug has not
+ // been set. This means you can safely us it in your code and nothing will happen if you don't
+ // assign the _debug object.
+ void debugOut(const char * format, ...);
+
+ // These are helper functions to SET or CLEAR bits. The mask should contain 1s in the position
+ // where the bits need to be set or cleared. One or more bits can be set or cleared this way.
+ void clearRegisterBit(const char regAddr, const char bitMask);
+ void setRegisterBit(const char regAddr, const char bitMask);
+
+ // Helper functions to check if data is ready in a register for either temperature or pressure.
+ // The pressureDataReady function works for both altitude and barometric pressure depending on
+ // the mode the sensor is in.
+ int dataReady(const char mask);
+ int pressureDataReady() { return dataReady(0x04); }
+ int temperatureDataReady() { return dataReady(0x02); }
+
+ // Called to force the sensor to take another sample
+ void toggleOneShot();
+
+ // Helper functions to read and write one value from the I2C bus using the sensor's address.
+ char i2cRead(char regAddr);
+ void i2cWrite(char regAddr, char value);
+};
+
+#endif // MPL3115A2_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Pressure.cpp Tue Apr 01 21:35:23 2014 +0000
@@ -0,0 +1,73 @@
+#include "Pressure.h"
+#include "mbed.h"
+
+Pressure::Pressure()
+{
+ _pressure = 0.0;
+
+ _compressed[0] = 0;
+ _compressed[1] = 0;
+ _compressed[2] = 0;
+}
+
+Pressure::Pressure(float a, unitsType units)
+{
+ setPressure(a, units);
+}
+
+Pressure::Pressure(const char* compressed)
+{
+ setPressure(compressed);
+}
+
+Pressure::Pressure(const char msb, const char csb, const char lsb)
+{
+ setPressure(msb, csb, lsb);
+}
+
+void Pressure::setPressure()
+{
+ setPressure(_compressed[0], _compressed[1], _compressed[2]);
+}
+
+void Pressure::setPressure(float a, unitsType units)
+{
+ // TODO:
+}
+
+void Pressure::setPressure(const char* compressed)
+{
+ setPressure(compressed[0], compressed[1], compressed[2]);
+}
+
+void Pressure::setPressure(const char msb, const char csb, const char lsb)
+{
+ long pressure_whole = (long)msb<<16 | (long)csb<<8 | (long)lsb;
+ pressure_whole >>= 6; // Pressure is an 18 bit number with 2 bits of decimal. Get rid of decimal portion.
+
+ float pressure_decimal = (float)((lsb&0x30)>>4)/4.0; // Turn it into fraction
+
+ _pressure = (float)pressure_whole + pressure_decimal;
+}
+
+float Pressure::pressure(unitsType units)
+{
+ // http://www.asknumbers.com/
+ switch (units)
+ {
+ case PSI:
+ return _pressure * 0.000145037738;
+ case INHG:
+ return _pressure * 0.00029529983071;
+ case MMHG:
+ return _pressure * 0.007500615613;
+ }
+
+ return _pressure;
+}
+
+const char* Pressure::print(unitsType units)
+{
+ sprintf(_printBuffer, "%.0f", pressure(units));
+ return _printBuffer;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Pressure.h Tue Apr 01 21:35:23 2014 +0000
@@ -0,0 +1,46 @@
+/*
+ MPL3115A2 Barometric Pressure and Tempurature Sensor Library
+ By: Michael Lange
+ Date: March 31, 2014
+ License: This code is public domain.
+
+ This class encapsulates a pressure reading from the sensor.
+
+ */
+
+
+#ifndef PRESSURE_H
+#define PRESSURE_H
+
+#include "mbed.h"
+
+class Pressure
+{
+public:
+
+ static const int size = 3;
+ enum unitsType { PASCALS, PSI, INHG, MMHG };
+
+ Pressure();
+ Pressure(float a, unitsType units = PASCALS);
+ Pressure(const char* compressed);
+ Pressure(const char msb, const char csb, const char lsb);
+
+ operator char*(void) { return _compressed; }
+ operator float(void) { return _pressure; }
+
+ float pressure(unitsType units = PASCALS);
+ void setPressure();
+ void setPressure(const char* compressed);
+ void setPressure(const char msb, const char csb, const char lsb);
+ void setPressure(float a, unitsType units = PASCALS);
+
+ const char* print(unitsType units = PASCALS);
+
+private:
+ float _pressure;
+ char _compressed[3];
+ char _printBuffer[9];
+};
+
+#endif // PRESSURE_H
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Temperature.cpp Tue Apr 01 21:35:23 2014 +0000
@@ -0,0 +1,66 @@
+#include "Temperature.h"
+#include "mbed.h"
+
+Temperature::Temperature()
+{
+ _temperature = 0.0;
+
+ _compressed[0] = 0;
+ _compressed[1] = 0;
+}
+
+Temperature::Temperature(float t, unitsType units)
+{
+ setTemperature(t, units);
+}
+
+Temperature::Temperature(const char* compressed)
+{
+ setTemperature(compressed);
+}
+
+Temperature::Temperature(const char msb, const char lsb)
+{
+ setTemperature(msb, lsb);
+}
+
+void Temperature::setTemperature()
+{
+ setTemperature(_compressed[0], _compressed[1]);
+}
+
+void Temperature::setTemperature(float t, unitsType units)
+{
+ // TODO:
+}
+
+void Temperature::setTemperature(const char* compressed)
+{
+ setTemperature(compressed[0], compressed[1]);
+}
+
+void Temperature::setTemperature(const char msb, const char lsb)
+{
+ float templsb = (lsb>>4) / 16.0; //temp, fraction of a degree
+ _temperature = (float)(msb + templsb);
+}
+
+float Temperature::temperature(unitsType units)
+{
+ switch (units)
+ {
+ case FAHRENHEIT:
+ return (_temperature * 9.0)/ 5.0 + 32.0;
+
+ case KELVIN:
+ return _temperature + 273.15;
+ }
+
+ return _temperature;
+}
+
+const char* Temperature::print(unitsType units)
+{
+ sprintf(_printBuffer, "%.1f", temperature(units));
+ return _printBuffer;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Temperature.h Tue Apr 01 21:35:23 2014 +0000
@@ -0,0 +1,46 @@
+/*
+ MPL3115A2 Barometric Pressure and Tempurature Sensor Library
+ By: Michael Lange
+ Date: March 31, 2014
+ License: This code is public domain.
+
+ This class encapsulates a temperature reading from the sensor.
+
+ */
+
+
+#ifndef TEMPERATURE_H
+#define TEMPERATURE_H
+
+#include "mbed.h"
+
+class Temperature
+{
+public:
+
+ static const int size = 2;
+ enum unitsType { CELSIUS, FAHRENHEIT, KELVIN };
+
+ Temperature();
+ Temperature(float a, unitsType units = FAHRENHEIT);
+ Temperature(const char* compressed);
+ Temperature(const char msb, const char lsb);
+
+ operator char*(void) { return _compressed; }
+ operator float(void) { return _temperature; }
+
+ float temperature(unitsType units = FAHRENHEIT);
+ void setTemperature();
+ void setTemperature(const char* compressed);
+ void setTemperature(const char msb, const char lsb);
+ void setTemperature(float a, unitsType units = FAHRENHEIT);
+
+ const char* print(unitsType units = FAHRENHEIT);
+
+private:
+ float _temperature;
+ char _compressed[2];
+ char _printBuffer[9];
+};
+
+#endif // TEMPERATURE_H
\ No newline at end of file
