Skip to content

Commit ec1c760

Browse files
committed
added round gauge widget
1 parent 684a5dc commit ec1c760

File tree

3 files changed

+153
-23
lines changed

3 files changed

+153
-23
lines changed

MicroView.cpp

Lines changed: 135 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -655,14 +655,14 @@ size_t MicroView::write(uint8_t c) {
655655
setX(newx);
656656
setY(newy);
657657
value=0;
658-
// if (min>max) {
659-
// setMinValue(max);
660-
// setMaxValue(min);
661-
// }
662-
// else {
663-
setMinValue(min);
664-
setMaxValue(max);
665-
// }
658+
// if (min>max) {
659+
// setMinValue(max);
660+
// setMaxValue(min);
661+
// }
662+
// else {
663+
setMinValue(min);
664+
setMaxValue(max);
665+
// }
666666
//drawFace();
667667
//setValue(min);
668668
}
@@ -686,18 +686,20 @@ size_t MicroView::write(uint8_t c) {
686686
}
687687
}
688688

689+
// -------------------------------------------------------------------------------------
690+
// Slider Widget - start
691+
// -------------------------------------------------------------------------------------
689692

690693
MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_t max):MicroViewWidget(newx, newy, min, max) {
691694
style=0;
692695
totalTicks=30;
693-
694696
needFirstDraw=true;
695697
prevValue=getMinValue();
696698
drawFace();
697699
draw();
698700
}
699701

700-
MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_t max, uint8_t sty):MicroViewWidget(newx, newy, min, max) {
702+
MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_t max, uint8_t sty):MicroViewWidget(newx, newy, min, max) {
701703
if (sty==WIDGETSTYLE0) {
702704
style=0;
703705
totalTicks=30;
@@ -720,10 +722,10 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_
720722
offsetY=getY();
721723

722724
if(style>0)
723-
majorLine=7;
725+
majorLine=7;
724726
else
725-
majorLine=4;
726-
727+
majorLine=4;
728+
727729
// Draw major tickers
728730
for (uint8_t i=0; i<majorLine;i++) {
729731
uView.lineV(offsetX+1+(i*10), offsetY+3, 5);
@@ -740,18 +742,18 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_
740742
}
741743

742744
if(style>0) {
743-
for (uint8_t i=0; i<4;i++) {
744-
uView.lineV(offsetX+33+(i*2), offsetY+5, 3);
745-
}
746-
if (style>0) {
747745
for (uint8_t i=0; i<4;i++) {
748-
uView.lineV(offsetX+43+(i*2), offsetY+5, 3);
746+
uView.lineV(offsetX+33+(i*2), offsetY+5, 3);
749747
}
750-
for (uint8_t i=0; i<4;i++) {
751-
uView.lineV(offsetX+53+(i*2), offsetY+5, 3);
748+
if (style>0) {
749+
for (uint8_t i=0; i<4;i++) {
750+
uView.lineV(offsetX+43+(i*2), offsetY+5, 3);
751+
}
752+
for (uint8_t i=0; i<4;i++) {
753+
uView.lineV(offsetX+53+(i*2), offsetY+5, 3);
754+
}
752755
}
753756
}
754-
}
755757

756758
}
757759

@@ -789,6 +791,118 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_
789791
uView.print(strBuffer);
790792
}
791793

794+
// -------------------------------------------------------------------------------------
795+
// Slider Widget - end
796+
// -------------------------------------------------------------------------------------
797+
798+
// -------------------------------------------------------------------------------------
799+
// Gauge Widget - start
800+
// -------------------------------------------------------------------------------------
801+
802+
MicroViewGauge::MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t max):MicroViewWidget(newx, newy, min, max) {
803+
style=0;
804+
radius=15;
805+
needFirstDraw=true;
806+
prevValue=getMinValue();
807+
drawFace();
808+
draw();
809+
}
810+
811+
MicroViewGauge::MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t max, uint8_t sty):MicroViewWidget(newx, newy, min, max) {
812+
if (sty==WIDGETSTYLE0) {
813+
style=0;
814+
radius=15;
815+
}
816+
else {
817+
style=1;
818+
radius=23;
819+
}
820+
needFirstDraw=true;
821+
prevValue=getMinValue();
822+
drawFace();
823+
draw();
824+
}
825+
826+
827+
void MicroViewGauge::drawFace() {
828+
uint8_t offsetX, offsetY, majorLine;
829+
float degreeSec, fromSecX, fromSecY, toSecX, toSecY;
830+
offsetX=getX();
831+
offsetY=getY();
832+
833+
uView.circle(offsetX,offsetY,radius);
834+
835+
for (int i=150;i<=390;i+=30) { // Major tick from 150 degree to 390 degree
836+
degreeSec=i*(PI/180);
837+
fromSecX = cos(degreeSec) * (radius / 1.5);
838+
fromSecY = sin(degreeSec) * (radius / 1.5);
839+
toSecX = cos(degreeSec) * (radius / 1);
840+
toSecY = sin(degreeSec) * (radius / 1);
841+
uView.line(1+offsetX+fromSecX,1+offsetY+fromSecY,1+offsetX+toSecX,1+offsetY+toSecY);
842+
}
843+
844+
if(radius>15) {
845+
for (int i=150;i<=390;i+=15) { // Minor tick from 150 degree to 390 degree
846+
degreeSec=i*(PI/180);
847+
fromSecX = cos(degreeSec) * (radius / 1.3);
848+
fromSecY = sin(degreeSec) * (radius / 1.3);
849+
toSecX = cos(degreeSec) * (radius / 1);
850+
toSecY = sin(degreeSec) * (radius / 1);
851+
uView.line(1+offsetX+fromSecX,1+offsetY+fromSecY,1+offsetX+toSecX,1+offsetY+toSecY);
852+
}
853+
}
854+
}
855+
856+
void MicroViewGauge::draw() {
857+
uint8_t offsetX, offsetY;
858+
uint8_t tickPosition=0;
859+
float degreeSec, fromSecX, fromSecY, toSecX, toSecY;
860+
861+
char strBuffer[5];
862+
offsetX=getX();
863+
offsetY=getY();
864+
865+
if (needFirstDraw) {
866+
degreeSec = (((float)(prevValue-getMinValue())/(float)(getMaxValue()-getMinValue()))*240); // total 240 degree in the widget
867+
degreeSec = (degreeSec+150) * (PI/180); // 150 degree starting point
868+
toSecX = cos(degreeSec) * (radius / 1.2);
869+
toSecY = sin(degreeSec) * (radius / 1.2);
870+
uView.line(offsetX,offsetY,1+offsetX+toSecX,1+offsetY+toSecY, WHITE,XOR);
871+
sprintf(strBuffer,"%4d", prevValue); // we need to force 4 digit so that blank space will cover larger value
872+
needFirstDraw=false;
873+
}
874+
else {
875+
// Draw previous pointer in XOR mode to erase it
876+
degreeSec = (((float)(prevValue-getMinValue())/(float)(getMaxValue()-getMinValue()))*240); // total 240 degree in the widget
877+
degreeSec = (degreeSec+150) * (PI/180);
878+
toSecX = cos(degreeSec) * (radius / 1.2);
879+
toSecY = sin(degreeSec) * (radius / 1.2);
880+
uView.line(offsetX,offsetY,1+offsetX+toSecX,1+offsetY+toSecY, WHITE,XOR);
881+
882+
degreeSec = (((float)(getValue()-getMinValue())/(float)(getMaxValue()-getMinValue()))*240); // total 240 degree in the widget
883+
degreeSec = (degreeSec+150) * (PI/180); // 150 degree starting point
884+
toSecX = cos(degreeSec) * (radius / 1.2);
885+
toSecY = sin(degreeSec) * (radius / 1.2);
886+
uView.line(offsetX,offsetY,1+offsetX+toSecX,1+offsetY+toSecY, WHITE,XOR);
887+
888+
sprintf(strBuffer,"%4d", getValue()); // we need to force 4 digit so that blank space will cover larger value
889+
prevValue=getValue();
890+
}
891+
892+
// Draw value
893+
if(style>0)
894+
uView.setCursor(offsetX-10,offsetY+10);
895+
else
896+
uView.setCursor(offsetX-11,offsetY+11);
897+
898+
uView.print(strBuffer);
899+
}
900+
901+
// -------------------------------------------------------------------------------------
902+
// Slider Widget - end
903+
// -------------------------------------------------------------------------------------
904+
905+
792906
void MVSPIClass::begin() {
793907
// Set SS to high so a connected chip will be "deselected" by default
794908
digitalWrite(SS, HIGH);

MicroView.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,17 @@ class MicroViewSlider: public MicroViewWidget{
174174
int16_t prevValue;
175175
};
176176

177+
class MicroViewGauge: public MicroViewWidget{
178+
public:
179+
MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t max);
180+
MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t max, uint8_t sty);
181+
void draw();
182+
void drawFace();
183+
private:
184+
uint8_t radius, style;
185+
bool needFirstDraw;
186+
int16_t prevValue;
187+
};
177188

178189
#define SPI_CLOCK_DIV4 0x00
179190
#define SPI_CLOCK_DIV16 0x01

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,19 @@ void loop() {
5454
<pre><code>
5555
#include &lt;MicroView.h&gt;
5656

57-
MicroViewWidget *widget;
57+
MicroViewWidget *widget,*widget2;
5858

5959
void setup() {
6060
uView.begin();
61-
widget = new MicroViewSlider(0,0,0,100); // draw Slider widget at x=0,y=0,min=0, max=100
61+
uView.clear(PAGE);
62+
widget= new MicroViewGauge(32,30,0,100); // draw Gauge widget at x=32,y=30,min=0, max=100
63+
widget2= new MicroViewSlider(0,0,0,100); // draw Slider widget at x=0,y=0,min=0, max=100
6264
}
6365

6466
void loop() {
6567
for(int i=0; i&lt;=100;i++) {
6668
widget->setValue(i); // give a value to widget
69+
widget2->setValue(i);
6770
uView.display(); // display current page buffer
6871
}
6972
}
@@ -72,6 +75,8 @@ void loop() {
7275
## History
7376
**v1.06b: by JP Liew**
7477
* fixed Slider negative value not working
78+
* added round Gauge widget
79+
* changed Example 3 to show round Gauge
7580

7681
**v1.05b: 6th February by JP Liew**
7782
* changed MICROVIEW class name to MicroView

0 commit comments

Comments
 (0)