-/* trackuino copyright (C) 2010  EA5HAV Javi
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-
-// Mpide 22 fails to compile Arduino code because it stupidly defines ARDUINO 
-// as an empty macro (hence the +0 hack). UNO32 builds are fine. Just use the
-// real Arduino IDE for Arduino builds. Optionally complain to the Mpide
-// authors to fix the broken macro.
-// #if (ARDUINO + 0) == 0
-// #error "Oops! We need the real Arduino IDE (version 22 or 23) for Arduino builds."
-// #error "See trackuino.pde for details on this"
-
-// // Refuse to compile on arduino version 21 or lower. 22 includes an 
-// // optimization of the USART code that is critical for real-time operation
-// // of the AVR code.
-// #elif (ARDUINO + 0) < 22
-// #error "Oops! We need Arduino 22 or 23"
-// #error "See trackuino.pde for details on this"
-
-// #endif
-
 // // Arduino/AVR libs
 // #if (ARDUINO + 1) >= 100
 // #  include <Arduino.h>

#include <NMEAGPS.h>
#include <NeoSWSerial.h>

NMEAGPS gps;  // The parsing object
gps_fix fix;  // GPS fields are stored here as they are parsed
uint8_t gpsUpdates = 0; // how many updates since power_save

NeoSWSerial gpsS(RXPIN, TXPIN);

//Internal Temp sensor
#include <Wire.h>

    
    gpsS.begin(9600);

    next_aprs = 0;
}

void loop() {
  while (gps.available(gpsS)) {
    fix = gps.read();
    next_aprs++;

    if (next_aprs >= 5) {
      next_aprs = 0;

      char latitude[9];
      char longitude[10];
      char datetime[7];
      if(fix.valid.location) {
        snprintf(latitude, 9, "%02d%02d.%02u%c", fix.latitudeDMS.degrees, fix.latitudeDMS.minutes, truncate(fix.latitudeDMS.seconds_whole, 2), fix.latitudeDMS.NS() );
        snprintf(longitude, 10, "%03d%02d.%02u%c", fix.longitudeDMS.degrees, fix.longitudeDMS.minutes, truncate(fix.longitudeDMS.seconds_whole, 2), fix.longitudeDMS.EW() );
      }


      snprintf(datetime, 7, "%02d%02d%02d", fix.dateTime.hours, fix.dateTime.minutes, fix.dateTime.seconds);

    
      //Check pressure the flight day in http://weather.noaa.gov/weather/current/LEMD.html
      //We take the value between parenthesis. If pressure is 1013 write in readAltitude
      //this value multiplied by 100
      APRSPacket packet(datetime,
                        bmp.readAltitude(101900),
                        fix.speed(),
                        fix.heading(),
                        bmp.readTemperature(),
                        bmp.readPressure());

      packet.setLatitude(latitude);
      packet.setLongitude(longitude);
      if(fix.valid.location){
        packet.setLatitudeF(fix.latitude());
        packet.setLongitudeF(fix.longitude());
      }
    
      packet.aprs_send();
    
      while (afsk_flush()) {
        power_save();
      }
    }
  }
}

