-

Dependencies:   CommandHandler HygroClip2 InterruptBasedEncoder SPI_TFT_ILI9341 mbed-src-no-hal

Revision:
3:3ef8c2d7b1bf
Parent:
2:81fc8f80fdb4
--- a/LineGraph.h	Wed Feb 17 07:30:14 2016 +0000
+++ b/LineGraph.h	Tue Mar 15 07:46:06 2016 +0000
@@ -2,6 +2,7 @@
 #define LINEGRAPH_H_
 
 #include "SPI_TFT_ILI9341.h"
+#include <cmath>
 
 template <size_t itemCount>
 class LineGraph
@@ -10,53 +11,50 @@
     LineGraph(SPI_TFT_ILI9341 * tft, int x, int y, int width, int height, int min, int max)
         : tft_(tft), x_(x), y_(y), width_(width), height_(height), min_(min), max_(max)
     {
-        /*memset(dataOld_,-1.0f,itemCount*sizeof(float));
-        memset(dataNew_,-1.0f,itemCount*sizeof(float));*/
         for(size_t i = 0; i<itemCount; i++)
         {
-            dataOld_[i] = static_cast<float>(min) - 1.0f;
-            dataNew_[i] = static_cast<float>(min) - 1.0f;
+            dataOld_[i] = NAN;
+            dataNew_[i] = NAN;
         }
     }
-    
+
     void addItem(float value)
     {
         if (!isInRange(value))
         {
-            value = min_-1;
+            value = NAN;
         }
-        
+
         memcpy(dataOld_,dataNew_,(itemCount)*sizeof(float));
         memmove(dataNew_,dataNew_+1,(itemCount-1)*sizeof(float));
         dataNew_[itemCount-1] = value;
     }
-    
+
     void draw(int color)
     {
         float pointStepSize = static_cast<float>(width_) / static_cast<float>(itemCount-1);
-        
+
         for (size_t index = 1; index < itemCount; index++)
         {
             size_t indexItem1 = index - 1;
             size_t indexItem2 = index;
-            
+
             float x1 = 1 + pointStepSize * static_cast<float>(indexItem1);
             float x2 = 1 + pointStepSize * static_cast<float>(indexItem2);
-            
+
             float y1Old = scaleY(dataOld_[indexItem1]);
-            float y2Old = scaleY(dataOld_[indexItem2]);   
-            
-            tft_->line(x1, y1Old, x2, y2Old, Black); 
-            
-            if ((dataNew_[indexItem1] == (min_-1)) ||
-                (dataNew_[indexItem2] == (min_-1)))
+            float y2Old = scaleY(dataOld_[indexItem2]);
+
+            tft_->line(x1, y1Old, x2, y2Old, Black);
+
+            if (isnan(dataNew_[indexItem1]) || isnan(dataNew_[indexItem2]))
             {
                 continue;
             }
-            
+
             float y1New = scaleY(dataNew_[indexItem1]);
-            float y2New = scaleY(dataNew_[indexItem2]);            
-            
+            float y2New = scaleY(dataNew_[indexItem2]);
+
             tft_->line(x1, y1New, x2, y2New, color);
         }
     }
@@ -65,19 +63,24 @@
     {
         static float valueRange = static_cast<float>(max_-min_);
         static float buttomOfGraph = static_cast<float>(y_+height_);
-        
+
+        if (isnan(ypos))
+        {
+            return buttomOfGraph;
+        }
+
         if (ypos < min_)
             ypos = min_;
         if (ypos > max_)
             ypos = max_;
-        
-        float scaled = buttomOfGraph - (height_ * ((ypos-min_) / valueRange));    
-        
+
+        float scaled = buttomOfGraph - (height_ * ((ypos-min_) / valueRange));
+
         return scaled;
     }
     bool isInRange(float value)
     {
-        return  value > min_ && 
+        return  value > min_ &&
                 value < max_;
     }
 
@@ -92,4 +95,4 @@
     int max_;
 };
 
-#endif
\ No newline at end of file
+#endif