From 0d8545d6f8e2d18b4a431b738053700d7fe2bb0b Mon Sep 17 00:00:00 2001 From: Scott Allen Date: Thu, 25 Sep 2014 12:48:21 -0400 Subject: [PATCH 1/3] Changed widget base class variables from private to protected Base variables are now accessed directly by child widgets instead of using methods --- MicroView.cpp | 97 ++++++++++++++++++++++----------------------------- MicroView.h | 10 +++--- 2 files changed, 46 insertions(+), 61 deletions(-) diff --git a/MicroView.cpp b/MicroView.cpp index cffada8..aa52e68 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -1362,8 +1362,8 @@ int MicroView::readSerial(void) The MicroViewWidget class is the parent class for child widget like MicroViewSlider and MicroViewGauge. */ MicroViewWidget::MicroViewWidget(uint8_t newx, uint8_t newy, int16_t min, int16_t max) { - x=newx; - y=newy; + posX=newx; + posY=newy; value=min; valLen=getInt16PrintLen(value); minValue=min; @@ -1372,16 +1372,16 @@ MicroViewWidget::MicroViewWidget(uint8_t newx, uint8_t newy, int16_t min, int16_ } /** \brief Get widget x position. */ -uint8_t MicroViewWidget::getX() { return x; } +uint8_t MicroViewWidget::getX() { return posX; } /** \brief Get widget y position. */ -uint8_t MicroViewWidget::getY() { return y; } +uint8_t MicroViewWidget::getY() { return posY; } /** \brief Set widget x position. */ -void MicroViewWidget::setX(uint8_t newx) { x = newx; } +void MicroViewWidget::setX(uint8_t newx) { posX = newx; } /** \brief Set widget y position. */ -void MicroViewWidget::setY(uint8_t newy) { y = newy; } +void MicroViewWidget::setY(uint8_t newy) { posY = newy; } /** \brief Get minimum value. @@ -1492,7 +1492,7 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ style=0; totalTicks=30; noValDraw=false; - prevValue=getMinValue(); + prevValue=minValue; needFirstDraw=true; drawFace(); draw(); @@ -1525,7 +1525,7 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ break; } - prevValue=getMinValue(); + prevValue=minValue; needFirstDraw=true; drawFace(); draw(); @@ -1536,32 +1536,30 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ Draw image/diagram representing the widget's face. */ void MicroViewSlider::drawFace() { - uint8_t offsetX, offsetY, endOffset; - offsetX=getX(); - offsetY=getY(); + uint8_t endOffset; //Horizontal styles, style 0 or 1 if (style==0 || style==1) { - endOffset = offsetX + totalTicks + 2; + endOffset = posX + totalTicks + 2; // Draw minor ticks - for (uint8_t i=offsetX+1; i15) { @@ -1697,7 +1687,7 @@ void MicroViewGauge::drawFace() { fromSecY = sin(degreeSec) * (radius / 1.3); toSecX = cos(degreeSec) * radius; toSecY = sin(degreeSec) * radius; - uView.line(1+offsetX+fromSecX,1+offsetY+fromSecY,1+offsetX+toSecX,1+offsetY+toSecY); + uView.line(1+posX+fromSecX, 1+posY+fromSecY, 1+posX+toSecX, 1+posY+toSecY); } } } @@ -1714,20 +1704,19 @@ void MicroViewGauge::draw() { needFirstDraw=false; } else { - prevValue=getValue(); + prevValue=value; // Draw current pointer drawPointer(); } // Draw numeric value if required if (!noValDraw) { - uint8_t offsetY = getY(); - uint8_t offsetX = getX() - (getMaxValLen() * 3 - 1); // Offset left of centre to print the value + uint8_t offsetX = posX - (maxValLen * 3 - 1); // Offset left of centre to print the value if (style > 0) - uView.setCursor(offsetX, offsetY+10); + uView.setCursor(offsetX, posY+10); else - uView.setCursor(offsetX, offsetY+11); + uView.setCursor(offsetX, posY+11); drawNumValue(prevValue); } @@ -1735,19 +1724,15 @@ void MicroViewGauge::draw() { // Use XOR mode to erase or draw the pointer for prevValue void MicroViewGauge::drawPointer() { - uint8_t offsetX, offsetY; float degreeSec, toSecX, toSecY; - offsetX = getX(); - offsetY = getY(); - // total 240 degree in the widget with 150 degree starting point - degreeSec = (((float)(uint16_t)(prevValue-getMinValue()) / (float)(uint16_t)(getMaxValue()-getMinValue()) + degreeSec = (((float)(uint16_t)(prevValue-minValue) / (float)(uint16_t)(maxValue-minValue) * 240) + 150) * (PI / 180); toSecX = cos(degreeSec) * (radius / 1.2); toSecY = sin(degreeSec) * (radius / 1.2); - uView.line(offsetX,offsetY,1+offsetX+toSecX,1+offsetY+toSecY, WHITE,XOR); + uView.line(posX, posY, 1+posX+toSecX, 1+posY+toSecY, WHITE, XOR); } // ------------------------------------------------------------------------------------- diff --git a/MicroView.h b/MicroView.h index 4d9b58d..471de12 100644 --- a/MicroView.h +++ b/MicroView.h @@ -236,7 +236,6 @@ public: uint8_t getY(); void setX(uint8_t newx); void setY(uint8_t newy); - int16_t getMinValue(); int16_t getMaxValue(); int16_t getValue(); @@ -252,15 +251,16 @@ public: void reDraw(); void drawNumValue(int16_t value); virtual ~MicroViewWidget(){}; -private: - void setMaxValLen(); - uint8_t x; - uint8_t y; +protected: + uint8_t posX; + uint8_t posY; int16_t maxValue; int16_t minValue; int16_t value; uint8_t valLen; uint8_t maxValLen; +private: + void setMaxValLen(); }; class MicroViewSlider: public MicroViewWidget{ From b05451f4c69f63cb25b7c5c29fb8efcc3120c13a Mon Sep 17 00:00:00 2001 From: Scott Allen Date: Thu, 25 Sep 2014 16:02:47 -0400 Subject: [PATCH 2/3] Use constructor initialisation lists for widget variables --- MicroView.cpp | 55 +++++++++++++++++++++++++++++---------------------- MicroView.h | 8 ++++---- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/MicroView.cpp b/MicroView.cpp index aa52e68..5b90a98 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -1361,13 +1361,15 @@ int MicroView::readSerial(void) The MicroViewWidget class is the parent class for child widget like MicroViewSlider and MicroViewGauge. */ -MicroViewWidget::MicroViewWidget(uint8_t newx, uint8_t newy, int16_t min, int16_t max) { - posX=newx; - posY=newy; - value=min; +MicroViewWidget::MicroViewWidget(uint8_t newx, uint8_t newy, int16_t min, int16_t max): + posX(newx), + posY(newy), + minValue(min), + maxValue(max), + value(min), + needFirstDraw(true) +{ valLen=getInt16PrintLen(value); - minValue=min; - maxValue=max; setMaxValLen(); } @@ -1488,12 +1490,13 @@ void MicroViewWidget::setMaxValLen() { Initialise the MicroViewSlider widget with default style. */ -MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_t max):MicroViewWidget(newx, newy, min, max) { - style=0; - totalTicks=30; - noValDraw=false; - prevValue=minValue; - needFirstDraw=true; +MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_t max): + MicroViewWidget(newx, newy, min, max), + style(0), + totalTicks(30), + noValDraw(false), + prevValue(value) +{ drawFace(); draw(); } @@ -1503,7 +1506,10 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ Initialise the MicroViewSlider widget with style WIDGETSTYLE0 or WIDGETSTYLE1 or WIDGETSTYLE2 (like 0, but vertical) or WIDGETSTYLE3 (like 1, but vertical). Add WIDGETNOVALUE to the style to suppress displaying the numeric value. E.g. WIDGETSTYLE0 + WIDGETNOVALUE */ -MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_t max, uint8_t sty):MicroViewWidget(newx, newy, min, max) { +MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_t max, uint8_t sty): + MicroViewWidget(newx, newy, min, max), + prevValue(value) +{ noValDraw = sty & WIDGETNOVALUE; // set "no value draw" flag as specified switch(sty & ~WIDGETNOVALUE) { @@ -1525,8 +1531,6 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ break; } - prevValue=minValue; - needFirstDraw=true; drawFace(); draw(); } @@ -1630,12 +1634,13 @@ void MicroViewSlider::drawPointer() { Initialise the MicroViewGauge widget with default style. */ -MicroViewGauge::MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t max):MicroViewWidget(newx, newy, min, max) { - style=0; - radius=15; - noValDraw=false; - prevValue=minValue; - needFirstDraw=true; +MicroViewGauge::MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t max): + MicroViewWidget(newx, newy, min, max), + style(0), + radius(15), + noValDraw(false), + prevValue(value) +{ drawFace(); draw(); } @@ -1645,7 +1650,10 @@ MicroViewGauge::MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t Initialise the MicroViewGauge widget with style WIDGETSTYLE0 or WIDGETSTYLE1. Add WIDGETNOVALUE to the style to suppress displaying the numeric value. E.g. WIDGETSTYLE0 + WIDGETNOVALUE */ -MicroViewGauge::MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t max, uint8_t sty):MicroViewWidget(newx, newy, min, max) { +MicroViewGauge::MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t max, uint8_t sty): + MicroViewWidget(newx, newy, min, max), + prevValue(value) +{ noValDraw = sty & WIDGETNOVALUE; // set "no value draw" flag as specified if ((sty & ~WIDGETNOVALUE) == WIDGETSTYLE0) { @@ -1656,8 +1664,7 @@ MicroViewGauge::MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t style=1; radius=23; } - prevValue=minValue; - needFirstDraw=true; + drawFace(); draw(); } diff --git a/MicroView.h b/MicroView.h index 471de12..8147ae9 100644 --- a/MicroView.h +++ b/MicroView.h @@ -230,7 +230,6 @@ private: class MicroViewWidget { public: - bool needFirstDraw; MicroViewWidget(uint8_t newx, uint8_t newy, int16_t min, int16_t max); uint8_t getX(); uint8_t getY(); @@ -254,11 +253,12 @@ public: protected: uint8_t posX; uint8_t posY; - int16_t maxValue; int16_t minValue; + int16_t maxValue; int16_t value; uint8_t valLen; uint8_t maxValLen; + bool needFirstDraw; private: void setMaxValLen(); }; @@ -271,7 +271,7 @@ public: void drawFace(); private: void drawPointer(); - uint8_t totalTicks, style; + uint8_t style, totalTicks; bool noValDraw; int16_t prevValue; }; @@ -284,7 +284,7 @@ public: void drawFace(); private: void drawPointer(); - uint8_t radius, style; + uint8_t style, radius; bool noValDraw; int16_t prevValue; }; From 5e24583c311d91851bc8bf05aa91b81593e78b5a Mon Sep 17 00:00:00 2001 From: Scott Allen Date: Thu, 25 Sep 2014 16:43:43 -0400 Subject: [PATCH 3/3] Refactored widget code to eliminate needFirstDraw variable --- MicroView.cpp | 37 ++++++++++++------------------------- MicroView.h | 7 ++++--- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/MicroView.cpp b/MicroView.cpp index 5b90a98..6c6f00c 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -1366,8 +1366,7 @@ MicroViewWidget::MicroViewWidget(uint8_t newx, uint8_t newy, int16_t min, int16_ posY(newy), minValue(min), maxValue(max), - value(min), - needFirstDraw(true) + value(min) { valLen=getInt16PrintLen(value); setMaxValLen(); @@ -1450,8 +1449,8 @@ uint8_t MicroViewWidget::getValLen() { return valLen; } Redraws the widget. */ void MicroViewWidget::reDraw() { - needFirstDraw=true; this->drawFace(); + this->drawPointer(); // initial pointer (will be erased) this->draw(); } @@ -1498,6 +1497,7 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ prevValue(value) { drawFace(); + drawPointer(); // Initial pointer (will be erased) draw(); } @@ -1532,6 +1532,7 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ } drawFace(); + drawPointer(); // Initial pointer (will be erased) draw(); } @@ -1573,17 +1574,9 @@ void MicroViewSlider::drawFace() { Convert the current value of the widget and draw the ticker representing the value. */ void MicroViewSlider::draw() { - // Draw the initial pointer or erase the previous pointer - drawPointer(); - - if (needFirstDraw) { - needFirstDraw=false; - } - else { - prevValue=value; - // Draw current pointer - drawPointer(); - } + drawPointer(); // Erase the previous pointer + prevValue=value; + drawPointer(); // Draw the current pointer // Draw numeric value if required if (!noValDraw) { @@ -1642,6 +1635,7 @@ MicroViewGauge::MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t prevValue(value) { drawFace(); + drawPointer(); // Initial pointer (will be erased) draw(); } @@ -1666,6 +1660,7 @@ MicroViewGauge::MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t } drawFace(); + drawPointer(); // Initial pointer (will be erased) draw(); } @@ -1704,17 +1699,9 @@ void MicroViewGauge::drawFace() { Convert the current value of the widget and draw the ticker representing the value. */ void MicroViewGauge::draw() { - // Draw the initial pointer or erase the previous pointer - drawPointer(); - - if (needFirstDraw) { - needFirstDraw=false; - } - else { - prevValue=value; - // Draw current pointer - drawPointer(); - } + drawPointer(); // Erase the previous pointer + prevValue=value; + drawPointer(); // Draw the current pointer // Draw numeric value if required if (!noValDraw) { diff --git a/MicroView.h b/MicroView.h index 8147ae9..bd29b57 100644 --- a/MicroView.h +++ b/MicroView.h @@ -244,8 +244,8 @@ public: uint8_t getValLen(); uint8_t getMaxValLen(); /** \brief Draw widget value overridden by child class. */ - virtual void draw(){}; - /** \brief Draw widget face overridden by child class. */ + virtual void draw(){}; + /** \brief Draw widget face overridden by child class. */ virtual void drawFace(){}; void reDraw(); void drawNumValue(int16_t value); @@ -258,8 +258,9 @@ protected: int16_t value; uint8_t valLen; uint8_t maxValLen; - bool needFirstDraw; private: + /** \brief Draw or erase the widget pointer. Overridden by child class. */ + virtual void drawPointer(){}; void setMaxValLen(); };