diff --git a/MicroView.cpp b/MicroView.cpp index 8856d4a..47c8a02 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -1376,27 +1376,28 @@ 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; - 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) +{ valLen=getInt16PrintLen(value); - minValue=min; - maxValue=max; setMaxValLen(); } /** \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. @@ -1463,8 +1464,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(); } @@ -1503,13 +1504,15 @@ 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=getMinValue(); - 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(); + drawPointer(); // Initial pointer (will be erased) draw(); } @@ -1518,7 +1521,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) { @@ -1540,9 +1546,8 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ break; } - prevValue=getMinValue(); - needFirstDraw=true; drawFace(); + drawPointer(); // Initial pointer (will be erased) draw(); } @@ -1551,32 +1556,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) { @@ -1712,7 +1704,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); } } } @@ -1722,27 +1714,18 @@ 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=getValue(); - // Draw current pointer - drawPointer(); - } + drawPointer(); // Erase the previous pointer + prevValue=value; + drawPointer(); // Draw the current pointer // 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); } @@ -1750,19 +1733,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..bd29b57 100644 --- a/MicroView.h +++ b/MicroView.h @@ -230,13 +230,11 @@ 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(); void setX(uint8_t newx); void setY(uint8_t newy); - int16_t getMinValue(); int16_t getMaxValue(); int16_t getValue(); @@ -246,21 +244,24 @@ 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); virtual ~MicroViewWidget(){}; -private: - void setMaxValLen(); - uint8_t x; - uint8_t y; - int16_t maxValue; +protected: + uint8_t posX; + uint8_t posY; int16_t minValue; + int16_t maxValue; int16_t value; uint8_t valLen; uint8_t maxValLen; +private: + /** \brief Draw or erase the widget pointer. Overridden by child class. */ + virtual void drawPointer(){}; + void setMaxValLen(); }; class MicroViewSlider: public MicroViewWidget{ @@ -271,7 +272,7 @@ public: void drawFace(); private: void drawPointer(); - uint8_t totalTicks, style; + uint8_t style, totalTicks; bool noValDraw; int16_t prevValue; }; @@ -284,7 +285,7 @@ public: void drawFace(); private: void drawPointer(); - uint8_t radius, style; + uint8_t style, radius; bool noValDraw; int16_t prevValue; };