*********************************In multiwii code. add a #define I2C_GPS in config.h

#define I2C_GPS      //I2C GPS and Navi modul
//#define GPS
//#define GPS_SERIAL 1 // should be 2 for flyduino v2. It's the serial port number on arduino MEGA
//#define GPS_BAUD   115200
//#define GPS_BAUD   9600


*********************************then add I2C_GPS register defines in def.h

#define I2C_GPS_ADDRESS                      0x40       
/*************** I2C GSP register definitions *********************************/

#define I2C_GPS_STATUS                            0x00 //(Read only)
        #define I2C_GPS_STATUS_NEW_DATA       0x01
        #define I2C_GPS_STATUS_2DFIX          0x02
        #define I2C_GPS_STATUS_3DFIX          0x04
        #define I2C_GPS_STATUS_WP_REACHED     0x08      //Active waypoint has been reached (not cleared until new waypoint is set)
        #define I2C_GPS_STATUS_NUMSATS        0xF0

#define I2C_GPS_COMMAND                           0x01 //(write only)
        #define I2C_GPS_COMMAND_POSHOLD       0x01      //copy current position to internal target location register
        #define I2C_GPS_COMMAND_RESUME        0x02      //copy last active WP to internal target location register
        #define I2C_GPS_COMMAND_SET_WP        0x04      //copy current position to given WP
        #define I2C_GPS_COMMAND_ACTIVATE_WP   0x08      //copy given WP position to internal target location register
        #define I2C_GPS_COMMAND_WP            0xF0      //Waypoint number

#define I2C_GPS_WP_REG                            0x06   //Waypoint register (Read only)
        #define I2C_GPS_WP_REG_ACTIVE          0x0F      //Active Waypoint
        #define I2C_GPS_WP_REG_PERVIOUS        0xF0      //pervious Waypoint
        
#define I2C_GPS_GROUND_SPEED                      0x07   //GPS ground speed in m/s*100 (uint16_t)      (Read Only)
#define I2C_GPS_ALTITUDE                          0x09   //GPS altitude in meters (uint16_t)           (Read Only)
#define I2C_GPS_TIME                              0x0b   //UTC Time from GPS in hhmmss.sss * 100 (uint32_t)(unneccesary precision) (Read Only)
#define I2C_GPS_DISTANCE                          0x0f   //Distance between current pos and internal target location register in meters (uint16_t) (Read Only)
#define I2C_GPS_DIRECTION                         0x11   //direction towards interal target location reg from current position (+/- 180 degree)    (read Only)
#define I2C_GPS_LOCATION                          0x13   //current position (8 bytes, lat and lon, 1 degree = 10 000 000                           (read only)
#define I2C_GPS_WP0                               0x1B   //Waypoint 0 used for RTH location      (R/W)
#define I2C_GPS_WP1                               0x23
#define I2C_GPS_WP2                               0x2B
#define I2C_GPS_WP3                               0x33
#define I2C_GPS_WP4                               0x3B
#define I2C_GPS_WP5                               0x43
#define I2C_GPS_WP6                               0x4B
#define I2C_GPS_WP7                               0x53
#define I2C_GPS_WP8                               0x5B
#define I2C_GPS_WP9                               0x63
#define I2C_GPS_WP10                              0x6B
#define I2C_GPS_WP11                              0x73
#define I2C_GPS_WP12                              0x7B
#define I2C_GPS_WP13                              0x83
#define I2C_GPS_WP14                              0x8B
#define I2C_GPS_WP15                              0x93
#define I2C_GPS_WP_NAV_PAR1						  0x9B	//Waypoint navigation parameter 1
		#define I2C_GPS_WP_NAV_PAR1_REACH_LIMIT	0x0F    //lover 4 bit, waypoint reached distance

**************************************************************In every place where a #if defined(GPS) occurs, replace it with 
		
#if defined(GPS) || defined(I2C_GPS)


**************************************************************In every place where a #if defined(GPS) occurs, replace it with 


.... mixTable();
  writeServos();
  writeMotors();


  #if defined(I2C_GPS)

  //Do not use i2c_writereg, since writing a register does not work if an i2c_stop command is issued at the end
  //Still investigating, however with separated i2c_repstart and i2c_write commands works... and did not caused i2c errors on a long term test.

  _i2c_gps_status = i2c_readReg(I2C_GPS_ADDRESS,I2C_GPS_STATUS);                    //Get status register 
  if (_i2c_gps_status & I2C_GPS_STATUS_3DFIX) {                                     //Check is we have a good 3d fix (numsats>5)
     GPS_fix = 1;                                                                   //Set fix
     GPS_numSat = (_i2c_gps_status & 0xf0) >> 4;                                    //Num of sats is stored the upmost 4 bits of status
     if (!GPS_fix_home) {        //if home is not set set home position to WP#0 and activate it

        i2c_rep_start(I2C_GPS_ADDRESS);i2c_write(I2C_GPS_COMMAND);i2c_write(I2C_GPS_COMMAND_SET_WP);//Store current position to WP#0 (this is used for RTH)
        i2c_rep_start(I2C_GPS_ADDRESS);i2c_write(I2C_GPS_COMMAND);i2c_write(I2C_GPS_COMMAND_ACTIVATE_WP);//Set WP#0 as the active WP
        GPS_fix_home = 1;                                                           //Now we have a home   
     }
     if (_i2c_gps_status & I2C_GPS_STATUS_NEW_DATA) {                               //Check about new data
        if (GPS_update) { GPS_update = 0;} else { GPS_update = 1;}                  //Fancy flash on GUI :D
        //Read GPS data for distance and heading
        i2c_rep_start(I2C_GPS_ADDRESS);
        i2c_write(I2C_GPS_DISTANCE);                                               //Start read from here 2x2 bytes distance and direction
        i2c_rep_start(I2C_GPS_ADDRESS+1);
        uint8_t *varptr = (uint8_t *)&GPS_distanceToHome;
        *varptr++ = i2c_readAck();
        *varptr   = i2c_readAck();
        varptr = (uint8_t *)&GPS_directionToHome;
        *varptr++ = i2c_readAck();
        *varptr   = i2c_readNak();
      }

  } else {                                                                          //We don't have a fix zero out distance and bearing (for safety reasons)
    GPS_distanceToHome = 0;
    GPS_directionToHome = 0;
    GPS_numSat = 0;
  }  
    
    
  if (rcData[AUX4]>1800)
   {
        i2c_rep_start(I2C_GPS_ADDRESS);i2c_write(I2C_GPS_COMMAND);i2c_write(I2C_GPS_COMMAND_SET_WP);//Store current position to WP#0 (this is used for RTH)
        i2c_rep_start(I2C_GPS_ADDRESS);i2c_write(I2C_GPS_COMMAND);i2c_write(I2C_GPS_COMMAND_ACTIVATE_WP);//Set WP#0 as the active WP
   }    
    
 #endif     