2929 .setOversampleRate(byte) Sets the # of samples from 1 to 128. See datasheet.
3030 .enableEventFlags() Sets the fundamental event flags. Required during setup.
3131
32- */
32+ Updated by PaulZC: October 19th, 2019
33+
34+ */
3335
3436#include < Wire.h>
3537
@@ -43,9 +45,11 @@ MPL3115A2::MPL3115A2()
4345// Begin
4446/* ******************************************************************************************/
4547// Start I2C communication
46- void MPL3115A2::begin (void )
48+ void MPL3115A2::begin (TwoWire &wirePort, uint8_t deviceAddress )
4749{
48- Wire.begin ();
50+ // Let's assume that Wire.begin(); has been done elsewhere
51+ _i2cPort = &wirePort;
52+ _I2Caddress = deviceAddress;
4953}
5054
5155
@@ -64,17 +68,17 @@ float MPL3115A2::readAltitude()
6468 }
6569
6670 // Read pressure registers
67- Wire. beginTransmission (MPL3115A2_ADDRESS );
68- Wire. write (OUT_P_MSB); // Address of data to get
69- Wire. endTransmission (false ); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
70- if (Wire. requestFrom (MPL3115A2_ADDRESS , 3 ) != 3 ) { // Request three bytes
71+ _i2cPort-> beginTransmission (_I2Caddress );
72+ _i2cPort-> write (OUT_P_MSB); // Address of data to get
73+ _i2cPort-> endTransmission (false ); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
74+ if (_i2cPort-> requestFrom (_I2Caddress , 3 ) != 3 ) { // Request three bytes
7175 return -999 ;
7276 }
7377
7478 byte msb, csb, lsb;
75- msb = Wire. read ();
76- csb = Wire. read ();
77- lsb = Wire. read ();
79+ msb = _i2cPort-> read ();
80+ csb = _i2cPort-> read ();
81+ lsb = _i2cPort-> read ();
7882
7983 // The least significant bytes l_altitude and l_temp are 4-bit,
8084 // fractional values, so you must cast the calulation in (float),
@@ -110,25 +114,25 @@ float MPL3115A2::readPressure()
110114 }
111115
112116 // Read pressure registers
113- Wire. beginTransmission (MPL3115A2_ADDRESS );
114- Wire. write (OUT_P_MSB); // Address of data to get
115- Wire. endTransmission (false ); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
116- if (Wire. requestFrom (MPL3115A2_ADDRESS , 3 ) != 3 ) { // Request three bytes
117+ _i2cPort-> beginTransmission (_I2Caddress );
118+ _i2cPort-> write (OUT_P_MSB); // Address of data to get
119+ _i2cPort-> endTransmission (false ); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
120+ if (_i2cPort-> requestFrom (_I2Caddress , 3 ) != 3 ) { // Request three bytes
117121 return -999 ;
118122 }
119123
120124 byte msb, csb, lsb;
121- msb = Wire. read ();
122- csb = Wire. read ();
123- lsb = Wire. read ();
125+ msb = _i2cPort-> read ();
126+ csb = _i2cPort-> read ();
127+ lsb = _i2cPort-> read ();
124128
125129 toggleOneShot (); // Toggle the OST bit causing the sensor to immediately take another reading
126130
127131 // Pressure comes back as a left shifted 20 bit number
128132 long pressure_whole = (long )msb<<16 | (long )csb<<8 | (long )lsb;
129133 pressure_whole >>= 6 ; // Pressure is an 18 bit number with 2 bits of decimal. Get rid of decimal portion.
130134
131- lsb &= B00110000; // Bits 5/4 represent the fractional component
135+ lsb &= 0x3F ; // B00110000; //Bits 5/4 represent the fractional component
132136 lsb >>= 4 ; // Get it right aligned
133137 float pressure_decimal = (float )lsb/4.0 ; // Turn it into fraction
134138
@@ -150,21 +154,21 @@ float MPL3115A2::readTemp()
150154 }
151155
152156 // Read temperature registers
153- Wire. beginTransmission (MPL3115A2_ADDRESS );
154- Wire. write (OUT_T_MSB); // Address of data to get
155- Wire. endTransmission (false ); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
156- if (Wire. requestFrom (MPL3115A2_ADDRESS , 2 ) != 2 ) { // Request two bytes
157+ _i2cPort-> beginTransmission (_I2Caddress );
158+ _i2cPort-> write (OUT_T_MSB); // Address of data to get
159+ _i2cPort-> endTransmission (false ); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
160+ if (_i2cPort-> requestFrom (_I2Caddress , 2 ) != 2 ) { // Request two bytes
157161 return -999 ;
158162 }
159163
160164 byte msb, lsb;
161- msb = Wire. read ();
162- lsb = Wire. read ();
165+ msb = _i2cPort-> read ();
166+ lsb = _i2cPort-> read ();
163167
164168 toggleOneShot (); // Toggle the OST bit causing the sensor to immediately take another reading
165169
166170 // Negative temperature fix by D.D.G.
167- word foo = 0 ;
171+ uint16_t foo = 0 ;
168172 bool negSign = false ;
169173
170174 // Check for 2s compliment
@@ -241,7 +245,7 @@ void MPL3115A2::setOversampleRate(byte sampleRate)
241245 sampleRate <<= 3 ; // Align it for the CTRL_REG1 register
242246
243247 byte tempSetting = IIC_Read (CTRL_REG1); // Read current settings
244- tempSetting &= B11000111; // Clear out old OS bits
248+ tempSetting &= 0xc7 ; // B11000111; //Clear out old OS bits
245249 tempSetting |= sampleRate; // Mask in new OS bits
246250 IIC_Write (CTRL_REG1, tempSetting);
247251}
@@ -271,19 +275,19 @@ void MPL3115A2::toggleOneShot(void)
271275byte MPL3115A2::IIC_Read (byte regAddr)
272276{
273277 // This function reads one byte over IIC
274- Wire. beginTransmission (MPL3115A2_ADDRESS );
275- Wire. write (regAddr); // Address of CTRL_REG1
276- Wire. endTransmission (false ); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
277- Wire. requestFrom (MPL3115A2_ADDRESS , 1 ); // Request the data...
278- return Wire. read ();
278+ _i2cPort-> beginTransmission (_I2Caddress );
279+ _i2cPort-> write (regAddr); // Address of CTRL_REG1
280+ _i2cPort-> endTransmission (false ); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
281+ _i2cPort-> requestFrom (_I2Caddress , 1 ); // Request the data...
282+ return _i2cPort-> read ();
279283}
280284
281285void MPL3115A2::IIC_Write (byte regAddr, byte value)
282286{
283287 // This function writes one byto over IIC
284- Wire. beginTransmission (MPL3115A2_ADDRESS );
285- Wire. write (regAddr);
286- Wire. write (value);
287- Wire. endTransmission (true );
288+ _i2cPort-> beginTransmission (_I2Caddress );
289+ _i2cPort-> write (regAddr);
290+ _i2cPort-> write (value);
291+ _i2cPort-> endTransmission (true );
288292}
289293
0 commit comments