Alternative to String..


i trying read row of record sd card , display on lcd. using ( bad ) string. works , not in tight corner flash space whole code 36k on mega 2560.  wanting find out more elegant , optimum way have done seems pretty convoluted ! :

the first part of code snippet : declare string variables , parse data sd card :
code: [select]

// declare globals

string slno;
string priority;
string freq;
string route;
string routeno;
string rfid;
string equipid;
string equdesc;
string grtype;
string grqty;

...
...
...
// function retrieve specific record , load feilds global variables

void parsecharstring (int recposition ) {

  char letter;
  string readstring;

  int delim1, delim2, delim3, delim4, delim5, delim6, delim7, delim8, delim9, delim10, delim11;

  file datafile = sd.open("actual.csv", file_read);         // open file
  datafile.seek(recposition);                               // go required row
  if (datafile) {
    while ( datafile.available()) {
      letter = datafile.read();
      if (letter == '>') {
        datafile.close();

        delim1 = readstring.indexof('<');
        delim2 = readstring.indexof(',');
        slno = readstring.substring( delim1 + 1, delim2);
        delim3 = readstring.indexof(',', delim2 + 1);
        priority = readstring.substring(delim2 + 1, delim3);
        delim4 = readstring.indexof(',', delim3 + 1);
        freq = readstring.substring(delim3 + 1, delim4);
        delim5 = readstring.indexof(',', delim4 + 1);
        route = readstring.substring(delim4 + 1, delim5);
        delim6 = readstring.indexof(',', delim5 + 1);
        routeno = readstring.substring(delim5 + 1, delim6);
        delim7 = readstring.indexof(',', delim6 + 1);
        rfid = readstring.substring(delim6 + 1, delim7);
        delim8 = readstring.indexof(',', delim7 + 1);
        equipid = readstring.substring(delim7 + 1, delim8);
        delim9 = readstring.indexof(',', delim8 + 1);
        equdesc = readstring.substring(delim8 + 1, delim9);
        delim10 = readstring.indexof(',', delim9 + 1);
        grtype = readstring.substring(delim9 + 1, delim10);
        delim11 = readstring.indexof('>', delim10 + 1);
        grqty = readstring.substring(delim10 + 1, delim11);

        readstring = "";
        break;
      }
      else {
        readstring += letter;
      }
    }
  }
  else {
    lcd.clear();
    lcd.print(f("actual.csv"));
    lcd.setcursor(0, 1);
    lcd.print(f("  sd file error2 !  "));
    lcd.setcursor(0, 2);
    lcd.print(f(" reset , verify.."));
    while (1);                                                        // hold message...and wait reset.
  }
}

...
...
...



now code display parsed data on lcd

code: [select]

// function display details , await user response...

void awaitscandisp() {

  lcd.clear();

  char buf[21];
  int len = sizeof(buf);       // directly displaying string variable has issues..

  route.tochararray( buf, len);
  lcd.setcursor(0, 0);         // part of first line
  lcd.print(buf);

  equipid.tochararray( buf, len);
  lcd.setcursor(12, 0);        // remaining part of first line
  lcd.print(buf);

  equdesc.tochararray( buf, len);
  lcd.setcursor(0, 1);         // second line
  lcd.print(buf);

  grtype.tochararray( buf, len);
  lcd.setcursor(0, 2);         // third line
  lcd.print(buf);

  lcd.setcursor(0, 3);         // fourth line. ( third line left blank )
  lcd.print(f( "scan rfid! togo:"));

  getnoofrecords();
  lcd.setcursor(18, 3);
  lcd.print( numtogrease);

}

wanting find out more elegant , optimum way this
the first obvious thing improve lose string class , thing char arrays.


Arduino Forum > Using Arduino > Programming Questions > Alternative to String..


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts