[chronojump/michrolab] Simplified functions for reading jump types



commit b6e552f9c044a845653edcf01073aaca2b8a3ed4
Author: Xavier Padullés <testing chronojump org>
Date:   Wed Jul 27 16:51:16 2022 +0200

    Simplified functions for reading jump types

 arduino/michrolab/exercsiseManage.ino | 143 ++++++++++++++++++++--------------
 arduino/michrolab/michrolab.ino       |  15 ++--
 2 files changed, 92 insertions(+), 66 deletions(-)
---
diff --git a/arduino/michrolab/exercsiseManage.ino b/arduino/michrolab/exercsiseManage.ino
index 3da140eb6..496344e78 100644
--- a/arduino/michrolab/exercsiseManage.ino
+++ b/arduino/michrolab/exercsiseManage.ino
@@ -1,50 +1,51 @@
-void readJumpsFile(){
-    /*
-     Example of jumpsTypes.txt
-     0,SJ,1
-     1,CMJ,1
-     2,ABK,1
-     3,DJna,0
-     4,SJl,1
-  */
-  String row = "";
-  char readChar;
-  File  jumpsFile = SD.open("/jumpType.txt");
-  if (jumpsFile)
-  {
-    currentJumpType = 0;
-    jumpsFile.seek(0);
-
-    // read from the file until there's nothing else in it:
-    while (currentJumpType < totalJumpTypes)
-    {
-      readChar = jumpsFile.read();
-      if (readChar != '\n' && readChar != '\r')
-      {
-        row = row + readChar;
-      } else if (readChar == '\n' || readChar == '\r')
-      {
-        addJump(row);
-        row = "";
-        currentJumpType++;
-      }
-    }
-    // close the file:
-    jumpsFile.close();
-  } else {
-    // if the file didn't open, print an error:
-    Serial.println("error opening jumpType.txt");
-  } 
-}
+//void readJumpsFile(){
+//    /*
+//     Example of jumpsType.txt
+//     0,SJ,1
+//     1,CMJ,1
+//     2,ABK,1
+//     3,DJna,0
+//     4,SJl,1
+//  */
+//  String row = "";
+//  char readChar;
+//  File  jumpsFile = SD.open("/jumpType.txt");
+//  if (jumpsFile)
+//  {
+//    currentJumpType = 0;
+//    jumpsFile.seek(0);
+//
+//    // read from the file until there's nothing else in it:
+//    while (currentJumpType < totalJumpTypes)
+//    {
+//      readChar = jumpsFile.read();
+//      if (readChar != '\n' && readChar != '\r')
+//      {
+//        row = row + readChar;
+//      } else if (readChar == '\n' || readChar == '\r')
+//      {
+//        //Serial.println(row);
+//        addJump(row);
+//        row = "";
+//        currentJumpType++;
+//      }
+//    }
+//    // close the file:
+//    jumpsFile.close();
+//    //printJumpTypesList();
+//  } else {
+//    // if the file didn't open, print an error:
+//    Serial.println("error opening jumpType.txt");
+//  }
+//}
 
 void addJump(String row)
 {
   int prevComaIndex = row.indexOf(":");
   int nextComaIndex = row.indexOf(",");
-  currentJumpType = row.substring(prevComaIndex + 1, nextComaIndex).toInt();
-  jumpTypes[currentJumpType].id = currentJumpType;
+  //currentJumpType = row.substring(prevComaIndex + 1, nextComaIndex).toInt();
+  jumpTypes[currentJumpType].id = row.substring(prevComaIndex + 1, nextComaIndex).toInt();
 
-  if (currentJumpType >= totalJumpTypes) totalJumpTypes = currentJumpType + 1;
   prevComaIndex = nextComaIndex;
   nextComaIndex = row.indexOf(",", prevComaIndex + 1 );
   jumpTypes[currentJumpType].name = row.substring(prevComaIndex + 1 , nextComaIndex);
@@ -54,34 +55,56 @@ void addJump(String row)
   jumpTypes[currentJumpType].startIn = (row.substring(prevComaIndex + 1 , nextComaIndex) == "1");
 }
 
-unsigned int getTotalJumpTypes()
+void readJumpsTypesFile()
 {
   char readChar;
   String readString = "";
+  unsigned long pos = 0;    //Position in the file
+  int numRows = 0;          //Number of valid rows in the file
+  
   File  jumpsFile = SD.open("jumpType.txt");
   if (jumpsFile)
   {
-    //Start reading from the last byte
-    unsigned long pos = jumpsFile.size() - 4;
-
-    //Reading the jump number of the last row
-    while (readChar != '\n' && readChar != '\r')
-    {
-      jumpsFile.seek(pos);
-      readChar = jumpsFile.peek();
-      pos--;
-    }
-    pos++;
-    jumpsFile.seek(pos);
-    readChar = jumpsFile.read();
-    while (readChar != ',')
+//    //Start reading from the last byte
+//    unsigned long pos = jumpsFile.size() - 4;
+//
+//    //Reading the jump number of the last row
+//    while (readChar != '\n' && readChar != '\r')
+//    {
+//      jumpsFile.seek(pos);
+//      readChar = jumpsFile.peek();
+//      pos--;
+//    }
+//    pos++;
+//    jumpsFile.seek(pos);
+//    readChar = jumpsFile.read();
+//    while (readChar != ',')
+//    {
+//      readChar = jumpsFile.read();
+//      readString = readString + readChar;
+//    }
+    while (pos <= jumpsFile.size())
     {
-      readChar = jumpsFile.read();
-      readString = readString + readChar;
+      readChar = NULL;
+      String readString = "";
+      while (readChar != '\n' && readChar != '\r' && pos<=jumpsFile.size())
+      {
+        readChar = jumpsFile.read();
+        readString = readString + readChar;
+        pos++;
+      }
+      //Check that it is a valid row
+      if ( isDigit(readString[0]) )
+      {
+        numRows++;
+        currentJumpType = numRows - 1;
+        addJump(readString);
+        totalJumpTypes = numRows;
+        Serial.print(readString);
+      }
     }
   }
-  totalJumpTypes = readString.toInt() + 1;
-  return (totalJumpTypes);
+ jumpsFile.close();
 }
 
 void printJumpTypesList()
diff --git a/arduino/michrolab/michrolab.ino b/arduino/michrolab/michrolab.ino
index d9830a96c..42768a818 100644
--- a/arduino/michrolab/michrolab.ino
+++ b/arduino/michrolab/michrolab.ino
@@ -387,9 +387,10 @@ void setup() {
   readPersonsFile();
 
   //TODO: Read jumps only if necessary
-  getTotalJumpTypes();
-  readJumpsFile();
+  readJumpsTypesFile();
+  Serial.println(totalJumpTypes);
   currentJumpType = 0;
+  printJumpTypesList();
   
   tft.fillScreen(BLACK);
   
@@ -1413,7 +1414,7 @@ void startJumpsCapture()
           Serial.println("r;");
         }
       }
-      saveJumps();
+      saveJump();
       lastRcaState = rcaState;
       lastRcaTime = rcaTime;
     }
@@ -1582,17 +1583,19 @@ void selectPerson()
   }
 }
 
-void saveJumps()
+void saveJump()
 {
   String fullFileName = "/" + dirName + "/" + fileName + ".txt";
   File dataFile = SD.open(fullFileName, FILE_WRITE);
+  String row = String(currentPerson) + ";" + jumpTypes[currentJumpType].id + ";" + String(rcaTime);
   if(rcaState)
   {
-    dataFile.println(String(currentPerson) + ";" + String(rcaTime) + "R");
+    row = row + "R";
   } else if(!rcaState)
   {
-    dataFile.println(String(currentPerson) + ";" + String(rcaTime) + "r");
+    row = row + "r";
   }
+  dataFile.println(row);
   dataFile.close();
 }
 void fakeFunction()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]