Float I2c

chucktodd:
Steveiboy,

The Wire library has a problem. When the onRequestevent() is triggered (inside ISR), the code only transmits the last Wire.write() data. if your onRequestEvent() is like this:

void onRequestEvent(){

Wire.write(1);
  Wire.write(2);
  Wire.write(3);
  Wire.write(4);
  Wire.write(5);
  Wire.write(6);
  Wire.write(7);
}




and the Master's code was


Wire.requestFrom(slave,7);
  while(Wire.available()){
    Serial.print(Wire.read(),HEX);
    Serial.print(' ');
    }




the Monitor would print:

7 FF FF FF FF FF FF

Not what you expected right?

I changed the code in my library to fix this and another Issue.


C:\Program Files\Arduino\hardware\arduino\avr\libraries\Wire\utility\twi.c


/*

  • Function twi_transmit
  • Desc    fills slave tx buffer with data
    *          must be called in slave tx event callback
  • Input    data: pointer to byte array
    *          length: number of bytes in array
  • Output  1 length too long for buffer
    *          2 not slave transmitter
    *          0 ok
    /
    uint8_t twi_transmit(const uint8_t
    data, uint8_t length)
    {
      uint8_t i;

// ensure data will fit into buffer
  //if(TWI_BUFFER_LENGTH < length){ // original
  if(TWI_BUFFER_LENGTH < (length+twi_txBufferLength)){ //chuck 15JUN2015
    return 1;
  }
 
  // ensure we are currently a slave transmitter
  if(TWI_STX != twi_state){
    return 2;
  }
 
  // set length and copy data into tx buffer
  //twi_txBufferLength = length; original
  for(i = 0; i < length; ++i){
  //  twi_txBuffer[i] = data[i]; original
    twi_txBuffer[twi_txBufferLength++] = data[i]; //chuck 15Jun2015
    }
  return 0;
}






Appended are my update twi.c and two test Sketches that work. 

Chuck.


---


Check out my Kickstarter Project [Memory Panes](https://www.kickstarter.com/projects/1052400055/mega2560-expansion-ram-arduino-compatible-xmem) an expansion RAM Shield for Mega2560's. It adds 1MB of RAM for those projects where 8KB is not enough.

i'm the new, how to do with your library and the twi file. Thanks