SdFat(Arduino库)重复读取功能

时间:2018-12-06 17:43:22

标签: arduino sd-card

目标:

当前,我有一个项目,该项目使用greiman's SdFat Library一次将N行2列.csv文件读入Arduino的内存中。

我想扩展我的功能以允许repeatend repeat行(此处为R,nR,/,但任何其他字符,最好在美国键盘上表示,则可以接受),例如:一个包含以下内容的.csv文件:

1,2
R,3
4,5
6,7
R,/
8,9

反复呼叫将返回。

1,2
4,5
6,7
4,5
6,7
4,5
6,7
8,9

期望

  • 对于我当前的应用程序,我不需要嵌套的功能 循环,但如果可以不花费过多精力就可以完成,那么将不胜感激。

  • 如果repeatend repeat rows的数量不匹配,则可以接受除无限循环之外的任何合理动作。

  • 如果代码的skip Specified lines部分使解决方案变得困难,则可以将其省略(我现在可以跳过文件中的头信息,但是可以在另一个函数中将其分开)< / p>

当前代码:

对于我的职能,我具有以下全局变量:

bool SdDebug标志将调试信息打印到屏幕上

SdFile ActiveFile对于使用其他功能打开的文件是全局的

就目前而言,我当前的功能如下:

bool SDClass::RunGet(int skip, double &ColA, unsigned long &ColB)
{ 
  //Setup Local vars  
  if(SdDebug){Serial.print("RunGet s=");Serial.println(skip);}
  bool goodread=false;
  char filechar;
  int16_t readbytes;
  char Linebuffer[50];

  //skip Specified lines
  for (int i=0; i < skip; i++)
  {
    ActiveFile.fgets(Linebuffer,sizeof(Linebuffer));
    if(SdDebug){Serial.print("skip line: ");Serial.println(Linebuffer);}
  }

  //Read Desired Line
  String data="";
  readbytes=ActiveFile.fgets(Linebuffer,sizeof(Linebuffer));
  data=Linebuffer;
  if(readbytes>0)
  {
    int commaIndex = data.indexOf(",");  
    String cola = data.substring(0,commaIndex);
    String colb = data.substring(commaIndex+1);
    ColA = cola.toDouble();
    ColB = 1000*atol(colb.c_str());
    goodread=true;            
  }
  return goodread;
  }

期望的解决方案:

我认为您可以使用库的available()从文件末尾获取位置,并使用seekCur(int32_t offset)在文件内移动位置。因此,我希望解决方案看起来像这样。

//Read Desired Line
uint32_t From_back = ActiveFile.available();
  String data="";
  readbytes=ActiveFile.fgets(Linebuffer,sizeof(Linebuffer));
  data=Linebuffer;
if(readbytes>0)
{
  int commaIndex = data.indexOf(",");  
  cola = data.substring(0,commaIndex);
  colb = data.substring(commaIndex+1);
  if (cola = 'R')
  {
    manage_ForLoop(From_back, colb);
    readbytes=ActiveFile.fgets(Linebuffer,sizeof(Linebuffer));
    commaIndex = data.
  }
  cola = data.substring(0,commaIndex);
  colb = data.substring(commaIndex+1);
  ColA = cola.toDouble();
  Colb = 1000*atol(colb.c_str());
  goodread =true;
}

哪里

void manage_ForLoop(uint32_t Fb, String colb)
{
  static uint32_t FromPosition;
  static int Rptno;
  if (colb = `/`)
  {
    Rptno--;
    if(Rptno > 0)
    {
      ActiveFile.seekEnd(FromPosition);
    }
  }
  else
  {
    FromPosition = Fb;
    Rptno=colb.toInt();
  }
}

但是,我想知道是否有其他方法可以解决此问题,以允许嵌套循环或更灵活。

0 个答案:

没有答案