[chronojump/FS-TFT-Menu] Drawinng optimization deleting only plotted lines



commit 64892bd2fecbc5ea4f962c84a317b0b188e32b1b
Author: Xavier Padullés <testing chronojump org>
Date:   Mon Apr 25 21:28:10 2022 +0200

    Drawinng optimization deleting only plotted lines

 arduino/ForceSensorTFT/ForceSensorTFT.ino | 263 ++++++++++++++++++------------
 1 file changed, 157 insertions(+), 106 deletions(-)
---
diff --git a/arduino/ForceSensorTFT/ForceSensorTFT.ino b/arduino/ForceSensorTFT/ForceSensorTFT.ino
index 7f98a8870..06edf4250 100644
--- a/arduino/ForceSensorTFT/ForceSensorTFT.ino
+++ b/arduino/ForceSensorTFT/ForceSensorTFT.ino
@@ -99,9 +99,13 @@ bool blueButtonState;
 unsigned short lcdDelay = 25; //to be able to see the screen. Seconds are also printed in delay but 25 
values are less than one second
 unsigned short lcdCount = 0;
 float measuredLcdDelayMax = 0; //The max in the lcdDelay periodca
-float measuredMax = 20; // The max since starting capture
-float measuredMin = -20;
-float measured = scale.get_units();
+float measuredMax = 100.0; // The max since starting capture
+float measuredMin = -20.0;
+float measured = 0;
+double newGraphMax = measuredMax;
+double newGraphMin = measuredMin;
+double graphMin = -20;
+double graphMax = 100;
 
 /***** Atention!!! *****
     lcd.createChar() function makes a mess with the cursor position and it must be specified
@@ -1038,31 +1042,52 @@ void capture(void)
   double graphY = 240;
 
   //Size of the graph
-  double graphW = 320;
+  double graphW = 290;
   double graphH = 240;
 
   //Minimum and maximum values to show
-  double xMin = -20;
-  double xMax = 280;
+  double xMin = 0;
+  double xMax = 290;
 
   //Size an num of divisions
   double yDivSize = 100;
   double yDivN = 10;
   double xDivSize = 100;
   double xDivN = 3;
+  double yBuffer[320];
+
+  bool resized = true;
+
+  for (int i = xMin; i < xMax; i++)
+  {
+    yBuffer[i] = 0;
+  }
 
   MsTimer2::stop();
 
   tft.fillScreen(BLACK);
 
-  double xGraph = 0;
+  double xGraph = 1;
 
+  tft.fillScreen(BLACK);
   while (capturing)
   {
     xGraph = 0;
-    tft.fillScreen(BLACK);
+    //Deleting the previous plotted points
+    for (int i = xMin; i < xMax; i++)
+    {
+      Graph(tft, i, yBuffer[i], graphX, graphY, graphW, graphH, xMin, xMax, xDivSize, graphMin, graphMax, 
yDivSize, "", "", "", WHITE, WHITE, BLACK, WHITE, BLACK, display1);
+    }
     display1 = true;
-    while (xGraph != 300) {
+
+    redrawAxes(tft, graphX, graphY, graphW, graphH, xMin, xMax, graphMin, graphMax, yDivSize, "", "", "", 
WHITE, BLACK, BLACK, BLACK, BLACK, resized);
+    graphMax = newGraphMax;
+    graphMin = newGraphMin;
+    yDivSize = (graphMax - graphMin) / yDivN;
+    redrawAxes(tft, graphX, graphY, graphW, graphH, xMin, xMax, graphMin, graphMax, yDivSize, "", "", "", 
WHITE, WHITE, BLACK, WHITE, BLACK, resized);
+    resized = false;
+
+    while (xGraph < xMax) {
       //Checking the RCA state
       if (rcaState != lastRcaState) {       //Event generated by the RCA
         Serial.print(rcaTime);
@@ -1145,19 +1170,23 @@ void capture(void)
         if (abs(measured) > abs(measuredLcdDelayMax)) {
           measuredLcdDelayMax = measured;
         }
-        if (measured > measuredMax) {
-          measuredMax = measured;
-          yDivSize = (measuredMax - measuredMin) / yDivN;
+        if (measured > newGraphMax) {
+          newGraphMax = measured;
+          resized = true;
         }
-        if (measured < measuredMin) {
-          measuredMin = measured;
-          yDivSize = (measuredMax - measuredMin) / yDivN;
+        if (measured < newGraphMin) {
+          newGraphMin = measured;
+          resized = true;
         }
 
+
         Serial.print(totalTime); Serial.print(";");
         Serial.println(measured, 2); //scale.get_units() returns a float
-
-        Graph(tft, xGraph, measured, graphX, graphY, graphW, graphH, xMin, xMax, xDivSize, measuredMin, 
measuredMax, yDivSize, "", "", "", WHITE, WHITE, BLUE, WHITE, BLACK, display1);
+        yBuffer[(int)xGraph] = measured;
+        //                        Serial.print(measured);
+        //                        Serial.print("\t");
+        //                Serial.println(yBuffer[(int)xGraph]);
+        Graph(tft, xGraph, measured, graphX, graphY, graphW, graphH, xMin, xMax, xDivSize, graphMin, 
graphMax, yDivSize, "", "", "", WHITE, WHITE, BLUE, WHITE, BLACK, display1);
         xGraph++;
       }
 
@@ -1200,18 +1229,18 @@ void printLcdFormat (float val, int xStart, int y, int decimal) {
 
   // Adding the extra characters to the left
   if (valLength > 0) {
-    xStart = valLength*charWidth[fontSize];
+    xStart = valLength * charWidth[fontSize];
   }
 
   // In negatives numbers the units are in the same position and the minus one position to the left
   if (val < 0) {
     xStart - charWidth[fontSize];
   }
-  tft.setCursor(xStart*charWidth[fontSize]  , y);
+  tft.setCursor(xStart * charWidth[fontSize]  , y);
   tft.print(val, decimal);
 }
 
-void printTftFormat (float val, int xStart, int y, int decimal) {
+void printTftFormat (float val, int xStart, int y, int fontSize, int decimal) {
 
   /*How many characters are to the left of the units number.
      Examples:
@@ -1222,13 +1251,12 @@ void printTftFormat (float val, int xStart, int y, int decimal) {
 
   //Font sizes: 5x8, 10x16, 15x24, or 20x32
   //Theres a pixel between characters
-  int fontSize = 2;
   int charWidth = 5 * fontSize + 1;
   int valLength = floor(log10(abs(val)));
 
   // Adding the extra characters to the left
   if (valLength > 0) {
-    xStart = xStart - valLength*charWidth;
+    xStart = xStart - valLength * charWidth;
   }
 
   // In negatives numbers the units are in the same position and the minus one position to the left
@@ -1287,7 +1315,7 @@ void start_capture()
   Serial.println("Starting capture...");
   totalTime = 0;
   lastTime = micros();
-  measuredMax = 0;
+  measuredMax = scale.get_units();
   impulse = 0;
 
   //filling the array of forces ant times with initial force
@@ -1599,50 +1627,51 @@ void showSystemInfo() {
 }
 
 void showResults() {
+  int textSize = 2;
   Serial.println("In showResults");
   int submenu = 4;
   redButtonState = false;
   tft.fillScreen(BLACK);
   tft.setTextSize(3);
-  tft.setCursor(100,0);
+  tft.setCursor(100, 0);
   tft.print("Results");
 
-  tft.drawLine(0,20,320,20, GREY);
-  tft.drawLine(160,240,160,20, GREY);
-  tft.setTextSize(2);
-  
+  tft.drawLine(0, 20, 320, 20, GREY);
+  tft.drawLine(160, 240, 160, 20, GREY);
+  tft.setTextSize(textSize);
+
   tft.setCursor(0, 40);
   tft.print("Fmax");
-  printTftFormat(measuredMax, 100, 40, 1);
+  printTftFormat(measuredMax, 100, 40, textSize, 1);
 
   tft.setCursor(170, 40);
   tft.print("Fmax1s");
-  printTftFormat(maxMeanForce1s, 280, 40, 1);
-  
+  printTftFormat(maxMeanForce1s, 280, 40, textSize, 1);
+
   tft.setCursor(0, 80);
   tft.print("Ftrig");
-  printTftFormat(forceTrigger, 100, 80, 1);
-    
+  printTftFormat(forceTrigger, 100, 80, textSize, 1);
+
   tft.setCursor(170, 80);
   tft.print("Imp");
-  printTftFormat(impulse, 280, 80, 1);
-    
+  printTftFormat(impulse, 280, 80, textSize, 1);
+
   tft.setCursor(0, 120);
   tft.print("RFD100");
-  printTftFormat(maxRFD100, 124, 120, 0);
-    
+  printTftFormat(maxRFD100, 124, 120, textSize, 0);
+
   tft.setCursor(170, 120);
   tft.print("RFD200");
-  printTftFormat(maxRFD200, 304, 120, 0);
-    
+  printTftFormat(maxRFD200, 304, 120, textSize, 0);
+
   tft.setCursor(0, 160);
   tft.print("RMSSD");
-  printTftFormat(RMSSD, 100, 160, 1);
-    
+  printTftFormat(RMSSD, 100, 160, textSize, 1);
+
   tft.setCursor(170, 160);
   tft.print("cvRMSSD");
-  printTftFormat(RMSSD, 280, 160, 1);
-  
+  printTftFormat(RMSSD, 280, 160, textSize, 1);
+
   //Red button exits results
   while (!redButtonState) {
     blueButtonState = !digitalRead(blueButtonPin);
@@ -1792,9 +1821,36 @@ void showSteadinessResults()
   showMenu();
 }
 
+/*
+  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(Adafruit_ILI9341 &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 &redraw)
 {
-
   double ydiv, xdiv;
   // initialize old x and old y in order to draw the first point of the graph
   // but save the transformed value
@@ -1805,84 +1861,79 @@ void Graph(Adafruit_ILI9341 &d, double x, double y, double gx, double gy, double
   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 (redraw == true)
   {
 
     redraw = false;
-    ox = (x - xlo) * ( w) / (xhi - xlo) + gx;
-    oy = (y - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
-    // draw y scale
-    for ( i = ylo; i <= yhi; i += yinc)
-    {
-      // compute the transform
-      temp =  (i - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
+    //In redraw, a point is plotted at the most left point
+    ox = x;
+    oy = y;
+  }
 
-      if (i == 0)
-      {
-        d.drawLine(gx, temp, gx + w, temp, acolor);
-      }
-      else
-      {
-        d.drawLine(gx, temp, gx + w, temp, gcolor);
-      }
+  //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;
 
-      d.setTextSize(1);
-      d.setTextColor(tcolor, bcolor);
-      //d.setCursor(gx - 40, temp);
-      d.setCursor(gx - 20, temp - 3);
-      // precision is default Arduino--this could really use some format control
-      d.println((int)round(i));
-    }
-    // draw x scale
-    for (i = xlo; i <= xhi; i += xinc) {
+}
 
-      // compute the transform
+void redrawAxes(Adafruit_ILI9341 &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, 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 i;
+  double yAxis;
+  double xAxis;
 
-      temp =  (i - xlo) * ( w) / (xhi - xlo) + gx;
-      if (i == 0) {
-        d.drawLine(temp, gy, temp, gy - h, acolor);
-      }
-      else {
-        d.drawLine(temp, gy, temp, gy - h, gcolor);
-      }
+  //Vertical line
+  d.drawLine(gx, gy, gx, gy - h, acolor);
+  
+  // draw y scale
+  for ( 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 (resize)
+    {
       d.setTextSize(1);
       d.setTextColor(tcolor, bcolor);
-      d.setCursor(temp, gy + 10);
-      // precision is default Arduino--this could really use some format control
-      d.println(round(i));
+      printTftFormat(i, gx-6, yAxis - 3, 1, 0);
     }
+  }
 
-    //now draw the labels
-    d.setTextSize(2);
-    d.setTextColor(tcolor, bcolor);
-    d.setCursor(gx , gy - h - 30);
-    d.println(title);
-
-    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);
-
+  //  xAxis =  (-xlo) * ( w) / (xhi - xlo) + gx;
+  //  d.drawLine(gx, gy, gx, gy - h, acolor);
 
-  }
+  //now draw the labels
+  d.setTextSize(2);
+  d.setTextColor(tcolor, bcolor);
+  d.setCursor(gx , gy - h - 30);
+  d.println(title);
 
-  //graph drawn now plot the data
-  // the entire plotting code are these few lines...
-  // recall that ox and oy are initialized as static above
-  x =  (x - xlo) * ( w) / (xhi - xlo) + gx;
-  y =  (y - ylo) * (gy - h - gy) / (yhi - ylo) + gy;
-  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;
+  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);
 }
 
 void drawMenuBackground() {


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