I need to interface an ATTiny88 MCU configured as an I²C slave with a Raspberry Pi configured as an I²C master. I may be using the wrong approach, but I have a structure consisting of four different fixed length data types I want to pass back to the Master whenever it requests data.
Source Code:
#include <Wire.h>
struct Response // 7 bytes
{
byte Command; // 1 byte
float Temperature; // 4 bytes
byte FanValue; // 1 byte
bool CoolStat; // 1 byte
};
struct Response Reply;
bool True = 1;
bool False = 0
void setup()
{
pinMode(ADDR1, INPUT_PULLUP); // Address Offset 0 or 1
pinMode(ADDR2, INPUT_PULLUP); // Address Offset 0 or 2
I2C = (2 * digitalRead(ADDR1)) + digitalRead(ADDR1) + 8; // Set I2C address 8 - 11
Wire.begin(I2C);
Wire.onRequest(requestEvent); // Call requestEvent when data requested
Reply.Command = 1;
Reply.Temperature = 35.62;
Reply.FanValue = 255
Reply.CoolStat = True
}
// Function that executes whenever data is requested from master
void requestEvent() {
Wire.write(Reply); // respond with message as expected by master
Responded = True;
}
This does not work, of course, because the library does not like the fact I am passing a structure, rather than a simple null terminated string. I am not sure how best to pass the structure to the Wire.write() function. I like the ability to change the variables in the structure to different values of different data types at different points in the code, but would I be better served to take a different approach and manually update the binary values in the string?