Adapting I2c_anything.h to different I2C library

Hello, I have a project with an mega328 (currently on an Uno) acting as an I2C master and an ESP8266 (Huzzah feather) as an I2C slave. Also in the I2C circuit is an ADXL345 accelerometer. Communication is generally reliable at a nice and slow 31khz but occasionally I’d get a hang on Wire.endtransmission() (from the mega) that would clear up when I hit the reset on the ESP.

From a high level: The mega is woken from sleep by an interrupt from the ADXL, starts polling it for x,y,z accelerations to determine the pitch. When pitch exceeds a certain value (provided by the ESP), the mega pulls the enable pin of the ESP high to wake it up, they exchange data (the mega sends some data to the ESP, the ESP sends back a status and some configuration data). Eventually the ESP tells the mega it is done, the mega turns the ESP off by pulling its enable pin low and then puts itself to sleep.

It is generally pretty reliable, but, one of the things the ESP is doing is updating an ePaper display over SPI which is slow and sometimes buggy and I suspect may interfere with the I2C communication.

Here are the master/slave code blocks where I get the I2C hang.

Master (atmega328)

void I2Ctransmit(){ //transmits accel, pour time, and battery voltage to esp.
  Wire.beginTransmission(SLAVE_ADDRESS);
  I2C_writeAnything(pitch); //float
  I2C_writeAnything(PourTime); //uint32
  I2C_writeAnything(Vbat); //uint16
  I2C_writeAnything(Status); //byte
  byte result = Wire.endTransmission();   //***I2C HANGS HERE***
  if (result != 0){
    Serial.print("I2C send failed:");Serial.println(result);
//  Output   0 .. success
//           1 .. length to long for buffer
//           2 .. address send, NACK received
//           3 .. data send, NACK received
//           4 .. other twi error (lost bus arbitration, bus error, ..)
//           5 .. twi module busy
//           6 .. twi module timeout
// 
    }
   else{
     // Serial.print("Sent ");
      PourTime=0;
   }
}

slave (ESP8266)

void receiveEvent (int howMany)
 {
  if (howMany >=  (sizeof pitch) + (sizeof mega_PourTime) + (sizeof Vbat)+ (sizeof mega_Status))
   {
   I2C_readAnything (pitch);
   I2C_readAnything (mega_PourTime); 
   I2C_readAnything (Vbat);
   I2C_readAnything (mega_Status);    
   haveData = true;
   if (mega_PourTime > 0) {
    PourTime += mega_PourTime;
    NewPour=true;
    }
   }
   if (mega_Status==1){haveRequest=true;}
 }  // end of receiveEvent

I had read people saying the I2C library by Wayne from DSS circuits could be more reliable so I have been converting my code to use it instead of wire. I have the ADXL library ported over and have been studying various ways to implement the I2C_writeAnything template. I’ve read about unions and bitwise operations but it is all greek to me (I got a C+ in intro to C++ 15 years ago but did go on to get an A+ in Fortran fwiw-and haven’t touched either since). I’m just spinning my wheels reading tab after tab.

Does anyone have any suggestions on how to modify the I2C_writeAnything template

template <typename T> unsigned int I2C_writeAnything (const T& value)
  {
    const byte * p = (const byte*) &value;
    unsigned int i;
    for (i = 0; i < sizeof value; i++)
          Wire.write(*p++);
    return i;
  }  // end of I2C_writeAnything

to work with one of the I2C.write functions (there are several ways to pass data, I’m thinking this is the one I need:

Function:         I2c.write(address, registerAddress, *data) - initate an I2C write operation, array of char.

                          Typically used to send an array of char starting at registerAddress location. 

Parameters:   (uint8_t)address:  7 bit I2C slave address
                   (uint8_t)registerAddress: Address of the register as per the datasheet
                   (char)*data: array of characters

Wayne's I2C library is not compatible with your use of the I2C bus. It implements a strict register based access model but your code on the ESP8266 doesn't support registers (as far as I can see, you haven't posted complete code).