Skip to content

Commit c7c0cda

Browse files
committed
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]>
1 parent 90ab31f commit c7c0cda

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

Meter.c

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

33-
#define GRAPH_HEIGHT 4 /* Unit: rows (lines) */
33+
#define DEFAULT_GRAPH_HEIGHT 4 /* Unit: rows (lines) */
3434

3535
typedef struct MeterMode_ {
3636
Meter_Draw draw;
@@ -247,6 +247,8 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
247247
w -= captionLen;
248248

249249
// Prepare parameters for drawing
250+
assert(this->h >= 1);
251+
int h = this->h;
250252
uint8_t maxItems = Meter_maxItems(this);
251253
bool isPercentChart = Meter_isPercentChart(this);
252254
GraphData* data = &this->drawData;
@@ -288,7 +290,7 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
288290
goto end;
289291
}
290292

291-
bool needsScaleDisplay = maxItems > 0 && GRAPH_HEIGHT >= 2;
293+
bool needsScaleDisplay = maxItems > 0 && h >= 2;
292294
if (needsScaleDisplay) {
293295
move(y + 1, x); // Cursor position for printing the scale
294296
}
@@ -342,14 +344,14 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
342344

343345
// Draw the actual graph
344346
for (int col = 0; i < nValues - 1; i += 2, col++) {
345-
int pix = GraphMeterMode_pixPerRow * GRAPH_HEIGHT;
347+
int pix = GraphMeterMode_pixPerRow * h;
346348
int v1 = (int) lround(CLAMP(data->values[i] / total * pix, 1.0, pix));
347349
int v2 = (int) lround(CLAMP(data->values[i + 1] / total * pix, 1.0, pix));
348350

349351
int colorIdx = GRAPH_1;
350-
for (int line = 0; line < GRAPH_HEIGHT; line++) {
351-
int line1 = CLAMP(v1 - (GraphMeterMode_pixPerRow * (GRAPH_HEIGHT - 1 - line)), 0, GraphMeterMode_pixPerRow);
352-
int line2 = CLAMP(v2 - (GraphMeterMode_pixPerRow * (GRAPH_HEIGHT - 1 - line)), 0, GraphMeterMode_pixPerRow);
352+
for (int line = 0; line < h; line++) {
353+
int line1 = CLAMP(v1 - (GraphMeterMode_pixPerRow * (h - 1 - line)), 0, GraphMeterMode_pixPerRow);
354+
int line2 = CLAMP(v2 - (GraphMeterMode_pixPerRow * (h - 1 - line)), 0, GraphMeterMode_pixPerRow);
353355

354356
attrset(CRT_colors[colorIdx]);
355357
mvaddstr(y + line, x + col, GraphMeterMode_dots[line1 * (GraphMeterMode_pixPerRow + 1) + line2]);
@@ -463,7 +465,7 @@ static const MeterMode Meter_modes[] = {
463465
},
464466
[GRAPH_METERMODE] = {
465467
.uiName = "Graph",
466-
.h = GRAPH_HEIGHT,
468+
.h = DEFAULT_GRAPH_HEIGHT,
467469
.draw = GraphMeterMode_draw,
468470
},
469471
[LED_METERMODE] = {

0 commit comments

Comments
 (0)