GY-63_MS5611/libraries/AD5245/AD5245.cpp

95 lines
1.6 KiB
C++
Raw Normal View History

2022-08-03 05:00:56 -04:00
//
// FILE: AD5245.cpp
// AUTHOR: Rob Tillaart
2024-01-12 11:06:06 -05:00
// VERSION: 0.4.0
2022-08-03 05:00:56 -04:00
// PURPOSE: Arduino library for I2C digital potentiometer AD5245.
// DATE: 2022-07-31
// URL: https://github.com/RobTillaart/AD5245
#include "AD5245.h"
2024-01-12 11:06:06 -05:00
2023-12-06 09:56:54 -05:00
#define AD5245_WRITE 0x00
#define AD5245_RESET 0x40
#define AD5245_SHUTDOWN 0x20
2022-08-03 05:00:56 -04:00
AD5245::AD5245(const uint8_t address, TwoWire *wire)
{
2023-01-16 08:38:52 -05:00
// address: 0x010110x = 0x2C or 0x2D
2022-08-03 05:00:56 -04:00
_address = address;
_wire = wire;
2023-12-06 09:56:54 -05:00
// power on reset => mid position
_lastValue = AD5245_MIDPOINT;
2022-08-03 05:00:56 -04:00
}
bool AD5245::begin()
{
2024-01-12 11:06:06 -05:00
if ((_address != 0x2C) && (_address != 0x2D)) return false;
2022-08-03 05:00:56 -04:00
if (! isConnected()) return false;
reset();
return true;
}
bool AD5245::isConnected()
{
_wire->beginTransmission(_address);
return ( _wire->endTransmission() == 0);
}
uint8_t AD5245::reset()
{
uint8_t cmd = AD5245_RESET;
2023-01-16 08:38:52 -05:00
_lastValue = AD5245_MIDPOINT;
2022-08-03 05:00:56 -04:00
return send(cmd, _lastValue);
}
uint8_t AD5245::write(const uint8_t value)
{
uint8_t cmd = AD5245_WRITE;
_lastValue = value;
return send(cmd, value);
}
uint8_t AD5245::read()
{
return _lastValue;
}
uint8_t AD5245::readDevice()
{
2023-02-03 02:42:59 -05:00
_wire->requestFrom(_address, (uint8_t)1);
return _wire->read();
2022-08-03 05:00:56 -04:00
}
2023-01-16 08:38:52 -05:00
// read datasheet
2022-08-03 05:00:56 -04:00
uint8_t AD5245::shutDown()
{
uint8_t cmd = AD5245_SHUTDOWN;
return send(cmd, 0);
}
//////////////////////////////////////////////////////////
//
2022-10-25 15:41:23 -04:00
// PRIVATE
2022-08-03 05:00:56 -04:00
//
uint8_t AD5245::send(const uint8_t cmd, const uint8_t value)
{
2023-02-03 02:42:59 -05:00
_wire->beginTransmission(_address);
_wire->write(cmd);
_wire->write(value);
return _wire->endTransmission();
2022-08-03 05:00:56 -04:00
}
2023-01-16 08:38:52 -05:00
// -- END OF FILE --