Pages: [1]   Go Down
Author Topic: Wire library - add return values for debugging  (Read 575 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13915
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
// 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

Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: