Skip to content

Commit 65b573d

Browse files
committed
Change writeRegisterByte so it returns int instead of bool
1 parent 9df046c commit 65b573d

File tree

3 files changed

+40
-32
lines changed

3 files changed

+40
-32
lines changed

src/SparkFun_Qwiic_KX13X.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ bool QwDevKX13X::initialize(uint8_t settings)
103103
//
104104
bool QwDevKX13X::softwareReset()
105105
{
106+
enableAccel(false); // Clear the PC1 bit in CNTL1
107+
106108
sfe_kx13x_cntl2_bitfield_t cntl2;
107109
cntl2.all = 0;
108110
cntl2.bits.srst = 1; // This is a long winded, but definitive way of setting the software reset bit
@@ -123,7 +125,7 @@ bool QwDevKX13X::softwareReset()
123125
//////////////////////////////////////////////////
124126
// enableAccel()
125127
//
126-
// Enables acceleromter data. In addition
128+
// Enables accelerometer data. In addition
127129
// some settings can only be set when the accelerometer is
128130
// powered down
129131
//
@@ -571,7 +573,7 @@ bool QwDevKX13X::configureInterruptPin(uint8_t pinVal)
571573
//
572574
bool QwDevKX13X::enablePhysInterrupt(bool enable, uint8_t pin)
573575
{
574-
int retVal;
576+
int retVal = -1;
575577
uint8_t tempVal;
576578

577579
if (pin > 2)
@@ -589,7 +591,7 @@ bool QwDevKX13X::enablePhysInterrupt(bool enable, uint8_t pin)
589591
inc1.bits.ien1 = enable; // This is a long winded but definitive way of setting/clearing the enable bit
590592
tempVal = inc1.all;
591593

592-
writeRegisterByte(SFE_KX13X_INC1, tempVal);
594+
retVal = writeRegisterByte(SFE_KX13X_INC1, tempVal);
593595
}
594596

595597
if (pin == 2)
@@ -604,10 +606,10 @@ bool QwDevKX13X::enablePhysInterrupt(bool enable, uint8_t pin)
604606
inc5.bits.ien2 = enable; // This is a long winded but definitive way of setting/clearing the enable bit
605607
tempVal = inc5.all;
606608

607-
writeRegisterByte(SFE_KX13X_INC5, tempVal);
609+
retVal = writeRegisterByte(SFE_KX13X_INC5, tempVal);
608610
}
609611

610-
return true;
612+
return (retVal == 0);
611613
}
612614

613615
//////////////////////////////////////////////////
@@ -621,7 +623,7 @@ bool QwDevKX13X::enablePhysInterrupt(bool enable, uint8_t pin)
621623
//
622624
bool QwDevKX13X::setPinMode(bool activeHigh, uint8_t pin)
623625
{
624-
int retVal;
626+
int retVal = -1;
625627
uint8_t tempVal;
626628

627629
if (pin > 2)
@@ -639,7 +641,7 @@ bool QwDevKX13X::setPinMode(bool activeHigh, uint8_t pin)
639641
inc1.bits.iea1 = activeHigh; // This is a long winded but definitive way of setting/clearing the level bit
640642
tempVal = inc1.all;
641643

642-
writeRegisterByte(SFE_KX13X_INC1, tempVal);
644+
retVal = writeRegisterByte(SFE_KX13X_INC1, tempVal);
643645
}
644646

645647
if (pin == 2)
@@ -654,10 +656,10 @@ bool QwDevKX13X::setPinMode(bool activeHigh, uint8_t pin)
654656
inc5.bits.iea2 = activeHigh; // This is a long winded but definitive way of setting/clearing the level bit
655657
tempVal = inc5.all;
656658

657-
writeRegisterByte(SFE_KX13X_INC5, tempVal);
659+
retVal = writeRegisterByte(SFE_KX13X_INC5, tempVal);
658660
}
659661

660-
return true;
662+
return (retVal == 0);
661663
}
662664

663665
//////////////////////////////////////////////////
@@ -672,7 +674,7 @@ bool QwDevKX13X::setPinMode(bool activeHigh, uint8_t pin)
672674
//
673675
bool QwDevKX13X::setLatchControl(bool pulsed, uint8_t pin)
674676
{
675-
int retVal;
677+
int retVal = -1;
676678
uint8_t tempVal;
677679

678680
if (pin > 2)
@@ -690,7 +692,7 @@ bool QwDevKX13X::setLatchControl(bool pulsed, uint8_t pin)
690692
inc1.bits.iel1 = pulsed; // This is a long winded but definitive way of setting/clearing the latch bit
691693
tempVal = inc1.all;
692694

693-
writeRegisterByte(SFE_KX13X_INC1, tempVal);
695+
retVal = writeRegisterByte(SFE_KX13X_INC1, tempVal);
694696
}
695697

696698
if (pin == 2)
@@ -705,10 +707,10 @@ bool QwDevKX13X::setLatchControl(bool pulsed, uint8_t pin)
705707
inc5.bits.iel2 = pulsed; // This is a long winded but definitive way of setting/clearing the latch bit
706708
tempVal = inc5.all;
707709

708-
writeRegisterByte(SFE_KX13X_INC5, tempVal);
710+
retVal = writeRegisterByte(SFE_KX13X_INC5, tempVal);
709711
}
710712

711-
return true;
713+
return (retVal == 0);
712714
}
713715

714716
//////////////////////////////////////////////////
@@ -722,7 +724,7 @@ bool QwDevKX13X::setLatchControl(bool pulsed, uint8_t pin)
722724
//
723725
bool QwDevKX13X::setPulseWidth(uint8_t width, uint8_t pin)
724726
{
725-
int retVal;
727+
int retVal = -1;
726728
uint8_t tempVal;
727729

728730
if ((width > 3) || (pin > 2))
@@ -740,7 +742,7 @@ bool QwDevKX13X::setPulseWidth(uint8_t width, uint8_t pin)
740742
inc1.bits.pw1 = width; // This is a long winded but definitive way of setting the pulse width
741743
tempVal = inc1.all;
742744

743-
writeRegisterByte(SFE_KX13X_INC1, tempVal);
745+
retVal = writeRegisterByte(SFE_KX13X_INC1, tempVal);
744746
}
745747

746748
if (pin == 2)
@@ -755,10 +757,10 @@ bool QwDevKX13X::setPulseWidth(uint8_t width, uint8_t pin)
755757
inc5.bits.pw2 = width; // This is a long winded but definitive way of setting the pulse width
756758
tempVal = inc5.all;
757759

758-
writeRegisterByte(SFE_KX13X_INC5, tempVal);
760+
retVal = writeRegisterByte(SFE_KX13X_INC5, tempVal);
759761
}
760762

761-
return true;
763+
return (retVal == 0);
762764
}
763765

764766
//////////////////////////////////////////////////
@@ -1558,7 +1560,7 @@ bool QwDevKX13X::forceWake()
15581560
//
15591561
int QwDevKX13X::readRegisterRegion(uint8_t reg, uint8_t *data, uint16_t len)
15601562
{
1561-
return (int)_sfeBus->readRegisterRegion(_i2cAddress, reg, data, len);
1563+
return (_sfeBus->readRegisterRegion(_i2cAddress, reg, data, len));
15621564
}
15631565

15641566
//////////////////////////////////////////////////////////////////////////////////
@@ -1574,7 +1576,7 @@ int QwDevKX13X::readRegisterRegion(uint8_t reg, uint8_t *data, uint16_t len)
15741576
//
15751577
int QwDevKX13X::writeRegisterRegion(uint8_t reg, uint8_t *data, uint16_t len)
15761578
{
1577-
return (int)_sfeBus->writeRegisterRegion(_i2cAddress, reg, data, len);
1579+
return (_sfeBus->writeRegisterRegion(_i2cAddress, reg, data, len));
15781580
}
15791581

15801582
//////////////////////////////////////////////////////////////////////////////////
@@ -1590,9 +1592,7 @@ int QwDevKX13X::writeRegisterRegion(uint8_t reg, uint8_t *data, uint16_t len)
15901592
//
15911593
int QwDevKX13X::writeRegisterByte(uint8_t reg, uint8_t data)
15921594
{
1593-
if (_sfeBus->writeRegisterByte(_i2cAddress, reg, data))
1594-
return 0;
1595-
return -1;
1595+
return (_sfeBus->writeRegisterByte(_i2cAddress, reg, data));
15961596
}
15971597

15981598
//***************************************** KX132 *********************************************************

src/sfe_bus.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,16 @@ bool QwI2C::ping(uint8_t i2c_address)
121121
//
122122
// Write a byte to a register
123123

124-
bool QwI2C::writeRegisterByte(uint8_t i2c_address, uint8_t offset, uint8_t dataToWrite)
124+
int QwI2C::writeRegisterByte(uint8_t i2c_address, uint8_t offset, uint8_t dataToWrite)
125125
{
126126

127127
if (!_i2cPort)
128-
return false;
128+
return -1;
129129

130130
_i2cPort->beginTransmission(i2c_address);
131131
_i2cPort->write(offset);
132132
_i2cPort->write(dataToWrite);
133-
return _i2cPort->endTransmission() == 0;
133+
return _i2cPort->endTransmission() == 0 ? 0 : -1; // 0 = success, -1 = error
134134
}
135135

136136

@@ -143,11 +143,14 @@ bool QwI2C::writeRegisterByte(uint8_t i2c_address, uint8_t offset, uint8_t dataT
143143
int QwI2C::writeRegisterRegion(uint8_t i2c_address, uint8_t offset, const uint8_t *data, uint16_t length)
144144
{
145145

146+
if (!_i2cPort)
147+
return -1;
148+
146149
_i2cPort->beginTransmission(i2c_address);
147150
_i2cPort->write(offset);
148151
_i2cPort->write(data, (int)length);
149152

150-
return _i2cPort->endTransmission() ? -1 : 0; // -1 = error, 0 = success
153+
return _i2cPort->endTransmission() == 0 ? 0 : -1; // 0 = success, -1 = error
151154
}
152155

153156
////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -282,11 +285,11 @@ bool SfeSPI::ping(uint8_t i2c_address)
282285
//
283286
// Write a byte to a register
284287

285-
bool SfeSPI::writeRegisterByte(uint8_t i2c_address, uint8_t offset, uint8_t dataToWrite)
288+
int SfeSPI::writeRegisterByte(uint8_t i2c_address, uint8_t offset, uint8_t dataToWrite)
286289
{
287290

288291
if( !_spiPort )
289-
return false;
292+
return -1;
290293

291294
// Apply settings
292295
_spiPort->beginTransaction(_sfeSPISettings);
@@ -300,7 +303,7 @@ bool SfeSPI::writeRegisterByte(uint8_t i2c_address, uint8_t offset, uint8_t data
300303
digitalWrite(_cs, HIGH);
301304
_spiPort->endTransaction();
302305

303-
return true;
306+
return 0;
304307
}
305308

306309

@@ -312,6 +315,9 @@ bool SfeSPI::writeRegisterByte(uint8_t i2c_address, uint8_t offset, uint8_t data
312315
int SfeSPI::writeRegisterRegion(uint8_t i2c_address, uint8_t offset, const uint8_t *data, uint16_t length)
313316
{
314317

318+
if( !_spiPort )
319+
return -1;
320+
315321
int i;
316322

317323
// Apply settings
@@ -328,6 +334,7 @@ int SfeSPI::writeRegisterRegion(uint8_t i2c_address, uint8_t offset, const uint8
328334
// End communication
329335
digitalWrite(_cs, HIGH);
330336
_spiPort->endTransaction();
337+
331338
return 0;
332339
}
333340

@@ -363,6 +370,7 @@ int SfeSPI::readRegisterRegion(uint8_t addr, uint8_t reg, uint8_t *data, uint16_
363370
// End transaction
364371
digitalWrite(_cs, HIGH);
365372
_spiPort->endTransaction();
373+
366374
return 0;
367375

368376
}

src/sfe_bus.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class QwIDeviceBus
6262

6363
virtual bool ping(uint8_t address) = 0;
6464

65-
virtual bool writeRegisterByte(uint8_t address, uint8_t offset, uint8_t data) = 0;
65+
virtual int writeRegisterByte(uint8_t address, uint8_t offset, uint8_t data) = 0;
6666

6767
virtual int writeRegisterRegion(uint8_t address, uint8_t offset, const uint8_t* data, uint16_t length) = 0;
6868

@@ -84,7 +84,7 @@ class QwI2C : public QwIDeviceBus
8484

8585
bool ping(uint8_t address);
8686

87-
bool writeRegisterByte(uint8_t address, uint8_t offset, uint8_t data);
87+
int writeRegisterByte(uint8_t address, uint8_t offset, uint8_t data);
8888

8989
int writeRegisterRegion(uint8_t address, uint8_t offset, const uint8_t* data, uint16_t length);
9090

@@ -111,7 +111,7 @@ class SfeSPI : public QwIDeviceBus
111111

112112
bool ping(uint8_t address);
113113

114-
bool writeRegisterByte(uint8_t address, uint8_t offset, uint8_t data);
114+
int writeRegisterByte(uint8_t address, uint8_t offset, uint8_t data);
115115

116116
int writeRegisterRegion(uint8_t address, uint8_t offset, const uint8_t* data, uint16_t length);
117117

0 commit comments

Comments
 (0)