-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlot.cpp
264 lines (222 loc) · 6.52 KB
/
Plot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "Plot.h"
#include <avr/dtostrf.h>
Plot::Plot(Adafruit_SSD1306 *display) {
_display = display;
}
// Accessors {{{
void Plot::setPlotSize(double x, double y, double w, double h) {
_xpos = x;
_ypos = y;
_width = w;
_height = h;
}
void Plot::setXRange(double xmin, double xmax) {
_xmin = xmin;
_xmax = xmax;
}
void Plot::setYRange(double ymin, double ymax) {
_ymin = ymin;
_ymax = ymax;
}
//void Plot::setYAutoRange(bool yautorange) {
// _yautorange = yautoRange;
//}
void Plot::setXTick(double xtick) {
_xtick = xtick;
}
void Plot::setYTick(double ytick) {
_ytick = ytick;
}
void Plot::setXPrecision(unsigned int xprecision) {
_xprecision = xprecision;
}
void Plot::setYPrecision(unsigned int yprecision) {
_yprecision = yprecision;
}
void Plot::setTitle(String title) {
_title = title;
}
void Plot::setXLabel(String xlabel) {
_xlabel = xlabel;
}
void Plot::setYLabel(String ylabel) {
_ylabel = ylabel;
}
void Plot::setForegroundColor(unsigned int fcolor) {
_fcolor = fcolor;
}
void Plot::setBackroundColor(unsigned int bcolor) {
_bcolor = bcolor;
}
void Plot::clear() {
_haslast = false;
_display->clearDisplay();
drawPlot();
}
// }}}
// Plotting {{{
void Plot::addDataPoint(double x, double y) {
double xg = domainToGraphX(x);
double yg = domainToGraphY(y);
if (_haslast) {
double lastxg = domainToGraphX(_lastx);
double lastyg = domainToGraphY(_lasty);
_display->drawLine(lastxg, lastyg, xg, yg, _fcolor);
} else {
_haslast = true;
_display->drawPixel(xg, yg, _fcolor);
}
_lastx = x;
_lasty = y;
_display->display();
}
void Plot::drawPlot() {
_graphheight = _height - titleHeight() - xLabelHeight() - xValueHeight();
_graphwidth = _width - yValueWidth() - yLabelWidth();
_graphxpos = _xpos + yValueWidth() + yLabelWidth();
_graphypos = _ypos - xLabelHeight() - xValueHeight();
_display->setTextSize(TEXT_SIZE);
_display->setTextColor(_fcolor, _bcolor);
displayTitle();
displayXLabel();
displayYLabel();
displayXValues();
displayYValues();
drawGrid();
_display->display();
}
void Plot::displayTitle() {
if (_title.length() > 0) {
// Title should be drawn center-aligned over the graph
_display->setCursor(_graphxpos + _graphwidth/2 - textpxwidth(_title, TEXT_SIZE)/2,
_graphypos - _graphheight - titleHeight());
_display->println(_title);
}
}
void Plot::displayXLabel() {
if (_xlabel.length() > 0) {
// x-label should be drawn center-aligned below the x values
_display->setCursor(_graphxpos + _graphwidth/2 - textpxwidth(_xlabel, TEXT_SIZE)/2,
_graphypos + xValueHeight());
_display->println(_xlabel);
}
}
void Plot::displayYLabel() {
if (_ylabel.length() > 0) {
_display->setCursor(_xpos, _graphypos - _graphheight/2 - textpxheight(_ylabel, TEXT_SIZE)/2);
_display->println(_ylabel);
}
}
void Plot::displayXValues() {
if (_xtick > 0) {
for (double x = _xmin; x <= _xmax; x += _xtick) {
String value = String(x, _xprecision);
double xg = domainToGraphX(x);
xg = xg - textpxwidth(value, TEXT_SIZE)/2;
xg = max(_graphxpos,
min(_graphxpos + _graphwidth - textpxwidth(value, TEXT_SIZE),
xg));
_display->setCursor(xg, _graphypos + 1);
_display->println(value);
}
}
}
void Plot::displayYValues() {
if (_ytick > 0) {
for (double y = _ymin; y <= _ymax; y += _ytick) {
double yg = domainToGraphY(y);
String value = String(y, _yprecision);
_display->setCursor(_graphxpos - textpxwidth(value, TEXT_SIZE), yg - textpxheight(value, TEXT_SIZE)/2);
_display->println(value);
}
}
}
void Plot::drawGrid() {
if (_ytick > 0) {
// Draw horizontal grid lines
for (double y = _ymin; y <= _ymax; y += _ytick) {
double yg = domainToGraphY(y);
_display->drawLine(_graphxpos, yg, _graphxpos + _graphwidth, yg, _fcolor);
}
}
if (_xtick > 0) {
// Draw vertical grid lines
for (double x = _xmin; x <= _xmax; x += _xtick) {
double xg = domainToGraphX(x);
_display->drawLine((int)xg, _graphypos - _graphheight, (int)xg, _graphypos, _fcolor);
}
}
}
unsigned int Plot::titleHeight() {
if (_title.length() > 0) {
return textpxheight(_title, TEXT_SIZE);
}
return 0;
}
unsigned int Plot::xLabelHeight() {
if (_xlabel.length() > 0) {
return textpxheight(_xlabel, TEXT_SIZE);
}
return 0;
}
unsigned int Plot::yLabelWidth() {
if (_ylabel.length() > 0) {
return textpxwidth(_ylabel, TEXT_SIZE);
}
return 0;
}
unsigned int Plot::xValueHeight() {
unsigned int maxheight = 0;
if (_xtick > 0) {
for (double x = _xmin; x <= _xmax; x += _xtick) {
maxheight = max(maxheight, textpxheight(String(x, _xprecision), TEXT_SIZE) + 1);
}
}
return maxheight;
}
unsigned int Plot::yValueWidth() {
unsigned int maxwidth = 0;
if (_ytick > 0) {
for (double y = _ymin; y <= _ymax; y += _ytick) {
maxwidth = max(maxwidth, textpxwidth(String(y, _yprecision), TEXT_SIZE));
}
}
return maxwidth;
}
// }}}
// Util {{{
double Plot::domainToGraphX(double x) {
return _graphxpos + ((x - _xmin) * _graphwidth / (_xmax - _xmin));
}
double Plot::domainToGraphY(double y) {
return _graphypos - ((y - _ymin) * _graphheight / (_ymax - _ymin));
}
char *Plot::mydtostrf(float f, byte precision) {
return removelpad(dtostrf(f, 30, precision, _floatStrBuf));
}
char *Plot::removelpad(char *str) {
while(isspace((unsigned char)*str)) {
str++;
}
return str;
}
unsigned int Plot::textpxwidth(String text, unsigned int fontsize) {
return text.length() * charpxwidth(fontsize);
}
unsigned int Plot::textpxheight(String text, unsigned int fontsize) {
return min(text.length(), 1) * charpxheight(fontsize);
}
unsigned int Plot::charpxwidth(unsigned int fontsize) {
if (fontsize != 1) {
Serial.println("Error: Char size calculation only implemented for font size of 1");
}
return 6;
}
unsigned int Plot::charpxheight(unsigned int fontsize) {
if (fontsize != 1) {
Serial.println("Error: Char size calculation only implemented for font size of 1");
}
return 8;
}
// }}}
// vim:foldmethod=marker:foldlevel=0