Improved, thread compatible. Adds new features

Dependents:   GroveGPS-Example

Fork of GroveGPS by Michael Ray

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GroveGPS.cpp Source File

GroveGPS.cpp

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #include "mbed.h"
00020 
00021 #include "GroveGPS.h"
00022 
00023 Serial gps_serial(D1, D0, 9600);
00024 
00025 
00026 
00027 //Thread gpsThread;
00028 GroveGPS gps;
00029     
00030 Semaphore parse;
00031 Thread parsethread(osPriorityBelowNormal);
00032 
00033 
00034     static void parseLine() {
00035         while(1) {
00036             parse.wait();
00037             if (gps._last_line.find("GPGGA") != std::string::npos) {
00038                 gps.parseGGA();
00039             }
00040             if (gps._last_line.find("GPZDA") != std::string::npos) {
00041                 gps.parseZDA();
00042             }
00043             if (gps._last_line.find("GPVTG") != std::string::npos) {
00044                 gps.parseVTG();
00045             }
00046             gps._last_line = "";
00047         }
00048     }
00049 
00050 
00051     void readCharacter(char newCharacter) {
00052         if (newCharacter == '\n') {
00053             parse.release();
00054         } else {
00055             gps._last_line += newCharacter;
00056         }
00057     }
00058         
00059 
00060 void service_serial(void) {
00061     readCharacter(gps_serial.getc());
00062     }
00063  
00064 
00065 int calc_cs(char * str) {
00066     char cs = 0;
00067     int x = 1;
00068 
00069     while(str[x] != '*') {
00070         cs ^= str[x++];
00071         }   
00072     return(cs);
00073     }
00074     
00075 void nema_send( void ) {
00076     
00077     char nema_mode[] = "$PMTK314,0,0,5,5,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0*";
00078     char nema_cmd[64];
00079    int x = 0;
00080     sprintf( nema_cmd, "%s%x\r\n", nema_mode, calc_cs(nema_mode));
00081     while(nema_cmd[x]) {
00082         gps_serial.putc(nema_cmd[x++]);
00083         }
00084     }
00085 
00086 int GPS_init() {
00087     gps._last_line = "";
00088    gps_serial.attach( &service_serial, Serial::RxIrq );
00089    gps.gps_gga.new_flag = 0;
00090    gps.gps_zda.new_flag = 0;
00091    gps.gps_vtg.new_flag = 0;
00092    nema_send();
00093    printf("\r\nGPS Init\r\n");
00094    parsethread.start(parseLine);
00095    return 0;
00096 }