-

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

LineGraph.h

Committer:
wolfsberger
Date:
2016-02-16
Revision:
0:9ed7238d49e2
Child:
2:81fc8f80fdb4

File content as of revision 0:9ed7238d49e2:

#ifndef LINEGRAPH_H_
#define LINEGRAPH_H_

#include "SPI_TFT_ILI9341.h"

template <size_t itemCount>
class LineGraph
{
public:
    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;
        }
    }
    
    void addItem(float value)
    {
        if (!isInRange(value))
        {
            value = min_-1;
        }
        
        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)))
            {
                continue;
            }
            
            float y1New = scaleY(dataNew_[indexItem1]);
            float y2New = scaleY(dataNew_[indexItem2]);            
            
            tft_->line(x1, y1New, x2, y2New, color);
        }
    }
private:
    float scaleY(float ypos)
    {
        static float valueRange = static_cast<float>(max_-min_);
        static float buttomOfGraph = static_cast<float>(y_+height_);
        
        if (ypos < min_)
            ypos = min_;
        if (ypos > max_)
            ypos = max_;
        
        float scaled = buttomOfGraph - (height_ * (ypos / valueRange));    
        
        return scaled;
    }
    bool isInRange(float value)
    {
        return  value > min_ && 
                value < max_;
    }

    float dataOld_[itemCount];
    float dataNew_[itemCount];
    SPI_TFT_ILI9341 * tft_;
    int x_;
    int y_;
    int width_;
    int height_;
    int min_;
    int max_;
};

#endif