XRAD'S How to Parse string to int line by line from SD ?

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
XRAD
 
Posts: 753
Joined: Sat Nov 19, 2016 3:28 pm

XRAD'S How to Parse string to int line by line from SD ?

Post by XRAD »

I am working on a GPS mapping program for display on Adafruit_HX8357. It is pretty straight forward except I'm stuck on one issue. I can get my decimal degrees converted to 'cartesian' style coords in x/y integers. I can draw pixels from these x/y coords to a tft as well as save them to an onboard SD. However, I plan to be able to scale the tft pixel map say from one pixel = 1m to 1 pixel = 1000m. This is easy as WHEN I am using reatime data from the GPS. But what I have to do so that the current map is not lost during redrawing to new scale, is to read the SD data, and redraw all the pixels at the new scale.

I can read the SD data(or buffer data) to serial output and it looks like this:
243,123
244,123
243,122
etc....

I can convert this to a string in a buffer (saved up to 400000 bytes in buffer no problems) and it looks the same on serial. What I need to do read the SD or buffer one line at a time, parse the data with a 'comma' delimiter, and then create new SDx and SDy from the string using atoi or whatever is needed. The flow seems fairly straight forward......However, I can't figure out how to do this even after reading many C++ sites regarding same. On the SD or in the buffer, x is ALWAYS the first set of caracters and will always be 3 bytes, y is the second set of characters and can be 1 to 3 bytes.

Can anyone point me in right direction?

sample bit of code where I suspect I need to create the function to handle the line by line parsing and int conversion. It's a hodgepodge right now because I'm learning about this conversion...

Code: Select all

      
// read from the file until there's nothing else in it:
      while (myFile.available()) {
        //Serial.write(myFile.read());//shows up fine

        buffer = myFile.readStringUntil('\n');//shows up fine
        //buffer = myFile.readStringUntil(',');//shows up split into line by line x then y, and no comma and one space where the comma was

          //this is where I need to parse and convert to
          //int and recreate the x/y positions from SD or buffer
          //do some action here with recreated x/y for EACH line parsed


        /*
          char myString[sizeof(myFile)];
          int myInt = atoi(myString);
          Serial.println(myInt);
 
        Serial.println(buffer); //debugging



        //int SDx;
        //SDx = atoi(myFile.readStringUntil(','));
        //SDx = buffer;
        //Serial.print("SDx: "); //debugging
        //Serial.println(SDx); //debugging
        //Serial.println(buffer); //debugging

  
          int SDx;
          char buffer[sizeof(buffer)];
          SDx = atoi(buffer);
          Serial.print(SDx); //debugging
        
           buffer = myFile.readStringUntil('\n');
           int SDy;
           SDy = atoi (buffer);
           Serial.println(SDy); //debugging
           // Serial.println(buffer); //debugging
         
          char str[] = "- This, a sample string.";
          char * pch;
          //printf ("Splitting string \"%s\" into tokens:\n", str);
          pch = strtok (str, " , ");
          while (pch != NULL)
          {
          //printf ("%s\n", pch);
          pch = strtok (NULL, " , ");
          }
     
         
          //zoom in or out with new SDx/SDy here
          // if (((SDdx > MAP_X_MIN) && (SDdx < MAP_X_MAX)) && ((SDdy > MAP_Y_MIN) && (SDdy < MAP_Y_MAX))) {
          // tft.drawPixel( SDdx, SDdy, ILI9341_WHITE);
        */
      }

User avatar
XRAD
 
Posts: 753
Joined: Sat Nov 19, 2016 3:28 pm

Re: XRAD'S How to Parse string to int line by line from SD ?

Post by XRAD »

Solved! Heto from stack overflow helped me out!

https://stackoverflow.com/questions/707 ... r-to-int-c

Code: Select all


 
// re-open the file for reading:
myFile = SD.open("GPS");
if (myFile) {
  //Serial.println("GPS:");

  drawMap();

  // read from the file until there's nothing else in it:
  while (myFile.available()) {
    //Serial.write(myFile.read());

    buffer = myFile.readStringUntil('\n');

    char *x_str;
    x_str = strtok((char*)buffer.c_str(), ",");
    int SDdx = atoi(x_str);

    char *y_str;
    y_str = strtok(NULL, ",");
    int SDdy = atoi(y_str);

 }

User avatar
Disciple
 
Posts: 852
Joined: Tue Jan 06, 2015 8:13 pm

Re: XRAD'S How to Parse string to int line by line from SD ?

Post by Disciple »

Excellent work. Thanks for sharing such a useful solution. Hope to see pics of your project when ready.

Hallelujah!
Disciple

User avatar
XRAD
 
Posts: 753
Joined: Sat Nov 19, 2016 3:28 pm

Re: XRAD'S How to Parse string to int line by line from SD ?

Post by XRAD »

Hi Disciple! Its another really cool build...a GPS mapping code with real-time scaling. I will do a write up when done. And i was also working on my lidar version 4 with a high speed RPLIDAR A2M8 scanner...so saving to SD and recalling the x/y plot(or distance/ angle) as shown here can apply to that code/device as well....

User avatar
XRAD
 
Posts: 753
Joined: Sat Nov 19, 2016 3:28 pm

Re: XRAD'S How to Parse string to int line by line from SD ?

Post by XRAD »


Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”