-

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GraphScale.cpp Source File

GraphScale.cpp

00001 #include "GraphScale.h"
00002 
00003 GraphScale::GraphScale(SPI_TFT_ILI9341 * tft, int x, int y, int width, int height, int min, int max, int scaleSteps)
00004     : tft_(tft), x_(x), y_(y), width_(width), height_(height), min_(min), max_(max), scaleSteps_(scaleSteps)
00005 {
00006 }
00007 
00008 void GraphScale::draw(int color)
00009 {
00010     int x1 = x_-1;
00011     int x2 = x_+width_+1;
00012     int y1 = y_-1;
00013     int y2 = y_+height_+1;
00014 
00015     tft_->rect(x1, y1, x2, y2, color);
00016 
00017     //int stepsize = height_ / ((max_-min_) / scaleSteps_);
00018     int stepposition = min_;
00019 
00020     while (stepposition <= max_)
00021     {
00022         float yPos = scaleY(stepposition);
00023         tft_->line(x2, yPos, x2+5, yPos, color);
00024         tft_->locate(x2+10,yPos-6);
00025         tft_->printf("%d",stepposition);
00026         stepposition += scaleSteps_;
00027     }
00028 }
00029 
00030 float GraphScale::scaleY(float ypos)
00031 {
00032     static float valueRange = static_cast<float>(max_-min_);
00033     static float buttomOfGraph = static_cast<float>(y_+height_);
00034 
00035     if (ypos < min_)
00036         ypos = min_;
00037     if (ypos > max_)
00038         ypos = max_;
00039 
00040     float scaled = buttomOfGraph - (height_ * ((ypos-min_) / valueRange));
00041 
00042     return scaled;
00043 }