Creating a library for MPU-6050 using KRODAL's code

Hi everyone! I'm trying to create a library from Krodal's code for MPU-6050 IMU. By using a library it would be a lot easier to code. Another reason for using a library is to create a function for zeroing sensor values, which needs to be in the void setup () part. The following variables (data stream from IMU) are required:

accel_t_gyro.value.n_accel
accel_t_gyro.value.n_gyro
where n=x,y,z

and a function for changing register data: MPU6050_write().

I tried to create it myself ( http://arduino.cc/en/Hacking/LibraryTutorial ), however I don't know how to go about a class structure inside the code and it's variables. Also not sure how to turn void setup() /loop () part of the code into *.cpp. I would be very grateful if you could help me with that. This library would be very beneficial for beginners, as MPU6050 is a common IMU in the market.

Below is my poor attempt to create a library with shortened version of the code. Please comment on what should I add in the header, *.cpp and keywords.txt files. Big thank you!

THE HEADER FILE (mpu6050.h):

#ifndef mpu6050_h
#define mpu6050_h
#include <Wire.h>

#define MPU6050_AUX_VDDIO 0x01 // R/W
#define MPU6050_SMPLRT_DIV 0x19 // R/W
.
all the defines as in the code: Arduino Playground - MPU-6050
.
#define MPU6050_LP_WAKE_10HZ MPU6050_LP_WAKE_CTRL_3
#define MPU6050_I2C_ADDRESS 0x68

class mpu6050
{
public:
int MPU6050_read(int start, uint8_t *buffer, int size);
int MPU6050_write(int start, const uint8_t *pData, int size);
int MPU6050_write_reg(int reg, uint8_t data);
private:
????????????
}
#endif

THE CPP FILE:
#include mpu6050.h

typedef union accel_t_gyro_union
{
struct
{
uint8_t x_accel_h;
uint8_t x_accel_l;
uint8_t y_accel_h;
uint8_t y_accel_l;
uint8_t z_accel_h;
uint8_t z_accel_l;
uint8_t t_h;
uint8_t t_l;
uint8_t x_gyro_h;
uint8_t x_gyro_l;
uint8_t y_gyro_h;
uint8_t y_gyro_l;
uint8_t z_gyro_h;
uint8_t z_gyro_l;
} reg;
struct
{
int x_accel;
int y_accel;
int z_accel;
int temperature;
int x_gyro;
int y_gyro;
int z_gyro;
} value;
};

void setup()
{
int error;
uint8_t c;
Serial.begin(9600);
Serial.println(F("InvenSense MPU-6050"));
Serial.println(F("June 2012"));
// Initialize the 'Wire' class for the I2C-bus.
Wire.begin();
error = MPU6050_read (MPU6050_WHO_AM_I, &c, 1);
Serial.print(F("WHO_AM_I : "));
Serial.print(c,HEX);
Serial.print(F(", error = "));
Serial.println(error,DEC);
error = MPU6050_read (MPU6050_PWR_MGMT_2, &c, 1);
Serial.print(F("PWR_MGMT_2 : "));
Serial.print(c,HEX);
Serial.print(F(", error = "));
Serial.println(error,DEC);
MPU6050_write_reg (MPU6050_PWR_MGMT_1, 0);
}

void loop()
{
int error;
double dT;
accel_t_gyro_union accel_t_gyro;
Serial.println(F(""));
Serial.println(F("MPU-6050"));
error = MPU6050_read (MPU6050_ACCEL_XOUT_H, (uint8_t *) &accel_t_gyro, sizeof(accel_t_gyro));
Serial.print(F("Read accel, temp and gyro, error = "));
Serial.println(error,DEC);
uint8_t swap;
#define SWAP(x,y) swap = x; x = y; y = swap
SWAP (accel_t_gyro.reg.x_accel_h, accel_t_gyro.reg.x_accel_l);
SWAP (accel_t_gyro.reg.y_accel_h, accel_t_gyro.reg.y_accel_l);
SWAP (accel_t_gyro.reg.z_accel_h, accel_t_gyro.reg.z_accel_l);
SWAP (accel_t_gyro.reg.t_h, accel_t_gyro.reg.t_l);
SWAP (accel_t_gyro.reg.x_gyro_h, accel_t_gyro.reg.x_gyro_l);
SWAP (accel_t_gyro.reg.y_gyro_h, accel_t_gyro.reg.y_gyro_l);
SWAP (accel_t_gyro.reg.z_gyro_h, accel_t_gyro.reg.z_gyro_l);
}

int mpu6050::MPU6050_read(int start, uint8_t *buffer, int size)
{
int i, n, error;
Wire.beginTransmission(MPU6050_I2C_ADDRESS);
n = Wire.write(start);
if (n != 1)
return (-10);
n = Wire.endTransmission(false); // hold the I2C-bus
if (n != 0)
return (n);
Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true);
i = 0;
while(Wire.available() && i<size)
{
buffer[i++]=Wire.read();
}
if ( i != size)
return (-11);
return (0); // return : no error
}

int mpu6050::MPU6050_write(int start, const uint8_t *pData, int size)
{
int n, error;
Wire.beginTransmission(MPU6050_I2C_ADDRESS);
n = Wire.write(start); // write the start address
if (n != 1)
return (-20);
n = Wire.write(pData, size); // write data bytes
if (n != size)
return (-21);
error = Wire.endTransmission(true); // release the I2C-bus
if (error != 0)
return (error);
return (0); // return : no error
}

int mpu6050::MPU6050_write_reg(int reg, uint8_t data)
{
int error;
error = MPU6050_write(reg, &data, 1);
return (error);
}

KEYWORDS.TXT
????

    int MPU6050_read(int start, uint8_t *buffer, int size);
    int MPU6050_write(int start, const uint8_t *pData, int size);
    int MPU6050_write_reg(int reg, uint8_t data);

This is in keeping with methods like HardwareSerial_read() and HardwareSerial_print()? I don't think so.

Also not sure how to turn void setup() /loop () part of the code into *.cpp.

You don't. setup() and loop() are not part of the library. They are part of the sketch that uses the library.

Please comment on what should I add in the header, *.cpp and keywords.txt files.

Variable declarations, like the union and the struct belong in the header file, not the source file.

Class declarations need to end with a semicolon.

There are two pages for a library:

http://playground.arduino.cc/Code/Library

I can't help with the library. My sketch is only the basic interface, following closely the datasheet.

I've read these tutorials few times, all the errors I'm getting now are to do with
typedef union accel_t_gyro_union { .. }
Don't know how to go about *.cpp file in the library

heh .. I give up.. :~

I've read these tutorials few times, all the errors I'm getting now are to do with
typedef union accel_t_gyro_union { .. }
Don't know how to go about *.cpp file in the library

And you're not going to show the source file or the errors?

heh .. I give up..

Well, if that's what you want...