Geo Electronics "Geo Counter" / Mbed 2 deprecated GEO_SHOW2ME_F411RE_CDY

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "./mbed/mbed.h"
00002 #include "SDFileSystem.h"
00003 #include "ST7565_LCD.h"
00004 #include "QEI.h"
00005 
00006 #define BAT_GAIN    5.0     
00007 #define BKL_TH_OFF  0.83    // Ambient ligh threshold for auto backlight off
00008 #define BKL_TH_ON   0.81    // Ambient ligh threshold for auto backlight on
00009 #define BKL_LOW     0.0     // Ambient ligh offset for auto backlight
00010 #define FRM_ROW1    10      // Background frame, first horizontal line
00011 #define FRM_ROW2    60
00012 #define BUZZ_VOL    0.1     // Buzzer/speaker volume
00013 
00014 
00015 PwmOut      BEEP        (D2);      // PA_10 Buzzer/speaker (PWM output)
00016 PwmOut      BKL         (D3);      // PB_3 LCD backlight control (PMW output)
00017 DigitalOut  KAL         (PC_8);    // PC_8 Keep-Alive/turn-off
00018 DigitalOut  BTC         (PC_4);    // PC_4 Aux BT module control
00019 DigitalIn   Button      (D4);      // PB_5 Pushbutton (digital input)
00020 AnalogIn    BATT        (A0);      // PA_0 Battery monitor
00021 AnalogIn    ALS         (A1);      // PA_1 Ambient Light sensor
00022 AnalogIn    VCH1        (A2);      // PA_4 Analog input 1
00023 AnalogIn    VCH2        (A3);      // PB_0 Analog input 2
00024 AnalogIn    VCH3        (A4);      // PC_1 Analog input 3
00025 AnalogIn    VCH4        (A5);      // PC_0 Analog input 4
00026 InterruptIn TRIG1       (PC_13);   // PC_13 Counter 1 trigger
00027 InterruptIn TRIG2       (PC_2);    // PC_2 Counter 2 trigger
00028 InterruptIn TRIG3       (PA_15);   // PA_15 Counter 3 trigger
00029 DigitalOut  PLED        (PC_3);    // PC_3 Pulse LED
00030 DigitalOut  ALED        (PC_9);    // PC_9 Alarm LED
00031 DigitalIn   CHRG        (PC_10);   // PC_10 Charge in progress
00032 DigitalIn   EOCH        (PC_12);   // PC_12 Endo Of Charge
00033 DigitalOut  SDPW        (PB_2);    // PB_2 SD-Card power enable
00034 
00035 SDFileSystem sd(PB_15, PB_14, PB_13, PB_1, "sd"); // MOSI, MISO, SCK, CS
00036 
00037 // Quadrature encoder
00038 QEI Wheel(D6, D5, NC, 16); // PB_10, PB_4
00039 // Tickers
00040 Ticker Sec_Beat;            // Timer ticker
00041 Ticker Display_Refresh;     // Display refresh ticker
00042 
00043 //Serial ports
00044 Serial PC(USBTX, USBRX);    // Virtual COM via USB
00045 
00046 // GPS module
00047 Serial GPS(PA_11, PA_12, 9600);    // PA_11=TX, PA_12=RX, default baud-rate 
00048 I2C GPS_I2C(PB_9,PB_8);      // SDA=PB_9, SCL=PB_8
00049 
00050 extern unsigned int buffer[128*64/8];   // RAM buffer used by LCD
00051 time_t seconds;     // timestamp
00052 char Text[40]="";   // Text string used by LCD 
00053 float Vbatt, AmbLight;   // battery votage and ambient light level
00054 uint16_t    CNT1, CNT2, CNT3;   // pulse counters
00055 uint8_t FLASH_Status;
00056 float V1, V2, V3, V4;
00057 bool  Pulse=0, ExtPwr=0;
00058 
00059 // used by GPS:
00060 double latitude = 0.0;
00061 double longitude = 0.0;
00062 double altitude = 0.0;
00063 int num_sat;
00064 float hori_dilute;
00065 float alt = 0.0;
00066 float geoid, GPS_time;
00067 char ns = 'N', ew='E';
00068 char GPS_stream[256]="none";
00069 char gu, hu;
00070 //const int GPS_addr = 0x42;
00071 
00072 // ------------------- Prototypes ----------------------- 
00073 void Timer_tick(void);
00074 void Update_Display(void);
00075 void Set_Time(void);
00076 void Read_Voltages(void);
00077 void PowerOff(void);
00078 void DrawFrame(void);
00079 void CNT1_count(void);
00080 void CNT2_count(void);
00081 void CNT3_count(void);
00082 uint8_t WriteEEPROM(uint32_t, uint8_t);
00083 uint8_t ReadEEPROM(uint32_t);
00084 void SDCard_test(void);
00085 void EEPROM_test(void);
00086 void Init_All(void);
00087 int GPS_test(void);
00088 void GPS_getline(void);
00089 int GPS_get_stream(void);
00090 
00091 int main() 
00092 {
00093     Init_All();
00094    
00095     if(Button)  // if turn-on via pushbutton
00096     {   
00097         KAL = 1;    // self sustain power from battery
00098         ExtPwr=0;            
00099     }    
00100     else
00101         ExtPwr = 1;  // otherwise power comes from USB
00102 
00103     // debug: force 
00104     //ExtPwr = 0;
00105 
00106 
00107     // enable LCD refresh ticker        
00108     Display_Refresh.attach(&Update_Display, 0.1);
00109     
00110     wait(1.5);
00111     Clear_buffer(buffer);
00112         
00113     if(Button)   // if pushbutton still pressed after splash-screen
00114         Set_Time();  // set RTC time and date  
00115     
00116     // launch self-tests   
00117     SDCard_test();
00118     wait(1);
00119     //EEPROM_test();
00120     
00121     // draw background frame
00122     Clear_buffer(buffer);    
00123     DrawFrame();
00124 
00125     if(ExtPwr)      // if powered via USB, no need for backlight (recharge)
00126         BKL.write(0);
00127 
00128     // enable sec-beat ticker  
00129     Sec_Beat.attach(&Timer_tick, 1.113);
00130 
00131     //tm t = RTC::getDefaultTM();
00132     //RTC::attach(&Sec_Beat, RTC::Second);
00133     
00134     // enable & attach interrupts on rising edge of digital inputs
00135     TRIG1.rise(&CNT1_count);     
00136     TRIG2.rise(&CNT2_count);     
00137     TRIG3.rise(&CNT3_count);    
00138    
00139     while(1) 
00140     {   
00141     
00142         if(Button)   
00143             PowerOff();   // Power-off test 
00144         /*     
00145         // DEBUG: PC/GPS serial pass-through
00146         if(PC.readable()) 
00147             GPS.putc(PC.getc());
00148             
00149         if(GPS.readable()) 
00150             PC.putc(GPS.getc());
00151         */                   
00152                    
00153     } 
00154     
00155 }
00156 
00157 
00158 //===========================================================================
00159 
00160 // ------------- Called every second ----------------------
00161 
00162 void Timer_tick() 
00163 { 
00164     seconds = time(NULL); 
00165     strftime(Text, 50, "%d-%b-%Y  %H:%M:%S", localtime(&seconds));
00166     LCD_drawstring(buffer, 0, 0, Text);
00167 
00168     //TRIG1.rise(NULL);      // detach counters
00169     //TRIG2.rise(NULL);      //
00170     //TRIG3.rise(NULL);      //
00171 
00172     // read voltages 
00173     if(ExtPwr)
00174         KAL = 1;
00175     Read_Voltages();
00176     if(ExtPwr)
00177         KAL = 0;
00178 
00179 
00180     if(!ExtPwr)
00181         if(AmbLight>BKL_TH_OFF)
00182             BKL.write(BKL_LOW);
00183         else
00184             if(AmbLight<BKL_TH_ON)
00185                 BKL.write(AmbLight+(1-BKL_TH_ON));        
00186     
00187     // write values to buffer
00188     sprintf(Text,"VBATT= %4.2f", Vbatt);
00189     LCD_drawstring(buffer, 0, 2, Text);    
00190     sprintf(Text,"%4.1f %4.1f %4.1f %4.1f", V1, V2, V3, V4 );
00191     LCD_drawstring(buffer, 0, 3, Text);
00192     sprintf(Text,"CPS1= %5d", CNT1);
00193     LCD_drawstring(buffer, 0, 4, Text);
00194     sprintf(Text,"CPS2= %5d", CNT2);
00195     LCD_drawstring(buffer, 0, 5, Text);
00196     sprintf(Text,"CPS3= %5d", CNT3);
00197     LCD_drawstring(buffer, 0, 6, Text); 
00198     
00199     CNT1=CNT2=CNT3=0;            
00200 
00201     //TRIG1.rise(&CNT1_count);      //attach CNT1_count(); to TRIG1
00202     //TRIG2.rise(&CNT2_count);      //attach CNT2_count(); to TRIG2
00203     //TRIG3.rise(&CNT3_count);      //attach CNT3_count(); to TRIG3
00204                                   
00205     GPS_get_stream(); // GPS test
00206           
00207     return;
00208 }
00209 
00210 
00211 //---------------------------------------------------------------------------
00212 void Update_Display(void)
00213 {
00214 
00215     if(Pulse)
00216     {
00217         PLED = 0;
00218         BEEP.write(0);
00219         Pulse = 0;
00220     }    
00221     LCD_write_buffer(buffer);   // LCD update 
00222     return;  
00223 }
00224 
00225 //---------------------------------------------------------------------------
00226 void Set_Time(void)
00227 {
00228     uint8_t Year=0, Month=0, Day=0, Hours=0, Mins=0, Secs=0;
00229     time_t seconds;
00230     struct tm t;
00231 
00232     Clear_buffer(buffer);
00233     sprintf(Text,"TIME & DATE SETTING");
00234     LCD_drawstring(buffer, 0, 0, Text); 
00235     
00236     // Set year
00237     while(Button);
00238     wait_ms(50);  
00239 
00240     while(!Button)
00241     { 
00242         if(int(Wheel.getPulses())<0)
00243             Wheel.reset();
00244         Year = (uint8_t)(Wheel.getPulses());
00245         
00246         if(Year>99) 
00247             Wheel.reset();
00248              
00249         sprintf(Text, "Year: %2d", Year);
00250         LCD_drawstring(buffer, 0, 2, Text);
00251    
00252     }
00253 
00254     // Set month
00255     while(Button);
00256     wait_ms(50);  
00257     Wheel.reset();
00258     while(!Button)
00259     { 
00260         if(int(Wheel.getPulses())<0)
00261             Wheel.reset();
00262         Month = (uint8_t)(Wheel.getPulses()/2);
00263         
00264         if(Month>11) 
00265             Wheel.reset();
00266             
00267         sprintf(Text, "Month: %2d", Month+1);
00268         LCD_drawstring(buffer, 0, 3, Text);
00269    
00270     }
00271 
00272 
00273     // Set day
00274     while(Button);
00275     wait_ms(50);  
00276     Wheel.reset();
00277     while(!Button)
00278     { 
00279         if(int(Wheel.getPulses())<0)
00280             Wheel.reset();
00281         Day = (uint8_t)(Wheel.getPulses()/2);
00282         
00283         if(Day>30) 
00284             Wheel.reset();
00285             
00286         sprintf(Text, "Day: %2d", Day+1);
00287         LCD_drawstring(buffer, 0, 4, Text);
00288    
00289     }
00290 
00291     // Set hours
00292     while(Button);
00293     wait_ms(50);  
00294     Wheel.reset();
00295     while(!Button)
00296     { 
00297         if(int(Wheel.getPulses())<0)
00298             Wheel.reset();
00299         Hours = (uint8_t)(Wheel.getPulses()/2);
00300         
00301         if(Hours>22) 
00302             Wheel.reset();
00303             
00304         sprintf(Text, "Hours: %2d", Hours);
00305         LCD_drawstring(buffer, 0, 5, Text);
00306    
00307     }
00308     //Hours++;
00309 
00310     // Set minutes
00311     while(Button);
00312     wait_ms(50);  
00313     Wheel.reset();
00314     while(!Button)
00315     { 
00316         if(int(Wheel.getPulses())<0)
00317             Wheel.reset();
00318         Mins = (uint8_t)(Wheel.getPulses()/2);
00319         
00320         if(Mins>59) 
00321             Wheel.reset();
00322             
00323         sprintf(Text, "Minutes: %2d", Mins);
00324         LCD_drawstring(buffer, 0, 6, Text);
00325    
00326     }
00327 
00328     t.tm_year = Year + 100;
00329     t.tm_mon  = Month;
00330     t.tm_mday = Day + 1;
00331     t.tm_hour = Hours;
00332     t.tm_min  = Mins;
00333     t.tm_sec  = Secs;
00334 
00335     seconds = mktime(&t);
00336     set_time(seconds);
00337     
00338     Clear_buffer(buffer);
00339         
00340     return;
00341 }    
00342 
00343 
00344 //---------------------------------------------------------------------------
00345 void Read_Voltages(void)
00346 {
00347 
00348     double ADC_value;
00349     uint8_t smooth = 10; // Number of samples to smooth
00350     uint8_t i;
00351     
00352     // Read battery voltage
00353     
00354     ADC_value = BATT.read();    // cleanup
00355     wait_ms(5);
00356     ADC_value = 0;    
00357     for(i=0;i<smooth;i++) 
00358         ADC_value += BATT.read();       
00359 
00360     ADC_value = ADC_value/smooth;
00361     Vbatt = (float)(ADC_value*BAT_GAIN);
00362     
00363     
00364     // Read Ambient Light Level
00365     
00366     ADC_value = ALS.read();    // cleanup
00367     wait_ms(5);
00368     ADC_value = 0;    
00369     for(i=0;i<smooth;i++) 
00370         ADC_value += ALS.read();       
00371 
00372     ADC_value = ADC_value/smooth;
00373     AmbLight = (float)(ADC_value);     
00374     
00375     
00376     // Read AIN1
00377     
00378     ADC_value = VCH1.read();    // cleanup
00379     wait_ms(5);
00380     ADC_value = 0;    
00381     for(i=0;i<smooth;i++) 
00382         ADC_value += VCH1.read();       
00383 
00384     ADC_value = ADC_value/smooth;
00385     V1 = (float)(ADC_value);   
00386     
00387     // Read AIN2
00388     
00389     ADC_value = VCH2.read();    // cleanup
00390     wait_ms(5);
00391     ADC_value = 0;    
00392     for(i=0;i<smooth;i++) 
00393         ADC_value += VCH2.read();       
00394 
00395     ADC_value = ADC_value/smooth;
00396     V2 = (float)(ADC_value);  
00397     
00398     // Read AIN3
00399     
00400     ADC_value = VCH3.read();    // cleanup
00401     wait_ms(5);
00402     ADC_value = 0;    
00403     for(i=0;i<smooth;i++) 
00404         ADC_value += VCH3.read();       
00405 
00406     ADC_value = ADC_value/smooth;
00407     V3 = (float)(ADC_value);      
00408     
00409     // Read AIN4
00410     
00411     ADC_value = VCH4.read();    // cleanup
00412     wait_ms(5);
00413     ADC_value = 0;    
00414     for(i=0;i<smooth;i++) 
00415         ADC_value += VCH4.read();       
00416 
00417     ADC_value = ADC_value/smooth;
00418     V4 = (float)(ADC_value);          
00419         
00420    
00421 return;
00422 }
00423 
00424 
00425 //--------------------------------------------------------------------------- 
00426 void PowerOff(void) 
00427 {
00428     BKL.write(1);
00429     Display_Refresh.detach();
00430     Clear_buffer(buffer);
00431     sprintf(Text,"POWERING OFF");
00432     LCD_drawstring(buffer, 30, 3, Text); 
00433     LCD_write_buffer(buffer);
00434     wait(2);
00435     Clear_buffer(buffer);
00436     KAL = 0;
00437 }
00438 
00439 
00440 //---------------------------------------------------------------------------
00441 void DrawFrame(void)
00442 {
00443     uint8_t i;
00444     
00445     for(i=0; i<128; i++)
00446     {       
00447         LCD_setpixel(buffer, i, FRM_ROW1, 1);
00448         //LCD_setpixel(buffer, i, FRM_ROW2, 1);
00449     }
00450     return;  
00451 }
00452 
00453 
00454 //---------------------------------------------------------------------------
00455 void CNT1_count(void)
00456 {                       //function to call upon interrupt
00457     CNT1++;                      //increment counter object
00458     PLED = 1;
00459     BEEP.write(BUZZ_VOL);
00460     Pulse = 1;    
00461     return; 
00462 }
00463 
00464 
00465 //---------------------------------------------------------------------------
00466 void CNT2_count(void)
00467 {                       //function to call upon interrupt
00468     CNT2++;                      //increment counter object    
00469     return; 
00470 }
00471 
00472 
00473 //---------------------------------------------------------------------------
00474 void CNT3_count(void)
00475 {                       //function to call upon interrupt
00476     CNT3++;                      //increment counter object
00477     return; 
00478 }
00479 
00480 
00481 //---------------------------------------------------------------------------
00482 uint8_t WriteEEPROM(uint32_t address, uint8_t data)
00483 {                      
00484     HAL_StatusTypeDef  status;
00485     
00486     address = address + 0x08000000;
00487     HAL_FLASH_Unlock();
00488     status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, address, data);
00489     HAL_FLASH_Lock();
00490     
00491     return status;        
00492 }
00493 
00494 
00495 //--------------------------------------------------------------------------- 
00496 uint8_t ReadEEPROM(uint32_t address) 
00497 {
00498     uint8_t tmp = 0;
00499     
00500     address = address + 0x08000000;
00501     tmp = *(__IO uint32_t*)address;
00502     
00503     return tmp;
00504 }
00505 
00506 
00507 
00508 //---------------------------------------------------------------------------
00509 void SDCard_test(void)
00510 {
00511     // SD-Card test  
00512     printf("SD-Card test... ");
00513     //mkdir("/sd/system", 0777);
00514     FILE *fp = fopen("/sd/system/version.dat", "a");
00515     if(fp == NULL) 
00516     {
00517         printf("ERROR\n");
00518         sprintf(Text,"SD-CARD ERROR");        
00519         LCD_drawstring(buffer, 15, 2, Text);  
00520         LCD_write_buffer(buffer); 
00521     }
00522     else
00523     {
00524         printf("OK\n");        
00525         sprintf(Text,"SD-CARD DETECTED");        
00526         LCD_drawstring(buffer, 15, 2, Text);  
00527         LCD_write_buffer(buffer);    
00528         fprintf(fp, "Geo Electronics 2107\n");
00529         fprintf(fp, __DATE__);
00530         fprintf(fp, "\t");        
00531         fprintf(fp, __TIME__);                
00532         fprintf(fp, "\n");
00533         fclose(fp); 
00534     }
00535     printf("SD-Card end of test\n");
00536 
00537     return;
00538 }    
00539 
00540 
00541 //---------------------------------------------------------------------------
00542 void EEPROM_test(void)
00543 {
00544     // internal EEPROM test
00545     PC.printf("Attempting to write to EEPROM...\n");
00546 
00547     for (uint32_t i = 0; i < 8; i++) 
00548     {
00549         FLASH_Status = WriteEEPROM(i,(uint8_t)(i));
00550         PC.printf("Writing %d at %d\n", i, i);
00551     }
00552     if(FLASH_Status == HAL_FLASH_ERROR_NONE) 
00553         {PC.printf("Success!!\r\n");}
00554     else
00555         {PC.printf("Failed!!\r\n");}
00556                
00557     for (uint32_t i = 0; i < 8; i++) 
00558     {    
00559             uint8_t storedValue = ReadEEPROM(i);
00560             PC.printf("Reading %d at %d\n", storedValue, i);
00561     } 
00562        
00563     // end of EEPROM test
00564     return;
00565     
00566 }
00567 
00568 
00569 //---------------------------------------------------------------------------
00570 void Init_All(void)
00571 {
00572     
00573     GPS_I2C.frequency(300000);    // I2C GPS speed (400k max)
00574     //Button.mode(PullUp);      // enable pushbutton pull-up
00575     BKL.period_ms(5);         // set LCD backlight PWM
00576     BKL.write(1.0);  
00577     BEEP.period_us(2000);      // set initial buzzer period and duty-cycle
00578     BEEP.write(0);
00579     Wheel.reset();        // clear encoder
00580     LCD_reset();   
00581     CNT1=CNT2=CNT3=0;   // clear counters
00582     SDPW = 0;   // Enable SC-card VDD
00583     // splash screen with date and time
00584     sprintf(Text,__DATE__);
00585     LCD_drawstring(buffer, 60, 5, Text);
00586     sprintf(Text,__TIME__);
00587     LCD_drawstring(buffer, 78, 6, Text);  
00588     LCD_write_buffer(buffer);
00589     
00590     // buzzer beep and blink LEDs
00591     BEEP.write(BUZZ_VOL);
00592     ALED = 1;
00593     wait(0.2);
00594     BEEP.period_us(1000);
00595     ALED = 0;
00596     PLED = 1;   
00597     wait(0.2);
00598     BEEP.write(0);         
00599     PLED = 0;
00600     // enable internal pull-ups for digital inputs
00601     TRIG1.mode(PullUp);           
00602     TRIG2.mode(PullUp);           
00603     TRIG3.mode(PullUp);          
00604        
00605     return;
00606     
00607 }
00608 
00609 
00610 
00611 //---------------------------------------------------------------------------
00612 int GPS_test() 
00613 {
00614     //float time, hori_dilute, alt,geoid;
00615 
00616 // $GPGGA,104534.000,7791.0381,N,06727.4434,E,1,08,0.9,510.4,M,43.9,M,,*47
00617 // $GPGGA,HHMMSS.SSS,latitude,N,longitude,E,FQ,NOS,HDP,altitude,M,height,M,,checksum data
00618    
00619     int lock, n;
00620 
00621     if(!GPS.readable())
00622     {       
00623         return -1;
00624     }    
00625         
00626     
00627     //PC.printf("Readable "); 
00628 
00629     GPS_stream[0] = '\0';
00630             
00631     for (n = 0; n < 456; n++) 
00632     {
00633         GPS_stream[n] = GPS.getc(); 
00634         if(GPS_stream[n] == '\r') 
00635         {
00636             GPS_stream[n] = '\0';
00637             PC.printf("%s\n", GPS_stream); 
00638             return;
00639         }        
00640                
00641     }    
00642        
00643        
00644  
00645        
00646     while(GPS.readable()) 
00647     {        
00648         
00649         GPS_getline();
00650         
00651         // Check if it is a GPGGA msg (matches both locked and non-locked msg)
00652         //wait(5);
00653         
00654         PC.printf("GPS: %s\n", GPS_stream);
00655         
00656         if(sscanf(GPS_stream, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f,%c,%f,%c", &GPS_time, &latitude, &ns, &longitude, &ew, &lock, &num_sat, &hori_dilute, &alt, &hu, &geoid, &gu/*, &age_diff, &diff_ID*/) >= 1) 
00657         { 
00658             if(!lock) 
00659             {
00660                 longitude = 0.0;
00661                 latitude = 0.0;  
00662                 ns = 'Z';
00663                 ew = 'Z';
00664                 alt = 0.0;      
00665                 return 0;
00666             } 
00667             else 
00668             {
00669                 //if(ns == 'S') {    latitude  *= -1.0; }
00670 //                if(ew == 'W') {    longitude *= -1.0; }
00671 //                float degrees = trunc(latitude / 100.0f);
00672 //                float minutes = latitude - (degrees * 100.0f);
00673 //                latitude = degrees + minutes / 60.0f;    
00674 //                degrees = trunc(longitude / 100.0f * 0.01f);
00675 //                minutes = longitude - (degrees * 100.0f);
00676 //                longitude = degrees + minutes / 60.0f;
00677 //                pc1.printf(msg);
00678                 PC.printf("\n\rlongitude is %f\n\r", longitude);
00679                 PC.printf("\n\rtime is %f\n\r", GPS_time);
00680                 PC.printf("ns is %c\n\r", ns);
00681                 PC.printf("ew is %c\n\r", ew);
00682                 PC.printf("alt is %f\n\r", alt);
00683                 
00684                 latitude /= 100;
00685                 longitude /= 100;
00686                 
00687                 return 1;
00688             }
00689         
00690         }
00691         
00692         return 0;
00693     }
00694     
00695  return(-1);
00696     
00697 }
00698 
00699 
00700 //---------------------------------------------------------------------------
00701 void GPS_getline() 
00702 {
00703     int i;
00704     char a;
00705     int n;
00706     
00707     //strcpy(GPS_stream, '\0');
00708     GPS_stream[0] = '\0';
00709       
00710             
00711     while(GPS.readable()) 
00712     {
00713     
00714         i = 0;
00715         a = GPS.getc();
00716         
00717         GPS_stream[i] = a;
00718         
00719         if (a == '$') 
00720         {
00721             //PC.printf("%c",a);
00722             a = GPS.getc();
00723             GPS_stream[i] = a;
00724             i++;
00725             if (a == 'G') 
00726             {
00727                 //PC.printf("%c",a);
00728                 a = GPS.getc();
00729                 GPS_stream[i] = a;
00730                 i++;
00731                 if (a == 'P') 
00732                 {
00733                     //PC.printf("%c",a);
00734                     a = GPS.getc();
00735                     GPS_stream[i] = a;
00736                     i++;
00737                     if (a == 'G') 
00738                     {
00739                         //PC.printf("%c",a);
00740                         a = GPS.getc();
00741                         GPS_stream[i] = a;
00742                         i++;
00743                         if (a == 'G') 
00744                         {
00745                             //PC.printf("%c",a);
00746                             a = GPS.getc();
00747                             GPS_stream[i] = a;
00748                             i++;
00749                             if (a == 'A') 
00750                             {
00751                                 //PC.printf("%c",a);
00752                                 //a = GPS.getc();
00753                                 //msg[i] = a;
00754                                 //PC.printf(msg);
00755                                 //PC.printf("\r\n");
00756         
00757                                 for (n = 5; n < 456; n++) 
00758                                 {
00759                                     GPS_stream[n] = GPS.getc();
00760                                     //PC.printf("%c", GPS_stream[n]);
00761                                     if(GPS_stream[n] == '\r') 
00762                                     {
00763                                         GPS_stream[n] = '0';
00764                                         return;
00765                                     }
00766                             }
00767                          }
00768                      }
00769                  }
00770             }
00771        }
00772        }                          
00773 //    while(GPS.getc() != '$') {
00774 //        //char a = GPS.getc();
00775 //        for(i = 0; i < 256; i++) {
00776 //            msg[i] = GPS.getc();
00777 //            pc1.printf("%c", msg[i]);
00778 //            if(msg[i] == '\r') {
00779 //             msg[i] = 0;
00780 //             return;
00781 //                }
00782 //                }
00783 //          
00784 //         
00785 //    }
00786 //    while(GPS.getc() != '$');    // wait for the start of a line
00787 //    for(int i=0; i<256; i++) {
00788 //        msg[i] = GPS.getc();
00789 //        if(msg[i] == '\r') {
00790 //            msg[i] = 0;
00791 //            return;
00792 //        }
00793 //    }
00794 //    error("Overflowed message limit");
00795 }
00796 
00797     return;
00798 }
00799 
00800 
00801 //---------------------------------------------------------------------------
00802 
00803 int GPS_get_stream(void)
00804 {
00805     char cmd[256];    
00806     int i;
00807 
00808     GPS_stream[0] = '\0';  // buffer cleanup
00809         
00810     cmd[0] = 0xFF;  
00811     GPS_I2C.write(0x42, cmd, 1);
00812     GPS_I2C.read(0x42, cmd, 100);
00813     //cmd[21] = '\0';    
00814     i=0;
00815     while((i<100))
00816     {    
00817         PC.printf("%c", cmd[i]); 
00818         //if(cmd[i]=='\n')
00819         //    i=100;
00820         //else
00821             i++;    
00822     }
00823     
00824     
00825 
00826     return 1;
00827 }
00828 
00829 // ==========================================================================