Skip to content

Commit ae3e1ca

Browse files
committed
Removes redundant read methods
* Changes the name of `writeBlock()` to `writeRegion()` * `writeRegion()` now calls `writeRegisterRegionAddress()` function * `writeRegisterRegionAddress()` now has a nullptr check before indexing to an address on write
1 parent 5333ef3 commit ae3e1ca

File tree

3 files changed

+10
-204
lines changed

3 files changed

+10
-204
lines changed

src/sfeTk/sfeTkIBus.h

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class sfeTkIBus
111111
* @retval sfeTkError_t - kSTkErrOk on successful execution.
112112
*
113113
*/
114-
virtual sfeTkError_t writeBlock(const uint8_t *data, size_t length) = 0;
114+
virtual sfeTkError_t writeRegion(const uint8_t *data, size_t length) = 0;
115115

116116
/**--------------------------------------------------------------------------
117117
* @brief Write a single byte to the given register
@@ -124,45 +124,6 @@ class sfeTkIBus
124124
*/
125125
virtual sfeTkError_t writeRegisterByte(uint8_t devReg, uint8_t data) = 0;
126126

127-
/**
128-
@brief Reads a byte of data from the device.
129-
130-
@note sfeTkIBus interface method
131-
132-
@param dataToWrite The data to write to the device.
133-
@param[out] data Data to read.
134-
135-
@retval kStkErrOk on success
136-
*/
137-
virtual sfeTkError_t readByte(uint8_t dataToWrite, uint8_t &data) = 0;
138-
139-
/**
140-
@brief Reads a word of data from the device.
141-
142-
@note sfeTkIBus interface method
143-
144-
@param dataToWrite The data to write to the device.
145-
@param[out] data Data to read.
146-
147-
@retval kSTkErrOk on success
148-
*/
149-
virtual sfeTkError_t readWord(uint8_t dataToWrite, uint16_t &data) = 0;
150-
151-
/**
152-
@brief Reads a block of data from the device.
153-
154-
@note sfeTkIBus interface method
155-
156-
@param dataToWrite The data to write to the device.
157-
@param[out] data Data buffer to read into
158-
@param numBytes Number of bytes to read/length of data buffer
159-
@param[out] readBytes - Number of bytes read
160-
161-
162-
@retval kSTkErrOk on success
163-
*/
164-
virtual sfeTkError_t readBlock(uint8_t dataToWrite, uint8_t *data, size_t numBytes, size_t &readBytes) = 0;
165-
166127
/**--------------------------------------------------------------------------
167128
* @brief Write a single word (16 bit) to the given register
168129
*

src/sfeTkArdI2C.cpp

Lines changed: 8 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -116,26 +116,22 @@ sfeTkError_t sfeTkArdI2C::writeWord(uint16_t dataToWrite)
116116
if (!_i2cPort)
117117
return kSTkErrBusNotInit;
118118

119-
return writeBlock((uint8_t *)&dataToWrite, sizeof(uint16_t));
119+
return writeRegion((uint8_t *)&dataToWrite, sizeof(uint16_t));
120120
}
121121

122122
//---------------------------------------------------------------------------------
123-
// writeBlock()
123+
// writeRegion()
124124
//
125125
// Writes a word to the device, without indexing to a register.
126126
//
127127
// Returns true on success, false on failure
128128
//
129-
sfeTkError_t sfeTkArdI2C::writeBlock(const uint8_t *data, size_t length)
129+
sfeTkError_t sfeTkArdI2C::writeRegion(const uint8_t *data, size_t length)
130130
{
131131
if (!_i2cPort)
132132
return kSTkErrBusNotInit;
133-
134133
// do the Arduino I2C work
135-
_i2cPort->beginTransmission(address());
136-
_i2cPort->write(data, (int)length);
137-
138-
return _i2cPort->endTransmission() == 0 ? kSTkErrOk : kSTkErrFail;
134+
return writeRegisterRegionAddress(nullptr, 0, data, length) == 0 ? kSTkErrOk : kSTkErrFail;
139135
}
140136

141137
//---------------------------------------------------------------------------------
@@ -188,7 +184,10 @@ sfeTkError_t sfeTkArdI2C::writeRegisterRegionAddress(uint8_t *devReg, size_t reg
188184
return kSTkErrBusNotInit;
189185

190186
_i2cPort->beginTransmission(address());
191-
_i2cPort->write(devReg, regLength);
187+
188+
if(devReg != nullptr && regLength > 0)
189+
_i2cPort->write(devReg, regLength);
190+
192191
_i2cPort->write(data, (int)length);
193192

194193
return _i2cPort->endTransmission() ? kSTkErrFail : kSTkErrOk;
@@ -219,122 +218,7 @@ sfeTkError_t sfeTkArdI2C::writeRegister16Region(uint16_t devReg, const uint8_t *
219218
return writeRegisterRegionAddress((uint8_t *)&devReg, 2, data, length);
220219
}
221220

222-
//---------------------------------------------------------------------------------
223-
// readByte()
224-
//
225-
// Reads a byte from the device, without indexing to a register.
226-
//
227-
// Returns true on success, false on failure
228-
//
229-
sfeTkError_t sfeTkArdI2C::readByte(uint8_t dataToWrite, uint8_t &dataToRead)
230-
{
231-
if (!_i2cPort)
232-
return kSTkErrBusNotInit;
233-
234-
// Return value
235-
uint8_t result = 0;
236-
237-
int nData = 0;
238-
239-
_i2cPort->beginTransmission(address());
240-
_i2cPort->write(dataToWrite);
241-
_i2cPort->endTransmission(stop());
242-
_i2cPort->requestFrom(address(), (uint8_t)1);
243-
244-
while (_i2cPort->available()) // slave may send less than requested
245-
{
246-
result = _i2cPort->read(); // receive a byte as a proper uint8_t
247-
nData++;
248-
}
249-
250-
if (nData == sizeof(uint8_t)) // Only update outputPointer if a single byte was returned
251-
dataToRead = result;
252-
253-
return (nData == sizeof(uint8_t) ? kSTkErrOk : kSTkErrFail);
254-
}
255-
256-
257-
//---------------------------------------------------------------------------------
258-
// readWord()
259-
//
260-
// Reads a word from the device, without indexing to a register.
261-
//
262-
// Returns true on success, false on failure
263-
//
264-
sfeTkError_t sfeTkArdI2C::readWord(uint8_t dataToWrite, uint16_t &dataToRead)
265-
{
266-
if (!_i2cPort)
267-
return kSTkErrBusNotInit;
268-
269-
size_t nRead;
270-
sfeTkError_t retval = readBlock(dataToWrite, (uint8_t *)&dataToRead, sizeof(uint16_t), nRead);
271-
272-
return (retval == kSTkErrOk && nRead == sizeof(uint16_t) ? kSTkErrOk : retval);
273-
}
274221

275-
//---------------------------------------------------------------------------------
276-
// readBlock()
277-
//
278-
// Reads a block of data from the device, without indexing to a register.
279-
//
280-
// Returns the number of bytes written, < 0 is an error
281-
//
282-
sfeTkError_t sfeTkArdI2C::readBlock(uint8_t dataToWrite, uint8_t *data, size_t numBytes, size_t &readBytes)
283-
{
284-
285-
// got port
286-
if (!_i2cPort)
287-
return kSTkErrBusNotInit;
288-
289-
// Buffer valid?
290-
if (!data)
291-
return kSTkErrBusNullBuffer;
292-
293-
readBytes = 0;
294-
295-
uint16_t nOrig = numBytes; // original number of bytes.
296-
uint8_t nChunk;
297-
uint16_t nReturned;
298-
uint16_t i; // counter in loop
299-
bool bFirstInter = true; // Flag for first iteration - used to send devRegister
300-
301-
while (numBytes > 0)
302-
{
303-
if (bFirstInter)
304-
{
305-
_i2cPort->beginTransmission(address());
306-
_i2cPort->write(dataToWrite);
307-
if (_i2cPort->endTransmission(stop()) != 0)
308-
return kSTkErrFail; // error with the end transmission
309-
310-
bFirstInter = false;
311-
}
312-
313-
// We're chunking in data - keeping the max chunk to kMaxI2CBufferLength
314-
nChunk = numBytes > _bufferChunkSize ? _bufferChunkSize : numBytes;
315-
316-
// Request the bytes. If this is the last chunk, always send a stop
317-
nReturned = _i2cPort->requestFrom((int)address(), (int)nChunk, (int)(nChunk == numBytes ? true : stop()));
318-
319-
// No data returned, no dice
320-
if (nReturned == 0)
321-
return kSTkErrBusUnderRead; // error
322-
323-
// Copy the retrieved data chunk to the current index in the data segment
324-
for (i = 0; i < nReturned; i++)
325-
*data++ = _i2cPort->read();
326-
327-
// Decrement the amount of data received from the overall data request amount
328-
numBytes = numBytes - nReturned;
329-
330-
} // end while
331-
332-
readBytes = nOrig - numBytes; // Bytes read.
333-
334-
return (readBytes == nOrig) ? kSTkErrOk : kSTkErrBusUnderRead; // Success
335-
}
336-
337-
//---------------------------------------------------------------------------------
338222

339223
/**
340224
* @brief Reads an array of bytes to a register on the target address. Supports any address size

src/sfeTkArdI2C.h

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class sfeTkArdI2C : public sfeTkII2C
136136
137137
@retval returns kStkErrOk on success
138138
*/
139-
sfeTkError_t writeBlock(const uint8_t *data, size_t length);
139+
sfeTkError_t writeRegion(const uint8_t *data, size_t length);
140140

141141
/**
142142
@brief Write a single byte to the given register
@@ -186,45 +186,6 @@ class sfeTkArdI2C : public sfeTkII2C
186186
*/
187187
sfeTkError_t writeRegister16Region(uint16_t devReg, const uint8_t *data, size_t length);
188188

189-
/**
190-
@brief Reads a byte of data from the device.
191-
192-
@note sfeTkIBus interface method
193-
194-
@param dataToWrite The data to write to the device.
195-
@param[out] data Data to read.
196-
197-
@retval kStkErrOk on success
198-
*/
199-
sfeTkError_t readByte(uint8_t dataToWrite, uint8_t &data);
200-
201-
/**
202-
@brief Reads a word of data from the device.
203-
204-
@note sfeTkIBus interface method
205-
206-
@param dataToWrite The data to write to the device.
207-
@param[out] data Data to read.
208-
209-
@retval kSTkErrOk on success
210-
*/
211-
sfeTkError_t readWord(uint8_t dataToWrite, uint16_t &data);
212-
213-
/**
214-
@brief Reads a block of data from the device.
215-
216-
@note sfeTkIBus interface method
217-
218-
@param dataToWrite The data to write to the device.
219-
@param[out] data Data buffer to read into
220-
@param numBytes Number of bytes to read/length of data buffer
221-
@param[out] readBytes - Number of bytes read
222-
223-
224-
@retval kSTkErrOk on success
225-
*/
226-
sfeTkError_t readBlock(uint8_t dataToWrite, uint8_t *data, size_t numBytes, size_t &readBytes);
227-
228189
/**
229190
@brief Reads a byte of data from the given register.
230191

0 commit comments

Comments
 (0)