Skip to content

Conversation

@SFE-Brudnerd
Copy link
Contributor

After the previous discussion based on Dryw's PR, I rewrote the interface with the discussed changes. SPI is unchanged in this PR. Tested with the OPT4048 library in the examples folder and it works.

sfe-SparkFro and others added 12 commits October 3, 2023 18:13
Remove I2C device address requirement for SPI busses
Add error and warning codes
Also refactor some things for consistency
Add gitattributes and gitignore files.
Rename LICENSE to LICENSE.md for language recognition.
Update library description.
Update top header text to SPDX licensing.
Refactor base class.
Refactor base i2c class.
Create Device settings class.
Add helper utility functions for i2c.
Refactor arduino i2c class.
Rename sfe_i2c_arduino.cpp to include the last _.
Add error for null dev settings.
Implement read and write functions.
Remove old commented out code.
Fix how passing in the device settings should work.
Changed () to {} in constructors.
*.mo

#Mr Developer
.mr.developer.cfg
Copy link
Contributor

Choose a reason for hiding this comment

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

Thoughts on changing the .gitignore to include only relevant things for this repo? I'm not really a fan of continually copying "the one master .gitignore" for all projects. What the heck even is .mr.develop.cfg?

author=SparkFun Electronics
maintainer=SparkFun Electronics
sentence=A utility library that other SparkFun libraries can take advantage of.
sentence=A generic communications utility library that other SparkFun libraries can take advantage of.
Copy link
Contributor

Choose a reason for hiding this comment

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

The reason it's called "Toolkit" is because Kirk was thinking about expanding it to do more than just communication at some point.

#pragma once

#include "sfe_i2c_arduino.h"
// #include "sfe_spi_arduino.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this just #include "sfe_bus.h"?


// To repeatedly use this toolkit, you may need to wrap this class in a namespace.
// namespace sfe_XXX {
/// @brief An abstract Bus address class for enabling multiple types of addresses.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest changing to something like "An abstract class for specifying parameters required to communicate with a particular device on a bus"

// To repeatedly use this toolkit, you may need to wrap this class in a namespace.
// namespace sfe_XXX {
/// @brief An abstract Bus address class for enabling multiple types of addresses.
class SFEBusDevSettings{}; // Nothing to see here...
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: thoughts on Params instead of Settings?

while (bytesToSend > 0)
{
// Limit sendLength to the size of the I2C buffer to send in chunks.
uint8_t sendLength = (bytesToSend > _i2cBufferSize) ? _i2cBufferSize : bytesToSend;
Copy link
Contributor

Choose a reason for hiding this comment

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

There may be an off-by-2 error here, because both pAddr->devAddr and regAddr have to be stored in the Wire.h buffer. Same with the other read/write functions.

Comment on lines +148 to +157
if (numRead < readLength)
return SFE_BUS_W_UNDER_READ;

if (_i2cBus->available())
{
for (uint8_t i = 0; i < readLength; i++)
data[readOffset + i] = _i2cBus->read();
}
else
return SFE_BUS_E_NO_RESPONSE;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this order should be flipped. First loop through numRead then check if (numRead < readLength). That way the user could still get partial data at least.

_i2cBus->write(regAddr);

// Repeat start condition, return if there's an error.
int8_t result = _mapError(_i2cBus->endTransmission(false));
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't think this should restart, it should send a stop bit.

Comment on lines +221 to +227
// Start transmission.
_i2cBus->beginTransmission(pAddr->devAddr);

// Repeat start condition, return if there's an error.
int8_t result = _mapError(_i2cBus->endTransmission(false));
if (SFE_BUS_OK != result)
return result;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is necessary, should just call _i2cBus->requestFrom()

Comment on lines +292 to +301
if (!error)
return SFE_BUS_OK;
else if (error == 1)
return SFE_BUS_E_DATA_TOO_LONG;
else if ((error == 2) || (error == 3))
return SFE_BUS_E_NO_RESPONSE;
else if (error == 5)
return SFE_BUS_E_TIMEOUT;
else
return SFE_BUS_E_UNKNOWN;
Copy link
Contributor

Choose a reason for hiding this comment

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

@gigapod gigapod closed this Dec 8, 2023
@SFE-Brudnerd SFE-Brudnerd deleted the rework_interfaces branch December 11, 2023 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants