//code arduino qui recupere la position gps toute les 5 sec et qui enregistre dans l'eeprom les 500 derniere position. ou l'on peut couper l'allimentation

#include <Adafruit_GPS.h>
#include <EEPROM.h>

// what's the name of the hardware serial port?
#define GPSSerial Serial1

// Connect to the GPS on the hardware port
Adafruit_GPS GPS(&GPSSerial);

// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO false

uint32_t timer = millis();

#define RX_PIN 18
#define TX_PIN 19
#define GPS_BAUD 9600

SoftwareSerial ss(RX_PIN, TX_PIN);

const int max_positions = 500;
const int eeprom_start_address = 0;
int eeprom_current_address = eeprom_start_address;

void setup() {
  Serial.begin(115200);

  
   Serial.println("Adafruit GPS library basic parsing test!");

  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);
  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time
  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz

  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_ANTENNA);

  delay(1000);

  // Ask for firmware version
  GPSSerial.println(PMTK_Q_RELEASE);
}

  void loop(){
	  
	  //si char on envoie la memoire
	  if(Serial.available()){
    Serial.read();
		  AfficheEeprom();
	  }
	  
  // read data from the GPS in the 'main loop'
  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
  // if a sentence is received, we can check the checksum, parse it...
  if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences!
    // so be very wary if using OUTPUT_ALLDATA and trying to print out data
    //Serial.print(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
    
    if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
      return; // we can fail to parse a sentence in which case we should just wait for another
  }

  
  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 5000) {
      timer=millis();
      /*Serial.print("Lat/Long: ");
      Serial.print(GPS.latitude);
      Serial.print(", ");
      Serial.println(GPS.longitude);
      Serial.print(", ");
      Serial.print(GPS.angle);
	  Serial.print(", ");
      Serial.println(GPS.speed);*/

      // Enregistrer la position dans l'EEPROM
      EEPROM.put(eeprom_current_address, GPS.latitude);
	  eeprom_current_address += sizeof(float);
	  
      EEPROM.put(eeprom_current_address + sizeof(float), GPS.longitude);
	  eeprom_current_address += sizeof(float);
	  
      EEPROM.put(eeprom_current_address + sizeof(float), GPS.angle);
	  eeprom_current_address += sizeof(float);
	  
	  EEPROM.put(eeprom_current_address + sizeof(float), GPS.speed);
	  eeprom_current_address += sizeof(float);
	  
  }
}

void AfficheEeprom() {
  int address = 0;
  while (address < EEPROM.length()) {
  float latitude, longitude,speed, angle;
    EEPROM.get(address, latitude); // Lecture de la latitude
    address += sizeof(float); // Ajout de la taille de la latitude à l'adresse de départ pour obtenir l'adresse de la longitude
    EEPROM.get(address, longitude); // Lecture de la longitude
    address += sizeof(float); // Ajout de la taille de la latitude à l'adresse de départ pour obtenir l'adresse de la longitude
    EEPROM.get(address, angle); // Lecture de la longitude
   address += sizeof(float);
    EEPROM.get(address, speed); // Lecture de la longitude
   address += sizeof(float);
  
    Serial.print("Latitude: ");
    Serial.print((latitude), 6);
    Serial.print(", Longitude: ");
    Serial.print((longitude), 6);
	Serial.print(", angle: ");
    Serial.print((longitude), 6);
    Serial.print(", speed: ");
	Serial.println(speed, 6);
  }
}


float gpsToDecimal(float gps_coord) {
  int degrees = int(gps_coord / 100); // Calcul des degrés
  float minutes = gps_coord - (degrees * 100); // Calcul des minutes
  float decimal_degrees = degrees + (minutes / 60); // Conversion en degrés décimaux
  return decimal_degrees;
}
