Test software for SatChat prototype hardware Platform - MAX32630FTHR
Dependencies: USBDevice max32630fthr
Diff: main.cpp
- Revision:
- 13:ff0b39177386
- Parent:
- 12:a895e3581542
- Child:
- 14:8ff04e82bda2
diff -r a895e3581542 -r ff0b39177386 main.cpp --- a/main.cpp Sun Jul 02 13:37:27 2017 +0000 +++ b/main.cpp Mon Jul 03 15:46:51 2017 +0000 @@ -2,9 +2,10 @@ #include "mbed.h" #include "max32630fthr.h" #include <stdbool.h> -#define on 1 -#define off 0 - +#define ON 0 +#define OFF 1 +#define EXIT_SUCCESS 0 +#define EXIT_FAILURE 1 Serial pc(USBTX, USBRX); Serial gps(P5_3, P5_4, 9600); I2C i2c(P5_7,P6_0); // SDA, SCL @@ -12,7 +13,6 @@ DigitalOut red_led(LED1,1); DigitalOut green_led(LED2,1); DigitalOut blue_led(LED3,1); - char gpsfix_last_utc_time[11] = {0}; char gpsfix_last_utc_date[7] = {0}; char gpsfix_longtitude[12] = {0}; @@ -23,23 +23,23 @@ char gpsfix_mag_var_ew[1] = {0};//Set but not used char gpsfix_ns = 0; char gpsfix_ew = 0; - +bool gps_data_present = false; //If this is false we can't even use stale GPS data. void gps_power(bool state) { char data[2]; data[0] = 0x16; //MAX14690 LDO3cfg register data[1] = 0xE0; //Disable LDO3 - if (state == on) { + if (state == ON) { data[1] = 0xE2; //Enable LDO3 } i2c.write( 0x50, data, 2 ); } -void set_epoch_from_last_gps_time(void) +int get_epoch_from_last_gps_time(void) { struct tm t; - time_t t_of_day; + time_t epoch; char two_char_str[3] = {0}; memcpy(two_char_str, gpsfix_last_utc_date+4, 2); t.tm_year = atoi(two_char_str)+100; //Years since 1900 @@ -51,20 +51,31 @@ t.tm_hour = atoi(two_char_str); memcpy(two_char_str, gpsfix_last_utc_time+2, 2); t.tm_min = atoi(two_char_str); - memcpy(two_char_str, gpsfix_last_utc_time+4, 2); + memcpy(two_char_str, gpsfix_last_utc_time+4, 2); t.tm_sec = atoi(two_char_str); t.tm_isdst = 0; // Is DST on? 1 = yes, 0 = no, -1 = unknown - t_of_day = mktime(&t); - printf("seconds since the Epoch: %ld\n", (long) t_of_day); + epoch = mktime(&t); + return epoch; } -void gps_update(void) +int gps_update(void) { - gps_power(on); - bool wait_for_fix = true; - while (wait_for_fix) { +#define GPS_TIMEOUT 120; //Wait two minutes maximum for GPS. + gps_power(ON); + time_t gps_on_time = time(NULL); //Start time for GPS timeout calculation. + bool wait_for_fix = true; //Set this to false once a fix is obtained. + blue_led = ON; + while (wait_for_fix) { //Keep monitoring the GPS until we get a fix. + if ((time(NULL) - gps_on_time ) > 120) { + gps_power(OFF); + blue_led = OFF; + while (gps.readable()) { + char dummy = gps.getc(); //Empty serial buffer because overflows reveal MBED bugs :-( + } + return EXIT_FAILURE; //Return an error if the GPS takes too long for a fix. + } int checksum = 0; - char nmea_sentence[82] = {0}; //Fill with NULL terminators to save doing it later + char nmea_sentence[83] = {0}; //NMEA length max is 82 + 1 terminator. Fill with NULL terminators to save doing it later. while (gps.getc()!='$'); //wait for start of sentence int nmea_index = 0; nmea_sentence[nmea_index] = '$'; //Manually insert the '$' because we don't want it included in the checksum loop @@ -75,39 +86,41 @@ nmea_sentence[++nmea_index] = ' '; //Pad consecutive comma with a space to make it possible to use strtok with empty values } nmea_sentence[++nmea_index] = nmea_char; //build the sentence with the next character - if (nmea_index > 80) { - nmea_index=80; //Don't overflow buffer + if (nmea_index > 81) { + nmea_index=81; //Don't overflow sentence buffer } nmea_char = gps.getc(); //get next char from GPS } - //Last character was the '*' so read the two hex digits of CS from gps + //Last character was the '*' so next two are CS char hex_checksum[3] = {0}; hex_checksum[0] = gps.getc(); hex_checksum[1] = gps.getc(); - if (checksum == (int)strtol(hex_checksum, NULL, 16) ) { + if (checksum == (int)strtol(hex_checksum, NULL, 16) ) { //Compare calc and read checksums. + //Valid sentence so check if it's a GPRMC - //pc.printf("Match\n\r"); const char gprmc[7] = "$GPRMC"; char *token; token = strtok(nmea_sentence, ","); if (strcmp(token,gprmc) == 0) { //GPRMC ? - pc.printf( " %s\n\r", token ); //Get the time + //pc.printf( " %s\n\r", token ); //Get the time if (token != NULL) { token = strtok(NULL, ","); - if (*token != 32) { //If there is a time present, record it. - pc.printf("Time: %s\n\r",token); + if (*token != 32) { //If there is a time present (anything but a space), record it. + //pc.printf("Time: %s\n\r",token); + blue_led =! blue_led; //Flash blue LED memcpy(gpsfix_last_utc_time, token, sizeof gpsfix_last_utc_time - 1); } } if (token != NULL) { token = strtok(NULL, ","); - if (*token == 'V') { + /* if (*token == 'V') { pc.printf("VOID"); - } - } + } */ + } if (*token == 'A') { //Is this an 'A'ctive (valid) fix? pc.printf("Got a fix\n\r"); - gps_power(off); //Yes - No need for GPS now + gps_power(OFF); //Yes - No need for GPS now + blue_led = OFF; wait_for_fix = false; //Stop looping now we have a fix. while (gps.readable()) { char dummy = gps.getc(); //Empty serial buffer because overflows reveal MBED bugs :-( @@ -134,7 +147,7 @@ } if (token != NULL) { token = strtok(NULL, ","); - //pc.printf("Speed in knots: %s\n\r",token); + pc.printf("Speed in knots: %s\n\r",token); } if (token != NULL) { token = strtok(NULL, ","); @@ -160,9 +173,10 @@ char dummy = gps.getc(); } } + return EXIT_SUCCESS; } -int main() +main() { char data[2]; data[0] = 0x1A; //MAX14690 BootCfg register @@ -172,14 +186,60 @@ data[0] = 0x17; //MAX14690 LDO3Vset register data[1] = 0x19; //3.3V i2c.write( 0x50, data, 2 ); - gps_power(off); + gps_power(OFF); wait(2); - while (1) { - gps_update(); - set_epoch_from_last_gps_time(); - wait(600); + pc.printf("\n\n\rOpen EPIRB - Simple mode\n\r"); + pc.printf("Press and hold both side buttons to signal rescue needed\n\r"); + pc.printf("Full functionality will start in:"); + for (int i=9; i > 0; i--) { + time_t seconds = time(NULL); //get current epoch + pc.printf("%d",i); + while(time(NULL) - seconds < 1) { + if (true/*button_combination()==SOS_PRESSED*/) { + //do the emergency code + if (gps_update()==EXIT_SUCCESS) { + gps_data_present = true; + int gps_epoch = get_epoch_from_last_gps_time(); + set_time(gps_epoch); + pc.printf("Got a GPS fix and time.\n\r"); + pc.printf("Sending SOS in 10 seconds"); + green_led=ON; + } else { + pc.printf("\n\rGPS timed out and we have no existing fix.\n\r"); + pc.printf("We can send an Iridium packet but coordinates are rough.\n\r"); + } + pc.printf("Sending SOS in 10 seconds"); + red_led=ON; + while(true) {}; //STOP HERE + } + } + pc.printf("\b"); //Backspace } -} + // normal functionality lives here + pc.printf("\n\rStarting normal operation\n\r"); +/* + if (true) { //Temp simulation + while (1) { + if (gps_update()==EXIT_SUCCESS) { + gps_data_present = true; + int gps_epoch = get_epoch_from_last_gps_time(); + set_time(gps_epoch); + pc.printf("Got a GPS fix and time.\n\r"); + } else { + pc.printf("GPS timed out and we have no existing fix.\n\r"); + pc.printf("We can send an Iridium packet but coordinates are rough.\n\r"); + } + time_t seconds = time(NULL); + //printf("Time as a basic string = %s", ctime(&seconds)); + wait(60); + seconds = time(NULL); + + wait(33); + seconds = time(NULL); + printf("Time as a basic string = %s", ctime(&seconds)); + wait(60); + } */ + }