Skip to content

Commit 1a1895b

Browse files
committed
Changes to fix multiple Issues
1 parent c9f1f4a commit 1a1895b

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

Diff for: src/SparkFunSX1509.cpp

+31-32
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ uint8_t SX1509::init(void)
5757
//Wire.begin();
5858

5959
// If the reset pin is connected
60-
if (pinReset != 255)
60+
if (pinReset != 255) {
6161
reset(1);
62-
else
62+
}
63+
else {
6364
reset(0);
64-
65+
}
6566
// Communication test. We'll read from two registers with different
6667
// default values to verify communication.
6768
uint16_t testRegisters = 0;
@@ -93,7 +94,7 @@ void SX1509::reset(bool hardware)
9394
writeByte(REG_MISC, regMisc);
9495
}
9596
// Reset the SX1509, the pin is active low
96-
::pinMode(pinReset, OUTPUT); // set reset pin as output
97+
::pinMode(pinReset, SX_OUTPUT); // set reset pin as output
9798
::digitalWrite(pinReset, LOW); // pull reset pin low
9899
delay(1); // Wait for the pin to settle
99100
::digitalWrite(pinReset, HIGH); // pull reset pin back high
@@ -112,7 +113,7 @@ void SX1509::pinDir(uint8_t pin, uint8_t inOut, uint8_t initialLevel)
112113
// 0: IO is configured as an output
113114
// 1: IO is configured as an input
114115
uint8_t modeBit;
115-
if ((inOut == OUTPUT) || (inOut == ANALOG_OUTPUT))
116+
if ((inOut == SX_OUTPUT) || (inOut == SX_ANALOG_OUTPUT))
116117
{
117118
uint16_t tempRegData = readWord(REG_DATA_B);
118119
if (initialLevel == LOW)
@@ -128,18 +129,19 @@ void SX1509::pinDir(uint8_t pin, uint8_t inOut, uint8_t initialLevel)
128129
}
129130

130131
uint16_t tempRegDir = readWord(REG_DIR_B);
131-
if (modeBit)
132+
if (modeBit) {
132133
tempRegDir |= (1 << pin);
133-
else
134+
}
135+
else {
134136
tempRegDir &= ~(1 << pin);
135-
137+
}
136138
writeWord(REG_DIR_B, tempRegDir);
137139

138140
// If INPUT_PULLUP was called, set up the pullup too:
139-
if (inOut == INPUT_PULLUP)
141+
if (inOut == INPUT_PULLUP) {
140142
writePin(pin, HIGH);
141-
142-
if (inOut == ANALOG_OUTPUT)
143+
}
144+
if (inOut == SX_ANALOG_OUTPUT)
143145
{
144146
ledDriverInit(pin);
145147
}
@@ -189,7 +191,7 @@ bool SX1509::digitalWrite(uint8_t pin, uint8_t highLow)
189191
return writePin(pin, highLow);
190192
}
191193

192-
uint8_t SX1509::readPin(uint8_t pin)
194+
bool SX1509::readPin(uint8_t pin)
193195
{
194196
uint16_t tempRegDir = readWord(REG_DIR_B);
195197

@@ -201,7 +203,7 @@ uint8_t SX1509::readPin(uint8_t pin)
201203
}
202204
else
203205
{
204-
// log_d("Pin %d not INPUT, REG_DIR_B: %d", pin, tempRegDir);
206+
log_w("SX1509", "Pin %d not INPUT, REG_DIR_B: %d", pin, tempRegDir);
205207
}
206208

207209
return 0;
@@ -230,7 +232,7 @@ bool SX1509::readPin(const uint8_t pin, bool *value)
230232
return false;
231233
}
232234

233-
uint8_t SX1509::digitalRead(uint8_t pin)
235+
bool SX1509::digitalRead(uint8_t pin)
234236
{
235237
return readPin(pin);
236238
}
@@ -281,21 +283,17 @@ void SX1509::ledDriverInit(uint8_t pin, uint8_t freq /*= 1*/, bool log /*= false
281283
tempByte &= ~(1 << 3); // set linear mode bank A
282284
}
283285

284-
// Use configClock to setup the clock divder
285-
if (_clkX == 0) // Make clckX non-zero
286+
// Use configClock to setup the clock divider
287+
if (_clkX == 0) // Check if Clock has already been set-up; Make clckX non-zero
286288
{
287289
// _clkX = 2000000.0 / (1 << (1 - 1)); // Update private clock variable
288290
_clkX = 2000000.0;
291+
freq = (freq & 0x7) << 4; // mask only 3 bits and shift to bit position 6:4
292+
tempByte |= freq;
289293

290-
// uint8_t freq = (1 & 0x07) << 4; // freq should only be 3 bits from 6:4
291-
// tempByte |= freq;
294+
writeByte(REG_MISC, tempByte);
292295
}
293296

294-
freq = (freq & 0x7) << 4; // mask only 3 bits and shift to bit position 6:4
295-
tempByte |= freq;
296-
297-
writeByte(REG_MISC, tempByte);
298-
299297
// Enable LED driver operation (REG_LED_DRIVER_ENABLE)
300298
tempWord = readWord(REG_LED_DRIVER_ENABLE_B);
301299
tempWord |= (1 << pin);
@@ -335,8 +333,8 @@ void SX1509::breathe(uint8_t pin, unsigned long tOn, unsigned long tOff, unsigne
335333
uint8_t onReg = calculateLEDTRegister(tOn);
336334
uint8_t offReg = calculateLEDTRegister(tOff);
337335

338-
uint8_t riseTime = calculateSlopeRegister(rise, onInt, offInt);
339-
uint8_t fallTime = calculateSlopeRegister(fall, onInt, offInt);
336+
uint16_t riseTime = calculateSlopeRegister(rise, onInt, offInt);
337+
uint16_t fallTime = calculateSlopeRegister(fall, onInt, offInt);
340338

341339
setupBlink(pin, onReg, offReg, onInt, offInt, riseTime, fallTime, log);
342340
}
@@ -506,7 +504,7 @@ void SX1509::sync(void)
506504
}
507505

508506
// Toggle nReset pin to sync LED timers
509-
::pinMode(pinReset, OUTPUT); // set reset pin as output
507+
::pinMode(pinReset, SX_OUTPUT); // set reset pin as output
510508
::digitalWrite(pinReset, LOW); // pull reset pin low
511509
delay(1); // Wait for the pin to settle
512510
::digitalWrite(pinReset, HIGH); // pull reset pin back high
@@ -596,13 +594,13 @@ void SX1509::enableInterrupt(uint8_t pin, uint8_t riseFall)
596594
uint8_t sensitivity = 0;
597595
switch (riseFall)
598596
{
599-
case CHANGE:
597+
case SX_CHANGE:
600598
sensitivity = 0b11;
601599
break;
602-
case FALLING:
600+
case SX_FALLING:
603601
sensitivity = 0b10;
604602
break;
605-
case RISING:
603+
case SX_RISING:
606604
sensitivity = 0b01;
607605
break;
608606
}
@@ -676,6 +674,7 @@ void SX1509::configClock(uint8_t oscSource /*= 2*/, uint8_t oscPinFunction /*= 0
676674
writeByte(REG_MISC, regMisc);
677675
}
678676

677+
//TODO CHECk
679678
uint8_t SX1509::calculateLEDTRegister(unsigned long ms)
680679
{
681680
uint8_t regOn1, regOn2;
@@ -698,7 +697,8 @@ uint8_t SX1509::calculateLEDTRegister(unsigned long ms)
698697
return regOn2;
699698
}
700699

701-
uint8_t SX1509::calculateSlopeRegister(unsigned long ms, uint8_t onIntensity, uint8_t offIntensity)
700+
// Changed return from uint8_t to uint16_t
701+
uint16_t SX1509::calculateSlopeRegister(unsigned long ms, uint8_t onIntensity, uint8_t offIntensity)
702702
{
703703
uint16_t regSlope1, regSlope2;
704704
float regTime1, regTime2;
@@ -785,8 +785,7 @@ bool SX1509::readWord(uint8_t registerAddress, uint16_t *value)
785785
uint8_t dest[2];
786786
if (readBytes(registerAddress, dest, 2))
787787
{
788-
value[0] = dest[1];
789-
value[1] = dest[0];
788+
value[0] = (dest[1] << 8) + dest[0];
790789
return true;
791790
}
792791
return false;

Diff for: src/SparkFunSX1509.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ Distributed as-is; no warranty is given.
4444
#define SOFTWARE_RESET 0
4545
#define HARDWARE_RESET 1
4646

47-
#define ANALOG_OUTPUT 0x3 // To set a pin mode for PWM output
47+
#define SX_ANALOG_OUTPUT 0x3 // To set a pin mode for PWM output
48+
#define SX_OUTPUT 0x1
49+
50+
#define SX_CHANGE 1
51+
#define SX_FALLING 2
52+
#define SX_RISING 3
4853

4954
class SX1509
5055
{
@@ -152,9 +157,9 @@ class SX1509
152157
// Outputs:
153158
// This function returns a 1 if HIGH, 0 if LOW
154159
// -----------------------------------------------------------------------------
155-
uint8_t digitalRead(uint8_t pin);
160+
bool digitalRead(uint8_t pin);
156161
bool digitalRead(uint8_t pin, bool *value);
157-
uint8_t readPin(uint8_t pin); // Legacy - use digitalRead
162+
bool readPin(uint8_t pin); // Legacy - use digitalRead
158163
bool readPin(const uint8_t pin, bool *value);
159164

160165
// -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)