Skip to content

Commit c316bdb

Browse files
committed
Trap bad requests
1 parent 8acb8d5 commit c316bdb

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/sfe_bus.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ namespace sfe_KX13X
163163
if (!_i2cPort)
164164
return -1;
165165

166-
int i; // counter in loop
166+
int i; // counter in loop
167167
int failCount = 0; // Keep track of how many times nReturned is != nChunk
168168

169-
while ((numBytes > 0) && (failCount < 5)) // Give up after 5 bad requests
169+
while ((numBytes > 0) && (failCount < 2)) // Give up after 2 bad requests
170170
{
171171
_i2cPort->beginTransmission(addr);
172172
_i2cPort->write(reg); // Write the register address we want to read from
@@ -175,7 +175,7 @@ namespace sfe_KX13X
175175

176176
// We're chunking in data - keeping the max chunk to kMaxI2CBufferLength
177177
// The register address counts as one byte so limit nChunk to kChunkSize -1
178-
nChunk = numBytes > (kChunkSize -1) ? (kChunkSize -1) : numBytes;
178+
nChunk = numBytes > (kChunkSize - 1) ? (kChunkSize - 1) : numBytes;
179179

180180
nReturned = _i2cPort->requestFrom((int)addr, (int)nChunk, (int)true); // Always send a stop
181181

@@ -201,7 +201,7 @@ namespace sfe_KX13X
201201

202202
} // end while
203203

204-
return 0; // Success
204+
return (numBytes == 0 ? 0 : -1); // 0 = success (all bytes read), -1 = error
205205
}
206206

207207
//////////////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)