AWind
ChartDC.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 */
23 class ChartDC
24 {
25  float _last_y;
26  float _scale_x;
27  float _scale_y;
28  float _offset_x;
29  float _offset_y;
30  float _min_y;
31  float _max_y;
32  int _dc_start_x;
33  int _dc_start_y;
34 public:
35  static const float AutoMin;
36  static const float AutoMax;
39  {
40  _scale_x=1;
41  _scale_y=1;
42  _offset_x=0;
43  _offset_y=0;
44  _dc_start_x=0;
45  _dc_start_y=0;
46  _min_y=0;
47  _max_y=0;
48  }
49  float calcScaling(int length,float &min_val,float &max_val)
50  {
51  if(min_val == max_val)
52  {
53  if(min_val == 0)
54  {
55  min_val=-1;
56  max_val=1;
57  }
58  else if(min_val<0)
59  max_val = 0;
60  else if(min_val>0)
61  min_val = 0;
62  }
63  return length/(max_val-min_val);
64  }
65  void setScalingX(int dc_offset,int length,float min_val,float max_val)
66  {
67  _scale_x=calcScaling(length,min_val,max_val);
68  _offset_x=min_val;
69  _dc_start_x=dc_offset;
70  /*Log::Number("Set scale x scale: ",_scale_x);
71  Log::Number(" offset: ",_offset_x);
72  Log::Number(" min: ",min_val);
73  Log::Number(" max: ",max_val,true);*/
74  }
75  void setScalingY(int dc_offset,int length,float min_val,float max_val)
76  {
77  _scale_y=calcScaling(length,min_val,max_val);
78  _offset_y=min_val;
79  /*Log::Number("Set scale y scale: ",_scale_y);
80  Log::Number(" offset: ",_offset_y);
81  Log::Number(" min: ",min_val);
82  Log::Number(" max: ",max_val,true);*/
83  _dc_start_y=dc_offset;
84  _min_y=min_val;
85  _max_y=max_val;
86  }
87  float MinY()
88  {
89  return _min_y;
90  }
91  float MaxY()
92  {
93  return _max_y;
94  }
96  float LCtoDC_x(float x)
97  {
98  return _dc_start_x+(x-_offset_x)*_scale_x;
99  }
101  float LCtoDC_y(float y)
102  {
103  return _dc_start_y-(y-_offset_y)*_scale_y;
104  }
106  void MoveTo(DC *dc,float x,float y)
107  {
108  _last_y=y;
109  dc->MoveTo(LCtoDC_x(x),LCtoDC_y(y));
110  //Log::Number("Move to x: ",x);
111  //Log::Number(" y: ",y,true);
112  }
114  void LineTo(DC *dc,float x,float y)
115  {
116  if(y<_min_y || y> _max_y || _last_y<_min_y || _last_y> _max_y)
117  dc->MoveTo(LCtoDC_x(x),LCtoDC_y(y));
118  else
119  dc->LineTo(LCtoDC_x(x),LCtoDC_y(y));
120  _last_y=y;
121  //Log::Number(" min_y: ",_min_y);
122  //Log::Number(" max_y: ",_max_y,true);
123 
124  /*Log::Number("Line: ",_last_x);
125  Log::Number(" ",_last_y);
126  Log::Number(" ",new_x);
127  Log::Number(" ",new_y,"");
128  Log::Number(" lc: ",x);
129  Log::Number(",",y,true);*/
130  }
131 };
132 const float ChartDC::AutoMin = 1e-8;
133 const float ChartDC::AutoMax = 1e8;
float LCtoDC_x(float x)
Converts logic x into window x.
Definition: ChartDC.h:96
Device context for chart window. Implement transformation of logic coordinates into window coordinate...
Definition: ChartDC.h:23
ChartDC()
Constructor.
Definition: ChartDC.h:38
Device context. Abstraction layer to the device specific drawing code. Coordinates in drawing functio...
Definition: DC.h:29
void MoveTo(DC *dc, float x, float y)
Moves actual position in logic coordinates.
Definition: ChartDC.h:106
float LCtoDC_y(float y)
Converts logic y into window y.
Definition: ChartDC.h:101
void LineTo(DC *dc, float x, float y)
Draws line from actual position to the new position.
Definition: ChartDC.h:114