Skip to content

Commit 1cf2e67

Browse files
Explorer09BenBE
andcommitted
Use "h" property as height when drawing Graph meter
This is a code quality change that avoids dependency on the hard-coded GRAPH_HEIGHT in GraphMeterMode_draw(). This doesn't enable variable graph heights per meter, but it makes room for implementing such feature. Signed-off-by: Kang-Che Sung <[email protected]> Co-Authored-By: Benny Baumann <[email protected]>
1 parent 36ea1ef commit 1cf2e67

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Meter.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ in the source distribution for its full text.
2929
#define UINT32_WIDTH 32
3030
#endif
3131

32-
#define GRAPH_HEIGHT 4 /* Unit: rows (lines) */
32+
#define DEFAULT_GRAPH_HEIGHT 4 /* Unit: rows (lines) */
3333

3434
typedef struct MeterMode_ {
3535
Meter_Draw draw;
@@ -215,6 +215,9 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
215215
}
216216
w -= captionLen;
217217

218+
assert(this->h >= 1);
219+
int h = this->h;
220+
218221
GraphData* data = &this->drawData;
219222

220223
// Expand the graph data buffer if necessary
@@ -276,15 +279,15 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
276279

277280
// Draw the actual graph
278281
for (int col = 0; i < nValues - 1; i += 2, col++) {
279-
int pix = GraphMeterMode_pixPerRow * GRAPH_HEIGHT;
282+
int pix = GraphMeterMode_pixPerRow * h;
280283
double total = MAXIMUM(this->total, 1);
281284
int v1 = (int) lround(CLAMP(data->values[i] / total * pix, 1.0, pix));
282285
int v2 = (int) lround(CLAMP(data->values[i + 1] / total * pix, 1.0, pix));
283286

284287
int colorIdx = GRAPH_1;
285-
for (int line = 0; line < GRAPH_HEIGHT; line++) {
286-
int line1 = CLAMP(v1 - (GraphMeterMode_pixPerRow * (GRAPH_HEIGHT - 1 - line)), 0, GraphMeterMode_pixPerRow);
287-
int line2 = CLAMP(v2 - (GraphMeterMode_pixPerRow * (GRAPH_HEIGHT - 1 - line)), 0, GraphMeterMode_pixPerRow);
288+
for (int line = 0; line < h; line++) {
289+
int line1 = CLAMP(v1 - (GraphMeterMode_pixPerRow * (h - 1 - line)), 0, GraphMeterMode_pixPerRow);
290+
int line2 = CLAMP(v2 - (GraphMeterMode_pixPerRow * (h - 1 - line)), 0, GraphMeterMode_pixPerRow);
288291

289292
attrset(CRT_colors[colorIdx]);
290293
mvaddstr(y + line, x + col, GraphMeterMode_dots[line1 * (GraphMeterMode_pixPerRow + 1) + line2]);
@@ -398,7 +401,7 @@ static const MeterMode Meter_modes[] = {
398401
},
399402
[GRAPH_METERMODE] = {
400403
.uiName = "Graph",
401-
.h = GRAPH_HEIGHT,
404+
.h = DEFAULT_GRAPH_HEIGHT,
402405
.draw = GraphMeterMode_draw,
403406
},
404407
[LED_METERMODE] = {

0 commit comments

Comments
 (0)