fixed Slider negative value not working

This commit is contained in:
JP
2014-02-07 14:05:52 +11:00
parent 1fcf3a0839
commit 7f15062e58
3 changed files with 113 additions and 42 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 {
// if (min>max) {
// setMinValue(max);
// setMaxValue(min);
// }
// else {
setMinValue(min);
setMaxValue(max);
}
setValue(min);
// }
//drawFace();
//setValue(min);
}
uint8_t MicroViewWidget::getX() { return x; }
@@ -685,22 +686,46 @@ size_t MicroView::write(uint8_t c) {
}
}
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 +738,58 @@ size_t MicroView::write(uint8_t c) {
for (uint8_t i=0; i<4;i++) {
uView.lineV(offsetX+23+(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() {
void MVSPIClass::begin() {
// Set SS to high so a connected chip will be "deselected" by default
digitalWrite(SS, HIGH);
@@ -768,11 +815,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 +828,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,9 +165,11 @@ 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;
};
@@ -184,7 +193,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 +210,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,7 +16,21 @@ 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();
}
void loop() {
uView.print("HelloWorld");
uView.display(); // display current page buffer
}
</code></pre>
### Example 2
<pre><code>
#include &lt;MicroView.h&gt;
@@ -37,6 +51,9 @@ void loop() {
</code></pre>
## History
**v1.06b: by JP Liew**
* fixed Slider negative value not working
**v1.05b: 6th February by JP Liew**
* changed MICROVIEW class name to MicroView
* created MICROVIEWWIDGET class
@@ -45,7 +62,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 +79,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.