diff --git a/MicroView.cpp b/MicroView.cpp index fdcaa12..3827595 100644 --- a/MicroView.cpp +++ b/MicroView.cpp @@ -143,7 +143,6 @@ void MicroView::begin() { command(DISPLAYALLONRESUME); // 0xA4 command(SEGREMAP | 0x1); - command(COMSCANDEC); command(SETCOMPINS); // 0xDA @@ -667,10 +666,28 @@ size_t MicroView::write(uint8_t c) { command(ACTIVATESCROLL); } - void MicroView::doCmd(uint8_t cmdCount) { - // decode command - switch (serCmd[0]) { - case CMD_CLEAR: { + void MicroView::flipVertical(boolean flip) { + if (flip) { + command(COMSCANINC); + } + else { + command(COMSCANDEC); + } + } + + void MicroView::flipHorizontal(boolean flip) { + if (flip) { + command(SEGREMAP | 0x0); + } + else { + command(SEGREMAP | 0x1); + } + } + +void MicroView::doCmd(uint8_t cmdCount) { + // decode command + switch (serCmd[0]) { + case CMD_CLEAR: { Serial.println("clear"); if (cmdCount==1) { clear(serCmd[1]); @@ -680,7 +697,7 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_INVERT: { + case CMD_INVERT: { Serial.println("invert"); if (cmdCount==1) { invert(serCmd[1]); @@ -688,7 +705,7 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_CONTRAST: { + case CMD_CONTRAST: { Serial.println("contrast"); if (cmdCount==1) { contrast(serCmd[1]); @@ -696,7 +713,7 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_DISPLAY: { + case CMD_DISPLAY: { Serial.println("display"); if (cmdCount==0) { display(); @@ -704,7 +721,7 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_SETCURSOR: { + case CMD_SETCURSOR: { Serial.println("setCursor"); if (cmdCount==2) { setCursor(serCmd[1], serCmd[2]); @@ -712,7 +729,7 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_PIXEL: { + case CMD_PIXEL: { Serial.println("pixel"); if (cmdCount==2) { pixel(serCmd[1],serCmd[2]); @@ -724,7 +741,7 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_LINE: { + case CMD_LINE: { Serial.println("line"); if (cmdCount==4) { line(serCmd[1],serCmd[2],serCmd[3],serCmd[4]); @@ -736,7 +753,7 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_LINEH: { + case CMD_LINEH: { Serial.println("lineH"); if (cmdCount==3) { lineH(serCmd[1], serCmd[2], serCmd[3]); @@ -748,7 +765,7 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_LINEV: { + case CMD_LINEV: { Serial.println("lineV"); if (cmdCount==3) { lineV(serCmd[1], serCmd[2], serCmd[3]); @@ -760,7 +777,7 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_RECT: { + case CMD_RECT: { Serial.println("rect"); if (cmdCount==4) { rect(serCmd[1], serCmd[2], serCmd[3], serCmd[4]); @@ -772,7 +789,7 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_RECTFILL: { + case CMD_RECTFILL: { Serial.println("rectFill"); if (cmdCount==4) { rectFill(serCmd[1], serCmd[2], serCmd[3], serCmd[4]); @@ -785,7 +802,7 @@ size_t MicroView::write(uint8_t c) { } - case CMD_CIRCLE: { + case CMD_CIRCLE: { Serial.println("circle"); if (cmdCount==3) { circle(serCmd[1], serCmd[2], serCmd[3]); @@ -797,7 +814,7 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_CIRCLEFILL: { + case CMD_CIRCLEFILL: { Serial.println("circleFill"); if (cmdCount==3) { @@ -810,7 +827,7 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_DRAWCHAR: { + case CMD_DRAWCHAR: { Serial.println("drawChar"); if (cmdCount==3) { drawChar(serCmd[1], serCmd[2], serCmd[3]); @@ -822,13 +839,13 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_DRAWBITMAP: { + case CMD_DRAWBITMAP: { // TODO break; } - case CMD_GETLCDWIDTH: { + case CMD_GETLCDWIDTH: { Serial.println("getLCDWidth"); if (cmdCount==0) { @@ -837,7 +854,7 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_GETLCDHEIGHT: { + case CMD_GETLCDHEIGHT: { Serial.println("getLCDHeight"); if (cmdCount==0) { Serial.println(getLCDHeight()); @@ -845,7 +862,7 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_SETCOLOR: { + case CMD_SETCOLOR: { Serial.println("setColor"); if (cmdCount==1) { setColor(serCmd[1]); @@ -853,7 +870,7 @@ size_t MicroView::write(uint8_t c) { break; } - case CMD_SETDRAWMODE: { + case CMD_SETDRAWMODE: { Serial.println("drawMode"); if (cmdCount==1) { setDrawMode(serCmd[1]); @@ -861,37 +878,37 @@ size_t MicroView::write(uint8_t c) { break; } - default: - break; - } + default: + break; } +} + +void MicroView::checkComm(void) { + int count = readSerial(); + char *result; + uint8_t index=0; + int temp; - void MicroView::checkComm(void) { - int count = readSerial(); - char *result; - uint8_t index=0; - int temp; - - if (count>0) { - // process Serial data - result=strtok(serInStr,","); - if (result !=NULL) { - temp=atoi(result); - serCmd[index]=(uint8_t)temp & 0xff; // we only need 8 bit number - index++; - for (uint8_t i;i0) { + // process Serial data + result=strtok(serInStr,","); + if (result !=NULL) { + temp=atoi(result); + serCmd[index]=(uint8_t)temp & 0xff; // we only need 8 bit number + index++; + for (uint8_t i;i0) + uView.setCursor(offsetX,offsetY+10); + else + uView.setCursor(offsetX+34,offsetY+1); + uView.print(strBuffer); +} - needFirstDraw=true; - prevValue=getMinValue(); - drawFace(); - draw(); - } +// ------------------------------------------------------------------------------------- +// Slider Widget - end +// ------------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------- +// Gauge Widget - start +// ------------------------------------------------------------------------------------- - 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; i0) { - 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); - } - } - } - - } +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(); +} - void MicroViewSlider::draw() { - uint8_t offsetX, offsetY; - uint8_t tickPosition=0; - char strBuffer[5]; - offsetX=getX(); - offsetY=getY(); - - if (needFirstDraw) { - 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-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()-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 - if(style>0) - uView.setCursor(offsetX,offsetY+10); - else - uView.setCursor(offsetX+34,offsetY+1); - 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) { +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; - 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(); + 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(); +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 + 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.5); - fromSecY = sin(degreeSec) * (radius / 1.5); + 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); } - - 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(); } - void MicroViewGauge::draw() { - uint8_t offsetX, offsetY; - uint8_t tickPosition=0; - float degreeSec, fromSecX, fromSecY, toSecX, toSecY; + // Draw value + if(style>0) + uView.setCursor(offsetX-10,offsetY+10); + else + uView.setCursor(offsetX-11,offsetY+11); + + uView.print(strBuffer); +} - char strBuffer[5]; - offsetX=getX(); - offsetY=getY(); +// ------------------------------------------------------------------------------------- +// Slider Widget - end +// ------------------------------------------------------------------------------------- - 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); +void MVSPIClass::begin() { + // Set SS to high so a connected chip will be "deselected" by default + digitalWrite(SS, HIGH); - sprintf(strBuffer,"%4d", getValue()); // we need to force 4 digit so that blank space will cover larger value - prevValue=getValue(); - } + // When the SS pin is set as OUTPUT, it can be used as + // a general purpose output port (it doesn't influence + // SPI operations). + pinMode(SS, OUTPUT); - // Draw value - if(style>0) - uView.setCursor(offsetX-10,offsetY+10); - else - uView.setCursor(offsetX-11,offsetY+11); - - uView.print(strBuffer); + // Warning: if the SS pin ever becomes a LOW INPUT then SPI + // automatically switches to Slave, so the data direction of + // the SS pin MUST be kept as OUTPUT. + SPCR |= _BV(MSTR); + SPCR |= _BV(SPE); + + // Set direction register for SCK and MOSI pin. + // MISO pin automatically overrides to INPUT. + // By doing this AFTER enabling SPI, we avoid accidentally + // clocking in a single bit since the lines go directly + // from "input" to SPI control. + // http://code.google.com/p/arduino/issues/detail?id=888 + pinMode(SCK, OUTPUT); + pinMode(MOSI, OUTPUT); +} + + +void MVSPIClass::end() { + SPCR &= ~_BV(SPE); +} + +void MVSPIClass::setBitOrder(uint8_t bitOrder) +{ + if(bitOrder == LSBFIRST) { + SPCR |= _BV(DORD); + } else { + SPCR &= ~(_BV(DORD)); } +} - // ------------------------------------------------------------------------------------- - // Slider Widget - end - // ------------------------------------------------------------------------------------- - - - void MVSPIClass::begin() { - // Set SS to high so a connected chip will be "deselected" by default - digitalWrite(SS, HIGH); - - // When the SS pin is set as OUTPUT, it can be used as - // a general purpose output port (it doesn't influence - // SPI operations). - pinMode(SS, OUTPUT); - - // Warning: if the SS pin ever becomes a LOW INPUT then SPI - // automatically switches to Slave, so the data direction of - // the SS pin MUST be kept as OUTPUT. - SPCR |= _BV(MSTR); - SPCR |= _BV(SPE); - - // Set direction register for SCK and MOSI pin. - // MISO pin automatically overrides to INPUT. - // By doing this AFTER enabling SPI, we avoid accidentally - // clocking in a single bit since the lines go directly - // from "input" to SPI control. - // http://code.google.com/p/arduino/issues/detail?id=888 - pinMode(SCK, OUTPUT); - pinMode(MOSI, OUTPUT); - } - - - void MVSPIClass::end() { - SPCR &= ~_BV(SPE); - } - - void MVSPIClass::setBitOrder(uint8_t bitOrder) - { - if(bitOrder == LSBFIRST) { - SPCR |= _BV(DORD); - } else { - SPCR &= ~(_BV(DORD)); - } - } - - void MVSPIClass::setDataMode(uint8_t mode) - { - SPCR = (SPCR & ~SPI_MODE_MASK) | mode; - } - - 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); - } - - MVSPIClass MVSPI; - MicroView uView; +void MVSPIClass::setDataMode(uint8_t mode) +{ + SPCR = (SPCR & ~SPI_MODE_MASK) | mode; +} + +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); +} + +MVSPIClass MVSPI; +MicroView uView; diff --git a/MicroView.h b/MicroView.h index 35b4953..4acb896 100644 --- a/MicroView.h +++ b/MicroView.h @@ -149,6 +149,8 @@ public: void scrollVertRight(uint8_t start, uint8_t stop); void scrollVertLeft(uint8_t start, uint8_t stop); void scrollStop(void); + void flipVertical(boolean flip); + void flipHorizontal(boolean flip); // Communication void checkComm(void); diff --git a/README.md b/README.md index 5265d09..0733627 100644 --- a/README.md +++ b/README.md @@ -87,19 +87,23 @@ void loop() { ## History -**v1.07b: 15th February by JP Liew** +**v1.08b: 18th February 2014 by JP Liew** +* added verticalFlip(), horizontalFlip() + + +**v1.07b: 15th February 2014 by JP Liew** * changed function name stopScroll to scrollStop for consistency * added contrast function * added invert function * added KEYWORD to keywords.txt * added checkComm() function to communicate with host PC -**v1.06b: 9th February by JP Liew** +**v1.06b: 9th February 2014 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** +**v1.05b: 6th February 2014 by JP Liew** * changed MICROVIEW class name to MicroView * created MICROVIEWWIDGET class * added routines to draw widget diff --git a/keywords.txt b/keywords.txt index 876c626..a10e127 100644 --- a/keywords.txt +++ b/keywords.txt @@ -48,6 +48,8 @@ scrollLeft KEYWORD2 scrollVertRight KEYWORD2 scrollVertLeft KEYWORD2 scrollStop KEYWORD2 +flipVertical KEYWORD2 +flipHorizontal KEYWORD2 getX KEYWORD2 getY KEYWORD2