You can more easily communicate over I2C using SerialTransfer.h.
Here's an example:
TX:
#include "I2CTransfer.h"
I2CTransfer myTransfer;
struct STRUCT {
char z;
float y;
} testStruct;
char arr[] = "hello";
void setup()
{
Serial.begin(115200);
Wire.begin();
myTransfer.begin(Wire);
testStruct.z = '$';
testStruct.y = 4.5;
}
void loop()
{
// use this variable to keep track of how many
// bytes we're stuffing in the transmit buffer
uint16_t sendSize = 0;
///////////////////////////////////////// Stuff buffer with struct
sendSize = myTransfer.txObj(testStruct, sendSize);
///////////////////////////////////////// Stuff buffer with array
sendSize = myTransfer.txObj(arr, sendSize);
///////////////////////////////////////// Send buffer
myTransfer.sendData(sendSize);
delay(500);
}
RX:
#include "I2CTransfer.h"
I2CTransfer myTransfer;
struct STRUCT {
char z;
float y;
} testStruct;
char arr[6];
void hi()
{
// use this variable to keep track of how many
// bytes we've processed from the receive buffer
uint16_t recSize = 0;
recSize = myTransfer.rxObj(testStruct, recSize);
Serial.print(testStruct.z);
Serial.print(testStruct.y);
Serial.print(" | ");
recSize = myTransfer.rxObj(arr, recSize);
Serial.println(arr);
}
// supplied as a reference - persistent allocation required
const functionPtr callbackArr[] = { hi };
void setup()
{
Serial.begin(115200);
Wire.begin(0);
configST myConfig;
myConfig.debug = true;
myConfig.callbacks = callbackArr;
myConfig.callbacksLen = sizeof(callbackArr) / sizeof(functionPtr);
myTransfer.begin(Wire, myConfig);
}
void loop()
{
// Do nothing
}