Skip to content

Commit f2044f0

Browse files
committed
Replace ints with uint16_t. Define I2C_ERROR_OK as needed.
1 parent c8d272d commit f2044f0

File tree

2 files changed

+46
-43
lines changed

2 files changed

+46
-43
lines changed

src/SparkFunSX1509.cpp

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ uint8_t SX1509::init(void)
6464

6565
// Communication test. We'll read from two registers with different
6666
// default values to verify communication.
67-
unsigned int testRegisters = 0;
67+
uint16_t testRegisters = 0;
6868
testRegisters = readWord(REG_INTERRUPT_MASK_A); // This should return 0xFF00
6969

7070
// Then read a byte that should be 0x00
@@ -377,7 +377,7 @@ void SX1509::setupBlink(uint8_t pin, uint8_t tOn, uint8_t tOff, uint8_t onIntens
377377
writeByte(REG_T_FALL[pin], tFall);
378378
}
379379

380-
void SX1509::keypad(uint8_t rows, uint8_t columns, unsigned int sleepTime, uint8_t scanTime, uint8_t debounceTime)
380+
void SX1509::keypad(uint8_t rows, uint8_t columns, uint16_t sleepTime, uint8_t scanTime, uint8_t debounceTime)
381381
{
382382
uint16_t tempWord;
383383
uint8_t tempByte;
@@ -388,21 +388,21 @@ void SX1509::keypad(uint8_t rows, uint8_t columns, unsigned int sleepTime, uint8
388388

389389
// Set regDir 0:7 outputs, 8:15 inputs:
390390
tempWord = readWord(REG_DIR_B);
391-
for (int i = 0; i < rows; i++)
391+
for (uint8_t i = 0; i < rows; i++)
392392
tempWord &= ~(1 << i);
393-
for (int i = 8; i < (columns * 2); i++)
393+
for (uint8_t i = 8; i < (columns * 2); i++)
394394
tempWord |= (1 << i);
395395
writeWord(REG_DIR_B, tempWord);
396396

397397
// Set regOpenDrain on 0:7:
398398
tempByte = readByte(REG_OPEN_DRAIN_A);
399-
for (int i = 0; i < rows; i++)
399+
for (uint8_t i = 0; i < rows; i++)
400400
tempByte |= (1 << i);
401401
writeByte(REG_OPEN_DRAIN_A, tempByte);
402402

403403
// Set regPullUp on 8:15:
404404
tempByte = readByte(REG_PULL_UP_B);
405-
for (int i = 0; i < columns; i++)
405+
for (uint8_t i = 0; i < columns; i++)
406406
tempByte |= (1 << i);
407407
writeByte(REG_PULL_UP_B, tempByte);
408408

@@ -432,7 +432,7 @@ void SX1509::keypad(uint8_t rows, uint8_t columns, unsigned int sleepTime, uint8
432432
{
433433
for (uint8_t i = 7; i > 0; i--)
434434
{
435-
if (sleepTime & ((unsigned int)1 << (i + 6)))
435+
if (sleepTime & ((uint16_t)1 << (i + 6)))
436436
{
437437
sleepTimeBits = i;
438438
break;
@@ -456,33 +456,33 @@ void SX1509::keypad(uint8_t rows, uint8_t columns, unsigned int sleepTime, uint8
456456
writeByte(REG_KEY_CONFIG_2, (rows << 3) | columns);
457457
}
458458

459-
unsigned int SX1509::readKeypad()
459+
uint16_t SX1509::readKeypad()
460460
{
461461
return readKeyData();
462462
}
463463

464-
unsigned int SX1509::readKeyData()
464+
uint16_t SX1509::readKeyData()
465465
{
466466
return (0xFFFF ^ readWord(REG_KEY_DATA_1));
467467
}
468468

469-
uint8_t SX1509::getRow(unsigned int keyData)
469+
uint8_t SX1509::getRow(uint16_t keyData)
470470
{
471471
uint8_t rowData = uint8_t(keyData & 0x00FF);
472472

473-
for (int i = 0; i < 8; i++)
473+
for (uint8_t i = 0; i < 8; i++)
474474
{
475475
if (rowData & (1 << i))
476476
return i;
477477
}
478478
return 0;
479479
}
480480

481-
uint8_t SX1509::getCol(unsigned int keyData)
481+
uint8_t SX1509::getCol(uint16_t keyData)
482482
{
483483
uint8_t colData = uint8_t((keyData & 0xFF00) >> 8);
484484

485-
for (int i = 0; i < 8; i++)
485+
for (uint8_t i = 0; i < 8; i++)
486486
{
487487
if (colData & (1 << i))
488488
return i;
@@ -544,7 +544,7 @@ void SX1509::debounceTime(uint8_t time)
544544
uint8_t configValue = 0;
545545
// We'll check for the highest set bit position,
546546
// and use that for debounceConfig
547-
for (int i = 7; i >= 0; i--)
547+
for (uint8_t i = 7; i >= 0; i--)
548548
{
549549
if (time & (1 << i))
550550
{
@@ -559,7 +559,7 @@ void SX1509::debounceTime(uint8_t time)
559559

560560
void SX1509::debounceEnable(uint8_t pin)
561561
{
562-
unsigned int debounceEnable = readWord(REG_DEBOUNCE_ENABLE_B);
562+
uint16_t debounceEnable = readWord(REG_DEBOUNCE_ENABLE_B);
563563
debounceEnable |= (1 << pin);
564564
writeWord(REG_DEBOUNCE_ENABLE_B, debounceEnable);
565565
}
@@ -575,9 +575,9 @@ void SX1509::debounceKeypad(uint8_t time, uint8_t numRows, uint8_t numCols)
575575
debounceTime(time);
576576

577577
// Set up debounce pins:
578-
for (int i = 0; i < numRows; i++)
578+
for (uint8_t i = 0; i < numRows; i++)
579579
debouncePin(i);
580-
for (int i = 0; i < (8 + numCols); i++)
580+
for (uint8_t i = 0; i < (8 + numCols); i++)
581581
debouncePin(i);
582582
}
583583

@@ -623,15 +623,15 @@ void SX1509::enableInterrupt(uint8_t pin, uint8_t riseFall)
623623
writeWord(senseRegister, tempWord);
624624
}
625625

626-
unsigned int SX1509::interruptSource(bool clear /* =true*/)
626+
uint16_t SX1509::interruptSource(bool clear /* =true*/)
627627
{
628-
unsigned int intSource = readWord(REG_INTERRUPT_SOURCE_B);
628+
uint16_t intSource = readWord(REG_INTERRUPT_SOURCE_B);
629629
if (clear)
630630
writeWord(REG_INTERRUPT_SOURCE_B, 0xFFFF); // Clear interrupts
631631
return intSource;
632632
}
633633

634-
bool SX1509::checkInterrupt(int pin)
634+
bool SX1509::checkInterrupt(uint8_t pin)
635635
{
636636
if (interruptSource(false) & (1 << pin))
637637
return true;
@@ -671,9 +671,9 @@ void SX1509::configClock(uint8_t oscSource /*= 2*/, uint8_t oscPinFunction /*= 0
671671
writeByte(REG_MISC, regMisc);
672672
}
673673

674-
uint8_t SX1509::calculateLEDTRegister(int ms)
674+
uint8_t SX1509::calculateLEDTRegister(uint8_t ms)
675675
{
676-
int regOn1, regOn2;
676+
uint8_t regOn1, regOn2;
677677
float timeOn1, timeOn2;
678678

679679
if (_clkX == 0)
@@ -693,9 +693,9 @@ uint8_t SX1509::calculateLEDTRegister(int ms)
693693
return regOn2;
694694
}
695695

696-
uint8_t SX1509::calculateSlopeRegister(int ms, uint8_t onIntensity, uint8_t offIntensity)
696+
uint8_t SX1509::calculateSlopeRegister(uint8_t ms, uint8_t onIntensity, uint8_t offIntensity)
697697
{
698-
int regSlope1, regSlope2;
698+
uint16_t regSlope1, regSlope2;
699699
float regTime1, regTime2;
700700

701701
if (_clkX == 0)
@@ -727,7 +727,7 @@ uint8_t SX1509::calculateSlopeRegister(int ms, uint8_t onIntensity, uint8_t offI
727727
uint8_t SX1509::readByte(uint8_t registerAddress)
728728
{
729729
uint8_t readValue;
730-
unsigned int timeout = RECEIVE_TIMEOUT_VALUE;
730+
uint16_t timeout = RECEIVE_TIMEOUT_VALUE;
731731

732732
_i2cPort->beginTransmission(deviceAddress);
733733
_i2cPort->write(registerAddress);
@@ -741,14 +741,14 @@ uint8_t SX1509::readByte(uint8_t registerAddress)
741741

742742
// readWord(uint8_t registerAddress)
743743
// This function will read a two-byte word beginning at registerAddress
744-
// - A 16-bit unsigned int will be returned.
744+
// - A 16-bit uint16_t will be returned.
745745
// - The msb of the return value will contain the value read from registerAddress
746746
// - The lsb of the return value will contain the value read from registerAddress + 1
747747
uint16_t SX1509::readWord(uint8_t registerAddress)
748748
{
749749
uint16_t readValue;
750750
uint16_t msb, lsb;
751-
unsigned int timeout = RECEIVE_TIMEOUT_VALUE * 2;
751+
uint16_t timeout = RECEIVE_TIMEOUT_VALUE * 2;
752752

753753
_i2cPort->beginTransmission(deviceAddress);
754754
_i2cPort->write(registerAddress);
@@ -769,7 +769,7 @@ bool SX1509::readByte(uint8_t registerAddress, uint8_t *value)
769769

770770
// readWord(uint8_t registerAddress)
771771
// This function will read a two-byte word beginning at registerAddress
772-
// - A 16-bit unsigned int will be set in value.
772+
// - A 16-bit uint16_t will be set in value.
773773
// - The msb of the return value will contain the value read from registerAddress
774774
// - The lsb of the return value will contain the value read from registerAddress + 1
775775
// - Return boolean true if succesfull
@@ -800,7 +800,7 @@ bool SX1509::readBytes(uint8_t firstRegisterAddress, uint8_t *destination, uint8
800800

801801
if (result)
802802
{
803-
for (int i = 0; i < length; i++)
803+
for (uint8_t i = 0; i < length; i++)
804804
{
805805
destination[i] = _i2cPort->read();
806806
}
@@ -821,7 +821,7 @@ bool SX1509::writeByte(uint8_t registerAddress, uint8_t writeValue)
821821
return result && (endResult == I2C_ERROR_OK);
822822
}
823823

824-
// writeWord(uint8_t registerAddress, ungisnged int writeValue)
824+
// writeWord(uint8_t registerAddress, uint16_t writeValue)
825825
// This function writes a two-byte word to registerAddress and registerAddress + 1
826826
// - the upper byte of writeValue is written to registerAddress
827827
// - the lower byte of writeValue is written to registerAddress + 1
@@ -848,7 +848,6 @@ bool SX1509::writeBytes(uint8_t firstRegisterAddress, uint8_t *writeArray, uint8
848848
{
849849
_i2cPort->beginTransmission(deviceAddress);
850850
bool result = _i2cPort->write(firstRegisterAddress);
851-
int i = 0;
852851
result = _i2cPort->write(writeArray, length);
853852
uint8_t endResult = _i2cPort->endTransmission();
854853
return result && (endResult == I2C_ERROR_OK);

src/SparkFunSX1509.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ Distributed as-is; no warranty is given.
2727
#ifndef SparkFunSX1509_H
2828
#define SparkFunSX1509_H
2929

30+
#ifndef I2C_ERROR_OK
31+
#define I2C_ERROR_OK 0
32+
#endif
33+
3034
#define RECEIVE_TIMEOUT_VALUE 1000 // Timeout for I2C receive
3135

3236
// These are used for setting LED driver to linear or log mode:
@@ -71,10 +75,10 @@ class SX1509
7175
// Helper functions:
7276
// calculateLEDTRegister - Try to estimate an LED on/off duration register,
7377
// given the number of milliseconds and LED clock frequency.
74-
uint8_t calculateLEDTRegister(int ms);
78+
uint8_t calculateLEDTRegister(uint8_t ms);
7579
// calculateSlopeRegister - Try to estimate an LED rise/fall duration
7680
// register, given the number of milliseconds and LED clock frequency.
77-
uint8_t calculateSlopeRegister(int ms, uint8_t onIntensity, uint8_t offIntensity);
81+
uint8_t calculateSlopeRegister(uint8_t ms, uint8_t onIntensity, uint8_t offIntensity);
7882

7983
public:
8084
// -----------------------------------------------------------------------------
@@ -268,7 +272,7 @@ class SX1509
268272
// Should be a millisecond duration between 0 and 64.
269273
// Possible values are 0 (0.5), 1, 2, 4, 8, 16, 32, 64.
270274
// -----------------------------------------------------------------------------
271-
void keypad(uint8_t rows, uint8_t columns, unsigned int sleepTime = 0, uint8_t scanTime = 1, uint8_t debounceTime = 0);
275+
void keypad(uint8_t rows, uint8_t columns, uint16_t sleepTime = 0, uint8_t scanTime = 1, uint8_t debounceTime = 0);
272276

273277
// -----------------------------------------------------------------------------
274278
// readKeypad(): This function returns a 16-bit value containing the status of
@@ -280,36 +284,36 @@ class SX1509
280284
// button in that row or column is being pressed. As such, at least two
281285
// bits should be set.
282286
// -----------------------------------------------------------------------------
283-
unsigned int readKeypad();
284-
unsigned int readKeyData(); // Legacy: use readKeypad();
287+
uint16_t readKeypad();
288+
uint16_t readKeyData(); // Legacy: use readKeypad();
285289

286290
// -----------------------------------------------------------------------------
287291
// getRow(): This function returns the first active row from the return value of
288292
// readKeypad().
289293
//
290294
// Input:
291-
// - keyData: Should be the unsigned int value returned from readKeypad().
295+
// - keyData: Should be the uint16_t value returned from readKeypad().
292296
// Output:
293297
// A 16-bit value is returned. The lower 8 bits represent the up-to 8 rows,
294298
// while the MSB represents the up-to 8 columns. Bit-values of 1 indicate a
295299
// button in that row or column is being pressed. As such, at least two
296300
// bits should be set.
297301
// -----------------------------------------------------------------------------
298-
uint8_t getRow(unsigned int keyData);
302+
uint8_t getRow(uint16_t keyData);
299303

300304
// -----------------------------------------------------------------------------
301305
// getCol(): This function returns the first active column from the return value of
302306
// readKeypad().
303307
//
304308
// Input:
305-
// - keyData: Should be the unsigned int value returned from readKeypad().
309+
// - keyData: Should be the uint16_t value returned from readKeypad().
306310
// Output:
307311
// A 16-bit value is returned. The lower 8 bits represent the up-to 8 rows,
308312
// while the MSB represents the up-to 8 columns. Bit-values of 1 indicate a
309313
// button in that row or column is being pressed. As such, at least two
310314
// bits should be set.
311315
// -----------------------------------------------------------------------------
312-
uint8_t getCol(unsigned int keyData);
316+
uint8_t getCol(uint16_t keyData);
313317

314318
// -----------------------------------------------------------------------------
315319
// sync(void): this function resets the PWM/Blink/Fade counters, syncing any
@@ -392,7 +396,7 @@ class SX1509
392396
void enableInterrupt(uint8_t pin, uint8_t riseFall);
393397

394398
// -----------------------------------------------------------------------------
395-
// interruptSource(void): Returns an unsigned int representing which pin caused
399+
// interruptSource(void): Returns an uint16_t representing which pin caused
396400
// an interrupt.
397401
//
398402
// Output: 16-bit value, with a single bit set representing the pin(s) that
@@ -402,7 +406,7 @@ class SX1509
402406
// - clear: boolean commanding whether the interrupt should be cleared
403407
// after reading or not.
404408
// -----------------------------------------------------------------------------
405-
unsigned int interruptSource(bool clear = true);
409+
uint16_t interruptSource(bool clear = true);
406410

407411
// -----------------------------------------------------------------------------
408412
// checkInterrupt(void): Checks if a single pin generated an interrupt.
@@ -411,7 +415,7 @@ class SX1509
411415
// Input:
412416
// - pin: Pin to be checked for generating an input.
413417
// -----------------------------------------------------------------------------
414-
bool checkInterrupt(int pin);
418+
bool checkInterrupt(uint8_t pin);
415419

416420
// -----------------------------------------------------------------------------
417421
// configClock(uint8_t oscSource, uint8_t oscPinFunction, uint8_t oscFreqOut, uint8_t oscDivider)

0 commit comments

Comments
 (0)