-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;

}

hello, trying to get the onboard button on an mbed application board to allow me to clear the screen and put new info, the button currently does nothing, i am getting the first 4 parts of info on the screen however this does not change when the button is pressed, i need help to try to make this work

JoeB
  • 1
  • 4
  • Recommendation: Get off the embedded platform and simulate your code on a PC first. Make sure your logic is good on a platform where the debugging is easy, then port. And don't put `using namespace std;` in a header. It brings too much pain. – user4581301 Apr 08 '16 at 18:40
  • ive developed it mostly on visual studio then changed the syntasx on the outputs from console outputs to the lcd syntax, i dont know why not use namespace std; i always have done with no problems – JoeB Apr 08 '16 at 19:20
  • You have been fortunate. Read more here: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice – user4581301 Apr 08 '16 at 19:32

1 Answers1

0

This does not directly answer OP's question, but this should be more helpful in the long run. Rather than trying to debug the program logic in a limited environment it is often helpful to replace the platform-specific functionality with functions and classes that allow simulation of of the platform on general purpose computing hardware without having to change the code.

By adding

#include <cstdarg>
#include <iostream>

and a fake 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);
    }
};

And a bogus mbed.h

#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;

to OP's code, I can now compile and run on the desktop in the Visual Studio IDE and throw the awesome might of the debugger at the problem. Stepping through the code quickly reveals the first of two logic errors. The second one is a bit more subtle. Watch your scope.

A quick recommendation:

Rather than using strtok, consider using the std::getline overload that takes a character delimiter. This allows

std::stringstream stream(str);
std::string token;
while (std::getline(stream, token, ','))
{
    // do stuff
}

to read through a comma separated stream of input like a NMEA string.

user4581301
  • 33,082
  • 7
  • 33
  • 54