Select one of the symbols to view example projects that use it.
 
Outline
#include <gui/graph_screen/AbstractGraph.hpp>
AbstractGraph::AbstractGraph()
AbstractGraph::~AbstractGraph()
AbstractGraph::widgetX(int) const
AbstractGraph::widgetY(int) const
AbstractGraph::widgetXindex(int) const
AbstractGraph::widgetYindex(int) const
AbstractGraph::setBuffer(GraphPoint *, int)
AbstractGraph::setLinkedGraph(AbstractGraph &)
AbstractGraph::setRange(int, int, int, int)
AbstractGraph::setX(int16_t)
AbstractGraph::setY(int16_t)
AbstractGraph::setWidth(int16_t)
AbstractGraph::setHeight(int16_t)
AbstractGraph::clearGraph()
AbstractGraph::addValue(int, int)
AbstractGraph::deleteValue(int)
AbstractGraph::drawCanvasWidget(const Rect &) const
AbstractGraph::invalidateRectContainingIndices(int, int) const
AbstractGraph::updateAndInvalidateCacheForLinesBetweenIndeces(int, int)
AbstractGraph::getMinimalRect() const
AbstractGraph::setNumPoints(int)
AbstractGraph::invalidateLineFromIndex(int)
AbstractGraph::insertPointAtIndex(int)
AbstractGraph::deletePointAtIndex(int)
AbstractGraph::findX(int)
AbstractGraph::assignRange(int, int, int, int)
AbstractGraph::assignWidth(int16_t)
AbstractGraph::assignHeight(int16_t)
Files
loading...
CodeScopeSTM32 Libraries and SamplesTouchGFXGui/gui/src/graph_screen/AbstractGraph.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * This file is part of the TouchGFX 4.10.0 distribution. * * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ #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) { // Point added at end of graph line 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) { // Point with given X not already in graph line, insert it invalidateLineFromIndex(index); insertPointAtIndex(index); points[index].x = x; points[index].y = y; updateAndInvalidateCacheForLinesBetweenIndeces(index - 1, index + 1); }if (points[index].x > x) { ... } else // (points[index].x == x) { // Point with given X already exists in graph line, replace it (unless the // exact same point was already there) if (points[index].y != y) { // New Y value, so update the point on the graph line invalidateLineFromIndex(index-1); invalidateLineFromIndex(index); points[index].x = x; // Actually not necessary 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) { // Point with given X found in graph line 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) { // A graph line with a single (or not even a single) point is invisible return true; }if (numPoints < 2) { ... } int firstIndex = 0; int lastIndex = numPoints - 1; // We can skip the line from index if the left part of the line at index+1 is to the left of the invalidated area while (firstIndex < lastIndex && invalidatedArea.x > (widgetXindex(firstIndex+1) + lineWidth/2).to<int>()) { firstIndex++; }while (firstIndex < lastIndex && invalidatedArea.x > (widgetXindex(firstIndex+1) + lineWidth/2).to()) { ... } // We can skip the line from index if the left part of the line at index-1 is to the right of the invalidated area 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) { // No width or no height return; }if (getWidth() == 0 || getHeight() == 0 || left == right ||top == bottom) { ... } // Calculate the slopes of all lines 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) // Should not happen, but if range is zero high/wide it does { 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"); // Allow insert at first position outside array 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) { // No points, or x larger than all existing x coordinates if (numPoints == 0 || x > points[numPoints-1].x) { return numPoints; }if (numPoints == 0 || x > points[numPoints-1].x) { ... } // Setup search limits int loIndex = 0; int hiIndex = numPoints-1; while (loIndex <= hiIndex) { int midIndex = (loIndex + hiIndex) / 2; int midX = points[midIndex].x; if (x < midX) { // Search lower half hiIndex = midIndex - 1; }if (x < midX) { ... } else if (x > midX) { // Search higher half loIndex = midIndex + 1; }else if (x > midX) { ... } else { // Found return midIndex; }else { ... } }while (loIndex <= hiIndex) { ... } // Return first index over wanted x coordinate 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) { ... } }{ ... }