1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
28
29
32
33
34
41
42
43
50
51
52
55
56
57
60
61
71
72
87
88
100
101
109
110
118
119
127
128
136
137
145
146
147
148
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
202
203
204
208
209
210
211
212
213
214
215
220
221
222
223
224
225
230
231
236
237
238
239
240
241
246
247
248
249
253
254
255
256
260
261
269
270
271
272
273
274
275
284
285
286
287
291
295
299
300
305
306
307
308
309
310
311
312
313
314
318
319
320
321
322
323
324
331
332
340
341
349
350
360
361
370
371
372
373
374
378
379
380
381
382
383
384
385
386
391
396
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
420
421
422
431
432
/* ... */
#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);
}if (graphWidth == 0) { ... }
return CWRUtil::toQ5(x - left) * widgetWidth / graphWidth;
...}
CWRUtil::Q5 AbstractGraph::widgetY(int y) const
{
if (graphHeight == 0)
{
return CWRUtil::toQ5(0);
}if (graphHeight == 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 newLleft, int newRight, int newTop, int newBottom)
{
if (left != newLleft || right != newRight || top != newTop || bottom != newBottom)
{
assignRange(newLleft, newRight, newTop, newBottom);
if (numPoints > 0)
{
updateAndInvalidateCacheForLinesBetweenIndeces(0, numPoints - 1);
}if (numPoints > 0) { ... }
}if (left != newLleft || 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 (firstIndex < 0) { ... }
if (lastIndex >= numPoints)
{
lastIndex = numPoints - 1;
}if (lastIndex >= numPoints) { ... }
if (firstIndex > lastIndex)
{
return;
}if (firstIndex > lastIndex) { ... }
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) { ... }
}{ ... }