[chronojump/michrolab] Separating and commenting functions in smaller files



commit 5d6c3f67a28749f1cb4096e9b336e33e3c5846f0
Author: Xavier Padullés <testing chronojump org>
Date:   Wed Jun 8 21:18:04 2022 +0200

    Separating and commenting functions in smaller files

 arduino/michrolab/graphs.ino        | 158 ++++++++++++++++++++++++++++++++++++
 arduino/michrolab/michrolab.h       | 111 +++++++++++++++++++++++++
 arduino/michrolab/michrolab.ino     | 142 --------------------------------
 arduino/michrolab/personsManage.ino | 141 ++++++++++++++++++++++++++++++++
 4 files changed, 410 insertions(+), 142 deletions(-)
---
diff --git a/arduino/michrolab/graphs.ino b/arduino/michrolab/graphs.ino
new file mode 100644
index 000000000..d97a881ec
--- /dev/null
+++ b/arduino/michrolab/graphs.ino
@@ -0,0 +1,158 @@
+/*
+  function to draw a cartesian coordinate system and plot whatever data you want
+  just pass x and y and the graph will be drawn
+  huge arguement list
+  &d name of your display object
+  x = x data point
+  y = y datapont
+  gx = x graph location (lower left)
+  gy = y graph location (lower left)
+  w = width of graph
+  h = height of graph
+  xlo = lower bound of x axis
+  xhi = upper bound of x asis
+  xinc = division of x axis (distance not count)
+  ylo = lower bound of y axis
+  yhi = upper bound of y asis
+  yinc = division of y axis (distance not count)
+  title = title of graph
+  xlabel = x axis label
+  ylabel = y axis label
+  gcolor = graph line colors
+  acolor = axis line colors
+  pcolor = color of your plotted data
+  tcolor = text color
+  bcolor = background color
+  &redraw = flag to redraw graph on fist call only
+*/
+
+void Graph(ILI9341_t3 & d, double x, double y, double gx, double gy, double w, double h, double xlo, double 
xhi, double xinc, double ylo, double yhi, double yinc, String title, String xlabel, String ylabel, unsigned 
int gcolor, unsigned int acolor, unsigned int pcolor, unsigned int tcolor, unsigned int bcolor, boolean & 
startOver)
+{
+  //double ydiv, xdiv;
+  // initialize old x and old y in order to draw the first point of the graph
+  // but save the transformed value
+  // note my transform funcition is the same as the map function, except the map uses long and we need 
doubles
+  //static double ox = (x - xlo) * ( w) / (xhi - xlo) + gx;
+  //static double oy = (y - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
+  //double temp;
+  //int rot, newrot;
+
+  //Mapping values to coordinates
+  x =  (x - xlo) * ( w) / (xhi - xlo) + gx;
+  y =  (y - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
+
+  if (startOver == true)
+  {
+
+    startOver = false;
+    //In startOver, a point is plotted at the most left point
+    ox = x;
+    oy = y;
+  }
+
+  //graph drawn now plot the data
+  // the entire plotting code are these few lines...
+  // recall that ox and oy are initialized as static above
+  //Drawing 3 lines slows the drawing and erasing
+  d.drawLine(ox, oy, x, y, pcolor);
+  //  d.drawLine(ox, oy + 1, x, y + 1, pcolor);
+  //  d.drawLine(ox, oy - 1, x, y - 1, pcolor);
+  ox = x;
+  oy = y;
+
+}
+
+void redrawAxes(ILI9341_t3 & d, double gx, double gy, double w, double h, double xlo, double xhi, double 
ylo, double yhi, double yinc, String title, String xlabel, String ylabel, unsigned int gcolor, unsigned int 
acolor, unsigned int pcolor, unsigned int tcolor, unsigned int bcolor, unsigned int goalColor, boolean resize)
+{
+  //double ydiv, xdiv;
+  // initialize old x and old y in order to draw the first point of the graph
+  // but save the transformed value
+  // note my transform funcition is the same as the map function, except the map uses long and we need 
doubles
+  //static double ox = (x - xlo) * ( w) / (xhi - xlo) + gx;
+  //static double oy = (y - ylo) * (- h) / (yhi - ylo) + gy;
+  double yAxis;
+  //double xAxis;
+
+  //Deleting goalForce line
+  if (capturingPreSteadiness || capturingSteadiness)
+  {
+    yAxis =  (forceGoal - ylo) * (-h) / (yhi - ylo) + gy;
+    d.drawLine(gx, yAxis, gx + w, yAxis, BLACK);
+  }
+
+  if (resize) tft.drawRect(0, 0, gx, gy, BLACK);
+
+  d.setTextSize(1);
+  d.setTextColor(tcolor, bcolor);
+
+  //Vertical line
+  d.drawLine(gx, gy, gx, gy - h, acolor);
+
+  // draw y scale
+  for (double i = ylo; i <= yhi; i += yinc)
+  {
+    // compute the transform
+    yAxis =  (i - ylo) * (-h) / (yhi - ylo) + gy;
+
+    d.drawLine(gx, yAxis, gx + w, yAxis, acolor);
+    //If the scale has changed the numbers must be redrawn
+    if (resize)
+    {
+      printTftFormat(i, gx - 6, yAxis - 3, 1, 0);
+    }
+  }
+
+  //  xAxis =  (-xlo) * ( w) / (xhi - xlo) + gx;
+  //  d.drawLine(gx, gy, gx, gy - h, acolor);
+
+  //now draw the labels
+
+  d.setTextSize(1);
+  d.setTextColor(acolor, bcolor);
+  d.setCursor(gx , gy + 20);
+  d.println(xlabel);
+
+  d.setTextSize(1);
+  d.setTextColor(acolor, bcolor);
+  d.setCursor(gx - 30, gy - h - 10);
+  d.println(ylabel);
+
+  d.setTextSize(2);
+  d.setTextColor(tcolor, bcolor);
+  d.setCursor(gx , gy - h - 30);
+  d.println(title);
+
+  if (capturingPreSteadiness || capturingSteadiness)
+  {
+    yAxis =  (forceGoal - ylo) * (-h) / (yhi - ylo) + gy;
+    d.drawLine(gx, yAxis, gx + w, yAxis, goalColor);
+  }
+}
+
+/*
+ * 
+ * a is the separation between bars
+ * b is the width of the bar
+ * System of 2 equations:
+ * abRatio = a/b
+ * width = n * (a+b) + a
+ */
+void barPlot (float gx, float gy, float w, float h, float yhi, int numBars, int currentIndex, float abRatio, 
unsigned int color)
+{
+  //Solution of the system of 2 equations
+  float b = w / (numBars + abRatio + abRatio * numBars);
+  float a = b * abRatio;
+  float localX = 0;
+  float barValue = 0;
+  float barPixHeight = 0;
+
+  for (int i = currentIndex; i< currentIndex + numBars; i++)
+  {
+    localX += a;
+    barValue = bars[ (i + 10 - numBars) % 10];
+    barPixHeight =  barValue * h / yhi;
+//    Serial.println(String(gx+localX) + "," + String(gy) + "\t" + String(b) + "," + String(bars[ (i + 10 - 
numBars) % 10]));
+    tft.fillRect(gx+localX, gy - barPixHeight , b, barPixHeight, color);
+    localX += b;
+  }
+}
diff --git a/arduino/michrolab/michrolab.h b/arduino/michrolab/michrolab.h
index 4ddaa451d..623a38db2 100644
--- a/arduino/michrolab/michrolab.h
+++ b/arduino/michrolab/michrolab.h
@@ -5,3 +5,114 @@ struct menuEntry {
     String description;
     functionPointer function;
 };
+
+//Starts reading from the load cell alone
+void startLoadCellCapture(void);
+
+//Calculations of the load cell data: Max, Mean, Impulse, RFD...
+void getLoadCellDynamics(void);
+
+void endLoadCellCapture();
+
+void showLoadCellResults();
+
+//Starts reading from the encoder alone
+void startEncoderCapture(void);
+
+void getEncoderDynamics();
+
+void showEncoderResults();
+
+void endEncoderCapture();
+
+//Sets the inertial mode on and sets the position of Con/Ecc change
+void startInertialEncoderCapture();
+
+//sets the position of Con/Ecc change
+void calibrateInertial();
+
+//Reads from the load cell and encoder simultaneously
+void startPowerCapture(void);
+
+void getPowerDynamics();
+
+void readEncoder();
+
+void endPowerCapture();
+
+void showPowerResults();
+
+//Starts to read from load cell and waits to press the red button to start the actual steadiness calculations
+void startSteadiness(void);
+
+void end_steadiness();
+
+//Reads from the RCA to calculate jump heights
+void startJumpsCapture();
+
+//Measuring the selected sensors as well as plotting and saving to SD
+//It also manage the buttons pressed
+void capture();
+
+//Prints a float number with the units number at the selected positoin and precission
+void printTftFormat (float val, int x, int y, int fontSize, int decimal);
+
+//Reads whatever it is in the serial buffer
+void serialEvent();
+
+//return the version of the firmware as well as the hardware type
+void get_version();
+
+//returns the load cell calibration factor
+void get_calibration_factor();
+
+//Sets the calibration factor of the load cell
+void set_calibration_factor(String inputString);
+
+//Process of the calibration. The inputString includes the load used to calibrate
+void calibrate(String inputString);
+
+//Process of offsetting the load cell
+void tare();
+
+//Process of tare and capture. Usefull when the body weight must be substracted from the load cell readings
+void startTareCapture(void);
+
+//Returns the offset of the load cell
+void get_tare();
+
+//Set the offset of the load cell
+void set_tare(String inputString);
+
+//Returns the argument of the command with the form [command]:[argument];
+String get_command_argument(String inputString);
+
+//Not used. Tells if the info sent to the Serial is in text or binary format
+void get_transmission_format();
+
+//Funcion called when the RCA state has changed
+void changingRCA();
+
+//Process of calibration controled from the tft
+void calibrateTFT(void);
+
+//function to read battery level. Not implemented in SportAnalyzer hardware version 1.0
+void showBatteryLevel();
+
+//Update the time in the graph every second
+void updateTime();
+
+//TODO: Add more information or eliminate
+void showSystemInfo(void);
+
+//Sets the horizontal line shown in steadiness measure
+void setForceGoal();
+
+//Saves the meadured data in the SD
+void saveSD(String fileName);
+
+//Count how many dirs exists. Used to create new dirs with the correct numeration
+int countDirs();
+
+//Process of changing the person with the blue button
+void selectPerson();
diff --git a/arduino/michrolab/michrolab.ino b/arduino/michrolab/michrolab.ino
index 5d6440ea5..e5a54ee59 100644
--- a/arduino/michrolab/michrolab.ino
+++ b/arduino/michrolab/michrolab.ino
@@ -1371,26 +1371,6 @@ String createNewDir()
   return (dirName);
 }
 
-void updatePersonSet()
-{
-  String personSet = "Set: " + addLeadingZeros(setNumber, 2) + "   Person: " + 
addLeadingZeros(currentPerson, 2);
-  tft.setTextSize(1);
-  tft.fillRect(148, 207, 120, 8, BLACK);
-  tft.setTextColor(BLACK);
-  tft.setCursor(148, 223);
-  tft.print(persons[currentPerson].name + " " + persons[currentPerson].surname);
-  currentPerson = (currentPerson + 1) % totalPersons;
-
-  personSet = "Set: " + addLeadingZeros(setNumber, 2) + "   Person: " + addLeadingZeros(currentPerson, 2);
-  fileName = "S" + addLeadingZeros(setNumber, 2) + "P" + addLeadingZeros(currentPerson, 2);
-  tft.setTextColor(WHITE);
-  tft.setCursor(148, 207);
-  tft.print(personSet);
-  tft.setCursor(148, 223);
-  tft.print(persons[currentPerson].name + " " + persons[currentPerson].surname);
-  tft.setTextSize(2);
-}
-
 String addLeadingZeros(int number, int totalDigits)
 {
   int leadingZeros = totalDigits - 1;
@@ -1403,128 +1383,6 @@ String addLeadingZeros(int number, int totalDigits)
   return (fixLenNumber);
 }
 
-unsigned int getTotalPerson()
-{
-  char readChar;
-  String readString = "";
-  File  personsFile = SD.open("persons.txt");
-  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 readPersonsFile(struct personType * persons)
-void readPersonsFile()
-{
-  /*
-     Ecample of persons.txt format
-    0,Blancaneus,160, 65,
-    1,Pulgarcito,16, 6,
-    3,Tres porquets,50, 20,
-  */
-  String row = "";
-  char readChar;
-  File  personsFile = SD.open("persons.txt");
-  if (personsFile)
-  {
-
-    currentPerson = 0;
-    personsFile.seek(0);
-
-    // read from the file until there's nothing else in it:
-    while (currentPerson < totalPersons)
-    {
-      readChar = personsFile.read();
-      if (readChar != '\n' && readChar != '\r')
-      {
-        row = row + readChar;
-      } else if (readChar == '\n' || readChar == '\r')
-      {
-        addPerson(row);
-        row = "";
-        currentPerson++;
-      }
-    }
-    // close the file:
-    personsFile.close();
-  } else {
-    // if the file didn't open, print an error:
-    Serial.println("error opening persons.txt");
-  }
-}
-
-void addPerson(String row)
-{
-  int prevComaIndex = row.indexOf(":");
-  int nextComaIndex = row.indexOf(",");
-  currentPerson = row.substring(prevComaIndex + 1, nextComaIndex).toInt();
-  persons[currentPerson].index = currentPerson;
-
-  if (currentPerson >= totalPersons) totalPersons = currentPerson + 1;
-  prevComaIndex = nextComaIndex;
-  nextComaIndex = row.indexOf(",", prevComaIndex + 1 );
-  persons[currentPerson].name = row.substring(prevComaIndex + 1 , nextComaIndex);
-  prevComaIndex = nextComaIndex;
-  nextComaIndex = row.indexOf(",", prevComaIndex + 1 );
-  persons[currentPerson].surname = row.substring(prevComaIndex + 1 , nextComaIndex);
-
-  prevComaIndex = nextComaIndex;
-  nextComaIndex = row.indexOf(",", prevComaIndex + 1 );
-  persons[currentPerson].weight = row.substring(prevComaIndex + 1 , nextComaIndex).toFloat();
-
-  prevComaIndex = nextComaIndex;
-  nextComaIndex = row.indexOf(",", prevComaIndex + 1 );
-  persons[currentPerson].heigh = row.substring(prevComaIndex + 1 , nextComaIndex).toFloat();
-}
-
-void printPersonsList()
-{
-  for (unsigned int i = 0; i < totalPersons; i++)
-  {
-    Serial.print(persons[i].index);
-    Serial.print("," + persons[i].name + "," + persons[i].surname + ",");
-    Serial.print(persons[i].weight);
-    Serial.print(",");
-    Serial.println(persons[i].heigh);
-  }
-}
-
-void savePersonsList()
-{
-  SD.remove("persons.txt");
-  File personsFile = SD.open("persons.txt");
-  for (unsigned int i = 0; i < totalPersons; i++)
-  {
-    personsFile.print(persons[i].index);
-    personsFile.print("," + persons[i].name + "," + persons[i].surname + ",");
-    personsFile.print(persons[i].weight);
-    personsFile.print(",");
-    personsFile.println(persons[i].heigh);
-  }
-  personsFile.close();
-}
-
 void startInertialEncoderCapture()
 {
   inertialMode = true;
diff --git a/arduino/michrolab/personsManage.ino b/arduino/michrolab/personsManage.ino
new file mode 100644
index 000000000..be4601558
--- /dev/null
+++ b/arduino/michrolab/personsManage.ino
@@ -0,0 +1,141 @@
+void savePersonsList()
+{
+  SD.remove("persons.txt");
+  File personsFile = SD.open("persons.txt");
+  for (unsigned int i = 0; i < totalPersons; i++)
+  {
+    personsFile.print(persons[i].index);
+    personsFile.print("," + persons[i].name + "," + persons[i].surname + ",");
+    personsFile.print(persons[i].weight);
+    personsFile.print(",");
+    personsFile.println(persons[i].heigh);
+  }
+  personsFile.close();
+}
+
+void printPersonsList()
+{
+  for (unsigned int i = 0; i < totalPersons; i++)
+  {
+    Serial.print(persons[i].index);
+    Serial.print("," + persons[i].name + "," + persons[i].surname + ",");
+    Serial.print(persons[i].weight);
+    Serial.print(",");
+    Serial.println(persons[i].heigh);
+  }
+}
+
+void addPerson(String row)
+{
+  int prevComaIndex = row.indexOf(":");
+  int nextComaIndex = row.indexOf(",");
+  currentPerson = row.substring(prevComaIndex + 1, nextComaIndex).toInt();
+  persons[currentPerson].index = currentPerson;
+
+  if (currentPerson >= totalPersons) totalPersons = currentPerson + 1;
+  prevComaIndex = nextComaIndex;
+  nextComaIndex = row.indexOf(",", prevComaIndex + 1 );
+  persons[currentPerson].name = row.substring(prevComaIndex + 1 , nextComaIndex);
+  prevComaIndex = nextComaIndex;
+  nextComaIndex = row.indexOf(",", prevComaIndex + 1 );
+  persons[currentPerson].surname = row.substring(prevComaIndex + 1 , nextComaIndex);
+
+  prevComaIndex = nextComaIndex;
+  nextComaIndex = row.indexOf(",", prevComaIndex + 1 );
+  persons[currentPerson].weight = row.substring(prevComaIndex + 1 , nextComaIndex).toFloat();
+
+  prevComaIndex = nextComaIndex;
+  nextComaIndex = row.indexOf(",", prevComaIndex + 1 );
+  persons[currentPerson].heigh = row.substring(prevComaIndex + 1 , nextComaIndex).toFloat();
+}
+
+//void readPersonsFile(struct personType * persons)
+void readPersonsFile()
+{
+  /*
+     Ecample of persons.txt format
+    0,Blancaneus,160, 65,
+    1,Pulgarcito,16, 6,
+    3,Tres porquets,50, 20,
+  */
+  String row = "";
+  char readChar;
+  File  personsFile = SD.open("persons.txt");
+  if (personsFile)
+  {
+
+    currentPerson = 0;
+    personsFile.seek(0);
+
+    // read from the file until there's nothing else in it:
+    while (currentPerson < totalPersons)
+    {
+      readChar = personsFile.read();
+      if (readChar != '\n' && readChar != '\r')
+      {
+        row = row + readChar;
+      } else if (readChar == '\n' || readChar == '\r')
+      {
+        addPerson(row);
+        row = "";
+        currentPerson++;
+      }
+    }
+    // close the file:
+    personsFile.close();
+  } else {
+    // if the file didn't open, print an error:
+    Serial.println("error opening persons.txt");
+  }
+}
+
+unsigned int getTotalPerson()
+{
+  char readChar;
+  String readString = "";
+  File  personsFile = SD.open("persons.txt");
+  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);
+  tft.setTextSize(1);
+  tft.fillRect(148, 207, 120, 8, BLACK);
+  tft.setTextColor(BLACK);
+  tft.setCursor(148, 223);
+  tft.print(persons[currentPerson].name + " " + persons[currentPerson].surname);
+  currentPerson = (currentPerson + 1) % totalPersons;
+
+  personSet = "Set: " + addLeadingZeros(setNumber, 2) + "   Person: " + addLeadingZeros(currentPerson, 2);
+  fileName = "S" + addLeadingZeros(setNumber, 2) + "P" + addLeadingZeros(currentPerson, 2);
+  tft.setTextColor(WHITE);
+  tft.setCursor(148, 207);
+  tft.print(personSet);
+  tft.setCursor(148, 223);
+  tft.print(persons[currentPerson].name + " " + persons[currentPerson].surname);
+  tft.setTextSize(2);
+}


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