aconno acnsensa project for iOS devices with iBeacon packets support.
Dependencies: LSM9DS1 Si7006A20 aconno_SEGGER_RTT aconno_bsp adc52832_common
Diff: MPL115A1/MPL115A1.cpp
- Revision:
- 3:78ceda8ef565
- Parent:
- 1:326ce5e200fb
- Child:
- 4:634796e5b8c3
--- a/MPL115A1/MPL115A1.cpp Wed Dec 13 09:46:08 2017 +0000 +++ b/MPL115A1/MPL115A1.cpp Fri Mar 02 12:37:27 2018 +0000 @@ -1,107 +1,116 @@ #include "MPL115A1.h" -float get_a0(uint16_t a0); -float get_b1(uint16_t b1); -float get_c1(uint16_t c1); -float get_c12(uint16_t c12); +#define a0FracBits 3 +#define b1FracBits 13 +#define b2FracBits 14 +#define c12FracBits 22 +#define a0FracMask 0x0007 +#define b1FracMask 0x1FFF +#define b2FracMask 0x3FFF +#define c12FracMask 0x1FFF + +float getA0(uint16_t a0); +float getB1(uint16_t b1); +float getC1(uint16_t c1); +float getC12(uint16_t c12); float getPressure(); MPL115A1::MPL115A1(SPI &spi, NRF52_DigitalOut &cs) : spi(spi), cs(cs) {} -float get_a0(uint16_t a0){ - float temp_a0; - float temp_frac; - int temp_a0_int; - uint8_t negative_flag = 0; +float getA0(uint16_t a0){ + float tempA0; + float tempFrac; + int tempA0Int; + uint8_t negativeFlag = 0; if(a0 & 0x8000){ a0 ^= 0xFFFF; // Transform from 2's complement a0 -= 1; - negative_flag = 1; + negativeFlag = 1; } - temp_a0_int = a0 & 0x7FF8; - temp_a0_int = temp_a0_int >> a0_frac_bits; + tempA0Int = a0 & 0x7FF8; + tempA0Int = tempA0Int >> a0FracBits; - temp_a0 = temp_a0_int * 1.0; // Int part - temp_frac = (1.0/(1<<3)); - temp_frac *= (a0 & a0_frac_mask); - temp_a0 = temp_a0 + temp_frac; + tempA0 = tempA0Int * 1.0; // Int part + tempFrac = (1.0/(1<<3)); + tempFrac *= (a0 & a0FracMask); + tempA0 = tempA0 + tempFrac; - if(negative_flag) temp_a0 = -temp_a0; + if(negativeFlag) tempA0 = -tempA0; - return temp_a0; + return tempA0; } -float get_b1(uint16_t b1){ - float temp_b1; - float temp_frac; - int temp_b1_int; - uint8_t negative_flag = 0; +float getB1(uint16_t b1){ + float tempB1; + float tempFrac; + int tempB1Int; + uint8_t negativeFlag = 0; if (b1 & 0x8000){ b1 ^= 0xFFFF; b1 -= 1; - negative_flag = 1; + negativeFlag = 1; } - temp_b1_int = b1 & 0x6000; - temp_b1_int = temp_b1_int >> b1_frac_bits; + tempB1Int = b1 & 0x6000; + tempB1Int = tempB1Int >> b1FracBits; - temp_b1 = temp_b1_int * 1.0; - temp_frac = (b1 & b1_frac_mask) * (1.0/(1<<b1_frac_bits)); - temp_b1 = temp_b1 + temp_frac; + tempB1 = tempB1Int * 1.0; + tempFrac = (b1 & b1FracMask) * (1.0/(1<<b1FracBits)); + tempB1 = tempB1 + tempFrac; - if(negative_flag) temp_b1 = -temp_b1; + if(negativeFlag) tempB1 = -tempB1; - return temp_b1; + return tempB1; } -float get_b2(uint16_t b2){ - float temp_b2; - float temp_frac; - int temp_b2_int; - uint8_t negative_flag = 0; +float getB2(uint16_t b2){ + float tempB2; + float tempFrac; + int tempB2Int; + uint8_t negativeFlag = 0; if (b2 & 0x8000){ b2 ^= 0xFFFF; b2 -= 1; - negative_flag = 1; + negativeFlag = 1; } - temp_b2_int = b2 & 0x4000; - temp_b2_int = temp_b2_int >> b2_frac_bits; + tempB2Int = b2 & 0x4000; + tempB2Int = tempB2Int >> b2FracBits; - temp_b2 = temp_b2_int * 1.0; - temp_frac = (b2 & b2_frac_mask) * (1.0/(1<<b2_frac_bits)); - temp_b2 = temp_b2 + temp_frac; + tempB2 = tempB2Int * 1.0; + tempFrac = (b2 & b2FracMask) * (1.0/(1<<b2FracBits)); + tempB2 = tempB2 + tempFrac; - if(negative_flag) temp_b2 = -temp_b2; + if(negativeFlag) tempB2 = -tempB2; - return temp_b2; + return tempB2; } -float get_c12(uint16_t c12){ - float temp_c12; - float temp_frac; - uint8_t negative_flag = 0; +float getC12(uint16_t c12){ + float tempC12; + float tempFrac; + uint8_t negativeFlag = 0; c12 = c12 >> 2; if (c12 & 0x2000){ c12 ^= 0xFFFF; c12 -= 1; - negative_flag = 1; + negativeFlag = 1; } - temp_c12 = 0.000000000; - temp_frac = (c12 & c12_frac_mask) * (1.0/(1<<c12_frac_bits)); - temp_c12 = temp_c12 + temp_frac; + tempC12 = 0.000000000; + tempFrac = (c12 & c12FracMask) * (1.0/(1<<c12FracBits)); + tempC12 = tempC12 + tempFrac; - if(negative_flag) temp_c12 = -temp_c12; + if(negativeFlag) tempC12 = -tempC12; - return temp_c12; + return tempC12; } float MPL115A1::getPressure(){ @@ -115,7 +124,7 @@ uint16_t padc = 0; uint16_t tadc = 0; - float a0_f, b1_f, b2_f, c12_f; + float a0F, b1F, b2F, c12F; cs = 0; spi.write(0x88); // MSB a0 a0 = spi.write(0x00); @@ -175,21 +184,21 @@ spi.write(0x00); cs = 1; - a0_f = get_a0(a0); - b1_f = get_b1(b1); - b2_f = get_b2(b2); - c12_f = get_c12(c12); + a0F = getA0(a0); + b1F = getB1(b1); + b2F = getB2(b2); + c12F = getC12(c12); /* - float c12x2 = c12_f * tadc; - float a1_f = b1_f + c12x2; - float a1x1 = a1_f * padc; - float y1 = a0_f + a1x1; - float a2x2 = b2_f * tadc; + float c12x2 = c12F * tadc; + float a1F = b1F + c12x2; + float a1x1 = a1F * padc; + float y1 = a0F + a1x1; + float a2x2 = b2F * tadc; pcomp = y1 + a2x2; */ - pcomp = a0_f + (b1_f + c12_f * tadc) * padc + b2_f * tadc; + pcomp = a0F + (b1F + c12F * tadc) * padc + b2F * tadc; pressure = pcomp * ((115-50)/(1023.0)) + 52; // Was + 50 pressure *= 10; // Calculate in hPa return pressure;