AWind
GaugeRadialPointer.h
1 #pragma once
2 /*
3  AWind.h - Arduino window library support for Color TFT LCD Boards
4  Copyright (C)2015 Andrei Degtiarev. All right reserved
5 
6 
7  You can always find the latest version of the library at
8  https://github.com/AndreiDegtiarev/AWind
9 
10 
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the CC BY-NC-SA 3.0 license.
13  Please see the included documents for further information.
14 
15  Commercial use of this library requires you to buy a license that
16  will allow commercial use. This includes using the library,
17  modified or not, as a tool to sell products.
18 
19  The license applies to all part of the library including the
20  examples and tools supplied with the library.
21 */
22 #include "Gauge.h"
24 class GaugeRadialPointer : public Gauge
25 {
26  int _numTicks;
27 
28 public:
29 
30  static const float _sector_angle_rad;
31 
33 
39  GaugeRadialPointer(int left,int top,int width,int height)
40  :Gauge(left,top,width,height),_numTicks(5)
41 
42  {
43  }
45 
48  void SetNumberOfTicks(int numTicks)
49  {
50  _numTicks=numTicks;
51  }
53 
56  void OnDraw(DC *dc)
57  {
58  dc->SetFont(SmallFont);
59  float range=_maxValue-_minValue;
60  float step_val=(range)/(_numTicks-1);
61  float factor=1000/range;
62  int radius=Height()*1;
63  int x0=Width()/2;
64  int y0=Height()*1.4;
65  if(!_drawOnlyPointer)
66  {
67  dc->DrawRoundRect(2,2,Width()-2,Height()-2);
68  dc->Sector(x0,y0,radius,_sector_angle_rad);
69  float angle_step=_sector_angle_rad/(_numTicks-1.0);
70  float angle;
71  float sin_val;
72  float cos_val;
73  float value;
74  int x_offset;
75  for(int i=0;i<_numTicks;i++)
76  {
77  angle=-_sector_angle_rad/2.0+angle_step*i;
78  sin_val=sin(angle);
79  cos_val=cos(angle);
80  dc->MoveTo(x0+radius*sin_val,y0-radius*cos_val);
81  dc->LineTo(x0+radius*1.1*sin_val,y0-radius*1.1*cos_val);
82  value=_minValue+step_val*i;
83  x_offset=AHelper::GetNumberLength(value,1);
84  if(value<0)
85  x_offset++;
86  if(i==0)
87  x_offset=0;
88  else if(i == _numTicks-1)
89  x_offset=-(x_offset-0.5)*dc->FontWidth();
90  else
91  x_offset=-x_offset/2*dc->FontWidth();
92  dc->DrawNumber(value,1,x0+radius*1.1*sin_val+x_offset,y0-radius*1.1*cos_val-(dc->FontHeight()*(i==0||i==_numTicks-1?1.5:1)));
93  }
94  }
95  dc->SetColor(_fillColor);
96  DrawPointer(_oldValue,x0,y0,radius,dc);
97  dc->SetColor(Color::Red);
98  DrawPointer(_value,x0,y0,radius,dc);
99  }
100 private:
102  void DrawPointer(float value,int x0,int y0,int radius,DC *dc)
103  {
104  //dc->SetColor(color);
105  float dc_val=1000/(_maxValue-_minValue)*(value-_minValue);
106  float angle=-_sector_angle_rad/2.0+_sector_angle_rad/1000.0*dc_val;
107  float sin_val=sin(angle);
108  float cos_val=cos(angle);
109  for(int i=-3;i<4;i++)
110  {
111  dc->MoveTo(x0+radius*0.95*sin_val,y0-radius*0.95*cos_val);
112  dc->LineTo(x0+i+radius*0.5*sin_val,y0-radius*0.5*cos_val);
113  }
114 
115  }
116 };
117 const float GaugeRadialPointer::_sector_angle_rad = 3.14 / 2 * 0.8;
Gauge radial pointer class.
Definition: GaugeRadialPointer.h:24
int FontHeight()
Returns symbol jeight for the current font.
Definition: DC.h:211
void Sector(int x0, int y0, int radius, float angle_rad)
Draws sector. Input coordinates have to be defined in the window coordinate system.
Definition: DC.h:250
void DrawNumber(int number, int x, int y)
Draws integer number. Input coordinates have to be defined in the window coordinate system...
Definition: DC.h:161
Color _fillColor
Gauge fill color.
Definition: Gauge.h:27
void OnDraw(DC *dc)
Implements drawing code.
Definition: GaugeRadialPointer.h:56
Gauge window class, that allows visualisaion of data in form of bar or radial pointer.
Definition: Gauge.h:24
Device context. Abstraction layer to the device specific drawing code. Coordinates in drawing functio...
Definition: DC.h:29
float _value
Current gauge value.
Definition: Gauge.h:28
int Height()
Returns window height.
Definition: Window.h:189
int FontWidth()
Returns symbol width for the current font.
Definition: DC.h:206
float _minValue
Gauge min limit.
Definition: Gauge.h:30
bool _drawOnlyPointer
Defines whether only pointer (not scale has to be drawn)
Definition: Gauge.h:32
float _maxValue
Gauge max limit.
Definition: Gauge.h:31
float _oldValue
Previos gauge value.
Definition: Gauge.h:29
void SetNumberOfTicks(int numTicks)
Sets number of ticks.
Definition: GaugeRadialPointer.h:48
GaugeRadialPointer(int left, int top, int width, int height)
Constructor.
Definition: GaugeRadialPointer.h:39
void DrawRoundRect(int left, int top, int right, int bottom)
Draws rounded rectangle. Input coordinates have to be defined in the window coordinate system...
Definition: DC.h:156
int Width()
Returns window width.
Definition: Window.h:184