@@ -57,11 +57,12 @@ uint8_t SX1509::init(void)
57
57
// Wire.begin();
58
58
59
59
// If the reset pin is connected
60
- if (pinReset != 255 )
60
+ if (pinReset != 255 ) {
61
61
reset (1 );
62
- else
62
+ }
63
+ else {
63
64
reset (0 );
64
-
65
+ }
65
66
// Communication test. We'll read from two registers with different
66
67
// default values to verify communication.
67
68
uint16_t testRegisters = 0 ;
@@ -93,7 +94,7 @@ void SX1509::reset(bool hardware)
93
94
writeByte (REG_MISC, regMisc);
94
95
}
95
96
// 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
97
98
::digitalWrite (pinReset, LOW); // pull reset pin low
98
99
delay (1 ); // Wait for the pin to settle
99
100
::digitalWrite (pinReset, HIGH); // pull reset pin back high
@@ -112,7 +113,7 @@ void SX1509::pinDir(uint8_t pin, uint8_t inOut, uint8_t initialLevel)
112
113
// 0: IO is configured as an output
113
114
// 1: IO is configured as an input
114
115
uint8_t modeBit;
115
- if ((inOut == OUTPUT ) || (inOut == ANALOG_OUTPUT ))
116
+ if ((inOut == SX_OUTPUT ) || (inOut == SX_ANALOG_OUTPUT ))
116
117
{
117
118
uint16_t tempRegData = readWord (REG_DATA_B);
118
119
if (initialLevel == LOW)
@@ -128,18 +129,19 @@ void SX1509::pinDir(uint8_t pin, uint8_t inOut, uint8_t initialLevel)
128
129
}
129
130
130
131
uint16_t tempRegDir = readWord (REG_DIR_B);
131
- if (modeBit)
132
+ if (modeBit) {
132
133
tempRegDir |= (1 << pin);
133
- else
134
+ }
135
+ else {
134
136
tempRegDir &= ~(1 << pin);
135
-
137
+ }
136
138
writeWord (REG_DIR_B, tempRegDir);
137
139
138
140
// If INPUT_PULLUP was called, set up the pullup too:
139
- if (inOut == INPUT_PULLUP)
141
+ if (inOut == INPUT_PULLUP) {
140
142
writePin (pin, HIGH);
141
-
142
- if (inOut == ANALOG_OUTPUT )
143
+ }
144
+ if (inOut == SX_ANALOG_OUTPUT )
143
145
{
144
146
ledDriverInit (pin);
145
147
}
@@ -189,7 +191,7 @@ bool SX1509::digitalWrite(uint8_t pin, uint8_t highLow)
189
191
return writePin (pin, highLow);
190
192
}
191
193
192
- uint8_t SX1509::readPin (uint8_t pin)
194
+ bool SX1509::readPin (uint8_t pin)
193
195
{
194
196
uint16_t tempRegDir = readWord (REG_DIR_B);
195
197
@@ -201,7 +203,7 @@ uint8_t SX1509::readPin(uint8_t pin)
201
203
}
202
204
else
203
205
{
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);
205
207
}
206
208
207
209
return 0 ;
@@ -230,7 +232,7 @@ bool SX1509::readPin(const uint8_t pin, bool *value)
230
232
return false ;
231
233
}
232
234
233
- uint8_t SX1509::digitalRead (uint8_t pin)
235
+ bool SX1509::digitalRead (uint8_t pin)
234
236
{
235
237
return readPin (pin);
236
238
}
@@ -281,21 +283,17 @@ void SX1509::ledDriverInit(uint8_t pin, uint8_t freq /*= 1*/, bool log /*= false
281
283
tempByte &= ~(1 << 3 ); // set linear mode bank A
282
284
}
283
285
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
286
288
{
287
289
// _clkX = 2000000.0 / (1 << (1 - 1)); // Update private clock variable
288
290
_clkX = 2000000.0 ;
291
+ freq = (freq & 0x7 ) << 4 ; // mask only 3 bits and shift to bit position 6:4
292
+ tempByte |= freq;
289
293
290
- // uint8_t freq = (1 & 0x07) << 4; // freq should only be 3 bits from 6:4
291
- // tempByte |= freq;
294
+ writeByte (REG_MISC, tempByte);
292
295
}
293
296
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
-
299
297
// Enable LED driver operation (REG_LED_DRIVER_ENABLE)
300
298
tempWord = readWord (REG_LED_DRIVER_ENABLE_B);
301
299
tempWord |= (1 << pin);
@@ -335,8 +333,8 @@ void SX1509::breathe(uint8_t pin, unsigned long tOn, unsigned long tOff, unsigne
335
333
uint8_t onReg = calculateLEDTRegister (tOn);
336
334
uint8_t offReg = calculateLEDTRegister (tOff);
337
335
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);
340
338
341
339
setupBlink (pin, onReg, offReg, onInt, offInt, riseTime, fallTime, log );
342
340
}
@@ -506,7 +504,7 @@ void SX1509::sync(void)
506
504
}
507
505
508
506
// 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
510
508
::digitalWrite (pinReset, LOW); // pull reset pin low
511
509
delay (1 ); // Wait for the pin to settle
512
510
::digitalWrite (pinReset, HIGH); // pull reset pin back high
@@ -596,13 +594,13 @@ void SX1509::enableInterrupt(uint8_t pin, uint8_t riseFall)
596
594
uint8_t sensitivity = 0 ;
597
595
switch (riseFall)
598
596
{
599
- case CHANGE :
597
+ case SX_CHANGE :
600
598
sensitivity = 0b11 ;
601
599
break ;
602
- case FALLING :
600
+ case SX_FALLING :
603
601
sensitivity = 0b10 ;
604
602
break ;
605
- case RISING :
603
+ case SX_RISING :
606
604
sensitivity = 0b01 ;
607
605
break ;
608
606
}
@@ -676,6 +674,7 @@ void SX1509::configClock(uint8_t oscSource /*= 2*/, uint8_t oscPinFunction /*= 0
676
674
writeByte (REG_MISC, regMisc);
677
675
}
678
676
677
+ // TODO CHECk
679
678
uint8_t SX1509::calculateLEDTRegister (unsigned long ms)
680
679
{
681
680
uint8_t regOn1, regOn2;
@@ -698,7 +697,8 @@ uint8_t SX1509::calculateLEDTRegister(unsigned long ms)
698
697
return regOn2;
699
698
}
700
699
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)
702
702
{
703
703
uint16_t regSlope1, regSlope2;
704
704
float regTime1, regTime2;
@@ -785,8 +785,7 @@ bool SX1509::readWord(uint8_t registerAddress, uint16_t *value)
785
785
uint8_t dest[2 ];
786
786
if (readBytes (registerAddress, dest, 2 ))
787
787
{
788
- value[0 ] = dest[1 ];
789
- value[1 ] = dest[0 ];
788
+ value[0 ] = (dest[1 ] << 8 ) + dest[0 ];
790
789
return true ;
791
790
}
792
791
return false ;
0 commit comments