Shinichiro Nakamura / Mbed 2 deprecated SerialGPS_TestProgram

Dependencies:   mbed RemoteIR SerialGPS SDFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * A test program for serial GPS module interface driver class (Version 0.0.1)
00003  * The interface driver supports NMEA-0183 serial based modules.
00004  *
00005  * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
00006  * http://shinta.main.jp/
00007  */
00008 #include "mbed.h"
00009 #include "TextLCD.h"
00010 #include "SerialGPS.h"
00011 #include "SDFileSystem.h"
00012 #include "ReceiverIR.h"
00013 #include "RemoteIR.h"
00014 
00015 /*
00016  * Objects.
00017  */
00018 TextLCD lcd(p24, p26, p27, p28, p29, p30);
00019 BusOut led(LED4, LED3, LED2, LED1);
00020 SerialGPS gps(p9, p10);
00021 SDFileSystem sd(p5, p6, p7, p8, "sd");
00022 Ticker ticker;
00023 ReceiverIR ir_rx(p15);
00024 
00025 /*
00026  * Variables.
00027  */
00028 static bool sd_disabled = false;
00029 static FILE *fp = NULL;
00030 static int display_mode = 0;
00031 
00032 /*
00033  * Functions.
00034  */
00035 
00036 /**
00037  * A ticker function.
00038  */
00039 void func_ticker(void) {
00040     led = led + 1;
00041 }
00042 
00043 /**
00044  * A callback function for logging data.
00045  */
00046 void cbfunc_log(char *s) {
00047     if (!sd_disabled) {
00048         fprintf(fp, "%s\n", s);
00049         fflush(fp);
00050     } else {
00051         printf("%s\n", s);
00052     }
00053 }
00054 
00055 /**
00056  * A callback function for GGA.
00057  *
00058  * GGA - Global Positioning System Fixed Data.
00059  */
00060 void cbfunc_gga(SerialGPS::gps_gga_t *p) {
00061     if (display_mode == 0) {
00062         lcd.locate(0, 1);
00063         lcd.printf("%02d:%02d:%02d(P%d,S%d)", p->hour, p->min, p->sec, p->position_fix, p->satellites_used);
00064     }
00065 
00066     if (display_mode == 1) {
00067         lcd.locate(0, 1);
00068         lcd.printf("%c=%10.4f", p->ns, p->latitude);
00069     }
00070     if (display_mode == 2) {
00071         lcd.locate(0, 1);
00072         lcd.printf("%c=%10.4f", p->ew, p->longitude);
00073     }
00074 }
00075 
00076 /**
00077  * A callback function for GSA.
00078  *
00079  * GSA - GNSS DOP and Active Satellites.
00080  */
00081 void cbfunc_gsa(SerialGPS::gps_gsa_t *p) {
00082     if (display_mode == 3) {
00083         lcd.locate(0, 1);
00084         lcd.printf("SEL:%c FIX:%d", p->selmode, p->fix);
00085     }
00086 }
00087 
00088 /**
00089  * A callback function for GSV.
00090  *
00091  * GSV - GNSS Satellites in View.
00092  */
00093 void cbfunc_gsv(SerialGPS::gps_gsv_t *p) {
00094     if (display_mode == 4) {
00095         lcd.locate(0, 1);
00096         lcd.printf("Satellites:%2d", p->satcnt);
00097     }
00098 }
00099 
00100 /**
00101  * A callback function for RMC.
00102  *
00103  * RMC - Recommended Minimum Specific GNSS Data.
00104  */
00105 void cbfunc_rmc(SerialGPS::gps_rmc_t *p) {
00106     if (display_mode == 5) {
00107         lcd.locate(0, 1);
00108         lcd.printf("%02d:%02d:%02d(%c)", p->hour, p->min, p->sec, p->status);
00109     }
00110 }
00111 
00112 /**
00113  * Entry point.
00114  */
00115 int main() {
00116     /*
00117      * Attach callback functions.
00118      */
00119     SerialGPS::gps_callback_t cb;
00120     cb.cbfunc_log = cbfunc_log;
00121     cb.cbfunc_gga = cbfunc_gga;
00122     cb.cbfunc_gsa = cbfunc_gsa;
00123     cb.cbfunc_gsv = cbfunc_gsv;
00124     cb.cbfunc_rmc = cbfunc_rmc;
00125     gps.attach(&cb);
00126 
00127     /*
00128      * Test a SD card.
00129      */
00130     fp = fopen("/sd/gpslog.txt", "w");
00131     if (fp == NULL) {
00132         sd_disabled = true;
00133     } else {
00134         sd_disabled = false;
00135         fprintf(fp, "Hello World!\n");
00136     }
00137 
00138     /*
00139      * Attach a ticker for interrupt test.
00140      */
00141     ticker.attach_us(&func_ticker, 250 * 1000);
00142 
00143     /*
00144      * Initial display.
00145      */
00146     lcd.cls();
00147     lcd.printf("GGA (Time)");
00148 
00149     /*
00150      * Loop.
00151      */
00152     int irlen;
00153     uint8_t irbuf[32];
00154     RemoteIR::Format irfmt;
00155     while (1) {
00156         irlen = ir_rx.getData(&irfmt, irbuf, sizeof(irbuf) * 8);
00157         if (0 < irlen) {
00158             uint64_t n = 0;
00159             for (int i = 0; i < irlen; i++) {
00160                 if (irbuf[i / 8] & (1 << (i % 8))) {
00161                     n = n | (1 << i);
00162                 }
00163             }
00164             // printf("%d:0x%llx\n", irlen, n);
00165             if ((irlen == 12) && (irfmt == RemoteIR::SONY)) {
00166                 switch (n) {
00167                     case 0x80:
00168                         display_mode = 0;
00169                         lcd.cls();
00170                         lcd.printf("GGA (Time)");
00171                         break;
00172                     case 0x81:
00173                         display_mode = 1;
00174                         lcd.cls();
00175                         lcd.printf("GGA (Latitude)");
00176                         break;
00177                     case 0x82:
00178                         display_mode = 2;
00179                         lcd.cls();
00180                         lcd.printf("GGA (Longitude)");
00181                         break;
00182                     case 0x83:
00183                         display_mode = 3;
00184                         lcd.cls();
00185                         lcd.printf("GSA (Statuses)");
00186                         break;
00187                     case 0x84:
00188                         display_mode = 4;
00189                         lcd.cls();
00190                         lcd.printf("GSV (Satellites)");
00191                         break;
00192                     case 0x85:
00193                         display_mode = 5;
00194                         lcd.cls();
00195                         lcd.printf("RMC (Time)");
00196                         break;
00197                     default:
00198                         break;
00199                 }
00200             }
00201         }
00202         gps.processing();
00203     }
00204 }