diff --git a/MicroView.cpp b/MicroView.cpp index e16e246..7873510 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -1552,45 +1552,23 @@ void MicroViewSlider::drawFace() { Convert the current value of the widget and draw the ticker representing the value. */ void MicroViewSlider::draw() { - uint8_t offsetX, offsetY; - uint8_t tickPosition=0; - - offsetX=getX(); - offsetY=getY(); - - // Draw pointer in XOR mode for first draw or to erase it - if (style==0 || style==1){ //Horizontal - tickPosition = ((float)(uint16_t)(prevValue-getMinValue())/(float)(uint16_t)(getMaxValue()-getMinValue()))*totalTicks; - uView.lineH(offsetX+tickPosition,offsetY, 3, WHITE, XOR); - uView.pixel(offsetX+1+tickPosition,offsetY+1, WHITE, XOR); - } - else { //Vertical - tickPosition = ((float)(uint16_t)(getMaxValue()-prevValue)/(float)(uint16_t)(getMaxValue()-getMinValue()))*totalTicks; - uView.lineV(offsetX+7, offsetY+tickPosition, 3, WHITE, XOR); - uView.pixel(offsetX+6, offsetY+1+tickPosition, WHITE, XOR); - } + // Draw the initial pointer or erase the previous pointer + drawPointer(); if (needFirstDraw) { needFirstDraw=false; } else { - // Draw current pointer - if (style==0 || style==1){ //Horizontal - tickPosition = ((float)(uint16_t)(getValue()-getMinValue())/(float)(uint16_t)(getMaxValue()-getMinValue()))*totalTicks; - uView.lineH(offsetX+tickPosition,offsetY, 3, WHITE, XOR); - uView.pixel(offsetX+1+tickPosition,offsetY+1, WHITE, XOR); - } - else { //Vertical - tickPosition = ((float)(uint16_t)(getMaxValue()-getValue())/(float)(uint16_t)(getMaxValue()-getMinValue()))*totalTicks; - uView.lineV(offsetX+7, offsetY+tickPosition, 3, WHITE, XOR); - uView.pixel(offsetX+6, offsetY+1+tickPosition, WHITE, XOR); - } - prevValue=getValue(); + // Draw current pointer + drawPointer(); } // Draw numeric value if required if (!noValDraw) { + uint8_t offsetX = getX(); + uint8_t offsetY = getY(); + switch(style) { case 0: uView.setCursor(offsetX+totalTicks+4, offsetY+1); @@ -1610,6 +1588,24 @@ void MicroViewSlider::draw() { } } +// Use XOR mode to erase or draw the pointer for prevValue +void MicroViewSlider::drawPointer() { + uint8_t tickPosition; + uint8_t offsetX = getX(); + uint8_t offsetY = getY(); + + if (style==0 || style==1) { // Horizontal + tickPosition = ((float)(uint16_t)(prevValue-getMinValue())/(float)(uint16_t)(getMaxValue()-getMinValue()))*totalTicks; + uView.lineH(offsetX+tickPosition,offsetY, 3, WHITE, XOR); + uView.pixel(offsetX+1+tickPosition,offsetY+1, WHITE, XOR); + } + else { // Vertical + tickPosition = ((float)(uint16_t)(getMaxValue()-prevValue)/(float)(uint16_t)(getMaxValue()-getMinValue()))*totalTicks; + uView.lineV(offsetX+7, offsetY+tickPosition, 3, WHITE, XOR); + uView.pixel(offsetX+6, offsetY+1+tickPosition, WHITE, XOR); + } +} + // ------------------------------------------------------------------------------------- // Slider Widget - end // ------------------------------------------------------------------------------------- @@ -1675,7 +1671,7 @@ void MicroViewGauge::drawFace() { uView.line(1+offsetX+fromSecX,1+offsetY+fromSecY,1+offsetX+toSecX,1+offsetY+toSecY); } - if(radius>15) { + if (radius>15) { for (int i=165;i<=375;i+=30) { // Minor tick from 165 degree to 375 degree degreeSec=i*(PI/180); fromSecX = cos(degreeSec) * (radius / 1.3); @@ -1692,46 +1688,49 @@ void MicroViewGauge::drawFace() { Convert the current value of the widget and draw the ticker representing the value. */ void MicroViewGauge::draw() { - uint8_t offsetX, offsetY; - float degreeSec, toSecX, toSecY; - - offsetX=getX(); - offsetY=getY(); - - // Draw pointer in XOR mode for first draw or to erase it - degreeSec = ((float)(uint16_t)(prevValue-getMinValue())/(float)(uint16_t)(getMaxValue()-getMinValue()))*240; // total 240 degree in the widget - degreeSec = (degreeSec+150) * (PI/180); // 150 degree starting point - toSecX = cos(degreeSec) * (radius / 1.2); - toSecY = sin(degreeSec) * (radius / 1.2); - uView.line(offsetX,offsetY,1+offsetX+toSecX,1+offsetY+toSecY, WHITE,XOR); + // Draw the initial pointer or erase the previous pointer + drawPointer(); if (needFirstDraw) { needFirstDraw=false; } else { - // draw current pointer - degreeSec = ((float)(uint16_t)(getValue()-getMinValue())/(float)(uint16_t)(getMaxValue()-getMinValue()))*240; // total 240 degree in the widget - degreeSec = (degreeSec+150) * (PI/180); // 150 degree starting point - toSecX = cos(degreeSec) * (radius / 1.2); - toSecY = sin(degreeSec) * (radius / 1.2); - uView.line(offsetX,offsetY,1+offsetX+toSecX,1+offsetY+toSecY, WHITE,XOR); - prevValue=getValue(); + // Draw current pointer + drawPointer(); } // Draw numeric value if required if (!noValDraw) { - uint8_t valX = offsetX - (getMaxValLen() * 3 - 1); // Offset left of centre to print the value + uint8_t offsetY = getY(); + uint8_t offsetX = getX() - (getMaxValLen() * 3 - 1); // Offset left of centre to print the value if (style > 0) - uView.setCursor(valX, offsetY+10); + uView.setCursor(offsetX, offsetY+10); else - uView.setCursor(valX, offsetY+11); + uView.setCursor(offsetX, offsetY+11); drawNumValue(prevValue); } } +// 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()) + * 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); +} + // ------------------------------------------------------------------------------------- // Gauge Widget - end // ------------------------------------------------------------------------------------- diff --git a/MicroView.h b/MicroView.h index b81c885..a784e09 100644 --- a/MicroView.h +++ b/MicroView.h @@ -266,6 +266,7 @@ public: void draw(); void drawFace(); private: + void drawPointer(); uint8_t totalTicks, style; bool noValDraw; int16_t prevValue; @@ -278,6 +279,7 @@ public: void draw(); void drawFace(); private: + void drawPointer(); uint8_t radius, style; bool noValDraw; int16_t prevValue;