I2C Library Question

01 Feb 2012

As a beginning disclaimer, I don't know a whole lot about C++ programming, so I'm trying to learn. I've done some Java, and Python, and embedded C, but mostly simple projects.

I'm planning out a library that would use the I2C bus to communicate with an IMU unit I'm using. I have the I2C interface working in my main code, but a library would make interfacing a little more organized and easier to deal with.

One of the devices on the IMU board is an ITG3200 gyro, so I took a look at the ITG3200 library for a starting point (I don't know anything about writing libraries). I see that in ITG3200.cpp that the constructor looks like this:

ITG3200::ITG3200(PinName sda, PinName scl) : i2c_(sda, scl) {}

Afterwards, the object "i2c_" is used to access the I2C bus. My confusion arises from the fact that nowhere in the code is the i2c_ object created (instantiated?) like you would in a normal program (shown below), as I would expect.

I2C i2c_(sda, scl);

I've looked everywhere I possibly could to see if there was a general instantiation of the i2c_ object (throughout the .cpp file from above, the .h file for the library, and in the mbed.h library code as well).

Can anyone explain how this process works? I could just modify the code and create my own library using a similar constructor, but I think its important to understand how this all works. Also, could anyone explain the syntax that constructor (or others like it)?

Thanks in advance,

Dan

01 Feb 2012

Of course, now that I post about this, I now see i2c_ instantiated in the private classes in ITG3200.h, so never mind on that part.

If anyone can still walk me through the constructor syntax I would still be interested.