-

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LineGraph.h Source File

LineGraph.h

00001 #ifndef LINEGRAPH_H_
00002 #define LINEGRAPH_H_
00003 
00004 #include "SPI_TFT_ILI9341.h"
00005 #include <cmath>
00006 
00007 template <size_t itemCount>
00008 class LineGraph
00009 {
00010 public:
00011     LineGraph(SPI_TFT_ILI9341 * tft, int x, int y, int width, int height, int min, int max)
00012         : tft_(tft), x_(x), y_(y), width_(width), height_(height), min_(min), max_(max)
00013     {
00014         for(size_t i = 0; i<itemCount; i++)
00015         {
00016             dataOld_[i] = NAN;
00017             dataNew_[i] = NAN;
00018         }
00019     }
00020 
00021     void addItem(float value)
00022     {
00023         if (!isInRange(value))
00024         {
00025             value = NAN;
00026         }
00027 
00028         memcpy(dataOld_,dataNew_,(itemCount)*sizeof(float));
00029         memmove(dataNew_,dataNew_+1,(itemCount-1)*sizeof(float));
00030         dataNew_[itemCount-1] = value;
00031     }
00032 
00033     void draw(int color)
00034     {
00035         float pointStepSize = static_cast<float>(width_) / static_cast<float>(itemCount-1);
00036 
00037         for (size_t index = 1; index < itemCount; index++)
00038         {
00039             size_t indexItem1 = index - 1;
00040             size_t indexItem2 = index;
00041 
00042             float x1 = 1 + pointStepSize * static_cast<float>(indexItem1);
00043             float x2 = 1 + pointStepSize * static_cast<float>(indexItem2);
00044 
00045             float y1Old = scaleY(dataOld_[indexItem1]);
00046             float y2Old = scaleY(dataOld_[indexItem2]);
00047 
00048             tft_->line(x1, y1Old, x2, y2Old, Black);
00049 
00050             if (isnan(dataNew_[indexItem1]) || isnan(dataNew_[indexItem2]))
00051             {
00052                 continue;
00053             }
00054 
00055             float y1New = scaleY(dataNew_[indexItem1]);
00056             float y2New = scaleY(dataNew_[indexItem2]);
00057 
00058             tft_->line(x1, y1New, x2, y2New, color);
00059         }
00060     }
00061 private:
00062     float scaleY(float ypos)
00063     {
00064         static float valueRange = static_cast<float>(max_-min_);
00065         static float buttomOfGraph = static_cast<float>(y_+height_);
00066 
00067         if (isnan(ypos))
00068         {
00069             return buttomOfGraph;
00070         }
00071 
00072         if (ypos < min_)
00073             ypos = min_;
00074         if (ypos > max_)
00075             ypos = max_;
00076 
00077         float scaled = buttomOfGraph - (height_ * ((ypos-min_) / valueRange));
00078 
00079         return scaled;
00080     }
00081     bool isInRange(float value)
00082     {
00083         return  value > min_ &&
00084                 value < max_;
00085     }
00086 
00087     float dataOld_[itemCount];
00088     float dataNew_[itemCount];
00089     SPI_TFT_ILI9341 * tft_;
00090     int x_;
00091     int y_;
00092     int width_;
00093     int height_;
00094     int min_;
00095     int max_;
00096 };
00097 
00098 #endif