![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Example code for reading from a serial GPS unit & parsing an RMC GPS string to a struct.
Fork of mbed_blinky by
main.cpp@16:1bf12f0bb4b9, 2017-03-09 (annotated)
- Committer:
- HackerLabATTKit01
- Date:
- Thu Mar 09 21:21:17 2017 +0000
- Revision:
- 16:1bf12f0bb4b9
- Parent:
- 4:81cea7a352b0
- Child:
- 17:15322357429b
Example code for reading from a serial GPS unit & parsing an RMC GPS string into a struct.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dan | 0:7dec7e9ac085 | 1 | #include "mbed.h" |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 2 | #include "GPRMCSentence.h" |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 3 | |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 4 | #define GPSBUFFSIZE 256 |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 5 | |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 6 | Serial pc(USBTX, USBRX); // tx, rx |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 7 | Serial xgps(PTC15, PTC14); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 8 | |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 9 | unsigned gpsSentenceIndex = 0; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 10 | char gpsSentence[GPSBUFFSIZE]; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 11 | volatile bool newline_detected = false; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 12 | GPRMCSentence lastPosition; |
dan | 0:7dec7e9ac085 | 13 | |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 14 | void clearGpsSentence() { |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 15 | for(int i=0;i<GPSBUFFSIZE;i++) gpsSentence[i] = '\0'; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 16 | gpsSentenceIndex = 0; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 17 | } |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 18 | |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 19 | int main() { |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 20 | pc.printf("Where am I? ----------- \n"); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 21 | clearGpsSentence(); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 22 | while(1) { |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 23 | gpsSentence[gpsSentenceIndex] = xgps.getc(); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 24 | pc.putc(gpsSentence[gpsSentenceIndex]); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 25 | gpsSentenceIndex++; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 26 | if(gpsSentence[gpsSentenceIndex - 1] == '\n') { |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 27 | gpsSentence[gpsSentenceIndex] = '\0'; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 28 | if (parseGPRMCStrToStruct(gpsSentence, &lastPosition) >= 0) { |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 29 | pc.printf("time: %s Status: %c \n\r" , lastPosition.time, lastPosition.status); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 30 | pc.printf("Lat : %s %c \n\r" , lastPosition.latitude, lastPosition.latdir); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 31 | pc.printf("Long: %s %c \n\r" , lastPosition.longitude, lastPosition.longdir); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 32 | } |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 33 | clearGpsSentence(); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 34 | } |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 35 | } |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 36 | } |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 37 | |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 38 | |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 39 | /*include "mbed.h" |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 40 | |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 41 | DigitalOut rled(LED_RED); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 42 | DigitalOut gled(LED_GREEN); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 43 | DigitalOut bled(LED_BLUE); |
dan | 0:7dec7e9ac085 | 44 | |
dan | 0:7dec7e9ac085 | 45 | int main() { |
dan | 0:7dec7e9ac085 | 46 | while(1) { |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 47 | rled = 0; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 48 | gled = 0; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 49 | bled = 0; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 50 | wait(0.2); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 51 | rled = 0; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 52 | gled = 0; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 53 | bled = 1; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 54 | wait(0.2); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 55 | rled = 0; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 56 | gled = 1; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 57 | bled = 0; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 58 | wait(0.2); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 59 | rled = 0; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 60 | gled = 1; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 61 | bled = 1; |
dan | 0:7dec7e9ac085 | 62 | wait(0.2); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 63 | rled = 1; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 64 | gled = 0; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 65 | bled = 0; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 66 | wait(0.2); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 67 | rled = 1; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 68 | gled = 0; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 69 | bled = 1; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 70 | wait(0.2); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 71 | rled = 1; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 72 | gled = 1; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 73 | bled = 0; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 74 | wait(0.2); |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 75 | rled = 1; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 76 | gled = 1; |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 77 | bled = 1; |
stevep | 4:81cea7a352b0 | 78 | wait(0.2); |
stevep | 4:81cea7a352b0 | 79 | } |
HackerLabATTKit01 | 16:1bf12f0bb4b9 | 80 | }*/ |