MPU6050 - function code

Hello,

I am making a project to track the movement of a users hand by using the MPU6050. I’ve been using Jeff Rowbergs library to get the thing working and have started to look into some of the functions he uses, mainly so I can do some other things with the data.

I’m no good at C programming and wanted to know how the program knows which function to run from the .cpp file when the .ino file uses the following function call:

mpu.dmpGetQuaternion(&q, fifoBuffer);

When I look in the .cpp file there are 3 functions, all with the same name and data types! Is it first come first served ? or something else

uint8_t MPU6050::dmpGetQuaternion(int32_t *data, const uint8_t* packet) {
    // TODO: accommodate different arrangements of sent data (ONLY default supported now)
    if (packet == 0) packet = dmpPacketBuffer;
    data[0] = (((uint32_t)packet[0] << 24) | ((uint32_t)packet[1] << 16) | ((uint32_t)packet[2] << 8) | packet[3]);
    data[1] = (((uint32_t)packet[4] << 24) | ((uint32_t)packet[5] << 16) | ((uint32_t)packet[6] << 8) | packet[7]);
    data[2] = (((uint32_t)packet[8] << 24) | ((uint32_t)packet[9] << 16) | ((uint32_t)packet[10] << 8) | packet[11]);
    data[3] = (((uint32_t)packet[12] << 24) | ((uint32_t)packet[13] << 16) | ((uint32_t)packet[14] << 8) | packet[15]);
    return 0;
}
uint8_t MPU6050::dmpGetQuaternion(int16_t *data, const uint8_t* packet) {
    // TODO: accommodate different arrangements of sent data (ONLY default supported now)
    if (packet == 0) packet = dmpPacketBuffer;
    data[0] = ((packet[0] << 8) | packet[1]);
    data[1] = ((packet[4] << 8) | packet[5]);
    data[2] = ((packet[8] << 8) | packet[9]);
    data[3] = ((packet[12] << 8) | packet[13]);
    return 0;
}
uint8_t MPU6050::dmpGetQuaternion(Quaternion *q, const uint8_t* packet) {
    // TODO: accommodate different arrangements of sent data (ONLY default supported now)
    int16_t qI[4];
    uint8_t status = dmpGetQuaternion(qI, packet);
    if (status == 0) {
        q -> w = (float)qI[0] / 16384.0f;
        q -> x = (float)qI[1] / 16384.0f;
        q -> y = (float)qI[2] / 16384.0f;
        q -> z = (float)qI[3] / 16384.0f;
        return 0;
    }
    return status; // int16 return value, indicates error if this line is reached
}

Thanks

I’m no good at C programming and wanted to know how the program knows which function to run from the .cpp file when the .ino file uses the following function call:

mpu.dmpGetQuaternion(&q, fifoBuffer);

It executes the function named dmpGetQuaternion().

When I look in the .cpp file there are 3 functions, all with the same name and data types!

Really? Does int32_t * really look like int16_t * to you? Do either one of them look like Quaternion * to you?

Maybe they do, but they look like three different types to the compiler.

Sorry for the dumb question. Yes the data types are different in the function definitions. Does the compiler only use the function definition with the same data types then

Does the compiler only use the function definition with the same data types then

The compiler will choose the function to call based on the best match between the arguments in the call and the available function(s). If there is no good match, an error will be generated.

If you have a defined two overloads of a function, that take one argument, and one of them takes an int and the other takes a long, and you call the function with an unsigned int, the compiler can choose a function to call. If you call the function with a float, it can't.

Thanks