-
Notifications
You must be signed in to change notification settings - Fork 16
/
Plot.cpp
211 lines (180 loc) · 4.4 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
#include "SSD1306Wire.h"
#include "Plot.h"
PlotClass::PlotClass(SSD1306Wire *disp, const char *name, uint32_t groupSize, uint32_t groups)
{
Display = disp;
strncpy(Name, name, 32);
GroupSize = groupSize;
Groups = groups;
ValueCount = GroupSize * Groups;
Values = new float[ValueCount];
}
void PlotClass::AddSample(float value)
{
if(Entries < ValueCount)
{
Values[StartPos + Entries] = value;
Entries++;
}
else
{
Values[StartPos] = value;
StartPos = (StartPos + 1) % (ValueCount);
}
}
float PlotClass::GetSample(int pos)
{
if(pos >= ValueCount)
{
return 0;
}
return Values[pos];
}
void PlotClass::SetSample(int pos, float value)
{
if(pos >= ValueCount)
{
return;
}
Values[pos] = value;
if(pos >= Entries)
{
Entries = pos + 1;
}
}
void PlotClass::Clear()
{
StartPos = 0;
Entries = 0;
}
void PlotClass::CalcStats(float *minValue, float *maxValue, bool scatter)
{
*minValue = 999999;
*maxValue = -999999;
if(scatter)
{
for(uint32_t pos = 0; pos < Entries; pos++)
{
float value = Values[(StartPos + pos) % (ValueCount)];
if(value < *minValue)
{
*minValue = value;
}
if(value > *maxValue)
{
*maxValue = value;
}
}
}
else
{
for(uint32_t pos = 0; pos < Entries; pos += GroupSize)
{
float avgValue = 0;
int entries = 0;
for(uint32_t grp = 0; grp < GroupSize; grp++)
{
if(pos + grp < Entries)
{
avgValue += Values[(StartPos + pos + grp) % (ValueCount)];
entries++;
}
}
avgValue /= entries;
if(avgValue < *minValue)
{
*minValue = avgValue;
}
if(avgValue > *maxValue)
{
*maxValue = avgValue;
}
}
}
float delta = *maxValue - *minValue;
float extra = delta/5;
*maxValue += extra;
*minValue -= extra;
}
void PlotClass::DrawGrid(int x, int y, int w, int h, int *xStart, int *yStart, int *xSpan, int *ySpan)
{
Display->drawLine(x+2, y, x+2, y+h-1-2);
Display->drawLine(x+2, y, x, y+2);
Display->drawLine(x+2, y+h-1-2, x+w-1, y+h-1-2);
Display->drawLine(x+w-1, y+h-1-2, x+w-1-2, y+h-1);
*xStart = x+4;
*yStart = y;
*xSpan = w-4;
*ySpan = h-4;
}
void PlotClass::DrawFullPlot(int x, int y, int w, int h, bool scatter, bool desc)
{
float maxValue = 0;
float minValue = 0;
CalcStats(&minValue, &maxValue, scatter);
int xStart = 0;
int yStart = 0;
int xSpan = 0;
int ySpan = 0;
DrawGrid(x, y, w, h, &xStart, &yStart, &xSpan, &ySpan);
if(scatter)
{
for(uint32_t pos = 0; pos < Entries; pos++)
{
float value = Values[(StartPos + pos) % (ValueCount)];
float relValue = (value - minValue) / (maxValue - minValue);
uint32_t xPos = xStart + ((float)pos / GroupSize) * xSpan / (float)Groups;
uint32_t yPos = yStart + ySpan - relValue * ySpan;
Display->setPixel(xPos, yPos);
}
}
else
{
for(uint32_t pos = 0; pos < Entries; pos += GroupSize)
{
float avgValue = 0;
int entries = 0;
for(uint32_t grp = 0; grp < GroupSize; grp++)
{
if(pos + grp < Entries)
{
avgValue += Values[(StartPos + pos + grp) % (ValueCount)];
entries++;
}
}
avgValue /= entries;
float relValue = (avgValue - minValue) / (maxValue - minValue);
uint32_t xPos = xStart + ((float)pos / GroupSize) * xSpan / (float)Groups;
uint32_t yPos = yStart + ySpan - relValue * ySpan;
Display->setPixel(xPos, yPos);
}
}
char msg[33];
Display->setFont(ArialMT_Plain_10);
snprintf(msg, 32, "%2.2f", maxValue);
/* draw a black halo around the bright text */
Display->setColor(BLACK);
for(int mod = 0; mod <9; mod++)
{
Display->drawString(x+8 -1 + mod/3, y - 1 + mod %3, msg);
}
Display->setColor(WHITE);
Display->drawString(x+8, y, msg);
snprintf(msg, 32, "%2.2f", minValue);
/* draw a black halo around the bright text */
Display->setColor(BLACK);
for(int mod = 0; mod <9; mod++)
{
Display->drawString(x+w+4 -1 + mod/3, y+4 - 1 + mod %3, msg);
}
Display->setColor(WHITE);
Display->drawString(x+8, y+ySpan-10, msg);
if(desc)
{
Display->setFont(ArialMT_Plain_16);
Display->drawString(x+w+4, y+4, Name);
Display->setFont(ArialMT_Plain_10);
snprintf(msg, 32, "(# %d)", Entries);
Display->drawString(x+w+4, y+16+4, msg);
}
}