Programming question for passing array as a function argument to read I2C values

Hi,
I have good experience with arduino and C++ but still there are times when things confuses me.
In the following code I want to read two bytes in the uint8_t array type called temo[2].

I am not sure for the syntex and kind of error I am getting.

supoose if I remove the [] from the declaration of the readbytes function like following

void readbytes(uint8_t reg, int count, uint8_t temp)

I get invalid type conversion error from uint8_t* to uint8_t. I am not sure where I have used pointer types in here?
Is it because the arrays are stored in memory with pointer to first element?

"invalid conversion from 'uint8_t*' {aka 'unsigned char*'} to 'uint8_t' {aka 'unsigned char'}"

Then I tried to change function call inside a loop() function like the following. Explicitly specifying array type
readbytes(Configuration, 2, temp[]);

then the error is

""error: expected primary-expression before ']' token"

void loop() {
  // put your main code here, to run repeatedly:

  uint8_t data[2];
  uint8_t temp[2];
  
  readbytes(Configuration, 2, temp); 
  delay(1000);
  
}

void readbytes(uint8_t reg, int count, uint8_t temp[]){
   
  Wire.beginTransmission(address);
  Wire.write(reg);
  Wire.endTransmission(false);
  Wire.requestFrom(address, count);

  for(int i=0; i< count; i++){
    temp[i] = Wire.read();
    Serial.println(temp[i]);   
    }
  
  }

The function definition needs the brackets

void readbytes(uint8_t reg, int count, uint8_t temp[])

The function call just needs the array name without the brackets.

readbytes(Configuration, 2, temp); 

This compiles without errors

#include <Wire.h>
int address = 0x50;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

  uint8_t data[2];
  uint8_t temp[2];
  byte Configuration = 5;

  readbytes(Configuration, 2, temp);
  delay(1000);

}

void readbytes(uint8_t reg, int count, uint8_t temp[])
{

  Wire.beginTransmission(address);
  Wire.write(reg);
  Wire.endTransmission(false);
  Wire.requestFrom(address, count);

  for (int i = 0; i < count; i++) {
    temp[i] = Wire.read();
    Serial.println(temp[i]);
  }

}
1 Like

Could you rename the function 'readbytes()' to something else ?

There is already a 'Wire.readBytes()' because the Wire library is part of the Stream class. See Stream.readBytes().

There is a undocumented 'Wire.requestFrom()' that first sets the register and then calls the normal 'Wire.requestFrom()'. On some platforms it is normally used, but for the Arduino Uno it is still undocumented.

1 Like

Looks useful

uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop)
{
  if (isize > 0) {
  // send internal address; this mode allows sending a repeated start to access
  // some devices' internal registers. This function is executed by the hardware
  // TWI module on other processors (for example Due's TWI_IADR and TWI_MMR registers)

  beginTransmission(address);

  // the maximum size of internal address is 3 bytes
  if (isize > 3){
    isize = 3;
  }

  // write internal register address - most significant byte first
  while (isize-- > 0)
    write((uint8_t)(iaddress >> (isize*8)));
  endTransmission(false);
  }

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

  return read;
}

Thank you for your replies.
Actually code was always working but some explanation was required. like I have mentioned regarding unsupported type conversion etc.
Of couse, I have changed the names of the functions..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.