This is a simple wrapper class for the MPL3115A5 pressure and temperature sensor. It allows either single readings for continuous readings using polling or interrupts. It also contains a wrapper class for mbed-rpc.

Committer:
rhourahane
Date:
Sun Mar 22 20:55:38 2015 +0000
Revision:
0:b12a7d396be9
Initial revision.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rhourahane 0:b12a7d396be9 1 #ifndef __MPL3115A2_H__
rhourahane 0:b12a7d396be9 2 #define __MPL3115A2_H__
rhourahane 0:b12a7d396be9 3 #include <mbed.h>
rhourahane 0:b12a7d396be9 4
rhourahane 0:b12a7d396be9 5 /// Class to provide simple control of the Freescale MPL311A2 pressure and
rhourahane 0:b12a7d396be9 6 /// temperature sensor. The class allows the user to read the pressure as
rhourahane 0:b12a7d396be9 7 /// barometric, altimeter or raw and the temperature in Celsius.
rhourahane 0:b12a7d396be9 8 ///
rhourahane 0:b12a7d396be9 9 /// Reading can be taken either on demand or continuously using either polling
rhourahane 0:b12a7d396be9 10 /// or interrupts to wait for the next reading.
rhourahane 0:b12a7d396be9 11 ///
rhourahane 0:b12a7d396be9 12 /// The correct modes should be get before either calling getReadings for
rhourahane 0:b12a7d396be9 13 /// single readings or activate to start continuous readings.
rhourahane 0:b12a7d396be9 14 class MPL3115A2 {
rhourahane 0:b12a7d396be9 15 public:
rhourahane 0:b12a7d396be9 16 /// Enum for how the pressure data is to be read.
rhourahane 0:b12a7d396be9 17 enum DataMode {
rhourahane 0:b12a7d396be9 18 BarometricMode, ///< Barometric presure in Pascals
rhourahane 0:b12a7d396be9 19 AltimeterMode, ///< Altitude in meters
rhourahane 0:b12a7d396be9 20 RawMode ///< Raw values from internal DACs
rhourahane 0:b12a7d396be9 21 };
rhourahane 0:b12a7d396be9 22
rhourahane 0:b12a7d396be9 23 /// Enum for how consecutive readings are made
rhourahane 0:b12a7d396be9 24 enum ReadMode {
rhourahane 0:b12a7d396be9 25 Single, ///< Make a single reading when calling getReadings, chip remains in standby the rest of the time
rhourahane 0:b12a7d396be9 26 Polling, ///< Make consecutive readings by polling isDataReady, chip is active all the time.
rhourahane 0:b12a7d396be9 27 Interupt ///< Make consecutive readings with an interrupt raised when data is ready, chip is active all the time.
rhourahane 0:b12a7d396be9 28 };
rhourahane 0:b12a7d396be9 29
rhourahane 0:b12a7d396be9 30 /// Enum to allow specifying which of the chips two interrupt pins to use.
rhourahane 0:b12a7d396be9 31 enum InterruptPin {
rhourahane 0:b12a7d396be9 32 INT1, ///< Pin called INT1 on chip
rhourahane 0:b12a7d396be9 33 INT2 ///< Pin called INT2 on chip
rhourahane 0:b12a7d396be9 34 };
rhourahane 0:b12a7d396be9 35
rhourahane 0:b12a7d396be9 36 /// Create a new instance of the class using the specified pins for the I2C bus.
rhourahane 0:b12a7d396be9 37 /// Once initialised the chip is in standby mode.
rhourahane 0:b12a7d396be9 38 /// @param sda The pin name for the sda line of the I2C bus.
rhourahane 0:b12a7d396be9 39 /// @param scl The pin name for the scl line of the I2C bus.
rhourahane 0:b12a7d396be9 40 MPL3115A2(PinName sda, PinName scl);
rhourahane 0:b12a7d396be9 41
rhourahane 0:b12a7d396be9 42 /// Read the chip id, this is a fixed value of 0xC4.
rhourahane 0:b12a7d396be9 43 /// @returns Chip id.
rhourahane 0:b12a7d396be9 44 int getId(void);
rhourahane 0:b12a7d396be9 45
rhourahane 0:b12a7d396be9 46 /// Set the how the pressure is read. The default is BarometricMode if to
rhourahane 0:b12a7d396be9 47 /// be change then is must be called before getReadings or activate.
rhourahane 0:b12a7d396be9 48 /// @param mode New mode to read pressure in.
rhourahane 0:b12a7d396be9 49 void setDataMode(DataMode mode);
rhourahane 0:b12a7d396be9 50
rhourahane 0:b12a7d396be9 51 /// Get the current setting for the pressure reading mode.
rhourahane 0:b12a7d396be9 52 /// @returns Current pressure reading mode.
rhourahane 0:b12a7d396be9 53 DataMode getDataMode(void);
rhourahane 0:b12a7d396be9 54
rhourahane 0:b12a7d396be9 55 /// Set the how get continuous reading. The default is Single if to
rhourahane 0:b12a7d396be9 56 /// be change then is must be called before getReadings or activate.
rhourahane 0:b12a7d396be9 57 /// @param mode New continuous reading mode.
rhourahane 0:b12a7d396be9 58 void setReadMode(ReadMode mode);
rhourahane 0:b12a7d396be9 59
rhourahane 0:b12a7d396be9 60 /// Get the current setting for the continuous reading mode.
rhourahane 0:b12a7d396be9 61 /// @returns Current continuous reading mode.
rhourahane 0:b12a7d396be9 62 ReadMode getReadMode(void);
rhourahane 0:b12a7d396be9 63
rhourahane 0:b12a7d396be9 64 /// Set the number of samples to be used for each reading.
rhourahane 0:b12a7d396be9 65 /// The number must be a power of two between 1 and 128.
rhourahane 0:b12a7d396be9 66 /// @param samples Number of samples per reading.
rhourahane 0:b12a7d396be9 67 void setOverSampling(int samples);
rhourahane 0:b12a7d396be9 68
rhourahane 0:b12a7d396be9 69 /// Get the number of samples to be used for each reading.
rhourahane 0:b12a7d396be9 70 /// @returns Number of samples per reading.
rhourahane 0:b12a7d396be9 71 int getOverSampling(void);
rhourahane 0:b12a7d396be9 72
rhourahane 0:b12a7d396be9 73 /// Sets the number of seconds between consecutive readings.
rhourahane 0:b12a7d396be9 74 /// The number must be a power of two between 1 and 32768.
rhourahane 0:b12a7d396be9 75 /// @param period Number of seconds between consecutive readings.
rhourahane 0:b12a7d396be9 76 void setSamplingPeriod(int period);
rhourahane 0:b12a7d396be9 77
rhourahane 0:b12a7d396be9 78 /// Get the number of seconds between consecutive readings.
rhourahane 0:b12a7d396be9 79 /// @returns Seconds between samples.
rhourahane 0:b12a7d396be9 80 int getSamplingPeriod(void);
rhourahane 0:b12a7d396be9 81
rhourahane 0:b12a7d396be9 82 /// Set which of the chips two interrupt pins should be used to signal
rhourahane 0:b12a7d396be9 83 /// that the data is ready. It is up to the user to attach the interrupt
rhourahane 0:b12a7d396be9 84 /// handler to the correct MCU pin and then call either getStatus or
rhourahane 0:b12a7d396be9 85 /// getReadings within the handler to clear the interrupt.
rhourahane 0:b12a7d396be9 86 /// @param intPin Chip interrupt pin to signal data ready interrupts.
rhourahane 0:b12a7d396be9 87 void setDataReadyInt(InterruptPin intPin);
rhourahane 0:b12a7d396be9 88
rhourahane 0:b12a7d396be9 89 /// Get the configured interrupt pin used to signal that the data
rhourahane 0:b12a7d396be9 90 /// is ready.
rhourahane 0:b12a7d396be9 91 /// @returns Current data ready interrupt pin.
rhourahane 0:b12a7d396be9 92 InterruptPin getDataReadyInt(void);
rhourahane 0:b12a7d396be9 93
rhourahane 0:b12a7d396be9 94 /// Read the chips STATUS register. The register contains data ready and
rhourahane 0:b12a7d396be9 95 /// overwrite flags.
rhourahane 0:b12a7d396be9 96 /// @returns Value of the STATUS register.
rhourahane 0:b12a7d396be9 97 int getStatus(void);
rhourahane 0:b12a7d396be9 98
rhourahane 0:b12a7d396be9 99 /// Returns true if there is data available to read.
rhourahane 0:b12a7d396be9 100 /// @returns True is data is available to read otherwise false.
rhourahane 0:b12a7d396be9 101 bool isDataReady(void);
rhourahane 0:b12a7d396be9 102
rhourahane 0:b12a7d396be9 103 /// Put the chip into active state to allow taking of consecutive
rhourahane 0:b12a7d396be9 104 /// samples. How often the samples are taken depends on the sampling
rhourahane 0:b12a7d396be9 105 /// period, the maximum frequency is every one second and the minimum
rhourahane 0:b12a7d396be9 106 /// every 9 hours.
rhourahane 0:b12a7d396be9 107 void activate();
rhourahane 0:b12a7d396be9 108
rhourahane 0:b12a7d396be9 109 /// Gives the active state of the chip
rhourahane 0:b12a7d396be9 110 /// @returns True if the chip is in the active state otherwise false.
rhourahane 0:b12a7d396be9 111 bool isActive();
rhourahane 0:b12a7d396be9 112
rhourahane 0:b12a7d396be9 113 /// Puts the chip into the standby state and stops automatic readings.
rhourahane 0:b12a7d396be9 114 void standby();
rhourahane 0:b12a7d396be9 115
rhourahane 0:b12a7d396be9 116 /// Wait for and get the next readings for pressure and temperature.
rhourahane 0:b12a7d396be9 117 /// If the reading mode is Single then the chip is setup and a wait
rhourahane 0:b12a7d396be9 118 /// loop entered until the data is available.
rhourahane 0:b12a7d396be9 119 /// If the reading mode is Polling then the wait loop is entered until
rhourahane 0:b12a7d396be9 120 /// the next reading is available.
rhourahane 0:b12a7d396be9 121 /// If the reading mode is Interrupt then the wait loop will exit imediately
rhourahane 0:b12a7d396be9 122 /// as the data is already available. This will clear the interrupt.
rhourahane 0:b12a7d396be9 123 /// @param pres Reference to variable to hold the pressure reading in Pascals
rhourahane 0:b12a7d396be9 124 /// meters depending on the data mode.
rhourahane 0:b12a7d396be9 125 /// @param temp Reference to variable to hold the temperature reading in
rhourahane 0:b12a7d396be9 126 /// Celsius.
rhourahane 0:b12a7d396be9 127 /// @returns True is the reading was sucessful otherwise false. The reading will
rhourahane 0:b12a7d396be9 128 /// be unsuccessful if the data mode is raw.
rhourahane 0:b12a7d396be9 129 bool getReadings(float &pres, float &temp);
rhourahane 0:b12a7d396be9 130
rhourahane 0:b12a7d396be9 131 private:
rhourahane 0:b12a7d396be9 132 float convTemperature(char *data);
rhourahane 0:b12a7d396be9 133 float convAltimeter(char *data);
rhourahane 0:b12a7d396be9 134 float convPressure(char *data);
rhourahane 0:b12a7d396be9 135
rhourahane 0:b12a7d396be9 136 void readRegs(char addr, char *data, int len);
rhourahane 0:b12a7d396be9 137 void writeRegs(const char *data, int len);
rhourahane 0:b12a7d396be9 138
rhourahane 0:b12a7d396be9 139 private:
rhourahane 0:b12a7d396be9 140 I2C m_i2c;
rhourahane 0:b12a7d396be9 141 int m_addr;
rhourahane 0:b12a7d396be9 142
rhourahane 0:b12a7d396be9 143 ReadMode m_readMode;
rhourahane 0:b12a7d396be9 144 DataMode m_dataMode;
rhourahane 0:b12a7d396be9 145 int m_overSample;
rhourahane 0:b12a7d396be9 146 int m_samplePeriod;
rhourahane 0:b12a7d396be9 147 InterruptPin m_dataReadInt;
rhourahane 0:b12a7d396be9 148 };
rhourahane 0:b12a7d396be9 149
rhourahane 0:b12a7d396be9 150 #endif