using Gert van der Knokke´s vga lib and Ivo van Poorten´ss fastlib to receive from GPS data and display on monitor

Dependencies:   mbed vga640x480g

Committer:
smayr
Date:
Sun Jan 01 21:30:01 2012 +0000
Revision:
0:2b28d7f11022
Child:
2:794da1b39773

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
smayr 0:2b28d7f11022 1 /*
smayr 0:2b28d7f11022 2 * 640x480 60Hz fullgraphic monochrome VGA Driver demo program
smayr 0:2b28d7f11022 3 *
smayr 0:2b28d7f11022 4 * Copyright (C) 2011 by Ivo van Poorten <ivop(at)euronet.nl>
smayr 0:2b28d7f11022 5 * and Gert van der Knokke <gertk(at)xs4all.nl>
smayr 0:2b28d7f11022 6 * This file is licensed under the terms of the GNU Lesser
smayr 0:2b28d7f11022 7 * General Public License, version 3.
smayr 0:2b28d7f11022 8 *
smayr 0:2b28d7f11022 9 * Inspired by Simon's (not Ford) Cookbook entry and Cliff Biffle's
smayr 0:2b28d7f11022 10 * assembly code.
smayr 0:2b28d7f11022 11 */
smayr 0:2b28d7f11022 12
smayr 0:2b28d7f11022 13 #include "mbed.h"
smayr 0:2b28d7f11022 14 #include "fastlib/common.h"
smayr 0:2b28d7f11022 15 #include "fastlib/pinsel.h"
smayr 0:2b28d7f11022 16 #include "fastlib/gpio.h"
smayr 0:2b28d7f11022 17 #include "fastlib/clock.h"
smayr 0:2b28d7f11022 18 #include "fastlib/power.h"
smayr 0:2b28d7f11022 19 #include "fastlib/pwm.h"
smayr 0:2b28d7f11022 20 #include "fastlib/i2s.h"
smayr 0:2b28d7f11022 21 #include "fastlib/uart.h"
smayr 0:2b28d7f11022 22 #include "fastlib/dma.h"
smayr 0:2b28d7f11022 23 #include "fastlib/nvic.h"
smayr 0:2b28d7f11022 24
smayr 0:2b28d7f11022 25 #include "vga640x480g.h"
smayr 0:2b28d7f11022 26 #include "TextLCD.h"
smayr 0:2b28d7f11022 27
smayr 0:2b28d7f11022 28 // visual feedback
smayr 0:2b28d7f11022 29 DigitalOut myled(LED1);
smayr 0:2b28d7f11022 30 DigitalOut led2(LED2);
smayr 0:2b28d7f11022 31 DigitalOut led3(LED3);
smayr 0:2b28d7f11022 32 DigitalOut led4(LED4); // Data sent LED
smayr 0:2b28d7f11022 33
smayr 0:2b28d7f11022 34 // Commands for SIRF III GPS
smayr 0:2b28d7f11022 35 // to turn on or off data strings
smayr 0:2b28d7f11022 36 #define GGA_ON "$PSRF103,00,00,01,01*25\r\n"
smayr 0:2b28d7f11022 37 #define GGA_OFF "$PSRF103,00,00,00,01*24\r\n"
smayr 0:2b28d7f11022 38 #define GLL_ON "$PSRF103,01,00,01,01*24\r\n"
smayr 0:2b28d7f11022 39 #define GLL_OFF "$PSRF103,01,00,00,01*25\r\n"
smayr 0:2b28d7f11022 40 #define GSA_ON "$PSRF103,02,00,01,01*27\r\n"
smayr 0:2b28d7f11022 41 #define GSA_OFF "$PSRF103,02,00,00,01*26\r\n"
smayr 0:2b28d7f11022 42 #define GSV_ON "$PSRF103,03,00,05,01*22\r\n"
smayr 0:2b28d7f11022 43 #define GSV_OFF "$PSRF103,03,00,00,01*27\r\n"
smayr 0:2b28d7f11022 44 #define RMC_ON "$PSRF103,04,00,01,01*21\r\n"
smayr 0:2b28d7f11022 45 #define RMC_OFF "$PSRF103,04,00,00,01*20\r\n"
smayr 0:2b28d7f11022 46 #define VTG_ON "$PSRF103,05,00,01,01*20\r\n"
smayr 0:2b28d7f11022 47 #define VTG_OFF "$PSRF103,05,00,00,01*21\r\n"
smayr 0:2b28d7f11022 48 //#define WAAS_ON "$PSRF151,1*3F\r\n"
smayr 0:2b28d7f11022 49 //#define WAAS_OFF "$PSRF151,0*3E\r\n"
smayr 0:2b28d7f11022 50 #define UARTPORT 3
smayr 0:2b28d7f11022 51
smayr 0:2b28d7f11022 52 // Set up serial ports and LEDs
smayr 0:2b28d7f11022 53 // SIRF GPS on Pin28 and Pin7
smayr 0:2b28d7f11022 54 //Serial gps(p9, p10); // tx, rx - 4800 baud required
smayr 0:2b28d7f11022 55
smayr 0:2b28d7f11022 56 //TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d0-d3
smayr 0:2b28d7f11022 57
smayr 0:2b28d7f11022 58 //char buffidx;
smayr 0:2b28d7f11022 59 char vgatxtbuff[80]; // Data buffer for VGA output
smayr 0:2b28d7f11022 60 char msg[256]; // GPS data buffer
smayr 0:2b28d7f11022 61 char status; // GPS status 'V' = Not locked; 'A' = Locked
smayr 0:2b28d7f11022 62 char *parseptr; // Pointer for parsing GPS message
smayr 0:2b28d7f11022 63 char time_buff[7] = "000000";
smayr 0:2b28d7f11022 64 char date_buff[7] = "000000";
smayr 0:2b28d7f11022 65 char sens_buff[7] = "123456";
smayr 0:2b28d7f11022 66 int i;
smayr 0:2b28d7f11022 67
smayr 0:2b28d7f11022 68 void putcharact(char chardata) {
smayr 0:2b28d7f11022 69
smayr 0:2b28d7f11022 70 while ((fl_uart_get_line_status(UARTPORT)& 0x60)==0) {
smayr 0:2b28d7f11022 71
smayr 0:2b28d7f11022 72 }
smayr 0:2b28d7f11022 73 *FL_UART(UARTPORT, THR)= chardata;
smayr 0:2b28d7f11022 74 }
smayr 0:2b28d7f11022 75 char getcharact(void) {
smayr 0:2b28d7f11022 76 while ((fl_uart_get_line_status(UARTPORT)& 0x01)==0) {
smayr 0:2b28d7f11022 77 }
smayr 0:2b28d7f11022 78 return fl_uart_receive_byte(UARTPORT);
smayr 0:2b28d7f11022 79 }
smayr 0:2b28d7f11022 80
smayr 0:2b28d7f11022 81 // Switch off all but $GPRMC string
smayr 0:2b28d7f11022 82 void setgps() {
smayr 0:2b28d7f11022 83 sprintf(msg,GGA_ON);
smayr 0:2b28d7f11022 84 i=0;
smayr 0:2b28d7f11022 85 while (msg[i]!= 0) {
smayr 0:2b28d7f11022 86 putcharact(msg[i]);
smayr 0:2b28d7f11022 87 i++;
smayr 0:2b28d7f11022 88 }
smayr 0:2b28d7f11022 89 sprintf(msg,GLL_ON);
smayr 0:2b28d7f11022 90 i=0;
smayr 0:2b28d7f11022 91 while (msg[i]!= 0) {
smayr 0:2b28d7f11022 92 putcharact(msg[i]);
smayr 0:2b28d7f11022 93 i++;
smayr 0:2b28d7f11022 94 }
smayr 0:2b28d7f11022 95 sprintf(msg,GSA_ON);
smayr 0:2b28d7f11022 96 i=0;
smayr 0:2b28d7f11022 97 while (msg[i]!= 0) {
smayr 0:2b28d7f11022 98 putcharact(msg[i]);
smayr 0:2b28d7f11022 99 i++;
smayr 0:2b28d7f11022 100 }
smayr 0:2b28d7f11022 101 sprintf(msg,GSV_ON);
smayr 0:2b28d7f11022 102 i=0;
smayr 0:2b28d7f11022 103 while (msg[i]!= 0) {
smayr 0:2b28d7f11022 104 putcharact(msg[i]);
smayr 0:2b28d7f11022 105 i++;
smayr 0:2b28d7f11022 106 }
smayr 0:2b28d7f11022 107 sprintf(msg,RMC_ON);
smayr 0:2b28d7f11022 108 i=0;
smayr 0:2b28d7f11022 109 while (msg[i]!= 0) {
smayr 0:2b28d7f11022 110 putcharact(msg[i]);
smayr 0:2b28d7f11022 111 i++;
smayr 0:2b28d7f11022 112 }
smayr 0:2b28d7f11022 113 sprintf(msg,VTG_ON);
smayr 0:2b28d7f11022 114 i=0;
smayr 0:2b28d7f11022 115 while (msg[i]!= 0) {
smayr 0:2b28d7f11022 116 putcharact(msg[i]);
smayr 0:2b28d7f11022 117 i++;
smayr 0:2b28d7f11022 118 }
smayr 0:2b28d7f11022 119 // gps.printf(WAAS_OFF);
smayr 0:2b28d7f11022 120 return;
smayr 0:2b28d7f11022 121 }
smayr 0:2b28d7f11022 122
smayr 0:2b28d7f11022 123 // Get line of data from GPS
smayr 0:2b28d7f11022 124 void getline() {
smayr 0:2b28d7f11022 125 while (getcharact() != '$'); // Wait for start of line
smayr 0:2b28d7f11022 126 for (int i=0; i<256; i++) {
smayr 0:2b28d7f11022 127 msg[i] = getcharact();
smayr 0:2b28d7f11022 128 if (msg[i] == '\r') {
smayr 0:2b28d7f11022 129 msg[i] = 0;
smayr 0:2b28d7f11022 130 return;
smayr 0:2b28d7f11022 131 }
smayr 0:2b28d7f11022 132 }
smayr 0:2b28d7f11022 133 }
smayr 0:2b28d7f11022 134
smayr 0:2b28d7f11022 135
smayr 0:2b28d7f11022 136 float trunc(float v) {
smayr 0:2b28d7f11022 137 if(v < 0.0) {
smayr 0:2b28d7f11022 138 v*= -1.0;
smayr 0:2b28d7f11022 139 v = floor(v);
smayr 0:2b28d7f11022 140 v*=-1.0;
smayr 0:2b28d7f11022 141 } else {
smayr 0:2b28d7f11022 142 v = floor(v);
smayr 0:2b28d7f11022 143 }
smayr 0:2b28d7f11022 144 return v;
smayr 0:2b28d7f11022 145 }
smayr 0:2b28d7f11022 146
smayr 0:2b28d7f11022 147 /** The longitude (call sample() to set) */
smayr 0:2b28d7f11022 148 float longitude;
smayr 0:2b28d7f11022 149 /** The latitude (call sample() to set) */
smayr 0:2b28d7f11022 150 float latitude;
smayr 0:2b28d7f11022 151 float gps_time;
smayr 0:2b28d7f11022 152 char ns, ew;
smayr 0:2b28d7f11022 153 int lock,sat_used;
smayr 0:2b28d7f11022 154 float HDOP;
smayr 0:2b28d7f11022 155 float altitude;
smayr 0:2b28d7f11022 156
smayr 0:2b28d7f11022 157 int toggle_flag=0;
smayr 0:2b28d7f11022 158 int gps_hour;
smayr 0:2b28d7f11022 159 int gps_min;
smayr 0:2b28d7f11022 160 int gps_sec;
smayr 0:2b28d7f11022 161 int s;
smayr 0:2b28d7f11022 162 unsigned power_state;
smayr 0:2b28d7f11022 163
smayr 0:2b28d7f11022 164 int main() {
smayr 0:2b28d7f11022 165 // init the VGA subsystem (always do this first!)
smayr 0:2b28d7f11022 166 init_vga();
smayr 0:2b28d7f11022 167 setbuf(stdout, NULL); // no buffering for this filehandle
smayr 0:2b28d7f11022 168
smayr 0:2b28d7f11022 169 // gps.baud(4800); // NB GPS baud rate 4800
smayr 0:2b28d7f11022 170 fl_power_uart3(FL_ON);
smayr 0:2b28d7f11022 171 fl_select_clock_uart3(FL_CLOCK_DIV1);
smayr 0:2b28d7f11022 172 *FL_UART(UARTPORT, LCR) = 0x00000083; //DLAB 1 for access to divisors
smayr 0:2b28d7f11022 173 fl_uart_set_fractional_divider(UARTPORT, 0, 0);
smayr 0:2b28d7f11022 174 fl_uart_set_divisor_latch(UARTPORT, 1302);
smayr 0:2b28d7f11022 175 *FL_UART(UARTPORT, FCR) = 0x00000007; // FIFO enable (must) and clear FIFOs
smayr 0:2b28d7f11022 176 *FL_UART(UARTPORT, LCR) = 0x00000003; // DLAB 0
smayr 0:2b28d7f11022 177 *FL_UART(UARTPORT, IER) = 0x00000000; // no interrupts
smayr 0:2b28d7f11022 178 fl_pinsel(0, 0, FL_FUNC_ALT2, FL_IGNORE, FL_IGNORE); // TX3
smayr 0:2b28d7f11022 179 fl_pinsel(0, 1, FL_FUNC_ALT2, FL_IGNORE, FL_IGNORE); // RX3
smayr 0:2b28d7f11022 180 fl_uart_transmitter_enable(UARTPORT, 1);
smayr 0:2b28d7f11022 181
smayr 0:2b28d7f11022 182 setgps();
smayr 0:2b28d7f11022 183
smayr 0:2b28d7f11022 184
smayr 0:2b28d7f11022 185 // clear the screen
smayr 0:2b28d7f11022 186 vga_cls();
smayr 0:2b28d7f11022 187 // circumfence the screen
smayr 0:2b28d7f11022 188 vga_box(0,0,639,479,WHITE);
smayr 0:2b28d7f11022 189 vga_putstring(400,370,"GPS DEMO",WHITE);
smayr 0:2b28d7f11022 190 /* // put a string on screen
smayr 0:2b28d7f11022 191 for (s=0; s<150; s++) {
smayr 0:2b28d7f11022 192 vga_putstring(400,370-s,"GPS DEMO",WHITE);
smayr 0:2b28d7f11022 193 wait(0.01);
smayr 0:2b28d7f11022 194 vga_putstring(400,370-s,"GPS DEMO",BLACK);
smayr 0:2b28d7f11022 195 }
smayr 0:2b28d7f11022 196 for (s=0; s<150; s++) {
smayr 0:2b28d7f11022 197 vga_putstring(400-s,220,"GPS DEMO",WHITE);
smayr 0:2b28d7f11022 198 wait(0.01);
smayr 0:2b28d7f11022 199 vga_putstring(400-s,220,"GPS DEMO",BLACK);
smayr 0:2b28d7f11022 200 }
smayr 0:2b28d7f11022 201 for (s=0; s<150; s++) {
smayr 0:2b28d7f11022 202 vga_putstring(250,220+s,"GPS DEMO",WHITE);
smayr 0:2b28d7f11022 203 wait(0.01);
smayr 0:2b28d7f11022 204 vga_putstring(250,220+s,"GPS DEMO",BLACK);
smayr 0:2b28d7f11022 205 }
smayr 0:2b28d7f11022 206 for (s=0; s<150; s++) {
smayr 0:2b28d7f11022 207 vga_putstring(250+s,370,"GPS DEMO",WHITE);
smayr 0:2b28d7f11022 208 wait(0.01);
smayr 0:2b28d7f11022 209 vga_putstring(250+s,370,"GPS DEMO",BLACK);
smayr 0:2b28d7f11022 210 }
smayr 0:2b28d7f11022 211 wait(1);
smayr 0:2b28d7f11022 212 for (s=0;s<30;s++){
smayr 0:2b28d7f11022 213 vga_putstring(400,370,"GPS DEMO",WHITE);
smayr 0:2b28d7f11022 214 wait(.1);
smayr 0:2b28d7f11022 215 vga_putstring(400,370,"GPS DEMO",BLACK);
smayr 0:2b28d7f11022 216 wait(.1);
smayr 0:2b28d7f11022 217 }
smayr 0:2b28d7f11022 218 */
smayr 0:2b28d7f11022 219
smayr 0:2b28d7f11022 220 vga_putstring(10,50,"CGA",WHITE);
smayr 0:2b28d7f11022 221 vga_putstring(10,70,"GLL",WHITE);
smayr 0:2b28d7f11022 222 vga_putstring(10,90,"GSA",WHITE);
smayr 0:2b28d7f11022 223 vga_putstring(10,110,"GSV",WHITE);
smayr 0:2b28d7f11022 224 vga_putstring(10,130,"RMC",WHITE);
smayr 0:2b28d7f11022 225 vga_putstring(10,150,"RMC",WHITE);
smayr 0:2b28d7f11022 226
smayr 0:2b28d7f11022 227
smayr 0:2b28d7f11022 228
smayr 0:2b28d7f11022 229 while (1) {
smayr 0:2b28d7f11022 230 led4=1;
smayr 0:2b28d7f11022 231 getline();
smayr 0:2b28d7f11022 232 if (sscanf(msg,"GPGGA,%s", &vgatxtbuff)>=1){
smayr 0:2b28d7f11022 233 vga_putstring(50,50,vgatxtbuff,WHITE);
smayr 0:2b28d7f11022 234 }
smayr 0:2b28d7f11022 235 if (sscanf(msg,"GPGLL,%s", &vgatxtbuff)>=1){
smayr 0:2b28d7f11022 236 vga_putstring(50,70,vgatxtbuff,WHITE);
smayr 0:2b28d7f11022 237 }
smayr 0:2b28d7f11022 238 if (sscanf(msg,"GPGSA,%s", &vgatxtbuff)>=1){
smayr 0:2b28d7f11022 239 vga_putstring(50,90,vgatxtbuff,WHITE);
smayr 0:2b28d7f11022 240 }
smayr 0:2b28d7f11022 241 if (sscanf(msg,"GPGSV,%s", &vgatxtbuff)>=1){
smayr 0:2b28d7f11022 242 vga_putstring(50,110,vgatxtbuff,WHITE);
smayr 0:2b28d7f11022 243 }
smayr 0:2b28d7f11022 244 if (sscanf(msg,"GPRMC,%s", &vgatxtbuff)>=1){
smayr 0:2b28d7f11022 245 vga_putstring(50,130,vgatxtbuff,WHITE);
smayr 0:2b28d7f11022 246 }
smayr 0:2b28d7f11022 247 if (sscanf(msg,"GPVTG,%s", &vgatxtbuff)>=1){
smayr 0:2b28d7f11022 248 vga_putstring(50,150,vgatxtbuff,WHITE);
smayr 0:2b28d7f11022 249 }
smayr 0:2b28d7f11022 250 // Check if it is a GPGGA msg (matches both locked and non-locked msg)
smayr 0:2b28d7f11022 251 if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f", &gps_time, &latitude, &ns, &longitude, &ew, &lock, &sat_used, &HDOP, &altitude) >= 1) {
smayr 0:2b28d7f11022 252 if(!lock) {
smayr 0:2b28d7f11022 253 led4 = 1;
smayr 0:2b28d7f11022 254 longitude = 0.0;
smayr 0:2b28d7f11022 255 latitude = 0.0;
smayr 0:2b28d7f11022 256 } else {
smayr 0:2b28d7f11022 257 led4 = 0;
smayr 0:2b28d7f11022 258 if(ns == 'S') { latitude *= -1.0; }
smayr 0:2b28d7f11022 259 if(ew == 'W') { longitude *= -1.0; }
smayr 0:2b28d7f11022 260 float degrees = trunc(latitude / 100.0f);
smayr 0:2b28d7f11022 261 float minutes = latitude - (degrees * 100.0f);
smayr 0:2b28d7f11022 262 latitude = degrees + minutes / 60.0f;
smayr 0:2b28d7f11022 263 degrees = trunc(longitude / 100.0f);
smayr 0:2b28d7f11022 264 minutes = longitude - (degrees * 100.0f);
smayr 0:2b28d7f11022 265 longitude = degrees + minutes / 60.0f;
smayr 0:2b28d7f11022 266 gps_hour = trunc(gps_time/10000.0f);
smayr 0:2b28d7f11022 267 gps_min = trunc((gps_time - gps_hour*10000.0f)/100.0f);
smayr 0:2b28d7f11022 268 gps_sec = trunc(gps_time-gps_hour*10000.0f-gps_min*100.0f);
smayr 0:2b28d7f11022 269 gps_hour++;
smayr 0:2b28d7f11022 270 if (gps_hour>=24) {
smayr 0:2b28d7f11022 271 gps_hour=0;
smayr 0:2b28d7f11022 272 }
smayr 0:2b28d7f11022 273 }
smayr 0:2b28d7f11022 274 sprintf(vgatxtbuff, "lat: %3.7f long: %3.7f time: %2.4f sat_used: %d alt: %5.3f",latitude, longitude,gps_time, sat_used, altitude);
smayr 0:2b28d7f11022 275 vga_putstring(10,30,vgatxtbuff,WHITE);
smayr 0:2b28d7f11022 276 }
smayr 0:2b28d7f11022 277 }
smayr 0:2b28d7f11022 278 }
smayr 0:2b28d7f11022 279