I2c communication for non supported devices in C++

Hello.
I’m new user of BrickPi3, I used to make robots with EV3 brick and RobotC IDE. Now I want more powerful hardware that’s why I chose BrickPi3.
I’m using C++ language and library.
I have an issue with my non supported i2c devices (angle sensor, motor multiplexer, sensor multiplexer…). I saw that there is a way to make BrickPi communicate with i2c but I’m lost in the function call.
To ensure that the link is OK, I tried with a Ultrasonic Sensor ( it works with C++ ultrasonic function). I sent a request to read version of my sensor but the answer, the read buffer, stays null.
Maybe I should complete the fields speed and delay but I don’t know what to put inside. I am also not sure if I should add the i2c adress in the first sent byte.

i2c_struct_t myi2c;
BrickPi3 BP;

int main(int argc, char **argv)
{
	printf("detect = %d\n",BP.detect()); 

	myi2c.address = 0x02;
	myi2c.length_write = 2;
	myi2c.length_read = 8;
	myi2c.buffer_write[0] = 0x02;
	myi2c.buffer_write[1] = 0x00;
	myi2c.buffer_write[2] = 0x03;
	printf("type = %d\n",BP.set_sensor_type(PORT_1,SENSOR_TYPE_I2C,0,&myi2c));
	for (int i=1;i<20;i++)
	{
		printf("result = %d\n",BP.transact_i2c(PORT_1, &myi2c));
		printf("value = %d\n",(int)myi2c.buffer_read[0]);
		printf("value = %s\n\n",myi2c.buffer_read[0]);
		sleep(1);
	}
}

All that is printed is 0. Even when I try to have the distance, it replies 0 (despite it’s real distance with Ultrasonic function). I tried many combination of buffer_write, buffer length, nothing works.

Could someone tell me what I did wrong and how to manage i2c communication for non supported devices?

1 Like

Did you set PORT_1 and SENSOR_TYPE_I2C somewhere, or should those be BP.PORT_1 and BP.SENSOR_TYPE_I2C ?

(I don’t know anything about BP, just looking at the code you posted.)

1 Like

PORT_1 and SENSOR_TYPE_I2C are defined in BrickPi3.h as part of the BrickPi C++ library.

2 Likes