From 24b54912510a9cb9dc0d14b4f7edd752da062d80 Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Thu, 18 Apr 2024 21:51:08 +0800 Subject: Make 'Meter_modes' private and simplify its structure All client codes that access this global `Meter_modes` array have been replaced in previous commits, thus it's no longer necessary to keep this internal information (draw functions, default heights, etc.) public. It was also a bad idea when the client codes need to avoid accessing `Meter_modes[0]` (which is reserved and contains null information) by themselves. Signed-off-by: Kang-Che Sung --- Meter.c | 67 +++++++++++++++++++++++++++++++++-------------------------------- Meter.h | 8 -------- 2 files changed, 34 insertions(+), 41 deletions(-) diff --git a/Meter.c b/Meter.c index fbfc2958..25f559a1 100644 --- a/Meter.c +++ b/Meter.c @@ -26,6 +26,12 @@ in the source distribution for its full text. #define GRAPH_HEIGHT 4 /* Unit: rows (lines) */ +typedef struct MeterMode_ { + Meter_Draw draw; + const char* uiName; + int h; +} MeterMode; + /* Meter drawing modes */ static inline void Meter_displayBuffer(const Meter* this, RichString* out) { @@ -324,37 +330,32 @@ static void LEDMeterMode_draw(Meter* this, int x, int y, int w) { RichString_delete(&out); } -static MeterMode BarMeterMode = { - .uiName = "Bar", - .h = 1, - .draw = BarMeterMode_draw, -}; - -static MeterMode TextMeterMode = { - .uiName = "Text", - .h = 1, - .draw = TextMeterMode_draw, -}; - -static MeterMode GraphMeterMode = { - .uiName = "Graph", - .h = GRAPH_HEIGHT, - .draw = GraphMeterMode_draw, -}; - -static MeterMode LEDMeterMode = { - .uiName = "LED", - .h = 3, - .draw = LEDMeterMode_draw, -}; - -const MeterMode* const Meter_modes[] = { - NULL, - &BarMeterMode, - &TextMeterMode, - &GraphMeterMode, - &LEDMeterMode, - NULL +static const MeterMode Meter_modes[] = { + [0] = { + .uiName = NULL, + .h = 0, + .draw = NULL, + }, + [BAR_METERMODE] = { + .uiName = "Bar", + .h = 1, + .draw = BarMeterMode_draw, + }, + [TEXT_METERMODE] = { + .uiName = "Text", + .h = 1, + .draw = TextMeterMode_draw, + }, + [GRAPH_METERMODE] = { + .uiName = "Graph", + .h = GRAPH_HEIGHT, + .draw = GraphMeterMode_draw, + }, + [LED_METERMODE] = { + .uiName = "LED", + .h = 3, + .draw = LEDMeterMode_draw, + }, }; /* Meter class and methods */ @@ -457,7 +458,7 @@ void Meter_setMode(Meter* this, int modeIndex) { this->drawData.values = NULL; this->drawData.nValues = 0; - const MeterMode* mode = Meter_modes[modeIndex]; + const MeterMode* mode = &Meter_modes[modeIndex]; this->draw = mode->draw; this->h = mode->h; } @@ -467,7 +468,7 @@ void Meter_setMode(Meter* this, int modeIndex) { ListItem* Meter_toListItem(const Meter* this, bool moving) { char mode[20]; if (this->mode) { - xSnprintf(mode, sizeof(mode), " [%s]", Meter_modes[this->mode]->uiName); + xSnprintf(mode, sizeof(mode), " [%s]", Meter_modes[this->mode].uiName); } else { mode[0] = '\0'; } diff --git a/Meter.h b/Meter.h index a587e4b5..1a16d19a 100644 --- a/Meter.h +++ b/Meter.h @@ -117,12 +117,6 @@ struct Meter_ { void* meterData; }; -typedef struct MeterMode_ { - Meter_Draw draw; - const char* uiName; - int h; -} MeterMode; - typedef enum { /* Meter mode 0 is reserved */ BAR_METERMODE = 1, @@ -155,8 +149,6 @@ void Meter_setMode(Meter* this, int modeIndex); ListItem* Meter_toListItem(const Meter* this, bool moving); -extern const MeterMode* const Meter_modes[]; - extern const MeterClass BlankMeter_class; #endif -- cgit v1.2.3