From 63e3228d83e55195ff9383cc46f3f2c609cb815e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esmit=20Pe=CC=81rez=20C?= Date: Fri, 12 Sep 2014 20:43:38 -0500 Subject: [PATCH 01/12] Code refactorings --- MicroView.cpp | 143 +++++++++++++++++++++++++++----------------------- 1 file changed, 77 insertions(+), 66 deletions(-) diff --git a/MicroView.cpp b/MicroView.cpp index cc3db1c..de90216 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -1470,21 +1470,25 @@ 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). If this list gets any longer, it might be better as a switch/case statement. */ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_t max, uint8_t sty):MicroViewWidget(newx, newy, min, max) { - if (sty==WIDGETSTYLE1) { - style=1; - totalTicks=60; - } - else if (sty==WIDGETSTYLE2) { - style=2; - totalTicks=20; - } - else if (sty==WIDGETSTYLE3) { - style=3; - totalTicks=40; - } - else { // Use style 0 if specified or invalid - style=0; - totalTicks=30; + + // @esmitperez: replaced with switch...but original if/else here because it's faster? + switch(sty){ + case WIDGETSTYLE1: + style=1; + totalTicks=60; + break; + case WIDGETSTYLE2: + style=2; + totalTicks=20; + break; + case WIDGETSTYLE3: + style=3; + totalTicks=40; + break; + default: + style=0; + totalTicks=30; + break; } prevValue=getMinValue(); @@ -1526,6 +1530,7 @@ void MicroViewSlider::drawFace() { uView.lineH(offsetX+3, i, 2); } } + } /** \brief Draw widget value. @@ -1543,61 +1548,59 @@ void MicroViewSlider::draw() { offsetX=getX(); offsetY=getY(); - if (needFirstDraw) { - 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); - } + // @esmitperez: this block executed all the time, so moved out of if/else + // Draw previous pointer in XOR mode 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); + } - sprintf(strBuffer, formatStr, prevValue); // print with fixed width so that blank space will cover larger value - needFirstDraw=false; + + if (needFirstDraw) { + sprintf(strBuffer, formatStr, prevValue); // print with fixed width so that blank space will cover larger value + needFirstDraw=false; } else { - // Draw previous pointer in XOR mode 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 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); + } - // 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); - } - - sprintf(strBuffer, formatStr, getValue()); // print with fixed width so that blank space will cover larger value - prevValue=getValue(); + sprintf(strBuffer, formatStr, getValue()); // print with fixed width so that blank space will cover larger value + prevValue=getValue(); } // Draw value - if (style==0) - uView.setCursor(offsetX+totalTicks+4, offsetY+1); - else if (style==1) - uView.setCursor(offsetX, offsetY+10); - else if (style==2) - uView.setCursor(offsetX+1, offsetY+totalTicks+4); - else //style==3 - uView.setCursor(offsetX+9, offsetY); + // @esmitperez: replaced with switch...but original if/else here because it's faster? + switch(style){ + case 0: + uView.setCursor(offsetX+totalTicks+4, offsetY+1); + break; + case 1: + uView.setCursor(offsetX, offsetY+10); + break; + case 2: + uView.setCursor(offsetX+1, offsetY+totalTicks+4); + break; + default: + uView.setCursor(offsetX+9, offsetY); + break; + } - uView.print(strBuffer); + uView.print(strBuffer); } // ------------------------------------------------------------------------------------- @@ -1691,16 +1694,24 @@ void MicroViewGauge::draw() { offsetX=getX(); offsetY=getY(); + // @esmitperez: this block executed regardless, so moved out of if/else + 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); + if (needFirstDraw) { + sprintf(strBuffer, formatStr, prevValue); // print with fixed width so that blank space will cover larger value + needFirstDraw=false; + } + else { 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); - sprintf(strBuffer, formatStr, prevValue); // print with fixed width so that blank space will cover larger value - needFirstDraw=false; - } - else { + // Draw previous pointer in XOR mode 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); From c5cf9692adb996351fc13b5d5d1ae957b597384a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esmit=20Pe=CC=81rez=20C?= Date: Fri, 12 Sep 2014 23:16:08 -0500 Subject: [PATCH 02/12] Documentation changes Removed comments regarding design decisions. MicroViewSlider:draw() retested to ensure refactoring did not introduce regressions. --- MicroView.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/MicroView.cpp b/MicroView.cpp index de90216..22835f1 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -1467,11 +1467,10 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ /** \brief MicroViewSlider class initialisation with style. - Initialise the MicroViewSlider widget with style WIDGETSTYLE0 or WIDGETSTYLE1 or WIDGETSTYLE2 (like 0, but vertical) or WIDGETSTYLE3 (like 1, but vertical). If this list gets any longer, it might be better as a switch/case statement. + Initialise the MicroViewSlider widget with style WIDGETSTYLE0 or WIDGETSTYLE1 or WIDGETSTYLE2 (like 0, but vertical) or WIDGETSTYLE3 (like 1, but vertical). */ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_t max, uint8_t sty):MicroViewWidget(newx, newy, min, max) { - // @esmitperez: replaced with switch...but original if/else here because it's faster? switch(sty){ case WIDGETSTYLE1: style=1; @@ -1584,7 +1583,6 @@ void MicroViewSlider::draw() { } // Draw value - // @esmitperez: replaced with switch...but original if/else here because it's faster? switch(style){ case 0: uView.setCursor(offsetX+totalTicks+4, offsetY+1); From cc6a70271290cfd85fbb00d1197ada61391eca37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esmit=20Pe=CC=81rez=20C?= Date: Sun, 14 Sep 2014 10:07:53 -0500 Subject: [PATCH 03/12] removed unneeded comments --- MicroView.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/MicroView.cpp b/MicroView.cpp index 22835f1..a201dd8 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -1547,7 +1547,6 @@ void MicroViewSlider::draw() { offsetX=getX(); offsetY=getY(); - // @esmitperez: this block executed all the time, so moved out of if/else // Draw previous pointer in XOR mode to erase it if (style==0 || style==1){ //Horizontal tickPosition = ((float)(uint16_t)(prevValue-getMinValue())/(float)(uint16_t)(getMaxValue()-getMinValue()))*totalTicks; @@ -1692,7 +1691,6 @@ void MicroViewGauge::draw() { offsetX=getX(); offsetY=getY(); - // @esmitperez: this block executed regardless, so moved out of if/else 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); From 88116856d41797c2eabedad93f957b58d34998c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esmit=20Pe=CC=81rez=20C?= Date: Sun, 14 Sep 2014 10:18:00 -0500 Subject: [PATCH 04/12] Code cleanups for previous refactoring --- MicroView.cpp | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/MicroView.cpp b/MicroView.cpp index a201dd8..950e99c 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -1702,20 +1702,7 @@ void MicroViewGauge::draw() { needFirstDraw=false; } else { - 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 previous pointer in XOR mode 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); - 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 current pointer + /// 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); From 23d05f13ee278e9b5732bde38f8160b74f21e101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esmit=20Pe=CC=81rez=20C?= Date: Sun, 14 Sep 2014 13:48:53 -0500 Subject: [PATCH 05/12] Indentation fixes --- MicroView.cpp | 140 +++++++++++++++++++++++++------------------------- 1 file changed, 70 insertions(+), 70 deletions(-) diff --git a/MicroView.cpp b/MicroView.cpp index 950e99c..1e394fd 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -1472,22 +1472,22 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_t max, uint8_t sty):MicroViewWidget(newx, newy, min, max) { switch(sty){ - case WIDGETSTYLE1: - style=1; - totalTicks=60; - break; - case WIDGETSTYLE2: - style=2; - totalTicks=20; - break; - case WIDGETSTYLE3: - style=3; - totalTicks=40; - break; - default: - style=0; - totalTicks=30; - break; + case WIDGETSTYLE1: + style=1; + totalTicks=60; + break; + case WIDGETSTYLE2: + style=2; + totalTicks=20; + break; + case WIDGETSTYLE3: + style=3; + totalTicks=40; + break; + default: + style=0; + totalTicks=30; + break; } prevValue=getMinValue(); @@ -1502,35 +1502,35 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ */ void MicroViewSlider::drawFace() { uint8_t offsetX, offsetY, endOffset; - offsetX=getX(); - offsetY=getY(); + offsetX=getX(); + offsetY=getY(); //Horizontal styles, style 0 or 1 - if (style==0 || style==1) { - endOffset = offsetX + totalTicks + 2; + if (style==0 || style==1) { + endOffset = offsetX + totalTicks + 2; // Draw minor ticks - for (uint8_t i=offsetX+1; i0) - uView.setCursor(offsetX-valOffset, offsetY+10); + uView.setCursor(offsetX-valOffset, offsetY+10); else - uView.setCursor(offsetX-valOffset, offsetY+11); + uView.setCursor(offsetX-valOffset, offsetY+11); uView.print(strBuffer); } From 04ed26e2b81d9c2a38e243402a86b78c33ca9f7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esmit=20Pe=CC=81rez=20C?= Date: Sun, 14 Sep 2014 14:56:43 -0500 Subject: [PATCH 06/12] Corrected indentation --- MicroView.cpp | 52 +++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/MicroView.cpp b/MicroView.cpp index 1e394fd..e349f84 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -1502,35 +1502,35 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ */ void MicroViewSlider::drawFace() { uint8_t offsetX, offsetY, endOffset; - offsetX=getX(); - offsetY=getY(); + offsetX=getX(); + offsetY=getY(); //Horizontal styles, style 0 or 1 - if (style==0 || style==1) { - endOffset = offsetX + totalTicks + 2; + if (style==0 || style==1) { + endOffset = offsetX + totalTicks + 2; // Draw minor ticks - for (uint8_t i=offsetX+1; i0) - uView.setCursor(offsetX-valOffset, offsetY+10); + uView.setCursor(offsetX-valOffset, offsetY+10); else - uView.setCursor(offsetX-valOffset, offsetY+11); + uView.setCursor(offsetX-valOffset, offsetY+11); uView.print(strBuffer); } From df383d88f2c46c2a28be7d4122c7e439b57fe3de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esmit=20Pe=CC=81rez=20C?= Date: Sun, 14 Sep 2014 15:09:32 -0500 Subject: [PATCH 07/12] Indentation fixes --- MicroView.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MicroView.cpp b/MicroView.cpp index e349f84..53f0ca3 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -1475,8 +1475,8 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ case WIDGETSTYLE1: style=1; totalTicks=60; - break; - case WIDGETSTYLE2: + break; + case WIDGETSTYLE2: style=2; totalTicks=20; break; From 13390ca75860ca5ae7ff04ec4f67e2e2ee22b473 Mon Sep 17 00:00:00 2001 From: Scott Allen Date: Mon, 15 Sep 2014 19:19:15 -0400 Subject: [PATCH 08/12] Fix compiler warning: signed/unsigned comparison --- MicroView.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MicroView.cpp b/MicroView.cpp index 53f0ca3..afa5eff 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -631,11 +631,11 @@ void MicroView::circleFill(uint8_t x0, uint8_t y0, uint8_t radius, uint8_t color bool intersected = false; // Optimization: relative x squared only changes with the outer loop/the horizontal scan. - int16_t rx2 = (x-x0) * (x-x0); + uint16_t rx2 = (x-x0) * (x-x0); // Scan vertically... for (uint16_t y = yStart; y <= yEnd; ++y) { - int16_t ry2 = (y-y0) * (y-y0); + uint16_t ry2 = (y-y0) * (y-y0); if (rx2 + ry2 <= radiusSq) { pixel(x, y, color, mode); intersected = true; From 810a0928cba544697c511acd28d47c208003c6d4 Mon Sep 17 00:00:00 2001 From: Scott Allen Date: Mon, 15 Sep 2014 19:32:33 -0400 Subject: [PATCH 09/12] Add destructor for MicroViewWidget base class Destructor added to prevent compiler warning for object delete. --- MicroView.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MicroView.h b/MicroView.h index 296956c..0a38d05 100644 --- a/MicroView.h +++ b/MicroView.h @@ -246,7 +246,7 @@ public: /** \brief Draw widget face overridden by child class. */ virtual void drawFace(){}; void reDraw(); - + virtual ~MicroViewWidget(){}; private: uint8_t x; uint8_t y; From 4b7b544fa46c2b2ccb0450a259e864a0609337ec Mon Sep 17 00:00:00 2001 From: Scott Allen Date: Wed, 17 Sep 2014 11:17:10 -0400 Subject: [PATCH 10/12] Improved drawing of gauge minor ticks Don't bother drawing gauge minor ticks over major ticks --- MicroView.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/MicroView.cpp b/MicroView.cpp index afa5eff..4ea5603 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -1656,18 +1656,18 @@ void MicroViewGauge::drawFace() { degreeSec=i*(PI/180); fromSecX = cos(degreeSec) * (radius / 1.5); fromSecY = sin(degreeSec) * (radius / 1.5); - toSecX = cos(degreeSec) * (radius / 1); - toSecY = sin(degreeSec) * (radius / 1); + toSecX = cos(degreeSec) * radius; + toSecY = sin(degreeSec) * radius; uView.line(1+offsetX+fromSecX,1+offsetY+fromSecY,1+offsetX+toSecX,1+offsetY+toSecY); } if(radius>15) { - for (int i=150;i<=390;i+=15) { // Minor tick from 150 degree to 390 degree + 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); fromSecY = sin(degreeSec) * (radius / 1.3); - toSecX = cos(degreeSec) * (radius / 1); - toSecY = sin(degreeSec) * (radius / 1); + toSecX = cos(degreeSec) * radius; + toSecY = sin(degreeSec) * radius; uView.line(1+offsetX+fromSecX,1+offsetY+fromSecY,1+offsetX+toSecX,1+offsetY+toSecY); } } From e9584301ab89abf06fb8f88660c67958cd58c949 Mon Sep 17 00:00:00 2001 From: Scott Allen Date: Wed, 17 Sep 2014 16:01:35 -0400 Subject: [PATCH 11/12] Widget numeric value supression option The user can add a flag to the style, for all widgets, to supress the display of the numeric value. Added new widget method drawNumValue() to simplify the code, which can also be called by the user, e.g. for a custom numeric value location. --- MicroView.cpp | 102 +++++++++++++++++++++++++++----------------------- MicroView.h | 10 ++++- keywords.txt | 3 +- 3 files changed, 66 insertions(+), 49 deletions(-) diff --git a/MicroView.cpp b/MicroView.cpp index 4ea5603..e16e246 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -1443,7 +1443,20 @@ void MicroViewWidget::reDraw() { this->drawFace(); this->draw(); } - + +/** \brief Draw a signed decimal numeric value at the current cursor location. + + The value is right justified with leading spaces, within a field the length of the maximum posible for the widget's value range. +*/ +void MicroViewWidget::drawNumValue(int16_t value) { + char strBuffer[7]; + char formatStr[] = "%1d"; + + formatStr[1] = '0' + getMaxValLen(); // Set the field width for the value range + sprintf(strBuffer, formatStr, value); + uView.print(strBuffer); +} + // ------------------------------------------------------------------------------------- // MicroViewWidget Class - end // ------------------------------------------------------------------------------------- @@ -1459,6 +1472,7 @@ void MicroViewWidget::reDraw() { 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; drawFace(); @@ -1467,11 +1481,13 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ /** \brief MicroViewSlider class initialisation with style. - Initialise the MicroViewSlider widget with style WIDGETSTYLE0 or WIDGETSTYLE1 or WIDGETSTYLE2 (like 0, but vertical) or WIDGETSTYLE3 (like 1, but vertical). + 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) { + noValDraw = sty & WIDGETNOVALUE; // set "no value draw" flag as specified - switch(sty){ + switch(sty & ~WIDGETNOVALUE) { case WIDGETSTYLE1: style=1; totalTicks=60; @@ -1529,7 +1545,6 @@ void MicroViewSlider::drawFace() { uView.lineH(offsetX+3, i, 2); } } - } /** \brief Draw widget value. @@ -1539,15 +1554,11 @@ void MicroViewSlider::drawFace() { void MicroViewSlider::draw() { uint8_t offsetX, offsetY; uint8_t tickPosition=0; - char strBuffer[7]; - char formatStr[] = "%1d"; - - formatStr[1] = '0' + getMaxValLen(); // Set the field width for the value range offsetX=getX(); offsetY=getY(); - // Draw previous pointer in XOR mode to erase it + // 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); @@ -1559,9 +1570,7 @@ void MicroViewSlider::draw() { uView.pixel(offsetX+6, offsetY+1+tickPosition, WHITE, XOR); } - if (needFirstDraw) { - sprintf(strBuffer, formatStr, prevValue); // print with fixed width so that blank space will cover larger value needFirstDraw=false; } else { @@ -1577,27 +1586,28 @@ void MicroViewSlider::draw() { uView.pixel(offsetX+6, offsetY+1+tickPosition, WHITE, XOR); } - sprintf(strBuffer, formatStr, getValue()); // print with fixed width so that blank space will cover larger value prevValue=getValue(); } - // Draw value - switch(style){ - case 0: - uView.setCursor(offsetX+totalTicks+4, offsetY+1); - break; - case 1: - uView.setCursor(offsetX, offsetY+10); - break; - case 2: - uView.setCursor(offsetX+1, offsetY+totalTicks+4); - break; - default: - uView.setCursor(offsetX+9, offsetY); - break; - } + // Draw numeric value if required + if (!noValDraw) { + switch(style) { + case 0: + uView.setCursor(offsetX+totalTicks+4, offsetY+1); + break; + case 1: + uView.setCursor(offsetX, offsetY+10); + break; + case 2: + uView.setCursor(offsetX+1, offsetY+totalTicks+4); + break; + default: + uView.setCursor(offsetX+9, offsetY); + break; + } - uView.print(strBuffer); + drawNumValue(prevValue); + } } // ------------------------------------------------------------------------------------- @@ -1615,6 +1625,7 @@ void MicroViewSlider::draw() { 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=getMinValue(); needFirstDraw=true; drawFace(); @@ -1624,9 +1635,12 @@ MicroViewGauge::MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t /** \brief MicroViewGauge class initialisation with style. 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) { - if (sty==WIDGETSTYLE0) { + noValDraw = sty & WIDGETNOVALUE; // set "no value draw" flag as specified + + if ((sty & ~WIDGETNOVALUE) == WIDGETSTYLE0) { style=0; radius=15; } @@ -1679,18 +1693,12 @@ void MicroViewGauge::drawFace() { */ void MicroViewGauge::draw() { uint8_t offsetX, offsetY; - uint8_t maxValLen, valOffset; float degreeSec, toSecX, toSecY; - char strBuffer[7]; - char formatStr[] = "%1d"; // The 1 will be replaced later by the proper length - - maxValLen = getMaxValLen(); - formatStr[1] = '0' + maxValLen; // Set the field width for the value range - valOffset = maxValLen * 3 - 1; // Offset left of centre to print the value 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); @@ -1698,28 +1706,30 @@ void MicroViewGauge::draw() { uView.line(offsetX,offsetY,1+offsetX+toSecX,1+offsetY+toSecY, WHITE,XOR); if (needFirstDraw) { - sprintf(strBuffer, formatStr, prevValue); // print with fixed width so that blank space will cover larger value needFirstDraw=false; } else { - /// draw current pointer + // 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); - sprintf(strBuffer, formatStr, getValue()); // print with fixed width so that blank space will cover larger value prevValue=getValue(); } - // Draw value - if (style>0) - uView.setCursor(offsetX-valOffset, offsetY+10); - else - uView.setCursor(offsetX-valOffset, offsetY+11); - - uView.print(strBuffer); + // Draw numeric value if required + if (!noValDraw) { + uint8_t valX = offsetX - (getMaxValLen() * 3 - 1); // Offset left of centre to print the value + + if (style > 0) + uView.setCursor(valX, offsetY+10); + else + uView.setCursor(valX, offsetY+11); + + drawNumValue(prevValue); + } } // ------------------------------------------------------------------------------------- diff --git a/MicroView.h b/MicroView.h index 0a38d05..b81c885 100644 --- a/MicroView.h +++ b/MicroView.h @@ -77,10 +77,13 @@ #define WIDGETSTYLE0 0 #define WIDGETSTYLE1 1 +// Added for Vertical slider styles #define WIDGETSTYLE2 2 -//Added for Vertical slider styles #define WIDGETSTYLE3 3 +// Flag to be added to widget style to indicate no numeric value display +#define WIDGETNOVALUE 0x80 + #define SETCONTRAST 0x81 #define DISPLAYALLONRESUME 0xA4 #define DISPLAYALLON 0xA5 @@ -237,8 +240,8 @@ public: int16_t getMinValue(); int16_t getMaxValue(); int16_t getValue(); + void setMinValue(int16_t min); void setMaxValue(int16_t max); - void setMinValue(int16_t max); void setValue(int16_t val); uint8_t getMaxValLen(); /** \brief Draw widget value overridden by child class. */ @@ -246,6 +249,7 @@ public: /** \brief Draw widget face overridden by child class. */ virtual void drawFace(){}; void reDraw(); + void drawNumValue(int16_t value); virtual ~MicroViewWidget(){}; private: uint8_t x; @@ -263,6 +267,7 @@ public: void drawFace(); private: uint8_t totalTicks, style; + bool noValDraw; int16_t prevValue; }; @@ -274,6 +279,7 @@ public: void drawFace(); private: uint8_t radius, style; + bool noValDraw; int16_t prevValue; }; diff --git a/keywords.txt b/keywords.txt index a8568d4..455e1b1 100644 --- a/keywords.txt +++ b/keywords.txt @@ -66,6 +66,7 @@ setValue KEYWORD2 draw KEYWORD2 reDraw KEYWORD2 drawFace KEYWORD2 +drawNumValue KEYWORD2 checkComm KEYWORD2 ####################################### @@ -82,5 +83,5 @@ WIDGETSTYLE0 LITERAL1 WIDGETSTYLE1 LITERAL1 WIDGETSTYLE2 LITERAL1 WIDGETSTYLE3 LITERAL1 - +WIDGETNOVALUE LITERAL1 From da7579a6e832a5c7ebf64426fe2f125605ed2361 Mon Sep 17 00:00:00 2001 From: Scott Allen Date: Wed, 17 Sep 2014 19:50:09 -0400 Subject: [PATCH 12/12] Code refactoring for widget pointers Added private methods to replace duplicated code --- MicroView.cpp | 103 +++++++++++++++++++++++++------------------------- MicroView.h | 2 + 2 files changed, 53 insertions(+), 52 deletions(-) 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;