From 7c3ec25d70b4a142b59cf66745164985d0bd46ae Mon Sep 17 00:00:00 2001 From: JP Date: Mon, 4 Aug 2014 10:36:33 +1000 Subject: [PATCH] added reDraw() --- MicroView.cpp | 53 ++++++++++--------- MicroView.h | 1 - README.md | 1 + .../MicroViewSliderRedraw.ino | 48 +++++++++++++++++ 4 files changed, 77 insertions(+), 26 deletions(-) create mode 100644 examples/MicroViewSliderRedraw/MicroViewSliderRedraw.ino diff --git a/MicroView.cpp b/MicroView.cpp index 778cb21..6f10d09 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -1413,7 +1413,6 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ totalTicks=30; prevValue=getMinValue(); needFirstDraw=true; - needToDrawPrev=true; drawFace(); draw(); } @@ -1423,7 +1422,7 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_ Redraws the widget. */ void MicroViewSlider::reDraw() { - needToDrawPrev=false; + needFirstDraw=true; drawFace(); draw(); } @@ -1546,6 +1545,7 @@ void MicroViewSlider::draw() { offsetY=getY(); if (needFirstDraw) { + /* if (style==0 || style==1){ //Horizontal uView.lineH(offsetX+tickPosition, offsetY, 3, WHITE, XOR); uView.pixel(offsetX+1+tickPosition, offsetY+1, WHITE, XOR); @@ -1554,22 +1554,30 @@ void MicroViewSlider::draw() { uView.lineV(offsetX+7, offsetY+tickPosition, 3, WHITE, XOR); uView.pixel(offsetX+6, offsetY+1+tickPosition, WHITE, XOR); } + */ + tickPosition= (((float)(prevValue-getMinValue())/(float)(getMaxValue()-getMinValue()))*totalTicks); + if (style==0 || style==1){ //Horizontal + uView.lineH(offsetX+tickPosition,offsetY, 3, WHITE, XOR); + uView.pixel(offsetX+1+tickPosition,offsetY+1, WHITE, XOR); + } + else { //Vertical + uView.lineV(offsetX+7, offsetY+tickPosition, 3, WHITE, XOR); + uView.pixel(offsetX+6, offsetY+1+tickPosition, WHITE, XOR); + } sprintf(strBuffer,"%4d", prevValue); // we need to force 4 digit so that blank space will cover larger value needFirstDraw=false; } else { - if (needToDrawPrev) { - // Draw previous pointer in XOR mode to erase it - tickPosition= (((float)(prevValue-getMinValue())/(float)(getMaxValue()-getMinValue()))*totalTicks); - if (style==0 || style==1){ //Horizontal - uView.lineH(offsetX+tickPosition,offsetY, 3, WHITE, XOR); - uView.pixel(offsetX+1+tickPosition,offsetY+1, WHITE, XOR); - } - else { //Vertical - uView.lineV(offsetX+7, offsetY+tickPosition, 3, WHITE, XOR); - uView.pixel(offsetX+6, offsetY+1+tickPosition, WHITE, XOR); - } + // Draw previous pointer in XOR mode to erase it + tickPosition= (((float)(prevValue-getMinValue())/(float)(getMaxValue()-getMinValue()))*totalTicks); + if (style==0 || style==1){ //Horizontal + uView.lineH(offsetX+tickPosition,offsetY, 3, WHITE, XOR); + uView.pixel(offsetX+1+tickPosition,offsetY+1, WHITE, XOR); + } + else { //Vertical + uView.lineV(offsetX+7, offsetY+tickPosition, 3, WHITE, XOR); + uView.pixel(offsetX+6, offsetY+1+tickPosition, WHITE, XOR); } // Draw current pointer @@ -1585,7 +1593,6 @@ void MicroViewSlider::draw() { sprintf(strBuffer,"%4d", getValue()); // we need to force 4 digit so that blank space will cover larger value prevValue=getValue(); - needToDrawPrev=true; } // Draw value @@ -1618,7 +1625,6 @@ MicroViewGauge::MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t radius=15; prevValue=getMinValue(); needFirstDraw=true; - needToDrawPrev=true; drawFace(); draw(); } @@ -1628,7 +1634,7 @@ MicroViewGauge::MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t Redraws the widget. */ void MicroViewGauge::reDraw() { - needToDrawPrev=false; + needFirstDraw=true; drawFace(); draw(); } @@ -1707,14 +1713,12 @@ void MicroViewGauge::draw() { needFirstDraw=false; } else { - if (needToDrawPrev) { - // Draw previous pointer in XOR mode to erase it - degreeSec = (((float)(prevValue-getMinValue())/(float)(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 previous pointer in XOR mode to erase it + degreeSec = (((float)(prevValue-getMinValue())/(float)(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 degreeSec = (((float)(getValue()-getMinValue())/(float)(getMaxValue()-getMinValue()))*240); // total 240 degree in the widget @@ -1725,7 +1729,6 @@ void MicroViewGauge::draw() { sprintf(strBuffer,"%4d", getValue()); // we need to force 4 digit so that blank space will cover larger value prevValue=getValue(); - needToDrawPrev=true; } // Draw value diff --git a/MicroView.h b/MicroView.h index 9774311..58a4323 100644 --- a/MicroView.h +++ b/MicroView.h @@ -226,7 +226,6 @@ private: class MicroViewWidget { public: - bool needToDrawPrev; MicroViewWidget(uint8_t newx, uint8_t newy, int16_t min, int16_t max); uint8_t getX(); uint8_t getY(); diff --git a/README.md b/README.md index f0f02f9..f9c9fa0 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ void loop() { **v1.17b: 4th August 2014 by JP Liew** * added reDraw() for MicroViewWidget class * removed Serial.begin() from uView.begin() so that user can have control +* added MicroViewSliderRedraw example **v1.16b: 3rd August 2014 by czetie** * added vertical slider widget diff --git a/examples/MicroViewSliderRedraw/MicroViewSliderRedraw.ino b/examples/MicroViewSliderRedraw/MicroViewSliderRedraw.ino new file mode 100644 index 0000000..8c51696 --- /dev/null +++ b/examples/MicroViewSliderRedraw/MicroViewSliderRedraw.ino @@ -0,0 +1,48 @@ +/* + MicroView Arduino Library + Copyright (C) 2014 GeekAmmo + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include + +MicroViewWidget *widget[2]; // declaring an array of 4 MicroViewWidget + +void setup() { + uView.begin(); // init and start MicroView + widget[0] = new MicroViewSlider(0,0,0,255); // declare widget0 as a Slider at x=0, y=0, min=0, max=100 + widget[1] = new MicroViewGauge(32,24,0,255,WIDGETSTYLE0); // declare widget0 as a Slider at x=0, y=10, min=0, max=150 + uView.clear(PAGE); // erase the memory buffer, when next uView.display() is called, the OLED will be cleared. +} + +void loop() { + + widget[0]->reDraw(); + for (int i=0;i<=255;i++) { + widget[0]->setValue(i); // set value i to widget0 + uView.display(); + } + + delay(1000); + uView.clear(PAGE); + + widget[1]->reDraw(); + for (int i=0;i<=255;i++) { + widget[1]->setValue(i); // set value i to widget0 + uView.display(); + } + + delay(1000); + uView.clear(PAGE); +}