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

321 lines
6.4 KiB
C++
Raw Normal View History

2016-04-24 02:26:22 -04:00
//
// FILE: PCA9635.cpp
// AUTHOR: Rob Tillaart
// DATE: 23-apr-2016
2022-11-19 10:54:49 -05:00
// VERSION: 0.4.2
2020-11-27 05:28:57 -05:00
// PURPOSE: Arduino library for PCA9635 I2C LED driver
// URL: https://github.com/RobTillaart/PCA9635
2016-04-24 02:26:22 -04:00
2022-06-02 06:11:57 -04:00
2021-01-29 06:31:58 -05:00
#include "PCA9635.h"
2016-04-24 02:26:22 -04:00
2020-11-27 05:28:57 -05:00
2021-01-29 06:31:58 -05:00
//////////////////////////////////////////////////////////////
//
2022-11-19 10:54:49 -05:00
// Constructor
2021-01-29 06:31:58 -05:00
//
PCA9635::PCA9635(const uint8_t deviceAddress, TwoWire *wire)
2016-04-24 02:26:22 -04:00
{
2022-01-06 14:19:35 -05:00
_address = deviceAddress;
_wire = wire;
_channelCount = 16;
2020-11-27 05:28:57 -05:00
}
2021-01-29 06:31:58 -05:00
2020-11-27 05:28:57 -05:00
#if defined (ESP8266) || defined(ESP32)
2022-09-10 14:47:37 -04:00
bool PCA9635::begin(int sda, int scl, uint8_t mode1_mask, uint8_t mode2_mask)
2020-11-27 05:28:57 -05:00
{
2021-01-29 06:31:58 -05:00
_wire = &Wire;
if ((sda < 255) && (scl < 255))
{
_wire->begin(sda, scl);
} else {
_wire->begin();
}
if (! isConnected()) return false;
2022-06-02 06:11:57 -04:00
configure(mode1_mask, mode2_mask);
2021-01-29 06:31:58 -05:00
return true;
2020-11-27 05:28:57 -05:00
}
#endif
2021-01-29 06:31:58 -05:00
2022-06-02 06:11:57 -04:00
bool PCA9635::begin(uint8_t mode1_mask, uint8_t mode2_mask)
2020-11-27 05:28:57 -05:00
{
2021-01-29 06:31:58 -05:00
_wire->begin();
if (! isConnected()) return false;
2022-06-02 06:11:57 -04:00
configure(mode1_mask, mode2_mask);
2021-01-29 06:31:58 -05:00
return true;
2020-11-27 05:28:57 -05:00
}
2021-01-29 06:31:58 -05:00
bool PCA9635::isConnected()
{
_wire->beginTransmission(_address);
_error = _wire->endTransmission();
return (_error == 0);
}
2022-06-02 06:11:57 -04:00
void PCA9635::configure(uint8_t mode1_mask, uint8_t mode2_mask)
2020-11-27 05:28:57 -05:00
{
_data = 0;
_error = 0;
2022-05-16 12:25:10 -04:00
2022-06-02 06:11:57 -04:00
setMode1(mode1_mask);
setMode2(mode2_mask);
2016-04-24 02:26:22 -04:00
}
2021-01-29 06:31:58 -05:00
2022-11-19 10:54:49 -05:00
// write value to single PWM registers
2020-11-27 05:28:57 -05:00
uint8_t PCA9635::write1(uint8_t channel, uint8_t value)
2016-04-24 02:26:22 -04:00
{
2020-11-27 05:28:57 -05:00
return writeN(channel, &value, 1);
2016-04-24 02:26:22 -04:00
}
2021-01-29 06:31:58 -05:00
2022-11-19 10:54:49 -05:00
// write three values in consecutive PWM registers
// typically for RGB values
2020-11-27 05:28:57 -05:00
uint8_t PCA9635::write3(uint8_t channel, uint8_t R, uint8_t G, uint8_t B)
2016-04-24 02:26:22 -04:00
{
2020-11-27 05:28:57 -05:00
uint8_t arr[3] = { R, G, B };
return writeN(channel, arr, 3);
2016-04-24 02:26:22 -04:00
}
2021-01-29 06:31:58 -05:00
2022-11-19 10:54:49 -05:00
// write count values in consecutive PWM registers
// checks if [channel + count - 1 > 15]
2020-11-27 05:28:57 -05:00
uint8_t PCA9635::writeN(uint8_t channel, uint8_t* arr, uint8_t count)
2016-04-24 02:26:22 -04:00
{
2022-01-06 14:19:35 -05:00
if (channel + count > _channelCount)
2020-11-27 05:28:57 -05:00
{
_error = PCA9635_ERR_WRITE;
return PCA9635_ERROR;
}
uint8_t base = PCA9635_PWM(channel);
2021-01-29 06:31:58 -05:00
_wire->beginTransmission(_address);
_wire->write(base);
2020-11-27 05:28:57 -05:00
for(uint8_t i = 0; i < count; i++)
{
2021-01-29 06:31:58 -05:00
_wire->write(arr[i]);
2020-11-27 05:28:57 -05:00
}
2021-01-29 06:31:58 -05:00
_error = _wire->endTransmission();
2020-11-27 05:28:57 -05:00
if (_error != 0)
{
_error = PCA9635_ERR_I2C;
return PCA9635_ERROR;
}
return PCA9635_OK;
2016-04-24 02:26:22 -04:00
}
2021-01-29 06:31:58 -05:00
2020-11-27 05:28:57 -05:00
uint8_t PCA9635::writeMode(uint8_t reg, uint8_t value)
2016-04-24 02:26:22 -04:00
{
2020-11-27 05:28:57 -05:00
if ((reg == PCA9635_MODE1) || (reg == PCA9635_MODE2))
{
writeReg(reg, value);
2021-01-29 06:31:58 -05:00
return PCA9635_OK;
2020-11-27 05:28:57 -05:00
}
_error = PCA9635_ERR_REG;
return PCA9635_ERROR;
2016-04-24 02:26:22 -04:00
}
2021-01-29 06:31:58 -05:00
2022-11-19 10:54:49 -05:00
// Note 0xFF can also mean an error.... check error flag..
2016-04-24 02:26:22 -04:00
uint8_t PCA9635::readMode(uint8_t reg)
{
2020-11-27 05:28:57 -05:00
if ((reg == PCA9635_MODE1) || (reg == PCA9635_MODE2))
{
2021-01-29 06:31:58 -05:00
_error = PCA9635_OK;
2020-11-27 05:28:57 -05:00
uint8_t value = readReg(reg);
return value;
}
_error = PCA9635_ERR_REG;
return PCA9635_ERROR;
2016-04-24 02:26:22 -04:00
}
2021-01-29 06:31:58 -05:00
2020-11-27 05:28:57 -05:00
uint8_t PCA9635::setLedDriverMode(uint8_t channel, uint8_t mode)
2016-04-24 02:26:22 -04:00
{
2022-01-06 14:19:35 -05:00
if (channel >= _channelCount)
2020-11-27 05:28:57 -05:00
{
_error = PCA9635_ERR_CHAN;
return PCA9635_ERROR;
}
2021-12-23 04:41:42 -05:00
if (mode > 3)
2020-11-27 05:28:57 -05:00
{
_error = PCA9635_ERR_MODE;
return PCA9635_ERROR;
}
uint8_t reg = PCA9635_LEDOUT_BASE + (channel >> 2);
2022-11-19 10:54:49 -05:00
// some bit magic
2020-11-27 05:28:57 -05:00
uint8_t shift = (channel & 0x03) * 2; // 0,2,4,6 places
uint8_t setmask = mode << shift;
uint8_t clrmask = ~(0x03 << shift);
uint8_t value = (readReg(reg) & clrmask) | setmask;
writeReg(reg, value);
return PCA9635_OK;
2016-04-24 02:26:22 -04:00
}
2021-01-29 06:31:58 -05:00
2022-11-19 10:54:49 -05:00
// returns 0..3 if OK, other values indicate an error
2016-04-24 02:26:22 -04:00
uint8_t PCA9635::getLedDriverMode(uint8_t channel)
{
2022-01-06 14:19:35 -05:00
if (channel >= _channelCount)
2020-11-27 05:28:57 -05:00
{
_error = PCA9635_ERR_CHAN;
2016-04-24 02:26:22 -04:00
return PCA9635_ERROR;
2020-11-27 05:28:57 -05:00
}
uint8_t reg = PCA9635_LEDOUT_BASE + (channel >> 2);
uint8_t shift = (channel & 0x03) * 2; // 0,2,4,6 places
uint8_t value = (readReg(reg) >> shift ) & 0x03;
return value;
2016-04-24 02:26:22 -04:00
}
2021-01-29 06:31:58 -05:00
2022-11-19 10:54:49 -05:00
// note error flag is reset after read!
2016-04-24 02:26:22 -04:00
int PCA9635::lastError()
{
2020-11-27 05:28:57 -05:00
int e = _error;
_error = 0;
return e;
2016-04-24 02:26:22 -04:00
}
2020-11-27 05:28:57 -05:00
2022-06-02 06:11:57 -04:00
/////////////////////////////////////////////////////
//
2022-11-19 10:54:49 -05:00
// SUB CALL - ALL CALL
2022-06-02 06:11:57 -04:00
//
bool PCA9635::enableSubCall(uint8_t nr)
{
if ((nr == 0) || (nr > 3)) return false;
uint8_t prev = getMode1();
uint8_t reg = prev;
if (nr == 1) reg |= PCA9635_MODE1_SUB1;
else if (nr == 2) reg |= PCA9635_MODE1_SUB2;
else reg |= PCA9635_MODE1_SUB3;
// only update if changed.
if (reg != prev) setMode1(reg);
return true;
}
bool PCA9635::disableSubCall(uint8_t nr)
{
if ((nr == 0) || (nr > 3)) return false;
uint8_t prev = getMode1();
uint8_t reg = prev;
if (nr == 1) reg &= ~PCA9635_MODE1_SUB1;
else if (nr == 2) reg &= ~PCA9635_MODE1_SUB2;
else reg &= ~PCA9635_MODE1_SUB3;
// only update if changed.
if (reg != prev) setMode1(reg);
return true;
}
bool PCA9635::isEnabledSubCall(uint8_t nr)
{
if ((nr == 0) || (nr > 3)) return false;
uint8_t reg = getMode1();
if (nr == 1) return (reg & PCA9635_MODE1_SUB1) > 0;
if (nr == 2) return (reg & PCA9635_MODE1_SUB2) > 0;
return (reg & PCA9635_MODE1_SUB3) > 0;
}
bool PCA9635::setSubCallAddress(uint8_t nr, uint8_t address)
{
if ((nr == 0) || (nr > 3)) return false;
writeReg(PCA9635_SUBADR(nr), address);
return true;
}
uint8_t PCA9635::getSubCallAddress(uint8_t nr)
{
if ((nr == 0) || (nr > 3)) return 0;
uint8_t address = readReg(PCA9635_SUBADR(nr));
return address;
}
bool PCA9635::enableAllCall()
{
uint8_t prev = getMode1();
uint8_t reg = prev | PCA9635_MODE1_ALLCALL;
// only update if changed.
if (reg != prev) setMode1(reg);
return true;
}
bool PCA9635::disableAllCall()
{
uint8_t prev = getMode1();
uint8_t reg = prev & ~PCA9635_MODE1_ALLCALL;
// only update if changed.
if (reg != prev) setMode1(reg);
return true;
}
bool PCA9635::isEnabledAllCall()
{
uint8_t reg = getMode1();
return reg & PCA9635_MODE1_ALLCALL;
}
bool PCA9635::setAllCallAddress(uint8_t address)
{
writeReg(PCA9635_ALLCALLADR, address);
return true;
}
uint8_t PCA9635::getAllCallAddress()
{
uint8_t address = readReg(PCA9635_ALLCALLADR);
return address;
}
2020-11-27 05:28:57 -05:00
/////////////////////////////////////////////////////
//
2022-11-19 10:54:49 -05:00
// PRIVATE
2020-11-27 05:28:57 -05:00
//
2021-01-29 06:31:58 -05:00
uint8_t PCA9635::writeReg(uint8_t reg, uint8_t value)
2016-04-24 02:26:22 -04:00
{
2021-01-29 06:31:58 -05:00
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->write(value);
_error = _wire->endTransmission();
if (_error == 0) _error = PCA9635_OK;
else _error = PCA9635_ERR_I2C;
return _error;
2016-04-24 02:26:22 -04:00
}
2020-11-27 05:28:57 -05:00
2016-04-24 02:26:22 -04:00
uint8_t PCA9635::readReg(uint8_t reg)
{
2021-01-29 06:31:58 -05:00
_wire->beginTransmission(_address);
_wire->write(reg);
_error = _wire->endTransmission();
if (_wire->requestFrom(_address, (uint8_t)1) != 1)
2020-11-27 05:28:57 -05:00
{
_error = PCA9635_ERROR;
return 0;
}
2021-01-29 06:31:58 -05:00
_error = PCA9635_OK;
_data = _wire->read();
2020-11-27 05:28:57 -05:00
return _data;
2016-04-24 02:26:22 -04:00
}
2021-12-23 04:41:42 -05:00
2020-11-27 05:28:57 -05:00
// -- END OF FILE --
2021-12-23 04:41:42 -05:00