Skip to content

Add writeWord writeBlock #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/sfeTk/sfeTkIBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,32 @@ class sfeTkIBus
{
public:
/**--------------------------------------------------------------------------
* @brief Write a single byte to the device*
* @brief Send a single byte to the device*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all the methods use a "write*" naming pattern, shouldn't this be Write?

FWIW: Normally send/receive are used with network based operations - at least in my experience ....

Copy link
Contributor Author

@edspark edspark Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're completely right. I was trying to get at the fact that it doesn't physically write bits/bytes to a register and so "send" seemed to fit. However, you're write (a joke there). I'll adjust to this: "Write a single byte, without indexing to a register". Something to that effect. I also have a few more things that I want to add to this pull request.

* @param data Data to write.
*
* @retval sfeTkError_t - kSTkErrOk on successful execution.
*
*/
virtual sfeTkError_t writeByte(uint8_t data) = 0;

/**--------------------------------------------------------------------------
* @brief Send a word to the device.
* @param data Data to write.
*
* @retval sfeTkError_t - kSTkErrOk on successful execution.
*
*/
virtual sfeTkError_t writeWord(uint16_t data) = 0;

/**--------------------------------------------------------------------------
* @brief Send an array of data to the device.
* @param data Data to write.
*
* @retval sfeTkError_t - kSTkErrOk on successful execution.
*
*/
virtual sfeTkError_t writeRegion(const uint8_t *data, size_t length) = 0;

/**--------------------------------------------------------------------------
* @brief Write a single byte to the given register
*
Expand Down
38 changes: 35 additions & 3 deletions src/sfeTkArdI2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "sfeTkArdI2C.h"
#include <cstddef>
#include <cstdint>

//---------------------------------------------------------------------------------
// init()
Expand Down Expand Up @@ -87,7 +89,7 @@ sfeTkError_t sfeTkArdI2C::ping()
//---------------------------------------------------------------------------------
// writeByte()
//
// Writes a single byte to the device.
// Writes a single byte to the device, without indexing to a register.
//
// Returns true on success, false on failure
//
Expand All @@ -102,6 +104,33 @@ sfeTkError_t sfeTkArdI2C::writeByte(uint8_t dataToWrite)
return _i2cPort->endTransmission() == 0 ? kSTkErrOk : kSTkErrFail;
}

//---------------------------------------------------------------------------------
// writeWord()
//
// Writes a word to the device, without indexing to a register.
//
// Returns true on success, false on failure
//
sfeTkError_t sfeTkArdI2C::writeWord(uint16_t dataToWrite)
{
if (!_i2cPort)
return kSTkErrBusNotInit;

return writeRegion((uint8_t *)&dataToWrite, sizeof(uint16_t));
}

//---------------------------------------------------------------------------------
// writeRegion()
//
// Writes a word to the device, without indexing to a register.
//
// Returns true on success, false on failure
//
sfeTkError_t sfeTkArdI2C::writeRegion(const uint8_t *data, size_t length)
{
return writeRegisterRegionAddress(nullptr, 0, data, length) == 0 ? kSTkErrOk : kSTkErrFail;
}

//---------------------------------------------------------------------------------
// writeRegisterByte()
//
Expand Down Expand Up @@ -152,7 +181,10 @@ sfeTkError_t sfeTkArdI2C::writeRegisterRegionAddress(uint8_t *devReg, size_t reg
return kSTkErrBusNotInit;

_i2cPort->beginTransmission(address());
_i2cPort->write(devReg, regLength);

if(devReg != nullptr && regLength > 0)
_i2cPort->write(devReg, regLength);

_i2cPort->write(data, (int)length);

return _i2cPort->endTransmission() ? kSTkErrFail : kSTkErrOk;
Expand Down Expand Up @@ -183,7 +215,7 @@ sfeTkError_t sfeTkArdI2C::writeRegister16Region(uint16_t devReg, const uint8_t *
return writeRegisterRegionAddress((uint8_t *)&devReg, 2, data, length);
}

//---------------------------------------------------------------------------------


/**
* @brief Reads an array of bytes to a register on the target address. Supports any address size
Expand Down
22 changes: 21 additions & 1 deletion src/sfeTkArdI2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class sfeTkArdI2C : public sfeTkII2C
sfeTkError_t ping();

/**
@brief Write a single byte to the device
@brief Sends a single byte to the device
@note sfeTkIBus interface method

@param data Data to write.
Expand All @@ -118,6 +118,26 @@ class sfeTkArdI2C : public sfeTkII2C
*/
sfeTkError_t writeByte(uint8_t data);

/**
@brief Sends a word to the device.
@note sfeTkIBus interface method

@param data Data to write.

@retval returns kStkErrOk on success
*/
sfeTkError_t writeWord(uint16_t data);

/**
@brief Sends a block of data to the device.
@note sfeTkIBus interface method

@param data Data to write.

@retval returns kStkErrOk on success
*/
sfeTkError_t writeRegion(const uint8_t *data, size_t length);

/**
@brief Write a single byte to the given register
@note sfeTkIBus interface method
Expand Down
45 changes: 43 additions & 2 deletions src/sfeTkArdSPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ sfeTkError_t sfeTkArdSPI::init(bool bInit)
}

//---------------------------------------------------------------------------------
// writeRegisterByte()
// writeByte()
//
// Writes a single byte to the device.
//
Expand All @@ -108,6 +108,46 @@ sfeTkError_t sfeTkArdSPI::writeByte(uint8_t dataToWrite)
return kSTkErrOk;
}

//---------------------------------------------------------------------------------
// writeWord()
//
// Writes a word to the device without indexing to a register.
//
// Returns kSTkErrOk on success
//
sfeTkError_t sfeTkArdSPI::writeWord(uint16_t dataToWrite)
{
return writeRegion((uint8_t *)&dataToWrite, sizeof(uint8_t)) > 0;
}


//---------------------------------------------------------------------------------
// writeRegion()
//
// Writes an array of data to the device without indexing to a register.
//
// Returns kSTkErrOk on success
//
sfeTkError_t sfeTkArdSPI::writeRegion(const uint8_t *dataToWrite, size_t length)
{

if (!_spiPort)
return kSTkErrBusNotInit;

_spiPort->beginTransaction(_sfeSPISettings);
// Signal communication start
digitalWrite(cs(), LOW);

for (size_t i = 0; i < length; i++)
_spiPort->transfer(*dataToWrite++);

// End communication
digitalWrite(cs(), HIGH);
_spiPort->endTransaction();

return kSTkErrOk;
}

//---------------------------------------------------------------------------------
// writeRegisterByte()
//
Expand Down Expand Up @@ -164,6 +204,7 @@ sfeTkError_t sfeTkArdSPI::writeRegisterRegion(uint8_t devReg, const uint8_t *dat

// Signal communication start
digitalWrite(cs(), LOW);

_spiPort->transfer(devReg);

for (size_t i = 0; i < length; i++)
Expand Down Expand Up @@ -278,4 +319,4 @@ sfeTkError_t sfeTkArdSPI::readRegister16Region(uint16_t devReg, uint8_t *data, s
readBytes = numBytes;

return kSTkErrOk;
}
}
19 changes: 19 additions & 0 deletions src/sfeTkArdSPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,25 @@ class sfeTkArdSPI : public sfeTkISPI
*/
sfeTkError_t writeByte(uint8_t data);

/**
@brief Write a word to the device without indexing to a register.

@param data Data to write.

@retval sfeTkError_t - kSTkErrOk on success
*/
sfeTkError_t writeWord(uint16_t data);

/**
@brief Write an array of data to the device without indexing to a register.

@param data Data to write
@param length Length of Data

@retval sfeTkError_t - kSTkErrOk on success
*/
sfeTkError_t writeRegion(const uint8_t *data, size_t length);

/**
@brief Write a single byte to the given register

Expand Down