From 85863358fbdccf06896eab6663d6f0e9259a07ce Mon Sep 17 00:00:00 2001 From: Max Paperno Date: Tue, 30 May 2017 12:37:38 -0400 Subject: [PATCH] [Horus] Outputs widget: Fix bar graph overflow with extended limits (#4969) * [Horus][Widgets] Outputs widget: Fix bar graph overflow with extended limits (closes #4968); Show standard/translatable source names (CHxx) when no custom name is defined; More const-ness, less math. * [OutputsWidget] Yet more const-ness and less calculation repetition. --- radio/src/gui/480x272/widgets/outputs.cpp | 54 +++++++++++------------ 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/radio/src/gui/480x272/widgets/outputs.cpp b/radio/src/gui/480x272/widgets/outputs.cpp index 1b649b4e7..1365e4f72 100644 --- a/radio/src/gui/480x272/widgets/outputs.cpp +++ b/radio/src/gui/480x272/widgets/outputs.cpp @@ -21,9 +21,10 @@ #include "opentx.h" #define RECT_BORDER 1 -#define RECT_WIDTH (w - RECT_BORDER * 2) #define ROW_HEIGHT 17 +#define VIEW_CHANNELS_LIMIT_PCT (g_model.extendedLimits ? LIMIT_EXT_PERCENT : 100) + class OutputsWidget: public Widget { public: @@ -34,40 +35,37 @@ class OutputsWidget: public Widget virtual void refresh(); - uint8_t drawChannels(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t firstChan, bool bg_shown, uint16_t bg_color) + uint8_t drawChannels(const uint16_t & x, const uint16_t & y, const uint16_t & w, const uint16_t & h, const uint8_t & firstChan, const bool & bg_shown, const uint16_t & bg_color) { - char chanString[] = "CH32"; - uint8_t numChan = h / ROW_HEIGHT; - uint8_t row_height = (h - numChan * ROW_HEIGHT >= numChan ? ROW_HEIGHT + 1 : ROW_HEIGHT); - uint8_t lastChan = firstChan + numChan; + const uint8_t numChan = h / ROW_HEIGHT; + const uint8_t lastChan = firstChan + numChan; + const uint8_t rowH = (h - numChan * ROW_HEIGHT >= numChan ? ROW_HEIGHT + 1 : ROW_HEIGHT); + const uint16_t barW = w - RECT_BORDER * 2; + const uint8_t barH = rowH - RECT_BORDER; + const uint16_t barLft = x + RECT_BORDER; + const uint16_t barMid = barLft + barW / 2; + + for (uint8_t curChan = firstChan; curChan < lastChan && curChan <= MAX_OUTPUT_CHANNELS; curChan++) { + const int16_t chanVal = calcRESXto100(channelOutputs[curChan-1]); + const uint16_t rowTop = y + (curChan - firstChan) * rowH; + const uint16_t barTop = rowTop + RECT_BORDER; + const uint16_t fillW = divRoundClosest(barW * limit(0, abs(chanVal), VIEW_CHANNELS_LIMIT_PCT), VIEW_CHANNELS_LIMIT_PCT * 2); - for (uint8_t curChan = firstChan; curChan < lastChan && curChan < 33; curChan++) { - int16_t chanVal = calcRESXto100(channelOutputs[curChan-1]); if (bg_shown) { lcdSetColor(bg_color); - lcdDrawSolidFilledRect(x + RECT_BORDER, y + RECT_BORDER + (curChan - firstChan) * row_height, RECT_WIDTH , row_height - RECT_BORDER, CUSTOM_COLOR); + lcdDrawSolidFilledRect(barLft, barTop, barW , barH, CUSTOM_COLOR); } - if (chanVal > 0) { - lcdDrawSolidFilledRect(x + RECT_BORDER + RECT_WIDTH / 2, y + RECT_BORDER + (curChan -firstChan) * row_height, divRoundClosest(RECT_WIDTH * chanVal, 200), row_height - RECT_BORDER, MAINVIEW_GRAPHICS_COLOR); + if (fillW) + lcdDrawSolidFilledRect((chanVal > 0 ? barMid : barMid - fillW), barTop, fillW, barH, MAINVIEW_GRAPHICS_COLOR); + lcd->drawSolidVerticalLine(barMid, barTop, barH, MAINVIEW_GRAPHICS_COLOR); + lcdDrawRect(x, rowTop, w, rowH + 1); + lcdDrawNumber(x + barW - 10, barTop, chanVal, SMLSIZE | TEXT_COLOR | RIGHT, 0, NULL, "%"); + if (g_model.limitData[curChan - 1].name[0] != 0) { + lcdDrawNumber(barLft + 1, barTop, curChan, SMLSIZE | TEXT_COLOR | LEFT | LEADING0, 2); + lcdDrawSizedText(barLft + 23, barTop, g_model.limitData[curChan - 1].name, sizeof(g_model.limitData[curChan - 1].name), SMLSIZE | TEXT_COLOR | LEFT | ZCHAR); } - else if (chanVal < 0) { - uint16_t startpoint = x + RECT_BORDER; - uint16_t endpoint = startpoint + RECT_WIDTH / 2; - uint16_t size = divRoundClosest(- RECT_WIDTH * chanVal, 200); - lcdDrawSolidFilledRect(endpoint - size, y + RECT_BORDER + (curChan - firstChan) * row_height, size, row_height - RECT_BORDER, MAINVIEW_GRAPHICS_COLOR); - } - lcd->drawSolidVerticalLine(x + RECT_BORDER + RECT_WIDTH / 2, y + RECT_BORDER + (curChan - firstChan) * row_height, row_height - RECT_BORDER, MAINVIEW_GRAPHICS_COLOR); - lcdDrawRect(x, y + (curChan - firstChan) * row_height, w, row_height+1); - lcdDrawNumber(x + RECT_WIDTH - 10, y + (curChan - firstChan) * row_height + 1, chanVal, SMLSIZE | TEXT_COLOR | RIGHT, 0, NULL, "%"); - if (g_model.limitData[curChan - 1].name[0] != 0) { - strAppendSigned(chanString, curChan, 2); - lcdDrawText(x + 2, y + (curChan - firstChan) * row_height + 1, chanString, SMLSIZE | TEXT_COLOR | LEFT); - lcdDrawSizedText(x + 25, y + (curChan - firstChan) * row_height + 1, g_model.limitData[curChan - 1].name, sizeof(g_model.limitData[curChan - 1].name), SMLSIZE | TEXT_COLOR | LEFT | ZCHAR); - } else { - strAppend(chanString, "CH"); - strAppendSigned(&chanString[2], curChan, 2); - lcdDrawText(x + 2, y + (curChan - firstChan) * row_height + 1, chanString, SMLSIZE | TEXT_COLOR | LEFT); + putsChn(barLft + 1, barTop, curChan, SMLSIZE | TEXT_COLOR | LEFT); } } return lastChan - 1;