[chronojump/michrolab] Person management allows to delete, get, save list and group change



commit 714862cd71310cc8f64239530c3c949fe90a7e24
Author: xpadulles <x padulles gmail com>
Date:   Tue Aug 30 19:13:12 2022 +0200

    Person management allows to delete, get, save list and group change

 .../SDExample/{gravType.txt => GRAVTYPE.TXT}       |     0
 .../michrolab/SDExample/{group0.txt => GROUP0.TXT} |     0
 .../michrolab/SDExample/{group1.txt => GROUP1.TXT} |     0
 .../michrolab/SDExample/{group2.txt => GROUP2.TXT} |     0
 .../michrolab/SDExample/{group3.txt => GROUP3.TXT} |     0
 .../michrolab/SDExample/{group4.txt => GROUP4.TXT} |     0
 .../michrolab/SDExample/{group5.txt => GROUP5.TXT} |     0
 .../michrolab/SDExample/{group6.txt => GROUP6.TXT} |     0
 .../michrolab/SDExample/{group7.txt => GROUP7.TXT} |     0
 .../michrolab/SDExample/{group8.txt => GROUP8.TXT} |     0
 .../michrolab/SDExample/{group9.txt => GROUP9.TXT} |     0
 .../SDExample/{JumpType.txt => JUMPTYPE.TXT}       |     0
 arduino/michrolab/SDExample/ML0002G9/-P.TXT        | 15843 -------------------
 arduino/michrolab/exercsiseManage.ino              |    35 +-
 arduino/michrolab/michrolab.h                      |     6 +-
 arduino/michrolab/michrolab.ino                    |    81 +-
 arduino/michrolab/personsManage.ino                |   107 +-
 17 files changed, 136 insertions(+), 15936 deletions(-)
---
diff --git a/arduino/michrolab/SDExample/gravType.txt b/arduino/michrolab/SDExample/GRAVTYPE.TXT
similarity index 100%
rename from arduino/michrolab/SDExample/gravType.txt
rename to arduino/michrolab/SDExample/GRAVTYPE.TXT
diff --git a/arduino/michrolab/SDExample/group0.txt b/arduino/michrolab/SDExample/GROUP0.TXT
similarity index 100%
rename from arduino/michrolab/SDExample/group0.txt
rename to arduino/michrolab/SDExample/GROUP0.TXT
diff --git a/arduino/michrolab/SDExample/group1.txt b/arduino/michrolab/SDExample/GROUP1.TXT
similarity index 100%
rename from arduino/michrolab/SDExample/group1.txt
rename to arduino/michrolab/SDExample/GROUP1.TXT
diff --git a/arduino/michrolab/SDExample/group2.txt b/arduino/michrolab/SDExample/GROUP2.TXT
similarity index 100%
rename from arduino/michrolab/SDExample/group2.txt
rename to arduino/michrolab/SDExample/GROUP2.TXT
diff --git a/arduino/michrolab/SDExample/group3.txt b/arduino/michrolab/SDExample/GROUP3.TXT
similarity index 100%
rename from arduino/michrolab/SDExample/group3.txt
rename to arduino/michrolab/SDExample/GROUP3.TXT
diff --git a/arduino/michrolab/SDExample/group4.txt b/arduino/michrolab/SDExample/GROUP4.TXT
similarity index 100%
rename from arduino/michrolab/SDExample/group4.txt
rename to arduino/michrolab/SDExample/GROUP4.TXT
diff --git a/arduino/michrolab/SDExample/group5.txt b/arduino/michrolab/SDExample/GROUP5.TXT
similarity index 100%
rename from arduino/michrolab/SDExample/group5.txt
rename to arduino/michrolab/SDExample/GROUP5.TXT
diff --git a/arduino/michrolab/SDExample/group6.txt b/arduino/michrolab/SDExample/GROUP6.TXT
similarity index 100%
rename from arduino/michrolab/SDExample/group6.txt
rename to arduino/michrolab/SDExample/GROUP6.TXT
diff --git a/arduino/michrolab/SDExample/group7.txt b/arduino/michrolab/SDExample/GROUP7.TXT
similarity index 100%
rename from arduino/michrolab/SDExample/group7.txt
rename to arduino/michrolab/SDExample/GROUP7.TXT
diff --git a/arduino/michrolab/SDExample/group8.txt b/arduino/michrolab/SDExample/GROUP8.TXT
similarity index 100%
rename from arduino/michrolab/SDExample/group8.txt
rename to arduino/michrolab/SDExample/GROUP8.TXT
diff --git a/arduino/michrolab/SDExample/group9.txt b/arduino/michrolab/SDExample/GROUP9.TXT
similarity index 100%
rename from arduino/michrolab/SDExample/group9.txt
rename to arduino/michrolab/SDExample/GROUP9.TXT
diff --git a/arduino/michrolab/SDExample/JumpType.txt b/arduino/michrolab/SDExample/JUMPTYPE.TXT
similarity index 100%
rename from arduino/michrolab/SDExample/JumpType.txt
rename to arduino/michrolab/SDExample/JUMPTYPE.TXT
diff --git a/arduino/michrolab/exercsiseManage.ino b/arduino/michrolab/exercsiseManage.ino
index 8112df109..3573ab7a2 100644
--- a/arduino/michrolab/exercsiseManage.ino
+++ b/arduino/michrolab/exercsiseManage.ino
@@ -1,5 +1,6 @@
 void addJump(String row)
 {
+  currentExerciseType = totalJumpTypes;
   int prevComaIndex = row.indexOf(":");
   int nextComaIndex = row.indexOf(",");
   //currentExerciseType = row.substring(prevComaIndex + 1, nextComaIndex).toInt();
@@ -37,18 +38,20 @@ void addJump(String row)
 
   prevComaIndex = nextComaIndex;
   jumpTypes[currentExerciseType].startIn = (row.substring(prevComaIndex + 1, prevComaIndex + 2) == "1");
+  totalJumpTypes++;
+  Serial.println("totalJumpTypes: " + String(totalJumpTypes));
 }
 
 void saveJumpsList()
 {
-  SD.remove("jumpType.txt");
-  if( !SD.exists("jumpType.txt") ) Serial.println("File doesn't exists");
-  else Serial.println("File exists");
+  SD.remove("JUMPTYPE.TXT");
  
-  File jumpFile = SD.open("jumpType.txt", FILE_WRITE);
+  File jumpFile = SD.open("JUMPTYPE.TXT", FILE_WRITE);
 
+//  if(jumpFile) Serial.println("File created");
+//  else Serial.println("Error creating file");
 
-  for (int i = 0; i < totalJumps; i++)
+  for (unsigned int i = 0; i < totalJumpTypes; i++)
   {
     jumpFile.print(jumpTypes[i].id);
     jumpFile.print("," + String(jumpTypes[i].name));
@@ -61,8 +64,22 @@ void saveJumpsList()
   }
   jumpFile.flush();
   jumpFile.close();
-  
-  Serial.println("Jump types saved in jumpType.txt");
+}
+
+void deleteJumpTypes()
+{
+  totalJumpTypes = 0;
+//  for (int i = 0; i < totalJumpTypes; i++)
+//  {
+//    jumpTypes[i].id = 0;
+//    jumpTypes[i].name = "";
+//    jumpTypes[i].jumpLimit = 0;
+//    jumpTypes[i].timeLimit = 0;
+//    jumpTypes[i].hardTimeLimit = false;
+//    jumpTypes[i].percentBodyWeight = 0.0;
+//    jumpTypes[i].fall = 0.0;
+//    jumpTypes[i].startIn = true;
+//  }
 }
 
 void addGravitatory(String row)
@@ -102,11 +119,11 @@ void readExercisesFile(exerciseType mode)
 
   if (mode == jumps) {
     //Serial.println("J");
-    file = "jumpType.txt";
+    file = "JUMPTYPE.TXT";
   }
   else if (mode == gravitatory) {
     //Serial.println("G");
-    file = "gravType.txt";
+    file = "GRAVTYPE.TXT";
   }
 
   File  exercisesFile = SD.open(file);
diff --git a/arduino/michrolab/michrolab.h b/arduino/michrolab/michrolab.h
index 19c2a363b..3f4e12fdb 100644
--- a/arduino/michrolab/michrolab.h
+++ b/arduino/michrolab/michrolab.h
@@ -142,13 +142,13 @@ void redrawAxes(ILI9341_t3 & d, double gx, double gy, double w, double h, double
 //Plot a set of bars stored in bars[]
 void barPlot (float gx, float gy, float w, float h, float yhi, int numBars, int currentIndex, float abRatio, 
unsigned int color);
 
-//Read the jumType.txt file and assign each row to a jumpTypes[] element
+//Read the JUMPTYPE.TXT file and assign each row to a jumpTypes[] element
 void readJumpsFile();
 
-//Assign a jumpType to a jumpTypes[] element. The input String is of the same format as in the jumpType.txt
+//Assign a jumpType to a jumpTypes[] element. The input String is of the same format as in the JUMPTYPE.TXT
 void addJump(String row);
 
-//Read how many rows has the jumpTypes.txt
+//Read how many rows has the JUMPTYPE.TXT
 void gettotalJumpTypes();
 
 //Print in the Serial a list of all jump types
diff --git a/arduino/michrolab/michrolab.ino b/arduino/michrolab/michrolab.ino
index 2d2f81f29..4ea83efeb 100644
--- a/arduino/michrolab/michrolab.ino
+++ b/arduino/michrolab/michrolab.ino
@@ -304,7 +304,7 @@ unsigned int setNumber = 0;
 unsigned int dirNumber;
 String dirName = "";
 String fileName = "";
-unsigned short group = 0;
+int group = 0;
 
 struct personType {
   unsigned int index;
@@ -402,8 +402,6 @@ void setup() {
   }
 
   EEPROM.get(groupAddress, group);
-  Serial.print("Group: ");
-  Serial.println(group);
   if (group == 65535) {
     group = 0;
     EEPROM.put(groupAddress, 0);
@@ -420,22 +418,13 @@ void setup() {
 #endif
 
   dirName = createNewDir();
-  totalPersons = getTotalPerson();
+  //totalPersons = getTotalPerson();
   readPersonsFile();
 
   //TODO: Read exercises only if necessary
   currentExerciseType = 0;
 
   tft.fillScreen(BLACK);
-
-  readExercisesFile(jumps);
-  printJumpTypesList();
-  addJump("14,Prova,,0,0,0,0,0,1");
-  printJumpTypesList();
-  saveJumpsList();
-  readExercisesFile(jumps);
-  printJumpTypesList();
-  
   drawMenuBackground();
   backMenu();
   showMenuEntry(currentMenuIndex);
@@ -521,7 +510,9 @@ void getLoadCellDynamics(void)
 }
 
 
-void printTftValue (float val, int x, int y, int fontSize, int decimal) {printTftValue (val, x, y, fontSize, 
decimal, WHITE);}
+void printTftValue (float val, int x, int y, int fontSize, int decimal) {
+  printTftValue (val, x, y, fontSize, decimal, WHITE);
+}
 void printTftValue (float val, int x, int y, int fontSize, int decimal, int color) {
 
   /*How many characters are to the left of the units number.
@@ -580,7 +571,7 @@ void printTftText(String text, int x, int y, unsigned int color, int fontSize, b
 void serialEvent() {
   String inputString = Serial.readString();
   String commandString = inputString.substring(0, inputString.lastIndexOf(":"));
-  String parameters = inputString.substring(commandString.indexOf(":") + 1);
+  String parameters = inputString.substring(inputString.lastIndexOf(":") + 1);
 
   if (commandString == "start_capture") {
     PcControlled = true;
@@ -612,11 +603,17 @@ void serialEvent() {
     */
   } else if (commandString == "addPerson") {
     addPerson(parameters);
+  } else if (commandString == "readPersonsFile") {
+    readPersonsFile();
   } else if (commandString == "getPersons") {
     printPersonsList();
   } else if (commandString == "savePersons") {
-    Serial.println("Going to savePersons...");
     savePersonsList();
+    Serial.println("Saved in GROUP" + String(group) + ".TXT");
+  } else if (commandString == "setGroup") {
+    setGroup(parameters);
+  } else if (commandString == "getGroup") {
+    Serial.println(group);
   } else if (commandString == "addJumpType") {
     addJump(parameters);
     Serial.println("Jump added");
@@ -624,8 +621,10 @@ void serialEvent() {
     printJumpTypesList();
   } else if (commandString == "saveJumpTypes") {
     saveJumpsList();
-//  } else if (commandString == "saveGravit") {
-//    saveGravitatory();
+  } else if (commandString == "deleteJumpTypes") {
+    deleteJumpTypes();
+    //  } else if (commandString == "saveGravit") {
+    //    saveGravitatory();
   } else {
     Serial.println("Not a valid command");
   }
@@ -1139,7 +1138,9 @@ void captureRaw()
   if (!capturingPreSteadiness) setNumber++;
 }
 
-void captureBars() {captureBars(false);}
+void captureBars() {
+  captureBars(false);
+}
 void captureBars(float fullScreen)
 {
   maxString = "V";
@@ -1151,18 +1152,18 @@ void captureBars(float fullScreen)
   else if (sensor == incRotEncoder) fileName = fileName + "-I";
   else if (sensor == loadCellincEncoder) fileName = fileName + "-P";
 
-  fullFileName = "/" + dirName + "/" + fileName + ".txt";
+  fullFileName = "/" + dirName + "/" + fileName + ".TXT";
   dataFile = SD.open(fullFileName.c_str(), FILE_WRITE);
   dataFile.println("Person:" + String(persons[currentPerson].index) + "," + persons[currentPerson].name + " 
" + persons[currentPerson].surname);
   dataFile.println("Exercise:" + String(gravTypes[currentExerciseType].id) + "," + 
gravTypes[currentExerciseType].name);
   dataFile.println("Load:" + String(load));
 
   tft.fillScreen(BLACK);
-  
+
   float h = 0;
   if (fullScreen) h = 240;
   else h = 200;
-  
+
   if (!fullScreen)
   {
     //Info at the lower part of the screen
@@ -1182,7 +1183,7 @@ void captureBars(float fullScreen)
     {
       currentSlot = (numRepetitions - 1) % 10;
       redrawBars = false;
-      if(bars[currentSlot] > maxAvgVelocity)
+      if (bars[currentSlot] > maxAvgVelocity)
       {
         if (!fullScreen) printTftValue(maxAvgVelocity, 94, 215, 2, 1, BLACK);
         maxAvgVelocity = bars[currentSlot];
@@ -1251,7 +1252,7 @@ void getEncoderDynamics()
     //TODO: Calculate positoion depending on the parameters of the encoder/machine
     if (inertialMode) position = - abs(position);
     measured = (float)(position - lastEncoderPosition) * 1000 / (duration);
-    if(measured > measuredMax) measuredMax = measured;
+    if (measured > measuredMax) measuredMax = measured;
     //measured = position;
     //    if(position != lastEncoderPosition) Serial.println(String(localMax) + "\t" + 
String(lastEncoderPosition) +
     //      "\t" + String(position) + "\t" + String(encoderPhase * (position - localMax)));
@@ -1293,7 +1294,7 @@ void getEncoderDynamics()
       avgVelocity = (float)(position - startPhasePosition) * 1000 / (lastMeasuredTime - startPhaseTime);
       bars[numRepetitions % 10] = abs(avgVelocity);
       redrawBars = true;
-      
+
       //printBarsValues();
 
       numRepetitions++;
@@ -1474,7 +1475,7 @@ void jumpsCapture()
   bool rowCreated = false;
 
   fileName = String("J") + "-S" + String(setNumber);
-  fullFileName = "/" + dirName + "/" + fileName + ".txt";
+  fullFileName = "/" + dirName + "/" + fileName + ".TXT";
   dataFile = SD.open(fullFileName.c_str(), FILE_WRITE);
 
   lastRcaState = !digitalRead(rcaPin);
@@ -1730,7 +1731,7 @@ void saveSD(String fileName)
     Serial.println("no sensor type");
     return;
   }
-  fullFileName = "/" + dirName + "/" + fileName + sensorString + ".txt";
+  fullFileName = "/" + dirName + "/" + fileName + sensorString + ".TXT";
   dataFile = SD.open(fullFileName.c_str(), FILE_WRITE);
   dataFile.println(String(lastSampleTime) + ";" + String(measured));
   dataFile.close();
@@ -1843,7 +1844,7 @@ void selectPerson()
 
 void saveSimpleJump(float lastPhaseTime)
 {
-  fullFileName = "/" + dirName + "/" + fileName + ".txt";
+  fullFileName = "/" + dirName + "/" + fileName + ".TXT";
   dataFile = SD.open(fullFileName.c_str(), FILE_WRITE);
   if ( !rcaState)
   {
@@ -1880,3 +1881,27 @@ void printBarsValues(int currentIndex)
   }
   Serial.println();
 }
+
+void printDirectory(File dir, int numTabs)
+{
+  while (true) {
+    File entry =  dir.openNextFile();
+    if (! entry) {
+      // no more files
+      break;
+    }
+    for (uint8_t i = 0; i < numTabs; i++) {
+      Serial.print('\t');
+    }
+    Serial.print(entry.name());
+    if (entry.isDirectory()) {
+      Serial.println("/");
+      printDirectory(entry, numTabs + 1);
+    } else {
+      // files have sizes, directories do not
+      Serial.print("\t\t");
+      Serial.println(entry.size(), DEC);
+    }
+    entry.close();
+  }
+}
diff --git a/arduino/michrolab/personsManage.ino b/arduino/michrolab/personsManage.ino
index 185b6306b..135c14fdd 100644
--- a/arduino/michrolab/personsManage.ino
+++ b/arduino/michrolab/personsManage.ino
@@ -1,7 +1,12 @@
 void savePersonsList()
 {
-  SD.remove("persons.txt");
-  File personsFile = SD.open("persons.txt");
+  SD.remove("GROUP" + String(group) + ".TXT");
+  File personsFile = SD.open("GROUP" + String(group) + ".TXT", FILE_WRITE);
+
+  if(personsFile) Serial.println("File created");
+  else Serial.println("Error creating file");
+
+  Serial.println("totalPersons:" + String(totalPersons));
   for (unsigned int i = 0; i < totalPersons; i++)
   {
     personsFile.print(persons[i].index);
@@ -10,11 +15,15 @@ void savePersonsList()
     personsFile.print(",");
     personsFile.println(persons[i].heigh);
   }
+  personsFile.flush();
   personsFile.close();
+  File root = SD.open("/");
+  printDirectory(root, 4);
 }
 
 void printPersonsList()
 {
+  Serial.println("Current group:" + String(group));
   for (unsigned int i = 0; i < totalPersons; i++)
   {
     Serial.print(persons[i].index);
@@ -25,102 +34,86 @@ void printPersonsList()
   }
 }
 
+void deletePersonsList()
+{
+  totalPersons = 0;
+}
+
+//TODO: Check the the id is unique
 void addPerson(String row)
 {
   int prevComaIndex = row.indexOf(":");
   int nextComaIndex = row.indexOf(",");
-  currentPerson = row.substring(prevComaIndex + 1, nextComaIndex).toInt();
-  persons[currentPerson].index = currentPerson;
+  
+  persons[totalPersons].index = row.substring(prevComaIndex + 1, nextComaIndex).toInt();
 
-  if (currentPerson >= totalPersons) totalPersons = currentPerson + 1;
   prevComaIndex = nextComaIndex;
   nextComaIndex = row.indexOf(",", prevComaIndex + 1 );
-  persons[currentPerson].name = row.substring(prevComaIndex + 1 , nextComaIndex);
+  persons[totalPersons].name = row.substring(prevComaIndex + 1 , nextComaIndex);
   prevComaIndex = nextComaIndex;
   nextComaIndex = row.indexOf(",", prevComaIndex + 1 );
-  persons[currentPerson].surname = row.substring(prevComaIndex + 1 , nextComaIndex);
+  persons[totalPersons].surname = row.substring(prevComaIndex + 1 , nextComaIndex);
 
   prevComaIndex = nextComaIndex;
   nextComaIndex = row.indexOf(",", prevComaIndex + 1 );
-  persons[currentPerson].weight = row.substring(prevComaIndex + 1 , nextComaIndex).toFloat();
+  persons[totalPersons].heigh = row.substring(prevComaIndex + 1 , nextComaIndex).toFloat();
 
   prevComaIndex = nextComaIndex;
   nextComaIndex = row.indexOf(",", prevComaIndex + 1 );
-  persons[currentPerson].heigh = row.substring(prevComaIndex + 1 , nextComaIndex).toFloat();
+  persons[totalPersons].weight = row.substring(prevComaIndex + 1 , nextComaIndex).toFloat();
+
+  totalPersons++;
 }
 
 //void readPersonsFile(struct personType * persons)
 void readPersonsFile()
 {
   /*
-     Ecample of persons.txt format
+    Example of persons file format
     0,Blancaneus,160, 65,
     1,Pulgarcito,16, 6,
     3,Tres porquets,50, 20,
   */
   String row = "";
   char readChar;
-  String filename = "group" + String(group) + ".txt";
-  File  personsFile = SD.open(filename.c_str());
+  String rowString = "";
+  unsigned long pos = 0;    //Position in the file
+  String fileName = "GROUP" + String(group) + ".TXT";
+  File  personsFile = SD.open(fileName.c_str());
   if (personsFile)
   {
     currentPerson = 0;
     personsFile.seek(0);
+    totalPersons = 0;
 
     // read from the file until there's nothing else in it:
-    while (currentPerson < totalPersons)
+    while ( pos <= personsFile.size() )
     {
-      readChar = personsFile.read();
-      if (readChar != '\n' && readChar != '\r')
+      readChar = '0';
+      rowString = "";
+
+      //Reading the new row
+      while (readChar != '\n' && readChar != '\r' && pos <= personsFile.size())
       {
-        row = row + readChar;
-      } else if (readChar == '\n' || readChar == '\r')
+        readChar = personsFile.read();
+        rowString = rowString + readChar;
+        pos++;
+      }
+
+      if ( isDigit(rowString[0]) )
       {
-        addPerson(row);
-        row = "";
-        currentPerson++;
+        addPerson(rowString);
       }
     }
     // close the file:
     personsFile.close();
   } else {
     // if the file didn't open, print an error:
-    Serial.println("error opening " + filename);
+    Serial.println("error opening " + fileName);
   }
   currentPerson = 0;
 }
 
-unsigned int getTotalPerson()
-{
-  char readChar;
-  String readString = "";
-  String filename = "group" + String(group) + ".txt";
-  File  personsFile = SD.open(filename.c_str());
-  if (personsFile)
-  {
-    //Start reading from the last byte
-    unsigned long pos = personsFile.size() - 4;
-
-    //Reading the person number of the last row
-    while (readChar != '\n' && readChar != '\r')
-    {
-      personsFile.seek(pos);
-      readChar = personsFile.peek();
-      pos--;
-    }
-    pos++;
-    personsFile.seek(pos);
-    readChar = personsFile.read();
-    while (readChar != ',')
-    {
-      readChar = personsFile.read();
-      readString = readString + readChar;
-    }
-  }
-  totalPersons = readString.toInt() + 1;
-  return (totalPersons);
-}
-
 void updatePersonSet()
 {
   String personSet = "Set: " + addLeadingZeros(setNumber, 2) + "   Person: " + 
addLeadingZeros(currentPerson, 2);
@@ -152,11 +145,12 @@ void updatePersonJump(int totalJumps)
   printTftText(persons[currentPerson].name + " " + persons[currentPerson].surname, 141, 223, WHITE, 1);
   tft.setTextSize(2);
 }
+
 void selectGroup()
 {
   group = selectValueDialog("Select the group number", "0,9", "1", 0);
   EEPROM.put(groupAddress, group);
-  totalPersons = getTotalPerson();
+  //totalPersons = getTotalPerson();
   readPersonsFile();
   dirNumber -= 1; //It makes not to increase the session number
   dirName = createNewDir();
@@ -216,3 +210,10 @@ void showPersonList(unsigned int color)
   printTftText((persons[currentPerson].name + " " + persons[currentPerson].surname).substring(0, 17),
                xPos, midYPos, color, 3);
 }
+
+void setGroup(String parameters)
+{
+  group = parameters.substring(0, parameters.lastIndexOf(";")).toInt();
+  EEPROM.put(groupAddress, group);
+  Serial.println("Group:" + String(group));
+}


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