TWI issue

Hi all!

I'm trying to write a simple library to connect to different I2C devices. All them share the same settings and their only difference is their I2C address.

When I write this

uint8_t PortI2C::readByte(uint8_t add) {
  Wire.beginTransmission( Device );
  Wire.send(add);
  Wire.endTransmission();

  Wire.requestFrom( this->Device, 2 );
  
  // return data
  while (Wire.available() < 1) ;
  return (Wire.receive());
}

After compiling I get

In member function 'uint8_t PortI2C::readByte(uint8_t)':
Ports:58: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
/home/samuel/Binaris/arduino-0022/libraries/Wire/Wire.h:53: note: candidate 1: uint8_t TwoWire::requestFrom(int, int)
/home/samuel/Binaris/arduino-0022/libraries/Wire/Wire.h:52: note: candidate 2: uint8_t TwoWire::requestFrom(uint8_t, uint8_t)

What's wrong?

Best Wishes for 2011!

/me

I've changed my code, so "Device" variable is an integer. Now it compiles fine!

What was Device before?

This is a code snippet from Wire.cpp:

uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
{
  // clamp to buffer length
  if(quantity > BUFFER_LENGTH){
    quantity = BUFFER_LENGTH;
  }
  // perform blocking read into buffer
  uint8_t read = twi_readFrom(address, rxBuffer, quantity);
  // set rx buffer iterator vars
  rxBufferIndex = 0;
  rxBufferLength = read;

  return read;
}

uint8_t TwoWire::requestFrom(int address, int quantity)
{
  return requestFrom((uint8_t)address, (uint8_t)quantity);
}

Looks like the int version might best have been inline to help the optimizer make it 'disappear'.

Hi!

Device has been an uint8_t or byte. Both them fail when compiling. To succeed, Device must be int.

I've seen this declaration but the compiler does not behave as it should. :o

Anyway it's solved with an integer, as I told before.

Thanks!

/me

Device has been an uint8_t or byte. Both them fail when compiling. To succeed, Device must be int.

Either that or the constant (2) needs to be cast as a uint8_t.