|
| 1 | +/* |
| 2 | + This file is part of the Arduino_LPS22HB library. |
| 3 | + Copyright (c) 2019 Arduino SA. All rights reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +#include <Wire.h> |
| 21 | + |
| 22 | +#include "BARO.h" |
| 23 | + |
| 24 | +#define LPS22HB_ADDRESS 0x5C |
| 25 | + |
| 26 | +#define LPS22HB_WHO_AM_I_REG 0x0f |
| 27 | +#define LPS22HB_CTRL2_REG 0x11 |
| 28 | +#define LPS22HB_STATUS_REG 0x27 |
| 29 | +#define LPS22HB_PRESS_OUT_XL_REG 0x28 |
| 30 | +#define LPS22HB_PRESS_OUT_L_REG 0x29 |
| 31 | +#define LPS22HB_PRESS_OUT_H_REG 0x2a |
| 32 | + |
| 33 | +LPS22HBClass::LPS22HBClass(TwoWire& wire) : |
| 34 | + _wire(&wire) |
| 35 | +{ |
| 36 | +} |
| 37 | + |
| 38 | +int LPS22HBClass::begin() |
| 39 | +{ |
| 40 | + _wire->begin(); |
| 41 | + |
| 42 | + if (i2cRead(LPS22HB_WHO_AM_I_REG) != 0xb1) { |
| 43 | + end(); |
| 44 | + return 0; |
| 45 | + } |
| 46 | + |
| 47 | + return 1; |
| 48 | +} |
| 49 | + |
| 50 | +void LPS22HBClass::end() |
| 51 | +{ |
| 52 | + _wire->end(); |
| 53 | +} |
| 54 | + |
| 55 | +float LPS22HBClass::readPressure(int units) |
| 56 | +{ |
| 57 | + // trigger one shot |
| 58 | + i2cWrite(LPS22HB_CTRL2_REG, 0x01); |
| 59 | + |
| 60 | + // wait for completion |
| 61 | + while ((i2cRead(LPS22HB_STATUS_REG) & 0x02) == 0) { |
| 62 | + yield(); |
| 63 | + } |
| 64 | + |
| 65 | + float reading = (i2cRead(LPS22HB_PRESS_OUT_XL_REG) | |
| 66 | + (i2cRead(LPS22HB_PRESS_OUT_L_REG) << 8) | |
| 67 | + (i2cRead(LPS22HB_PRESS_OUT_H_REG) << 16)) / 40960.0; |
| 68 | + |
| 69 | + if (units == MILLIBAR) { // 1 kPa = 10 MILLIBAR |
| 70 | + return reading * 10; |
| 71 | + } else if (units == PSI) { // 1 kPa = 0.145038 PSI |
| 72 | + return reading * 0.145038; |
| 73 | + } else { |
| 74 | + return reading; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +int LPS22HBClass::i2cRead(uint8_t reg) |
| 79 | +{ |
| 80 | + _wire->beginTransmission(LPS22HB_ADDRESS); |
| 81 | + _wire->write(reg); |
| 82 | + if (_wire->endTransmission(false) != 0) { |
| 83 | + return -1; |
| 84 | + } |
| 85 | + |
| 86 | + if (_wire->requestFrom(LPS22HB_ADDRESS, 1) != 1) { |
| 87 | + return -1; |
| 88 | + } |
| 89 | + |
| 90 | + return _wire->read(); |
| 91 | +} |
| 92 | + |
| 93 | +int LPS22HBClass::i2cWrite(uint8_t reg, uint8_t val) |
| 94 | +{ |
| 95 | + _wire->beginTransmission(LPS22HB_ADDRESS); |
| 96 | + _wire->write(reg); |
| 97 | + _wire->write(val); |
| 98 | + if (_wire->endTransmission() != 0) { |
| 99 | + return 0; |
| 100 | + } |
| 101 | + |
| 102 | + return 1; |
| 103 | +} |
| 104 | + |
| 105 | +LPS22HBClass BARO(Wire1); |
0 commit comments