2021-01-29 06:31:58 -05:00
|
|
|
//
|
|
|
|
// FILE: I2C_ASDX.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-11-12 09:43:08 -05:00
|
|
|
// VERSION: 0.3.2
|
2021-01-29 06:31:58 -05:00
|
|
|
// PURPOSE: I2C_asdx library for Arduino.
|
|
|
|
// URL: https://github.com/RobTillaart/I2C_ASDX
|
|
|
|
//
|
2022-11-12 09:43:08 -05:00
|
|
|
// HISTORY: see changelog.md
|
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
|
|
|
|