4D Systems serial microOLED driver http://www.4dsystems.com.au/prod.php?id=77 http://www.sparkfun.com/products/8538

Files at this revision

API Documentation at this revision

Comitter:
evwijk
Date:
Fri Nov 19 13:30:42 2010 +0000
Parent:
0:d64e7277c3e5
Commit message:
0.2

Changed in this revision

MicroOLED.cpp Show annotated file Show diff for this revision Revisions of this file
MicroOLED.h Show annotated file Show diff for this revision Revisions of this file
--- a/MicroOLED.cpp	Wed Nov 17 20:53:12 2010 +0000
+++ b/MicroOLED.cpp	Fri Nov 19 13:30:42 2010 +0000
@@ -1,286 +1,289 @@
-#include "MicroOLED.h"
-#include "mbed.h"
-
-
-#define     OLED_INITDELAYMS            500
-
-#define     OLED_ACK                    0x06  // Ok
-#define     OLED_NAK                    0x15  // Error
-
-
-
-MicroOLED::MicroOLED(PinName serialTX, PinName serialRX, PinName reset) :
-    _oled(serialTX, serialRX),
-    _reset(reset) {
-
-        _oled.baud(57600);
-
-        //_reset = 0;
-}
-
-/******************/
-/* Protected      */
-/******************/
-void MicroOLED::resetDisplay() {
-    _reset = 0;
-    wait_ms(20);
-    _reset = 1;
-    wait_ms(20);
-}
-
-
-/******************/
-/* Public         */
-/******************/
-short MicroOLED::getRGB(char red, char green, char blue) {
-    int outR = ((red * 31) / 255);
-    int outG = ((green * 63) / 255);
-    int outB = ((blue * 31) / 255);
-    
-    return (outR << 11) | (outG << 5) | outB;
-}
-
-bool MicroOLED::addBitmappedCharacter(char character, char data[8]) {
-    _oled.putc(0x41);
-    _oled.putc(character);
-    for (int i=0; i<8; i++) _oled.putc(data[i]);
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::blockCopyPaste(char sourceX, char sourceY, char destinationX, char destinationY, char width, char height) {
-    _oled.putc(0x63);
-    _oled.putc(sourceX);
-    _oled.putc(sourceY);
-    _oled.putc(destinationX);
-    _oled.putc(destinationY);
-    _oled.putc(width);
-    _oled.putc(height);
-
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::displayControl(char mode) {
-    _oled.putc(0x59);
-    _oled.putc(mode);
-    
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::displayUserBitmappedCharacter(char character, char x, char y, short color) {
-    _oled.putc(0x44);  
-    _oled.putc(character);
-    _oled.putc(x);
-    _oled.putc(y);
-    _oled.putc(color >> 8);
-    _oled.putc(color & 0xFF);
-    
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::drawCharacter(char character, char column, char row, short color) {
-    _oled.putc(0x54);
-    _oled.putc(character);
-    _oled.putc(column);
-    _oled.putc(row);    
-    _oled.putc(color >> 8);
-    _oled.putc(color & 0xFF);
-    
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::drawCircle(char x, char y, char radius, short color) {
-    _oled.putc(0x43);     
-    _oled.putc(x);
-    _oled.putc(y);
-    _oled.putc(radius);
-    _oled.putc(color >> 8);
-    _oled.putc(color & 0xFF);
-    
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::drawImage(char x, char y, char width, char height, char colorMode, char *pixels) {
-    _oled.putc(0x49);
-    _oled.putc(x);
-    _oled.putc(y);
-    _oled.putc(width);
-    _oled.putc(height);
-    _oled.putc(colorMode);
-    for (int i=0; i<(width * height); i++) {
-        _oled.putc(pixels[i]);
-    }
-
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::drawLine(char x1, char y1, char x2, char y2, short color) {
-    _oled.putc(0x4C);
-    _oled.putc(x1);
-    _oled.putc(y1);
-    _oled.putc(x2);
-    _oled.putc(y2);
-    _oled.putc(color >> 8);
-    _oled.putc(color & 0xFF);
-    
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::drawPolygon(char vertices, char *x, char *y, short color) {
-    _oled.putc(0x67);
-    _oled.putc(vertices);
-    for (int i=0; i<vertices; i++) {
-        _oled.putc(x[i]);
-        _oled.putc(y[i]);
-    }
-    _oled.putc(color >> 8);
-    _oled.putc(color & 0xFF);
-    
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::drawRectangle(char x, char y, char width, char height, short color) {
-    _oled.putc(0x72);
-    _oled.putc(x);
-    _oled.putc(y);   
-    _oled.putc(x + width);
-    _oled.putc(y + height);
-    _oled.putc(color >> 8);
-    _oled.putc(color & 0xFF);
-
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::drawText(char column, char row, char font, short color, char *text) {
-    _oled.putc(0x73);
-    _oled.putc(column);
-    _oled.putc(row);
-    _oled.putc(font);   
-    _oled.putc(color >> 8);
-    _oled.putc(color & 0xFF);  
-    for (int i=0 ; i<strlen(text) ; i++) _oled.putc(text[i]);
-    _oled.putc(0x00);
-    
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::drawTriangle(char x1, char y1, char x2, char y2, char x3, char y3, short color) {
-    _oled.putc(0x47);
-    _oled.putc(x1);
-    _oled.putc(y1);
-    _oled.putc(x2);
-    _oled.putc(y2);
-    _oled.putc(x3);
-    _oled.putc(y3);
-    _oled.putc(color >> 8);
-    _oled.putc(color & 0xFF);
-
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::eraseScreen() {
-    _oled.putc(0x45);
-
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::init() {
-    resetDisplay();  
-    
-    wait_ms(OLED_INITDELAYMS); 
-    _oled.putc(0x55);
-    
-    _oled.getc();
-    
-    _oled.putc(0x4F);
-    _oled.putc(0x01);
-
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::penSize(char size) {
-    _oled.putc(0x70);
-    _oled.putc(size);
-    
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::putPixel(char x, char y, short color) {
-    _oled.putc(0x50);
-    _oled.putc(x);
-    _oled.putc(y);
-    
-    _oled.putc(color >> 8);
-    _oled.putc(color & 0xFF);
-    
-    return (_oled.getc() == OLED_ACK);
-}
-
-short MicroOLED::readPixel(char x, char y) {
-    short returnValue;
-    
-    _oled.putc(0x52);
-    _oled.putc(x);
-    _oled.putc(y);
-    returnValue = (_oled.getc() << 8);
-    returnValue += _oled.getc();
-    
-    return returnValue;
-}
-
-bool MicroOLED::setBackgroundColor(short color) {
-    _oled.putc(0x42);
-
-    _oled.putc(color >> 8);
-    _oled.putc(color & 0xFF);
-
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::setFontSize(char fontType) {
-    _oled.putc(0x46);
-    _oled.putc(fontType);
-    
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::textButton(char state, char x, char y, short buttonColor, char font, short textColor, char textWidth, char textHeight, char *text) {
-    _oled.putc(0x62);
-    _oled.putc(state);
-    _oled.putc(x);
-    _oled.putc(y);
-    _oled.putc(buttonColor >> 8);
-    _oled.putc(buttonColor & 0xFF);
-    _oled.putc(font);
-    _oled.putc(textColor >> 8);
-    _oled.putc(textColor & 0xFF);
-    _oled.putc(textWidth);
-    _oled.putc(textHeight);
-    for (int i=0 ; i<strlen(text) ; i++) _oled.putc(text[i]);   
-    _oled.putc(0x00);
-    
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::textMode(char mode) {
-    _oled.putc(0x4F);
-    _oled.putc(mode);
-    
-    return (_oled.getc() == OLED_ACK);
-}
-
-bool MicroOLED::versionInfo(bool onScreen, char *info) {
-    _oled.putc(0x56);
-    if (onScreen) {
-        _oled.putc(0x01);
-    } else {
-        _oled.putc(0x00);
-    }
-    
-    info[0] = _oled.getc();
-    info[1] = _oled.getc();
-    info[2] = _oled.getc();
-    info[3] = _oled.getc();
-    info[4] = _oled.getc();
-    
-    return true;    
+#include "MicroOLED.h"
+#include "mbed.h"
+
+
+#define     OLED_INITDELAYMS            500
+
+#define     OLED_ACK                    0x06  // Ok
+#define     OLED_NAK                    0x15  // Error
+
+
+
+MicroOLED::MicroOLED(PinName serialTX, PinName serialRX, PinName reset) :
+    _oled(serialTX, serialRX),
+    _reset(reset) {
+
+    _oled.baud(230400);
+}
+
+/******************/
+/* Protected      */
+/******************/
+void MicroOLED::resetDisplay() {
+    _reset = 0;
+    wait_ms(20);
+    _reset = 1;
+    wait_ms(20);
+}
+
+
+/******************/
+/* Public         */
+/******************/
+short MicroOLED::getRGB(char red, char green, char blue) {
+    int outR = ((red * 31) / 255);
+    int outG = ((green * 63) / 255);
+    int outB = ((blue * 31) / 255);
+    
+    return (outR << 11) | (outG << 5) | outB;
+}
+
+bool MicroOLED::addBitmappedCharacter(char character, char data[8]) {
+    _oled.putc(0x41);
+    _oled.putc(character);
+    for (int i=0; i<8; i++) _oled.putc(data[i]);
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::blockCopyPaste(char sourceX, char sourceY, char destinationX, char destinationY, char width, char height) {
+    _oled.putc(0x63);
+    _oled.putc(sourceX);
+    _oled.putc(sourceY);
+    _oled.putc(destinationX);
+    _oled.putc(destinationY);
+    _oled.putc(width);
+    _oled.putc(height);
+
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::displayControl(char mode) {
+    _oled.putc(0x59);
+    _oled.putc(mode);
+    
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::displayUserBitmappedCharacter(char character, char x, char y, short color) {
+    _oled.putc(0x44);  
+    _oled.putc(character);
+    _oled.putc(x);
+    _oled.putc(y);
+    _oled.putc(color >> 8);
+    _oled.putc(color & 0xFF);
+    
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::drawCharacter(char character, char column, char row, short color) {
+    _oled.putc(0x54);
+    _oled.putc(character);
+    _oled.putc(column);
+    _oled.putc(row);    
+    _oled.putc(color >> 8);
+    _oled.putc(color & 0xFF);
+    
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::drawCircle(char x, char y, char radius, short color) {
+    _oled.putc(0x43);     
+    _oled.putc(x);
+    _oled.putc(y);
+    _oled.putc(radius);
+    _oled.putc(color >> 8);
+    _oled.putc(color & 0xFF);
+    
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::drawImage(char x, char y, char width, char height, char colorMode, char *pixels) {
+    int numberOfBytesPerPixel = 1;
+    
+    _oled.putc(0x49);
+    _oled.putc(x);
+    _oled.putc(y);
+    _oled.putc(width);
+    _oled.putc(height);
+    _oled.putc(colorMode);
+    
+    if (colorMode == 16) numberOfBytesPerPixel = 2;
+        
+    for (int i=0; i<width * height * numberOfBytesPerPixel; i++) {
+        _oled.putc(pixels[i]);
+    }
+
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::drawLine(char x1, char y1, char x2, char y2, short color) {
+    _oled.putc(0x4C);
+    _oled.putc(x1);
+    _oled.putc(y1);
+    _oled.putc(x2);
+    _oled.putc(y2);
+    _oled.putc(color >> 8);
+    _oled.putc(color & 0xFF);
+    
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::drawPolygon(char vertices, char *x, char *y, short color) {
+    _oled.putc(0x67);
+    _oled.putc(vertices);
+    for (int i=0; i<vertices; i++) {
+        _oled.putc(x[i]);
+        _oled.putc(y[i]);
+    }
+    _oled.putc(color >> 8);
+    _oled.putc(color & 0xFF);
+    
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::drawRectangle(char x, char y, char width, char height, short color) {
+    _oled.putc(0x72);
+    _oled.putc(x);
+    _oled.putc(y);   
+    _oled.putc(x + width);
+    _oled.putc(y + height);
+    _oled.putc(color >> 8);
+    _oled.putc(color & 0xFF);
+
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::drawText(char column, char row, char font, short color, char *text) {
+    _oled.putc(0x73);
+    _oled.putc(column);
+    _oled.putc(row);
+    _oled.putc(font);   
+    _oled.putc(color >> 8);
+    _oled.putc(color & 0xFF);  
+    for (int i=0 ; i<strlen(text) ; i++) _oled.putc(text[i]);
+    _oled.putc(0x00);
+    
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::drawTriangle(char x1, char y1, char x2, char y2, char x3, char y3, short color) {
+    _oled.putc(0x47);
+    _oled.putc(x1);
+    _oled.putc(y1);
+    _oled.putc(x2);
+    _oled.putc(y2);
+    _oled.putc(x3);
+    _oled.putc(y3);
+    _oled.putc(color >> 8);
+    _oled.putc(color & 0xFF);
+
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::eraseScreen() {
+    _oled.putc(0x45);
+
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::init() {
+    resetDisplay();  
+    
+    wait_ms(OLED_INITDELAYMS); 
+    _oled.putc(0x55);
+    
+    _oled.getc();
+    
+    _oled.putc(0x4F);
+    _oled.putc(0x01);
+
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::penSize(char size) {
+    _oled.putc(0x70);
+    _oled.putc(size);
+    
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::putPixel(char x, char y, short color) {
+    _oled.putc(0x50);
+    _oled.putc(x);
+    _oled.putc(y);
+    
+    _oled.putc(color >> 8);
+    _oled.putc(color & 0xFF);
+    
+    return (_oled.getc() == OLED_ACK);
+}
+
+short MicroOLED::readPixel(char x, char y) {
+    short returnValue;
+    
+    _oled.putc(0x52);
+    _oled.putc(x);
+    _oled.putc(y);
+    returnValue = (_oled.getc() << 8);
+    returnValue += _oled.getc();
+    
+    return returnValue;
+}
+
+bool MicroOLED::setBackgroundColor(short color) {
+    _oled.putc(0x42);
+
+    _oled.putc(color >> 8);
+    _oled.putc(color & 0xFF);
+
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::setFontSize(char fontType) {
+    _oled.putc(0x46);
+    _oled.putc(fontType);
+    
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::textButton(char state, char x, char y, short buttonColor, char font, short textColor, char textWidth, char textHeight, char *text) {
+    _oled.putc(0x62);
+    _oled.putc(state);
+    _oled.putc(x);
+    _oled.putc(y);
+    _oled.putc(buttonColor >> 8);
+    _oled.putc(buttonColor & 0xFF);
+    _oled.putc(font);
+    _oled.putc(textColor >> 8);
+    _oled.putc(textColor & 0xFF);
+    _oled.putc(textWidth);
+    _oled.putc(textHeight);
+    for (int i=0 ; i<strlen(text) ; i++) _oled.putc(text[i]);   
+    _oled.putc(0x00);
+    
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::textMode(char mode) {
+    _oled.putc(0x4F);
+    _oled.putc(mode);
+    
+    return (_oled.getc() == OLED_ACK);
+}
+
+bool MicroOLED::versionInfo(bool onScreen, char *info) {
+    _oled.putc(0x56);
+    if (onScreen) {
+        _oled.putc(0x01);
+    } else {
+        _oled.putc(0x00);
+    }
+    
+    info[0] = _oled.getc();
+    info[1] = _oled.getc();
+    info[2] = _oled.getc();
+    info[3] = _oled.getc();
+    info[4] = _oled.getc();
+    
+    return true;    
 }
\ No newline at end of file
--- a/MicroOLED.h	Wed Nov 17 20:53:12 2010 +0000
+++ b/MicroOLED.h	Fri Nov 19 13:30:42 2010 +0000
@@ -44,4 +44,50 @@
     DigitalOut  _reset;
     
     void resetDisplay();
+};#include "mbed.h"
+
+#define     OLED_FONT5X7                    0x01
+#define     OLED_FONT8X8                    0x02
+#define     OLED_FONT8X12                   0x03
+
+#define     OLED_DISPLAYCONTROL_DISPLAY     0x01
+#define     OLED_DISPLAYCONTROL_CONTRAST    0x02
+#define     OLED_DISPLAYCONTROL_POWER       0x03
+
+class MicroOLED {
+public:
+
+    MicroOLED(PinName serialTX, PinName serialRX, PinName reset);
+
+    short getRGB(char red, char green, char blue);
+
+    bool addBitmappedCharacter(char character, char data[8]);
+    bool blockCopyPaste(char sourceX, char sourceY, char destinationX, char destinationY, char width, char height);
+    bool displayControl(char mode);
+    bool displayUserBitmappedCharacter(char character, char x, char y, short color);
+    bool drawCircle(char x, char y, char radius, short color);
+    bool drawCharacter(char character, char column, char row, short color);
+    bool drawImage(char x, char y, char width, char height, char colorMode, char *pixels);
+    bool drawLine(char x1, char y1, char x2, char y2, short color);
+    bool drawPolygon(char vertices, char *x, char *y, short color);
+    bool drawRectangle(char x, char y, char width, char height, short color);
+    bool drawText(char column, char row, char font, short color, char *text);
+    bool drawTriangle(char x1, char y1, char x2, char y2, char x3, char y3, short color);
+    bool eraseScreen();
+    bool init();
+    bool penSize(char size);
+    bool putPixel(char x, char y, short color);
+    short readPixel(char x, char y);
+    bool setBackgroundColor(short color);
+    bool setFontSize(char fontType);
+    bool textButton(char state, char x, char y, short buttonColor, char font, short textColor, char textWidth, char textHeight, char *text);
+    bool textMode(char mode);
+    bool versionInfo(bool onScreen, char *info);
+    
+    
+protected:
+    Serial      _oled;
+    DigitalOut  _reset;
+    
+    void resetDisplay();
 };
\ No newline at end of file