added round gauge widget

This commit is contained in:
JP
2014-02-08 20:34:28 +11:00
parent 684a5dc5c8
commit 99fafd53bc
3 changed files with 153 additions and 23 deletions

View File

@@ -655,14 +655,14 @@ size_t MicroView::write(uint8_t c) {
setX(newx);
setY(newy);
value=0;
// if (min>max) {
// setMinValue(max);
// setMaxValue(min);
// }
// else {
setMinValue(min);
setMaxValue(max);
// }
// if (min>max) {
// setMinValue(max);
// setMaxValue(min);
// }
// else {
setMinValue(min);
setMaxValue(max);
// }
//drawFace();
//setValue(min);
}
@@ -686,18 +686,20 @@ size_t MicroView::write(uint8_t c) {
}
}
// -------------------------------------------------------------------------------------
// Slider Widget - start
// -------------------------------------------------------------------------------------
MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_t max):MicroViewWidget(newx, newy, min, max) {
style=0;
totalTicks=30;
needFirstDraw=true;
prevValue=getMinValue();
drawFace();
draw();
}
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) {
if (sty==WIDGETSTYLE0) {
style=0;
totalTicks=30;
@@ -720,10 +722,10 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_
offsetY=getY();
if(style>0)
majorLine=7;
majorLine=7;
else
majorLine=4;
majorLine=4;
// Draw major tickers
for (uint8_t i=0; i<majorLine;i++) {
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_
}
if(style>0) {
for (uint8_t i=0; i<4;i++) {
uView.lineV(offsetX+33+(i*2), offsetY+5, 3);
}
if (style>0) {
for (uint8_t i=0; i<4;i++) {
uView.lineV(offsetX+43+(i*2), offsetY+5, 3);
uView.lineV(offsetX+33+(i*2), offsetY+5, 3);
}
for (uint8_t i=0; i<4;i++) {
uView.lineV(offsetX+53+(i*2), offsetY+5, 3);
if (style>0) {
for (uint8_t i=0; i<4;i++) {
uView.lineV(offsetX+43+(i*2), offsetY+5, 3);
}
for (uint8_t i=0; i<4;i++) {
uView.lineV(offsetX+53+(i*2), offsetY+5, 3);
}
}
}
}
}
@@ -789,6 +791,118 @@ MicroViewSlider::MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_
uView.print(strBuffer);
}
// -------------------------------------------------------------------------------------
// Slider Widget - end
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
// Gauge Widget - start
// -------------------------------------------------------------------------------------
MicroViewGauge::MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t max):MicroViewWidget(newx, newy, min, max) {
style=0;
radius=15;
needFirstDraw=true;
prevValue=getMinValue();
drawFace();
draw();
}
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) {
style=0;
radius=15;
}
else {
style=1;
radius=23;
}
needFirstDraw=true;
prevValue=getMinValue();
drawFace();
draw();
}
void MicroViewGauge::drawFace() {
uint8_t offsetX, offsetY, majorLine;
float degreeSec, fromSecX, fromSecY, toSecX, toSecY;
offsetX=getX();
offsetY=getY();
uView.circle(offsetX,offsetY,radius);
for (int i=150;i<=390;i+=30) { // Major tick from 150 degree to 390 degree
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);
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
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);
uView.line(1+offsetX+fromSecX,1+offsetY+fromSecY,1+offsetX+toSecX,1+offsetY+toSecY);
}
}
}
void MicroViewGauge::draw() {
uint8_t offsetX, offsetY;
uint8_t tickPosition=0;
float degreeSec, fromSecX, fromSecY, toSecX, toSecY;
char strBuffer[5];
offsetX=getX();
offsetY=getY();
if (needFirstDraw) {
degreeSec = (((float)(prevValue-getMinValue())/(float)(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,"%4d", prevValue); // we need to force 4 digit so that blank space will cover larger value
needFirstDraw=false;
}
else {
// 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);
degreeSec = (((float)(getValue()-getMinValue())/(float)(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,"%4d", getValue()); // we need to force 4 digit so that blank space will cover larger value
prevValue=getValue();
}
// Draw value
if(style>0)
uView.setCursor(offsetX-10,offsetY+10);
else
uView.setCursor(offsetX-11,offsetY+11);
uView.print(strBuffer);
}
// -------------------------------------------------------------------------------------
// Slider Widget - end
// -------------------------------------------------------------------------------------
void MVSPIClass::begin() {
// Set SS to high so a connected chip will be "deselected" by default
digitalWrite(SS, HIGH);

View File

@@ -174,6 +174,17 @@ private:
int16_t prevValue;
};
class MicroViewGauge: public MicroViewWidget{
public:
MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t max);
MicroViewGauge(uint8_t newx, uint8_t newy, int16_t min, int16_t max, uint8_t sty);
void draw();
void drawFace();
private:
uint8_t radius, style;
bool needFirstDraw;
int16_t prevValue;
};
#define SPI_CLOCK_DIV4 0x00
#define SPI_CLOCK_DIV16 0x01

View File

@@ -54,16 +54,19 @@ void loop() {
<pre><code>
#include &lt;MicroView.h&gt;
MicroViewWidget *widget;
MicroViewWidget *widget,*widget2;
void setup() {
uView.begin();
widget = new MicroViewSlider(0,0,0,100); // draw Slider widget at x=0,y=0,min=0, max=100
uView.clear(PAGE);
widget= new MicroViewGauge(32,30,0,100); // draw Gauge widget at x=32,y=30,min=0, max=100
widget2= new MicroViewSlider(0,0,0,100); // draw Slider widget at x=0,y=0,min=0, max=100
}
void loop() {
for(int i=0; i&lt;=100;i++) {
widget->setValue(i); // give a value to widget
widget2->setValue(i);
uView.display(); // display current page buffer
}
}
@@ -72,6 +75,8 @@ void loop() {
## History
**v1.06b: by JP Liew**
* fixed Slider negative value not working
* added round Gauge widget
* changed Example 3 to show round Gauge
**v1.05b: 6th February by JP Liew**
* changed MICROVIEW class name to MicroView