I2C Temprature Sensor Progemm MPL3115A2
Dependents: I2C_Temprature_raspiboard
Fork of MPL3115A2 by
Pressure.cpp@0:beb43bc3d6d4, 2014-04-01 (annotated)
- Committer:
- sophtware
- Date:
- Tue Apr 01 21:35:23 2014 +0000
- Revision:
- 0:beb43bc3d6d4
Initial Checkin
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sophtware | 0:beb43bc3d6d4 | 1 | #include "Pressure.h" |
sophtware | 0:beb43bc3d6d4 | 2 | #include "mbed.h" |
sophtware | 0:beb43bc3d6d4 | 3 | |
sophtware | 0:beb43bc3d6d4 | 4 | Pressure::Pressure() |
sophtware | 0:beb43bc3d6d4 | 5 | { |
sophtware | 0:beb43bc3d6d4 | 6 | _pressure = 0.0; |
sophtware | 0:beb43bc3d6d4 | 7 | |
sophtware | 0:beb43bc3d6d4 | 8 | _compressed[0] = 0; |
sophtware | 0:beb43bc3d6d4 | 9 | _compressed[1] = 0; |
sophtware | 0:beb43bc3d6d4 | 10 | _compressed[2] = 0; |
sophtware | 0:beb43bc3d6d4 | 11 | } |
sophtware | 0:beb43bc3d6d4 | 12 | |
sophtware | 0:beb43bc3d6d4 | 13 | Pressure::Pressure(float a, unitsType units) |
sophtware | 0:beb43bc3d6d4 | 14 | { |
sophtware | 0:beb43bc3d6d4 | 15 | setPressure(a, units); |
sophtware | 0:beb43bc3d6d4 | 16 | } |
sophtware | 0:beb43bc3d6d4 | 17 | |
sophtware | 0:beb43bc3d6d4 | 18 | Pressure::Pressure(const char* compressed) |
sophtware | 0:beb43bc3d6d4 | 19 | { |
sophtware | 0:beb43bc3d6d4 | 20 | setPressure(compressed); |
sophtware | 0:beb43bc3d6d4 | 21 | } |
sophtware | 0:beb43bc3d6d4 | 22 | |
sophtware | 0:beb43bc3d6d4 | 23 | Pressure::Pressure(const char msb, const char csb, const char lsb) |
sophtware | 0:beb43bc3d6d4 | 24 | { |
sophtware | 0:beb43bc3d6d4 | 25 | setPressure(msb, csb, lsb); |
sophtware | 0:beb43bc3d6d4 | 26 | } |
sophtware | 0:beb43bc3d6d4 | 27 | |
sophtware | 0:beb43bc3d6d4 | 28 | void Pressure::setPressure() |
sophtware | 0:beb43bc3d6d4 | 29 | { |
sophtware | 0:beb43bc3d6d4 | 30 | setPressure(_compressed[0], _compressed[1], _compressed[2]); |
sophtware | 0:beb43bc3d6d4 | 31 | } |
sophtware | 0:beb43bc3d6d4 | 32 | |
sophtware | 0:beb43bc3d6d4 | 33 | void Pressure::setPressure(float a, unitsType units) |
sophtware | 0:beb43bc3d6d4 | 34 | { |
sophtware | 0:beb43bc3d6d4 | 35 | // TODO: |
sophtware | 0:beb43bc3d6d4 | 36 | } |
sophtware | 0:beb43bc3d6d4 | 37 | |
sophtware | 0:beb43bc3d6d4 | 38 | void Pressure::setPressure(const char* compressed) |
sophtware | 0:beb43bc3d6d4 | 39 | { |
sophtware | 0:beb43bc3d6d4 | 40 | setPressure(compressed[0], compressed[1], compressed[2]); |
sophtware | 0:beb43bc3d6d4 | 41 | } |
sophtware | 0:beb43bc3d6d4 | 42 | |
sophtware | 0:beb43bc3d6d4 | 43 | void Pressure::setPressure(const char msb, const char csb, const char lsb) |
sophtware | 0:beb43bc3d6d4 | 44 | { |
sophtware | 0:beb43bc3d6d4 | 45 | long pressure_whole = (long)msb<<16 | (long)csb<<8 | (long)lsb; |
sophtware | 0:beb43bc3d6d4 | 46 | pressure_whole >>= 6; // Pressure is an 18 bit number with 2 bits of decimal. Get rid of decimal portion. |
sophtware | 0:beb43bc3d6d4 | 47 | |
sophtware | 0:beb43bc3d6d4 | 48 | float pressure_decimal = (float)((lsb&0x30)>>4)/4.0; // Turn it into fraction |
sophtware | 0:beb43bc3d6d4 | 49 | |
sophtware | 0:beb43bc3d6d4 | 50 | _pressure = (float)pressure_whole + pressure_decimal; |
sophtware | 0:beb43bc3d6d4 | 51 | } |
sophtware | 0:beb43bc3d6d4 | 52 | |
sophtware | 0:beb43bc3d6d4 | 53 | float Pressure::pressure(unitsType units) |
sophtware | 0:beb43bc3d6d4 | 54 | { |
sophtware | 0:beb43bc3d6d4 | 55 | // http://www.asknumbers.com/ |
sophtware | 0:beb43bc3d6d4 | 56 | switch (units) |
sophtware | 0:beb43bc3d6d4 | 57 | { |
sophtware | 0:beb43bc3d6d4 | 58 | case PSI: |
sophtware | 0:beb43bc3d6d4 | 59 | return _pressure * 0.000145037738; |
sophtware | 0:beb43bc3d6d4 | 60 | case INHG: |
sophtware | 0:beb43bc3d6d4 | 61 | return _pressure * 0.00029529983071; |
sophtware | 0:beb43bc3d6d4 | 62 | case MMHG: |
sophtware | 0:beb43bc3d6d4 | 63 | return _pressure * 0.007500615613; |
sophtware | 0:beb43bc3d6d4 | 64 | } |
sophtware | 0:beb43bc3d6d4 | 65 | |
sophtware | 0:beb43bc3d6d4 | 66 | return _pressure; |
sophtware | 0:beb43bc3d6d4 | 67 | } |
sophtware | 0:beb43bc3d6d4 | 68 | |
sophtware | 0:beb43bc3d6d4 | 69 | const char* Pressure::print(unitsType units) |
sophtware | 0:beb43bc3d6d4 | 70 | { |
sophtware | 0:beb43bc3d6d4 | 71 | sprintf(_printBuffer, "%.0f", pressure(units)); |
sophtware | 0:beb43bc3d6d4 | 72 | return _printBuffer; |
sophtware | 0:beb43bc3d6d4 | 73 | } |