|
| 1 | +// |
| 2 | +// FILE: I2C_ABP2.cpp |
| 3 | +// AUTHOR: Rob Tillaart |
| 4 | +// VERSION: 0.1.0 |
| 5 | +// DATE: 2026-05-27 |
| 6 | +// PURPOSE: Arduino library for the Honeywell I2C ABP2 pressure sensors. |
| 7 | +// URL: https://github.com/RobTillaart/I2C_ABP2 |
| 8 | +// https://github.com/RobTillaart/pressure (conversions) |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +#include "I2C_ABP2.h" |
| 13 | + |
| 14 | + |
| 15 | +I2C_ABP2::I2C_ABP2(uint8_t address, TwoWire *wire) |
| 16 | +{ |
| 17 | + _address = address; |
| 18 | + _wire = wire; |
| 19 | + _error = 0; |
| 20 | + _lastRead = 0; |
| 21 | + _Bar = 0; |
| 22 | + _celsius = 0; |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +bool I2C_ABP2::begin(float minBar, float maxBar) |
| 27 | +{ |
| 28 | + _minBar = minBar; |
| 29 | + _maxBar = maxBar; |
| 30 | + // reset variables |
| 31 | + _error = 0; |
| 32 | + _lastRead = 0; |
| 33 | + _Bar = 0; |
| 34 | + _celsius = 0; |
| 35 | + |
| 36 | + if (! isConnected()) |
| 37 | + { |
| 38 | + return false; |
| 39 | + } |
| 40 | + return true; |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +bool I2C_ABP2::isConnected() |
| 45 | +{ |
| 46 | + _wire->beginTransmission(_address); |
| 47 | + return (_wire->endTransmission() == 0); |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +uint8_t I2C_ABP2::getAddress() |
| 52 | +{ |
| 53 | + return _address; |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +///////////////////////////////////////////// |
| 58 | +// |
| 59 | +// ASYNC INTERFACE |
| 60 | +// |
| 61 | +// par 6.8 datasheet |
| 62 | +// split into an ASYNC interface |
| 63 | +// |
| 64 | +int I2C_ABP2::request() |
| 65 | +{ |
| 66 | + uint8_t readCommand[3] = { 0xAA, 0x00, 0x00 }; |
| 67 | + _wire->beginTransmission(_address); |
| 68 | + _wire->write(readCommand, 3); |
| 69 | + _error = _wire->endTransmission(); |
| 70 | + if (_error != 0) return _error; |
| 71 | + |
| 72 | + _error = I2C_ABP2_OK; |
| 73 | + return _error; |
| 74 | +} |
| 75 | + |
| 76 | +int I2C_ABP2::read() |
| 77 | +{ |
| 78 | + // read data |
| 79 | + if (7 != _wire->requestFrom((uint8_t)_address, (uint8_t)7)) |
| 80 | + { |
| 81 | + return I2C_ABP2_REQUEST_ERROR; |
| 82 | + } |
| 83 | + // get state |
| 84 | + _state = _wire->read(); |
| 85 | + // get pressure |
| 86 | + uint32_t rawP = 0; |
| 87 | + for (int i = 0; i < 3; i++) |
| 88 | + { |
| 89 | + rawP <<= 8; |
| 90 | + rawP += _wire->read(); |
| 91 | + } |
| 92 | + const float maxCnt = 15099494.0; |
| 93 | + const float minCnt = 1677722.0; |
| 94 | + _Bar = ((rawP - minCnt) * (_maxBar - _minBar)) / (maxCnt - minCnt) + _minBar; |
| 95 | + // get temperature |
| 96 | + uint32_t rawT = 0; |
| 97 | + for (int i = 0; i < 3; i++) |
| 98 | + { |
| 99 | + rawT <<= 8; |
| 100 | + rawT += _wire->read(); |
| 101 | + } |
| 102 | + _celsius = rawT * (200.0 / 16777215.0) - 50; |
| 103 | + _lastRead = millis(); |
| 104 | + _error = I2C_ABP2_OK; |
| 105 | + return _error; |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +///////////////////////////////////////////// |
| 110 | +// |
| 111 | +// DEBUG |
| 112 | +// |
| 113 | +int I2C_ABP2::getLastError() |
| 114 | +{ |
| 115 | + int e = _error; |
| 116 | + _error = 0; |
| 117 | + return e; |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +/////////////////////////////////////////////// |
| 122 | +// |
| 123 | +// PRIVATE |
| 124 | +// |
| 125 | + |
| 126 | + |
| 127 | +// -- END OF FILE -- |
| 128 | + |
0 commit comments