main.cpp 1.78 KB
#include "mbed.h"
 
#define LENGTH 112
#define BAUD 9600

I2C i2c(D14, D15, BAUD);
DigitalOut led1(LED1);
Serial pc(SERIAL_TX, SERIAL_RX);
 
unsigned char adress_DSGPM;//= 0xD0, 0xD1, 0xD2 ou 0xD3 en fonction des jumpers branchés sur le GPS
char data[LENGTH];

bool findGPSDevices();

int main()
{
    pc.printf("starting..."); 
            
    if(findGPSDevices()){
        char BaseReg_Address = 0;
        char cmd[1]={BaseReg_Address};
        
        while(1) 
        {
            led1 = 1;
            wait(0.2);
            i2c.write(adress_DSGPM, cmd, 1);
            i2c.read(adress_DSGPM, data, LENGTH); 
            pc.printf("Time : %d%d:%d%d:%d%d\n", data[0], data[1], data[2], data[3], data[4], data[5]); 
            pc.printf("Date : %d%d/%d%d/%d%d%d%d\n", data[6], data[7], data[8], data[9], data[10], data[11], data[12], data[13]); 
            pc.printf("Latitude : %d%d degres\n", data[14], data[15]); 
            pc.printf("Longitude : %d%d%d degres\n", data[23], data[24], data[25]); 
            pc.printf("Altitude : %d%d%d%d%d metres\n\n", data[39], data[40], data[41], data[42], data[43]); 
            led1 = 0;
            wait(0.5);
        }
    }
}

/*
 * Permet de trouver les GPS et leurs adresses. Assigne une adresse trouvée à 
 * la variable 'adress_DSGPM'.
 */
bool findGPSDevices() {
    int count = 0;
    bool res = false;
    
    pc.printf("Searching for I2C devices...\n");
    for (int address=0xD0; address<0xD7; address+=2)   
    {
        if (!i2c.write(address, NULL, 0)) //0 est renvoyé = GPS trouvé 
        { 
            pc.printf(" - I2C device found at address 0x%02X\n", address);
            adress_DSGPM = address;
            res = true;
            count++;
        }
            
    }
    pc.printf("%d devices found\n\n", count);
    return res;
}