diff --git a/src/sfTk/sfTkIBus.h b/src/sfTk/sfTkIBus.h index 5536dff..7cf9228 100644 --- a/src/sfTk/sfTkIBus.h +++ b/src/sfTk/sfTkIBus.h @@ -462,10 +462,11 @@ class sfTkIBus * @param data The data to buffer to read into * @param numBytes The length of the data buffer * @param readBytes[out] The number of bytes read + * @param read_delay After sending the address, delay in milliseconds before reading the data * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code */ virtual sfTkError_t readRegister(uint8_t *devReg, size_t regLength, uint8_t *data, size_t numBytes, - size_t &readBytes) = 0; + size_t &readBytes, uint32_t read_delay = 0) = 0; /**-------------------------------------------------------------------------- * @brief Read a single byte from the given register * diff --git a/src/sfTk/sfTkISerialBus.h b/src/sfTk/sfTkISerialBus.h index 0ece81d..085e41a 100644 --- a/src/sfTk/sfTkISerialBus.h +++ b/src/sfTk/sfTkISerialBus.h @@ -17,6 +17,7 @@ #include "sfTkError.h" #include "sfTkIBus.h" #include "sfTkISerial.h" +#include "sfToolkit.h" // clang-format on const uint8_t ksfTkBusTypeSerialBus = 0x03; @@ -81,24 +82,30 @@ class sfTkISerialBus : sfTkIBus * @param data The data to buffer to read into * @param numBytes The length of the data buffer * @param readBytes[out] The number of bytes read + * @param read_delay After sending the address, delay in milliseconds before reading the data * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code */ virtual sfTkError_t readRegister(uint8_t *devReg, size_t regLength, uint8_t *data, size_t numBytes, - size_t &readBytes) override + size_t &readBytes, uint32_t read_delay = 0) override { // Buffer valid? if (!data) return ksfTkErrBusNullBuffer; + if (devReg == nullptr || regLength == 0) + return ksfTkErrInvalidParam; + sfTkError_t retVal = ksfTkErrOk; // Do we have a register? If so, write it, else skip. - if (devReg != nullptr && regLength > 0) - retVal = write(devReg, regLength); + retVal = write(devReg, regLength); if (retVal != ksfTkErrOk) return retVal; + if (read_delay) + sftk_delay_ms(read_delay); + // Read the data. retVal = read(data, numBytes, readBytes); diff --git a/src/sfTkArdI2C.cpp b/src/sfTkArdI2C.cpp index 422ffa8..ec9743b 100644 --- a/src/sfTkArdI2C.cpp +++ b/src/sfTkArdI2C.cpp @@ -138,10 +138,11 @@ sfTkError_t sfTkArdI2C::writeRegister(uint8_t *devReg, size_t regLength, const u * @param data The data to buffer to read into * @param numBytes The length of the data buffer * @param readBytes[out] The number of bytes read + * @param read_delay After sending the address, delay in milliseconds before reading the data * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code */ sfTkError_t sfTkArdI2C::readRegister(uint8_t *devReg, size_t regLength, uint8_t *data, size_t numBytes, - size_t &readBytes) + size_t &readBytes, uint32_t read_delay) { // got port if (!_i2cPort) @@ -151,6 +152,9 @@ sfTkError_t sfTkArdI2C::readRegister(uint8_t *devReg, size_t regLength, uint8_t if (!data) return ksfTkErrBusNullBuffer; + if (devReg == nullptr || regLength == 0) + return ksfTkErrInvalidParam; + readBytes = 0; uint16_t nOrig = numBytes; // original number of bytes. @@ -171,6 +175,10 @@ sfTkError_t sfTkArdI2C::readRegister(uint8_t *devReg, size_t regLength, uint8_t return ksfTkErrFail; // error with the end transmission bFirstInter = false; + + // Was a delay requested between write addr and read bytes + if (read_delay > 0) + delay(read_delay); } // We're chunking in data - keeping the max chunk to kMaxI2CBufferLength diff --git a/src/sfTkArdI2C.h b/src/sfTkArdI2C.h index e24a494..8ab38b7 100644 --- a/src/sfTkArdI2C.h +++ b/src/sfTkArdI2C.h @@ -115,9 +115,11 @@ class sfTkArdI2C : public sfTkII2C * @param data Pointer to the buffer where the read data will be stored. * @param numBytes Number of bytes to read from the register. * @param readBytes Reference to a variable where the number of bytes actually read will be stored. + * @param read_delay After sending the address, delay in milliseconds before reading the data * @return sfTkError_t Error code indicating the success or failure of the read operation. */ - sfTkError_t readRegister(uint8_t *devReg, size_t regLength, uint8_t *data, size_t numBytes, size_t &readBytes); + sfTkError_t readRegister(uint8_t *devReg, size_t regLength, uint8_t *data, size_t numBytes, size_t &readBytes, + uint32_t read_delay = 0); // Buffer size chunk getter/setter /** @brief set the buffer chunk size diff --git a/src/sfTkArdSPI.cpp b/src/sfTkArdSPI.cpp index 0434df8..40b62ab 100644 --- a/src/sfTkArdSPI.cpp +++ b/src/sfTkArdSPI.cpp @@ -163,7 +163,7 @@ sfTkError_t sfTkArdSPI::writeRegister(uint16_t devReg, const uint16_t *data, siz // Returns ksfTkErrOk on success // sfTkError_t sfTkArdSPI::readRegister(uint8_t *devReg, size_t regLength, uint8_t *data, size_t numBytes, - size_t &readBytes) + size_t &readBytes, uint32_t read_delay) { if (!_spiPort) @@ -190,6 +190,9 @@ sfTkError_t sfTkArdSPI::readRegister(uint8_t *devReg, size_t regLength, uint8_t _spiPort->endTransaction(); return ksfTkErrInvalidParam; } + // Was a delay requested between write addr and read bytes + if (read_delay > 0) + delay(read_delay); // data now! for (size_t i = 0; i < numBytes; i++) diff --git a/src/sfTkArdSPI.h b/src/sfTkArdSPI.h index 1998adc..3b42bbe 100644 --- a/src/sfTkArdSPI.h +++ b/src/sfTkArdSPI.h @@ -148,9 +148,11 @@ class sfTkArdSPI : public sfTkISPI * @param data Pointer to the buffer where the read data will be stored. * @param numBytes Number of bytes to read from the register. * @param readBytes Reference to a variable where the number of bytes actually read will be stored. + * @param read_delay After sending the address, delay in milliseconds before reading the data * @return sfTkError_t Error code indicating the success or failure of the read operation. */ - sfTkError_t readRegister(uint8_t *devReg, size_t regLength, uint8_t *data, size_t numBytes, size_t &readBytes); + sfTkError_t readRegister(uint8_t *devReg, size_t regLength, uint8_t *data, size_t numBytes, size_t &readBytes, + uint32_t read_delay = 0); /** @brief Reads a block of data from the given register.