5 Commits

Author SHA1 Message Date
JP
ec1c760c7c added round gauge widget 2014-02-08 20:34:28 +11:00
JP
684a5dc5c8 fixed Example 3 2014-02-07 14:15:49 +11:00
JP
8c12a660e6 added Example 3 2014-02-07 14:15:12 +11:00
JP
af4f0bea23 added Example 3 2014-02-07 14:13:35 +11:00
JP
7f15062e58 fixed Slider negative value not working 2014-02-07 14:05:52 +11:00
3 changed files with 267 additions and 47 deletions

View File

@@ -655,15 +655,16 @@ 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);
}
setValue(min);
// if (min>max) {
// setMinValue(max);
// setMaxValue(min);
// }
// else {
setMinValue(min);
setMaxValue(max);
// }
//drawFace();
//setValue(min);
}
uint8_t MicroViewWidget::getX() { return x; }
@@ -685,22 +686,48 @@ 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) {
totalTicks=40;
style=0;
totalTicks=30;
needFirstDraw=true;
prevValue=getMinValue();
drawFace();
draw();
}
void MicroViewSlider::draw() {
uint8_t offsetX, offsetY;
uint8_t tickPosition=0;
char strBuffer[5];
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;
}
else {
style=1;
totalTicks=60;
}
needFirstDraw=true;
prevValue=getMinValue();
drawFace();
draw();
}
void MicroViewSlider::drawFace() {
uint8_t offsetX, offsetY, majorLine;
offsetX=getX();
offsetY=getY();
if(style>0)
majorLine=7;
else
majorLine=4;
// Draw major tickers
for (uint8_t i=0; i<5;i++) {
for (uint8_t i=0; i<majorLine;i++) {
uView.lineV(offsetX+1+(i*10), offsetY+3, 5);
}
// Draw minor tickers
@@ -713,36 +740,170 @@ size_t MicroView::write(uint8_t c) {
for (uint8_t i=0; i<4;i++) {
uView.lineV(offsetX+23+(i*2), offsetY+5, 3);
}
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+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);
}
for (uint8_t i=0; i<4;i++) {
uView.lineV(offsetX+53+(i*2), offsetY+5, 3);
}
}
}
}
void MicroViewSlider::draw() {
uint8_t offsetX, offsetY;
uint8_t tickPosition=0;
char strBuffer[5];
offsetX=getX();
offsetY=getY();
if (needFirstDraw) {
uView.lineH(offsetX,offsetY, 3, WHITE,XOR);
uView.pixel(offsetX+1,offsetY+1, WHITE,XOR);
uView.lineH(offsetX+tickPosition,offsetY, 3, WHITE, XOR);
uView.pixel(offsetX+1+tickPosition,offsetY+1, 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
tickPosition= (((float)prevValue/(float)(getMaxValue()-getMinValue()))*totalTicks);
tickPosition= (((float)(prevValue-getMinValue())/(float)(getMaxValue()-getMinValue()))*totalTicks);
uView.lineH(offsetX+tickPosition,offsetY, 3, WHITE, XOR);
uView.pixel(offsetX+1+tickPosition,offsetY+1, WHITE, XOR);
// Draw current pointer
tickPosition= (((float)getValue()/(float)(getMaxValue()-getMinValue()))*totalTicks);
tickPosition= (((float)(getValue()-getMinValue())/(float)(getMaxValue()-getMinValue()))*totalTicks);
uView.lineH(offsetX+tickPosition,offsetY, 3, WHITE, XOR);
uView.pixel(offsetX+1+tickPosition,offsetY+1, WHITE, XOR);
sprintf(strBuffer,"%4d", getValue()); // we need to force 4 digit so that blank space will cover larger value
prevValue=getValue();
}
// Draw value
uView.setCursor(offsetX+44,offsetY);
sprintf(strBuffer,"%3d", getValue()); // we need to force only 3 digit or it will go over the display.
if(style>0)
uView.setCursor(offsetX,offsetY+10);
else
uView.setCursor(offsetX+34,offsetY+1);
uView.print(strBuffer);
prevValue=getValue();
}
void SPIClass::begin() {
// -------------------------------------------------------------------------------------
// 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);
@@ -768,11 +929,11 @@ size_t MicroView::write(uint8_t c) {
}
void SPIClass::end() {
void MVSPIClass::end() {
SPCR &= ~_BV(SPE);
}
void SPIClass::setBitOrder(uint8_t bitOrder)
void MVSPIClass::setBitOrder(uint8_t bitOrder)
{
if(bitOrder == LSBFIRST) {
SPCR |= _BV(DORD);
@@ -781,18 +942,18 @@ size_t MicroView::write(uint8_t c) {
}
}
void SPIClass::setDataMode(uint8_t mode)
void MVSPIClass::setDataMode(uint8_t mode)
{
SPCR = (SPCR & ~SPI_MODE_MASK) | mode;
}
void SPIClass::setClockDivider(uint8_t rate)
void MVSPIClass::setClockDivider(uint8_t rate)
{
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (rate & SPI_CLOCK_MASK);
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((rate >> 2) & SPI_2XCLOCK_MASK);
}
SPIClass MVSPI;
MVSPIClass MVSPI;
MicroView uView;

View File

@@ -8,10 +8,11 @@
#define swap(a, b) { uint8_t t = a; a = b; b = t; }
#define DC 8
#define RESET 12
// SS, SCK, MOSI already defined by original SPI.h
//#define CS 10
//#define SCK 13
//#define MOSI 11
#define RESET 12
//#define CS 10
#define BLACK 0
#define WHITE 1
@@ -26,6 +27,10 @@
#define PAGE 0
#define ALL 1
#define WIDGETSTYLE0 0
#define WIDGETSTYLE1 1
#define WIDGETSTYLE2 2
#define SETCONTRAST 0x81
#define DISPLAYALLONRESUME 0xA4
#define DISPLAYALLON 0xA5
@@ -147,6 +152,8 @@ public:
void setMinValue(int16_t max);
void setValue(int16_t val);
virtual void draw(){};
virtual void drawFace(){};
private:
uint8_t x;
uint8_t y;
@@ -158,13 +165,26 @@ private:
class MicroViewSlider: public MicroViewWidget{
public:
MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_t max);
MicroViewSlider(uint8_t newx, uint8_t newy, int16_t min, int16_t max, uint8_t sty);
void draw();
void drawFace();
private:
uint8_t totalTicks;
uint8_t totalTicks, style;
bool needFirstDraw;
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
@@ -184,7 +204,7 @@ private:
#define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR
#define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR
class SPIClass {
class MVSPIClass {
public:
inline static byte transfer(byte _data);
@@ -201,20 +221,20 @@ public:
static void setClockDivider(uint8_t);
};
extern SPIClass MVSPI;
extern MVSPIClass MVSPI;
byte SPIClass::transfer(byte _data) {
byte MVSPIClass::transfer(byte _data) {
SPDR = _data;
while (!(SPSR & _BV(SPIF)))
;
return SPDR;
}
void SPIClass::attachInterrupt() {
void MVSPIClass::attachInterrupt() {
SPCR |= _BV(SPIE);
}
void SPIClass::detachInterrupt() {
void MVSPIClass::detachInterrupt() {
SPCR &= ~_BV(SPIE);
}

View File

@@ -16,13 +16,27 @@ Arduino library for MicroView.
2. Start Arduino IDE.
3. MicroView example is located at, File--->Example--->MicroView--->MicroViewDemo
### Example
### Example 1
<pre><code>
#include &lt;MicroView.h&gt;
void setup() {
uView.begin();
uView.clear(PAGE); // clear the page buffer
}
void loop() {
uView.print("HelloWorld");
uView.display(); // display current page buffer
}
</code></pre>
### Example 2
<pre><code>
#include &lt;MicroView.h&gt;
void setup() {
uView.begin();
uView.clear(PAGE); // clear the page buffer
}
void loop() {
@@ -32,11 +46,38 @@ void loop() {
uView.pixel(50,5);
uView.setCursor(0,40);
uView.print(" MicroView");
uView.display(); // display current page buffer
uView.display(); // display current page buffer
}
</code></pre>
### Example 3
<pre><code>
#include &lt;MicroView.h&gt;
MicroViewWidget *widget,*widget2;
void setup() {
uView.begin();
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
}
}
</code></pre>
## 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
* created MICROVIEWWIDGET class
@@ -45,7 +86,6 @@ void loop() {
* merged MicroViewWidget into MicroView
* merged SPI.h into MicroView
**v1.04b: 3rd February 2014 by JP Liew**
* declared permanent uView variable.
* cleaned code and added some remarks.
@@ -63,5 +103,4 @@ void loop() {
* added analog clock demo.
**v1.00b: 30th January 2014 by JP Liew**
* Initial commit. Beta with minor bugs.
* Initial commit. Beta with minor bugs.