Go Down

Topic: Wire library - add return values for debugging (Read 588 times) previous topic - next topic

robtillaart

Debugging an I2C session today and I think the Wire lib can be expanded with more return values to be more informative. Some functions allready do e.g. endTransmission() returns good info. But there are several that return void. The idea is to extend these with a meaningfull returnvalue.

e.g. the existing send function:
Code: [Select]
void TwoWire::send(uint8_t data)
{
 if(transmitting){
 // in master transmitter mode
   // don't bother if buffer is full
   if(txBufferLength >= BUFFER_LENGTH){
     return;
   }
   // put byte in tx buffer
   txBuffer[txBufferIndex] = data;
   ++txBufferIndex;
   // update amount in buffer  
   txBufferLength = txBufferIndex;
 }else{
 // in slave send mode
   // reply to master
   twi_transmit(&data, 1);
 }
}


could become something like

Code: [Select]

// Output:
// -1 : no room in buffer
//  0 : data send
//  1 : data added to the internal send buffer
uint8_t TwoWire::send(uint8_t data)
{
 if(transmitting){
 // in master transmitter mode
   // don't bother if buffer is full
   if(txBufferLength >= BUFFER_LENGTH){
     return -1;
   }
   // put byte in tx buffer
   txBuffer[txBufferIndex] = data;
   ++txBufferIndex;
   // update amount in buffer  
   txBufferLength = txBufferIndex;
   return 1;
 } else {
 // in slave send mode
   // reply to master
   twi_transmit(&data, 1);
   return 0;
 }
}


Similar for other wire functions.

Changing signatures from void to uint8_t is backwards compatible, gives relevant info to be able to program robustly and costs only little extra space or performance.

Remarks?
Rob

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up