Skip to content

Multiple Fixes. HAS BREAKING CHANGES #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 35 additions & 32 deletions src/SparkFunSX1509.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ uint8_t SX1509::init(void)
//Wire.begin();

// If the reset pin is connected
if (pinReset != 255)
if (pinReset != 255) {
reset(1);
else
}
else {
reset(0);

}
// Communication test. We'll read from two registers with different
// default values to verify communication.
uint16_t testRegisters = 0;
Expand Down Expand Up @@ -93,7 +94,7 @@ void SX1509::reset(bool hardware)
writeByte(REG_MISC, regMisc);
}
// Reset the SX1509, the pin is active low
::pinMode(pinReset, OUTPUT); // set reset pin as output
::pinMode(pinReset, SX_OUTPUT); // set reset pin as output
::digitalWrite(pinReset, LOW); // pull reset pin low
delay(1); // Wait for the pin to settle
::digitalWrite(pinReset, HIGH); // pull reset pin back high
Expand All @@ -112,7 +113,7 @@ void SX1509::pinDir(uint8_t pin, uint8_t inOut, uint8_t initialLevel)
// 0: IO is configured as an output
// 1: IO is configured as an input
uint8_t modeBit;
if ((inOut == OUTPUT) || (inOut == ANALOG_OUTPUT))
if ((inOut == SX_OUTPUT) || (inOut == SX_ANALOG_OUTPUT))
{
uint16_t tempRegData = readWord(REG_DATA_B);
if (initialLevel == LOW)
Expand All @@ -128,18 +129,19 @@ void SX1509::pinDir(uint8_t pin, uint8_t inOut, uint8_t initialLevel)
}

uint16_t tempRegDir = readWord(REG_DIR_B);
if (modeBit)
if (modeBit) {
tempRegDir |= (1 << pin);
else
}
else {
tempRegDir &= ~(1 << pin);

}
writeWord(REG_DIR_B, tempRegDir);

// If INPUT_PULLUP was called, set up the pullup too:
if (inOut == INPUT_PULLUP)
if (inOut == INPUT_PULLUP) {
writePin(pin, HIGH);

if (inOut == ANALOG_OUTPUT)
}
if (inOut == SX_ANALOG_OUTPUT)
{
ledDriverInit(pin);
}
Expand Down Expand Up @@ -189,7 +191,7 @@ bool SX1509::digitalWrite(uint8_t pin, uint8_t highLow)
return writePin(pin, highLow);
}

uint8_t SX1509::readPin(uint8_t pin)
bool SX1509::readPin(uint8_t pin)
{
uint16_t tempRegDir = readWord(REG_DIR_B);

Expand All @@ -201,7 +203,7 @@ uint8_t SX1509::readPin(uint8_t pin)
}
else
{
// log_d("Pin %d not INPUT, REG_DIR_B: %d", pin, tempRegDir);
log_w("SX1509", "Pin %d not INPUT, REG_DIR_B: %d", pin, tempRegDir);
}

return 0;
Expand Down Expand Up @@ -230,7 +232,7 @@ bool SX1509::readPin(const uint8_t pin, bool *value)
return false;
}

uint8_t SX1509::digitalRead(uint8_t pin)
bool SX1509::digitalRead(uint8_t pin)
{
return readPin(pin);
}
Expand Down Expand Up @@ -281,20 +283,20 @@ void SX1509::ledDriverInit(uint8_t pin, uint8_t freq /*= 1*/, bool log /*= false
tempByte &= ~(1 << 3); // set linear mode bank A
}

// Use configClock to setup the clock divder
if (_clkX == 0) // Make clckX non-zero
// Use configClock to setup the clock divider
if (_clkX == 0) // Check if Clock has already been set-up; Make clckX non-zero
{
/* Call Config Clock with default values instead of code below
// _clkX = 2000000.0 / (1 << (1 - 1)); // Update private clock variable
_clkX = 2000000.0;
freq = (freq & 0x7) << 4; // mask only 3 bits and shift to bit position 6:4
tempByte |= freq;

// uint8_t freq = (1 & 0x07) << 4; // freq should only be 3 bits from 6:4
// tempByte |= freq;
}

freq = (freq & 0x7) << 4; // mask only 3 bits and shift to bit position 6:4
tempByte |= freq;
writeByte(REG_MISC, tempByte);
*/

writeByte(REG_MISC, tempByte);
configClock(2, 0, 0, 1);
}

// Enable LED driver operation (REG_LED_DRIVER_ENABLE)
tempWord = readWord(REG_LED_DRIVER_ENABLE_B);
Expand Down Expand Up @@ -335,8 +337,8 @@ void SX1509::breathe(uint8_t pin, unsigned long tOn, unsigned long tOff, unsigne
uint8_t onReg = calculateLEDTRegister(tOn);
uint8_t offReg = calculateLEDTRegister(tOff);

uint8_t riseTime = calculateSlopeRegister(rise, onInt, offInt);
uint8_t fallTime = calculateSlopeRegister(fall, onInt, offInt);
uint16_t riseTime = calculateSlopeRegister(rise, onInt, offInt);
uint16_t fallTime = calculateSlopeRegister(fall, onInt, offInt);

setupBlink(pin, onReg, offReg, onInt, offInt, riseTime, fallTime, log);
}
Expand Down Expand Up @@ -506,7 +508,7 @@ void SX1509::sync(void)
}

// Toggle nReset pin to sync LED timers
::pinMode(pinReset, OUTPUT); // set reset pin as output
::pinMode(pinReset, SX_OUTPUT); // set reset pin as output
::digitalWrite(pinReset, LOW); // pull reset pin low
delay(1); // Wait for the pin to settle
::digitalWrite(pinReset, HIGH); // pull reset pin back high
Expand Down Expand Up @@ -596,13 +598,13 @@ void SX1509::enableInterrupt(uint8_t pin, uint8_t riseFall)
uint8_t sensitivity = 0;
switch (riseFall)
{
case CHANGE:
case SX_CHANGE:
sensitivity = 0b11;
break;
case FALLING:
case SX_FALLING:
sensitivity = 0b10;
break;
case RISING:
case SX_RISING:
sensitivity = 0b01;
break;
}
Expand Down Expand Up @@ -676,6 +678,7 @@ void SX1509::configClock(uint8_t oscSource /*= 2*/, uint8_t oscPinFunction /*= 0
writeByte(REG_MISC, regMisc);
}

//TODO CHECk
uint8_t SX1509::calculateLEDTRegister(unsigned long ms)
{
uint8_t regOn1, regOn2;
Expand All @@ -698,7 +701,8 @@ uint8_t SX1509::calculateLEDTRegister(unsigned long ms)
return regOn2;
}

uint8_t SX1509::calculateSlopeRegister(unsigned long ms, uint8_t onIntensity, uint8_t offIntensity)
// Changed return from uint8_t to uint16_t
uint16_t SX1509::calculateSlopeRegister(unsigned long ms, uint8_t onIntensity, uint8_t offIntensity)
{
uint16_t regSlope1, regSlope2;
float regTime1, regTime2;
Expand Down Expand Up @@ -785,8 +789,7 @@ bool SX1509::readWord(uint8_t registerAddress, uint16_t *value)
uint8_t dest[2];
if (readBytes(registerAddress, dest, 2))
{
value[0] = dest[1];
value[1] = dest[0];
value[0] = (dest[1] << 8) + dest[0];
return true;
}
return false;
Expand Down
13 changes: 9 additions & 4 deletions src/SparkFunSX1509.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ Distributed as-is; no warranty is given.
#define SOFTWARE_RESET 0
#define HARDWARE_RESET 1

#define ANALOG_OUTPUT 0x3 // To set a pin mode for PWM output
#define SX_ANALOG_OUTPUT 0x3 // To set a pin mode for PWM output
#define SX_OUTPUT 0x1

#define SX_CHANGE 1
#define SX_FALLING 2
#define SX_RISING 3

class SX1509
{
Expand Down Expand Up @@ -78,7 +83,7 @@ class SX1509
uint8_t calculateLEDTRegister(unsigned long ms);
// calculateSlopeRegister - Try to estimate an LED rise/fall duration
// register, given the number of milliseconds and LED clock frequency.
uint8_t calculateSlopeRegister(unsigned long ms, uint8_t onIntensity, uint8_t offIntensity);
uint16_t calculateSlopeRegister(unsigned long ms, uint8_t onIntensity, uint8_t offIntensity);

public:
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -152,9 +157,9 @@ class SX1509
// Outputs:
// This function returns a 1 if HIGH, 0 if LOW
// -----------------------------------------------------------------------------
uint8_t digitalRead(uint8_t pin);
bool digitalRead(uint8_t pin);
bool digitalRead(uint8_t pin, bool *value);
uint8_t readPin(uint8_t pin); // Legacy - use digitalRead
bool readPin(uint8_t pin); // Legacy - use digitalRead
bool readPin(const uint8_t pin, bool *value);

// -----------------------------------------------------------------------------
Expand Down