2021-01-29 06:31:58 -05:00
|
|
|
//
|
|
|
|
// FILE: I2C_ASDX.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2021-12-19 11:54:58 -05:00
|
|
|
// VERSION: 0.3.1
|
2021-01-29 06:31:58 -05:00
|
|
|
// PURPOSE: I2C_asdx library for Arduino.
|
|
|
|
// URL: https://github.com/RobTillaart/I2C_ASDX
|
|
|
|
//
|
|
|
|
// HISTORY:
|
|
|
|
// 0.1.0 2013-11-14 initial version
|
|
|
|
// 0.2.0 2020-03-18 refactor
|
|
|
|
// 0.2.1 2020-07-04 add getBar(), getMilliBar(), getPSI()
|
2021-12-19 11:54:58 -05:00
|
|
|
// 0.2.2 2020-12-29 add Arduino-CI + unit test (minimal), keywords update.
|
2021-06-06 13:59:53 -04:00
|
|
|
// 0.2.3 2021-06-06 add different units to read pressure
|
2021-06-07 05:49:28 -04:00
|
|
|
// 0.3.0 2021-06-07 add multiWire interface
|
2021-12-19 11:54:58 -05:00
|
|
|
// 0.3.1 2021-12-19 update library.json, license, minor edits
|
2021-06-07 05:49:28 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
#include "I2C_ASDX.h"
|
|
|
|
|
2021-06-07 05:49:28 -04:00
|
|
|
|
|
|
|
I2C_ASDX::I2C_ASDX(uint8_t address, uint8_t psi, TwoWire *wire)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-06-07 05:49:28 -04:00
|
|
|
reset();
|
|
|
|
_address = address;
|
|
|
|
_wire = wire;
|
2021-01-29 06:31:58 -05:00
|
|
|
_maxPressure = 0;
|
2021-06-07 05:49:28 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
if ((psi == 100) || (psi == 60) || (psi == 30) ||
|
|
|
|
(psi == 15) || (psi == 05) || (psi == 01))
|
|
|
|
{
|
|
|
|
_maxPressure = psi * PSI2MILLIBAR;
|
|
|
|
}
|
2021-06-07 05:49:28 -04:00
|
|
|
_state = I2C_ASDX_INIT;
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-06-07 05:49:28 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#if defined (ESP8266) || defined(ESP32)
|
2021-06-07 05:49:28 -04:00
|
|
|
bool I2C_ASDX::begin(uint8_t sda, uint8_t scl)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
reset();
|
2021-06-07 05:49:28 -04:00
|
|
|
_wire = &Wire;
|
|
|
|
_wire->begin(sda, scl);
|
|
|
|
if (! isConnected())
|
|
|
|
{
|
|
|
|
_state = I2C_ASDX_CONNECT_ERROR;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_state = I2C_ASDX_OK;
|
|
|
|
return true;
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-07 05:49:28 -04:00
|
|
|
|
|
|
|
bool I2C_ASDX::begin()
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
reset();
|
2021-06-07 05:49:28 -04:00
|
|
|
_wire->begin();
|
|
|
|
if (! isConnected())
|
|
|
|
{
|
|
|
|
_state = I2C_ASDX_CONNECT_ERROR;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_state = I2C_ASDX_OK;
|
|
|
|
return true;
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-06-07 05:49:28 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void I2C_ASDX::reset()
|
|
|
|
{
|
|
|
|
_errorCount = 0;
|
|
|
|
_lastRead = 0;
|
|
|
|
_pressure = 0;
|
|
|
|
}
|
|
|
|
|
2021-06-07 05:49:28 -04:00
|
|
|
|
2021-06-06 13:59:53 -04:00
|
|
|
bool I2C_ASDX::isConnected()
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-06-07 05:49:28 -04:00
|
|
|
_wire->beginTransmission(_address);
|
|
|
|
return (_wire->endTransmission() == 0);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-06-07 05:49:28 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int I2C_ASDX::read()
|
|
|
|
{
|
2021-06-07 05:49:28 -04:00
|
|
|
_wire->requestFrom(_address, (uint8_t)2);
|
|
|
|
if (_wire->available() != 2)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
_errorCount++;
|
|
|
|
_state = I2C_ASDX_READ_ERROR;
|
|
|
|
return _state;
|
|
|
|
}
|
2021-06-07 05:49:28 -04:00
|
|
|
int count = _wire->read() * 256; // hi byte
|
|
|
|
count += _wire->read(); // lo byte
|
2021-01-29 06:31:58 -05:00
|
|
|
if (count & 0xC000)
|
|
|
|
{
|
|
|
|
_errorCount++;
|
|
|
|
_state = I2C_ASDX_C000_ERROR; // no documentation, bits may not be set?
|
|
|
|
return _state;
|
|
|
|
}
|
2021-06-07 05:49:28 -04:00
|
|
|
|
2021-12-19 11:54:58 -05:00
|
|
|
// _pressure = map(count, 1638, 14746, 0, _maxPressure);
|
|
|
|
// _pressure = (count - 1638) * (_maxPressure - 0) / ( 14746 - 1638);
|
2021-01-29 06:31:58 -05:00
|
|
|
_pressure = (count - 1638) * _maxPressure * 7.62892889838E-5;
|
|
|
|
_lastRead = millis();
|
2021-06-07 05:49:28 -04:00
|
|
|
|
|
|
|
_state = I2C_ASDX_OK;
|
2021-01-29 06:31:58 -05:00
|
|
|
return _state;
|
|
|
|
}
|
|
|
|
|
2021-06-07 05:49:28 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
// -- END OF FILE --
|
2021-12-19 11:54:58 -05:00
|
|
|
|