2016-04-08 24 views
-1
#include "mbed.h" 
    #include "C12832_lcd.h" 
    #include<cstring> 
    #include<string> 
    #include<sstream> 

    C12832_LCD lcd;//creating LCD object 
    Serial s_comms(USBTX, USBRX);//creating a serial comms object 

    DigitalIn Button(p14);//using button to change pages 


    int main() 

    { 

    char str[100] = "$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A"; 

char*point; 
point = strtok(str, ","); 

int page_state = 0; 

for (int i = 0; point != NULL; i++){ 


    //time 
    if (i == 1 and page_state == 0){ 
     //using substrings to extract time elements 
     string time = point; 
     string hrs = time.substr(0, 2); 
     string mins = time.substr(2, 2); 
     string sec = time.substr(4, 2); 

     //using string streams to reformat time string 
     ostringstream tim; 
     tim << hrs << ":" << mins << ":" << sec; 
     time = tim.str(); 

     lcd.cls(); 
     lcd.locate(0, 1); 
     lcd.printf("%s\n", time.c_str()); 

    } 

    //date 
    if (i == 9 and page_state == 0){ 
     string date = point; 
     string day = date.substr(0, 2); 
     string month = date.substr(2, 2); 
     string year = date.substr(4, 2); 

     //Converting the numerical month into abbreviation ect. 
     if (month == "03"){ 
      month = "Mar"; 
     } 

     if (month == "04"){ 
      month = "Apr"; 
     } 

     ostringstream dat; 
     dat << day << "-" << month << "-20" << year; 
     date = dat.str(); 

     lcd.locate(0, 9); 
     lcd.printf("%s\n", date.c_str()); 

    } 

    //latitude 
    if (i == 3 and page_state == 0){ 
     string lati = point; 
     string lati_deg = lati.substr(0, 2); 
     string sml_latideg = lati.substr(2, 6); 

     ostringstream lat; 
     lat << "Lat: " << lati_deg << " deg " << sml_latideg << "'"; 
     lati = lat.str(); 

     lcd.locate(0, 18); 
     lcd.printf("%s", lati.c_str()); 
    } 


    //latitude direction (N or S) 
    if (i == 4 and page_state == 0){ 
     string lat_dir = point; 
     lcd.printf("%s\n", lat_dir.c_str()); 
     } 

    point = strtok(NULL, ","); 
    } 


    //Change page 

    if (Button == 1){ 
     page_state = !page_state;//toggle page state  
     wait(0.2);//debounce timer 
     lcd.cls(); 
     } 


    //second page 
    for (int j = 0; point != NULL; j++){ 

     char str[100]  ="$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A"; 
     char*point; 
     point = strtok(str, ","); 
     //longitude 
     if (j == 5 and page_state == 1){ 
      string lngi = point; 
      string lngi_deg = lngi.substr(0, 2); 
      string sml_lngideg = lngi.substr(2, 6); 

     ostringstream lng; 
     lng << "Lng: " << lngi_deg << " deg " << sml_lngideg << "'"; 
     lngi = lng.str(); 

     lcd.locate(0, 1); 
     lcd.printf("%s", lngi.c_str()); 
     } 

     //longitude direction (E or W) 
     if (j == 6 and page_state == 1){ 
     string lng_dir = point; 

     lcd.printf("%s\n", lng_dir.c_str()); 
     } 

     //speed 
     if (j == 7 and page_state == 1){ 
     string speed = point; 

     ostringstream spd; 
     spd << "Speed: " << speed; 
     speed = spd.str(); 

     lcd.locate(0, 9); 
     lcd.printf("%s\n", speed.c_str()); 
     } 

     point = strtok(NULL, ","); 
     } 


    return 0; 

} 

merhaba, ben ekranı temizlemek ve yeni bilgi koymak için izin vermek için mBED uygulama gemide gemide düğmesine almaya çalışırken, düğme şu anda hiçbir şey yapmaz, ben ilk 4 alıyorum ekrana bilgi parçaları ancak bu düğmeye basıldığında değişmez, bu işi yapmaya çalışmak için yardıma ihtiyacım varmBED uygulama kurulu lcd ekran düğme kontrolü

+0

Öneri: Gömülü platformdan çıkıp kodunuzu önce bir PC'de simüle edin. Mantığınızın hata ayıklamanın kolay olduğu bir platformda ve bağlantı noktasında iyi olduğundan emin olun. Ve bir başlıkta 'namespace std;' kullanarak koymayın. Çok fazla acı getirir. – user4581301

+0

ive çoğunlukla görsel stüdyosunda geliştirdi, daha sonra konsol çıkışlarından gelen sintaksı lcd sintaksine değiştirdi, neden isim-alanı kullanmadığını bilmiyorum; Her zaman problemsiz yaptım – JoeB

+0

Şanslı oldun. Daha fazla bilgi için: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice – user4581301

cevap

0

Bu OP'nin sorusunu doğrudan yanıtlamaz, ancak bu uzun vadede daha yararlı olacaktır. Program mantığını sınırlı bir ortamda hata ayıklamaya çalışmak yerine, platforma özgü işlevselliği, kodu değiştirmek zorunda kalmadan genel amaçlı bilgi işlem donanımında platformun benzetimini sağlayan işlevler ve sınıflar ile değiştirmek genellikle yararlıdır.

#include <cstdarg> 
#include <iostream> 

eklenmesi ve sahte C12832_lcd.h

#pragma once 
#include <cstdarg> 
#include <iostream> 

// Sim LCD class. Just writes LCD commands to console 
class C12832_LCD 
{ 
public: 
    void cls() 
    { 
     std::cout << "LCD: Cleared" << std::endl; 
    } 
    void locate(int row, int col) 
    { 
     std::cout << "LCD: positioned " << row << "," << col << std::endl; 
    } 
    void printf(const char * fmt, ...) 
    { 
     char buffer[4096]; 
     va_list args; 

     va_start(args, fmt); 
     vsnprintf(buffer, sizeof(buffer), fmt, args); 
     std::cout << buffer << std::endl; 
     va_end(args); 
    } 
}; 

Ve sahte mbed.h tarafından

OP'ın koduna
#pragma once 

// Sim DigitalIn class. Toggles true and false on the button. First call will be true 
class DigitalIn 
{ 
private: 
    bool mVal; 
public: 
    DigitalIn(int): mVal(false) 
    { 

    } 
    bool operator==(int) 
    { 
     mVal = !mVal; 
     return mVal; 
    } 

}; 

//Sim serial Does nothing yet. 
class Serial 
{ 
public: 
    Serial(int, int) 
    { 

    } 
}; 

//sim wait. We don't need to wait in simulation, so does nothing. 
void wait(double) 
{ 

} 

const int p14 = 14; 
const int USBTX = 0; 
const int USBRX = 0; 

, şimdi derlemek ve çalışabilir Visual Studio IDE'deki masaüstünü açın ve hata ayıklayıcısını sorunun üst kısmına atın. Kod aracılığıyla adım atmak, iki mantık hatasının ilkini çabucak ortaya çıkarır. İkincisi biraz daha incelikli. Kapsamına dikkat et.

Hızlı bir öneri:

yerine strtok, consider using the std::getline overload that takes a character delimiter kullanarak. Bu, bir NMEA dizesi gibi girişlerin virgülle ayrılmış bir akışını okumak için

'a izin verir.