1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
27
28
31
32
33
37
38
39
43
44
45
48
49
50
53
54
64
65
80
81
93
94
102
103
111
112
120
121
129
130
138
139
140
141
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
195
196
197
201
202
203
204
205
206
207
208
213
214
215
216
217
218
223
224
229
230
231
232
233
234
239
240
241
242
246
247
248
249
253
254
262
263
264
265
266
267
268
277
278
279
280
281
282
283
284
289
290
291
292
293
294
295
296
297
298
302
303
304
305
306
307
308
315
316
324
325
333
334
344
345
354
355
356
357
358
362
363
364
365
366
367
368
369
370
375
380
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
404
405
406
415
416
/* ... */
#include <gui/graph_screen/AbstractGraph.hpp>
using namespace touchgfx;
AbstractGraph::AbstractGraph() :
CanvasWidget(), linkedGraph(0), left(-1), right(-1), top(-1), bottom(-1), points(0), maxPoints(0), lineWidth(0)
{
clearGraph();
}{ ... }
AbstractGraph::~AbstractGraph()
{
}{ ... }
CWRUtil::Q5 AbstractGraph::widgetX(int x) const
{
if (graphWidth == 0) return CWRUtil::toQ5(0);
return CWRUtil::toQ5(x - left) * widgetWidth / graphWidth;
...}
CWRUtil::Q5 AbstractGraph::widgetY(int y) const
{
if (graphHeight == 0) return CWRUtil::toQ5(0);
return CWRUtil::toQ5(y - top) * widgetHeight / graphHeight;
...}
CWRUtil::Q5 AbstractGraph::widgetXindex(int index) const
{
return widgetX(points[index].x);
...}
CWRUtil::Q5 AbstractGraph::widgetYindex(int index) const
{
return widgetY(points[index].y);
...}
void AbstractGraph::setBuffer(GraphPoint *pointsBuffer, int numPointsInBuffer)
{
points = pointsBuffer;
maxPoints = numPointsInBuffer;
numPoints = 0;
if (linkedGraph)
{
linkedGraph->setBuffer(pointsBuffer, numPointsInBuffer);
}if (linkedGraph) { ... }
}{ ... }
void AbstractGraph::setLinkedGraph(AbstractGraph &other)
{
if (linkedGraph == &other)
{
return;
}if (linkedGraph == &other) { ... }
if (linkedGraph != 0)
{
linkedGraph->setLinkedGraph(other);
}if (linkedGraph != 0) { ... }
else
{
linkedGraph = &other;
}else { ... }
}{ ... }
void AbstractGraph::setRange(int newLeft, int newRight, int newTop, int newBottom)
{
if (left != newLeft || right != newRight || top != newTop || bottom != newBottom)
{
assignRange(newLeft, newRight, newTop, newBottom);
if (numPoints > 0)
{
updateAndInvalidateCacheForLinesBetweenIndeces(0, numPoints - 1);
}if (numPoints > 0) { ... }
}if (left != newLeft || right != newRight || top != newTop || bottom != newBottom) { ... }
}{ ... }
void AbstractGraph::setX(int16_t x)
{
CanvasWidget::setX(x);
if (linkedGraph)
{
linkedGraph->setX(x);
}if (linkedGraph) { ... }
}{ ... }
void AbstractGraph::setY(int16_t y)
{
CanvasWidget::setY(y);
if (linkedGraph)
{
linkedGraph->setY(y);
}if (linkedGraph) { ... }
}{ ... }
void AbstractGraph::setWidth(int16_t width)
{
assignWidth(width);
if (numPoints > 0)
{
updateAndInvalidateCacheForLinesBetweenIndeces(0, numPoints - 1);
}if (numPoints > 0) { ... }
}{ ... }
void AbstractGraph::setHeight(int16_t height)
{
assignHeight(height);
if (numPoints > 0)
{
updateAndInvalidateCacheForLinesBetweenIndeces(0, numPoints - 1);
}if (numPoints > 0) { ... }
}{ ... }
void AbstractGraph::clearGraph()
{
numPoints = 0;
if (linkedGraph)
{
linkedGraph->clearGraph();
}if (linkedGraph) { ... }
}{ ... }
bool AbstractGraph::addValue(int x, int y)
{
if (numPoints >= maxPoints)
{
return false;
}if (numPoints >= maxPoints) { ... }
if (numPoints == 0 || x > points[numPoints-1].x)
{
points[numPoints].x = x;
points[numPoints].y = y;
setNumPoints(numPoints + 1);
updateAndInvalidateCacheForLinesBetweenIndeces(numPoints - 2, numPoints - 1);
}if (numPoints == 0 || x > points[numPoints-1].x) { ... }
else
{
int index = findX(x);
assert(points[index].x >= x && "Bug in binary search algorithm");
if (points[index].x > x)
{
invalidateLineFromIndex(index);
insertPointAtIndex(index);
points[index].x = x;
points[index].y = y;
updateAndInvalidateCacheForLinesBetweenIndeces(index - 1, index + 1);
}if (points[index].x > x) { ... }
else
{
if (points[index].y != y)
{
invalidateLineFromIndex(index-1);
invalidateLineFromIndex(index);
points[index].x = x;
points[index].y = y;
updateAndInvalidateCacheForLinesBetweenIndeces(index - 1, index + 1);
}if (points[index].y != y) { ... }
}else { ... }
}else { ... }
return true;
}{ ... }
bool AbstractGraph::deleteValue(int x)
{
int index = findX(x);
if (index < numPoints && points[index].x == x)
{
if (index > 0)
{
invalidateLineFromIndex(index-1);
}if (index > 0) { ... }
invalidateLineFromIndex(index);
deletePointAtIndex(index);
if (numPoints > 0)
{
updateAndInvalidateCacheForLinesBetweenIndeces(MAX(0, index - 1), index);
}if (numPoints > 0) { ... }
return true;
}if (index < numPoints && points[index].x == x) { ... }
return false;
}{ ... }
bool AbstractGraph::drawCanvasWidget(const Rect& invalidatedArea) const
{
if (numPoints < 2)
{
return true;
}if (numPoints < 2) { ... }
int firstIndex = 0;
int lastIndex = numPoints - 1;
while (firstIndex < lastIndex &&
invalidatedArea.x > (widgetXindex(firstIndex+1) + lineWidth/2).to<int>())
{
firstIndex++;
}while (firstIndex < lastIndex && invalidatedArea.x > (widgetXindex(firstIndex+1) + lineWidth/2).to()) { ... }
while (lastIndex > firstIndex &&
invalidatedArea.right() < (widgetXindex(lastIndex-1) - lineWidth/2).to<int>())
{
lastIndex--;
}while (lastIndex > firstIndex && invalidatedArea.right() < (widgetXindex(lastIndex-1) - lineWidth/2).to()) { ... }
if (firstIndex < lastIndex)
{
Canvas canvas(this, invalidatedArea);
canvas.moveTo(xAboveOutgoing(firstIndex ), yAboveOutgoing(firstIndex ));
canvas.lineTo(xAboveIncoming(firstIndex+1), yAboveIncoming(firstIndex+1));
for (int index = firstIndex + 1; index < lastIndex; index++)
{
canvas.lineTo(xAboveOutgoing(index ), yAboveOutgoing(index ));
canvas.lineTo(xAboveIncoming(index+1), yAboveIncoming(index+1));
}for (int index = firstIndex + 1; index < lastIndex; index++) { ... }
if (lineWidth == 0)
{
CWRUtil::Q5 xBelow = xBelowIncoming(lastIndex);
if (xBelow > xAboveIncoming(lastIndex))
{
canvas.lineTo(xBelow, yBelowIncoming(lastIndex));
}if (xBelow > xAboveIncoming(lastIndex)) { ... }
canvas.lineTo(xAboveIncoming(lastIndex), widgetY(bottom));
canvas.lineTo(xAboveOutgoing(firstIndex), widgetY(bottom));
xBelow = xBelowOutgoing(firstIndex);
if (xBelow < xAboveOutgoing(firstIndex))
{
canvas.lineTo(xBelow, yBelowOutgoing(firstIndex));
}if (xBelow < xAboveOutgoing(firstIndex)) { ... }
}if (lineWidth == 0) { ... }
else
{
for (int index = lastIndex-1; index >= firstIndex; index--)
{
canvas.lineTo(xBelowIncoming(index+1), yBelowIncoming(index+1));
canvas.lineTo(xBelowOutgoing(index ), yBelowOutgoing(index ));
}for (int index = lastIndex-1; index >= firstIndex; index--) { ... }
}else { ... }
return canvas.render();
}if (firstIndex < lastIndex) { ... }
return true;
...}
void AbstractGraph::invalidateRectContainingIndices(int firstIndex, int lastIndex) const
{
Rect minimalRect = getMinimalRectContainingIndices(firstIndex, lastIndex);
invalidateRect(minimalRect);
if (linkedGraph)
{
linkedGraph->invalidateRectContainingIndices(firstIndex, lastIndex);
}if (linkedGraph) { ... }
...}
void AbstractGraph::updateAndInvalidateCacheForLinesBetweenIndeces(int firstIndex, int lastIndex)
{
if (firstIndex < 0) firstIndex = 0;
if (lastIndex >= numPoints) lastIndex = numPoints - 1;
if (firstIndex > lastIndex) return;
if (getWidth() == 0 || getHeight() == 0 || left == right ||top == bottom)
{
return;
}if (getWidth() == 0 || getHeight() == 0 || left == right ||top == bottom) { ... }
for (int index = firstIndex; index < lastIndex; index++)
{
CWRUtil::Q5 dy = (widgetYindex(index+1) - widgetYindex(index));
CWRUtil::Q5 dx = (widgetXindex(index+1) - widgetXindex(index));
CWRUtil::Q5 d = CWRUtil::sqrtQ10(dy*dy+dx*dx);
if (d != 0)
{
points[index].dy = (lineWidth * dy / d) / 2;
points[index].dx = (lineWidth * dx / d) / 2;
...}
}for (int index = firstIndex; index < lastIndex; index++) { ... }
invalidateRectContainingIndices(firstIndex, lastIndex);
}{ ... }
Rect AbstractGraph::getMinimalRect() const
{
if (numPoints < 1 || graphWidth == 0 || graphHeight == 0)
{
return Rect(0,0,0,0);
}if (numPoints < 1 || graphWidth == 0 || graphHeight == 0) { ... }
return getMinimalRectContainingIndices(0, numPoints-1);
...}
void AbstractGraph::setNumPoints(int numPointsUsed)
{
numPoints = numPointsUsed;
if (linkedGraph)
{
linkedGraph->setNumPoints(numPointsUsed);
}if (linkedGraph) { ... }
}{ ... }
void AbstractGraph::invalidateLineFromIndex(int index)
{
invalidateRectContainingIndices(index, index + 1);
if (linkedGraph)
{
linkedGraph->invalidateLineFromIndex(index);
}if (linkedGraph) { ... }
}{ ... }
void AbstractGraph::insertPointAtIndex(int index)
{
assert(numPoints < maxPoints && "Unable to insert more points in array");
assert(index >= 0 && index <= numPoints && "Illegal index");
for (int i = numPoints; i > index; i--)
{
points[i] = points[i - 1];
}for (int i = numPoints; i > index; i--) { ... }
setNumPoints(numPoints + 1);
}{ ... }
void AbstractGraph::deletePointAtIndex(int index)
{
assert(index >= 0 && index < numPoints && "Illegal index");
setNumPoints(numPoints - 1);
for (int i = index; i < numPoints; i++)
{
points[i] = points[i + 1];
}for (int i = index; i < numPoints; i++) { ... }
}{ ... }
int AbstractGraph::findX(int x)
{
if (numPoints == 0 || x > points[numPoints-1].x)
{
return numPoints;
}if (numPoints == 0 || x > points[numPoints-1].x) { ... }
int loIndex = 0;
int hiIndex = numPoints-1;
while (loIndex <= hiIndex)
{
int midIndex = (loIndex + hiIndex) / 2;
int midX = points[midIndex].x;
if (x < midX)
{
hiIndex = midIndex - 1;
}if (x < midX) { ... }
else if (x > midX)
{
loIndex = midIndex + 1;
}else if (x > midX) { ... }
else
{
return midIndex;
}else { ... }
}while (loIndex <= hiIndex) { ... }
return loIndex;
}{ ... }
void AbstractGraph::assignRange(int newLeft, int newRight, int newTop, int newBottom)
{
left = newLeft;
right = newRight;
top = newTop;
bottom = newBottom;
graphWidth = CWRUtil::toQ5(newRight - newLeft);
graphHeight = CWRUtil::toQ5(newBottom - newTop);
if (linkedGraph)
{
linkedGraph->assignRange(newLeft, newRight, newTop, newBottom);
}if (linkedGraph) { ... }
}{ ... }
void AbstractGraph::assignWidth(int16_t width)
{
CanvasWidget::setWidth(width);
widgetWidth = CWRUtil::toQ5(getWidth());
if (linkedGraph)
{
linkedGraph->assignWidth(width);
}if (linkedGraph) { ... }
}{ ... }
void AbstractGraph::assignHeight(int16_t height)
{
CanvasWidget::setHeight(height);
widgetHeight = CWRUtil::toQ5(getHeight());
if (linkedGraph)
{
linkedGraph->assignHeight(height);
}if (linkedGraph) { ... }
}{ ... }