-
Notifications
You must be signed in to change notification settings - Fork 68
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
Support I2C request and add examples #115
Open
tommag
wants to merge
2
commits into
PowerBroker2:master
Choose a base branch
from
tommag:i2c-request
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "I2CTransfer.h" | ||
|
||
|
||
I2CTransfer myTransfer; | ||
|
||
struct __attribute__((packed)) STRUCT { | ||
char z; | ||
float y; | ||
} testStruct; | ||
|
||
void I2C_RequestHandler(void) | ||
{ | ||
myTransfer.replyWithDatum(testStruct); | ||
} | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
|
||
Wire.begin(0x10); // Begin I2C as peripheral (slave) with address 0x10 | ||
Wire.onRequest(I2C_RequestHandler); //Setup I2C_RequestHandler function as I2C request callback | ||
|
||
myTransfer.begin(Wire); | ||
|
||
testStruct.z = '$'; | ||
testStruct.y = 4.5; | ||
} | ||
|
||
|
||
void loop() | ||
{ | ||
// Do nothing | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include "I2CTransfer.h" | ||
|
||
|
||
I2CTransfer myTransfer; | ||
|
||
struct __attribute__((packed)) STRUCT { | ||
char z; | ||
float y; | ||
} testStruct; | ||
|
||
|
||
/////////////////////////////////////////////////////////////////// Callbacks | ||
void hi() | ||
{ | ||
myTransfer.rxObj(testStruct); | ||
Serial.print(testStruct.z); | ||
Serial.println(testStruct.y); | ||
} | ||
|
||
// supplied as a reference - persistent allocation required | ||
const functionPtr callbackArr[] = { hi }; | ||
/////////////////////////////////////////////////////////////////// | ||
|
||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
Wire.begin(); // Begin I2C as controller (master) | ||
|
||
///////////////////////////////////////////////////////////////// Config Parameters | ||
configST myConfig; | ||
myConfig.debug = true; | ||
myConfig.callbacks = callbackArr; | ||
myConfig.callbacksLen = sizeof(callbackArr) / sizeof(functionPtr); | ||
///////////////////////////////////////////////////////////////// | ||
|
||
myTransfer.begin(Wire, myConfig); | ||
} | ||
|
||
|
||
void loop() | ||
{ | ||
// Request datum from connected peripheral at address 0x10 | ||
// Callback function hi() will be called on reception | ||
myTransfer.requestDatum(sizeof(testStruct), 0x10); | ||
delay(500); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,8 @@ void I2CTransfer::begin(TwoWire& _port, const bool& _debug, Stream& _debugPort) | |
to send as the payload in the next packet | ||
* const uint8_t &packetID - The packet 8-bit identifier | ||
* const uint8_t &targetAddress - I2C address to the device the packet | ||
will be transmitted to | ||
will be transmitted to. If targetAddress == 0xFF (invalid value in I2C), | ||
data is transmitted as a reply to an I2C request. | ||
Return: | ||
------- | ||
* uint8_t numBytesIncl - Number of payload bytes included in packet | ||
|
@@ -66,11 +67,15 @@ uint8_t I2CTransfer::sendData(const uint16_t& messageLen, const uint8_t& packetI | |
|
||
numBytesIncl = packet.constructPacket(messageLen, packetID); | ||
|
||
port->beginTransmission(targetAddress); | ||
if (targetAddress < 0xFF) | ||
port->beginTransmission(targetAddress); | ||
|
||
port->write(packet.preamble, sizeof(packet.preamble)); | ||
port->write(packet.txBuff, numBytesIncl); | ||
port->write(packet.postamble, sizeof(packet.postamble)); | ||
port->endTransmission(); | ||
|
||
if (targetAddress < 0xFF) | ||
port->endTransmission(); | ||
|
||
return numBytesIncl; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tommag I think if the target address is invalid, the function should return 0 since nothing was transferred. Maybe we could put everything dealing with port writing, port closing, and reporting |
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tommag I think it would be good to double check that the requested data length matches the struct the sender wants to report. There should be a check for this either at the sketch or lib level. Also, it might be nice to request a specific packet type (Packet ID field)