An mbed library for 4D Systems uOLED-160-G1

Dependents:   OLED-Driver

Files at this revision

API Documentation at this revision

Comitter:
sblair
Date:
Sun Dec 19 16:42:32 2010 +0000
Commit message:
Initial version

Changed in this revision

OLED160G1.cpp Show annotated file Show diff for this revision Revisions of this file
OLED160G1.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OLED160G1.cpp	Sun Dec 19 16:42:32 2010 +0000
@@ -0,0 +1,281 @@
+/**
+ * mbed library for 4D Systems uOLED-160-G1
+ *
+ *
+ * Copyright (c) 2010 Steven Blair
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "mbed.h"
+#include "OLED160G1.h"
+
+
+OLED160G1::OLED160G1(PinName serialTx, PinName serialRx, PinName resetPin) : s(serialTx, serialRx), reset(resetPin) {
+    s.baud(OLED_BAUDRATE);
+    
+    while (s.readable()) {
+        s.getc();
+    }
+    
+    locate(0, 0);
+    setFontColor(0xFFFF);
+    _fontSize = OLED_FONT5X7;
+}
+
+void OLED160G1::resetDisplay() {
+    reset = 0;
+    wait_ms(200);
+    reset = 1;
+    wait_ms(200);
+}
+
+void OLED160G1::getResponse() {
+    char response = OLED_NAK;
+    lastCount = 0;
+    NAKCount = 0;
+    
+    while (!s.readable() || response == OLED_NAK) {
+        wait_ms(1);
+        lastCount++;
+        if (s.readable()) {
+            response = s.getc();    // Read response
+            if (response == OLED_ACK) {
+                return;
+            }
+            else if (response == OLED_NAK) {
+                NAKCount++;
+            }
+        }
+    }
+}
+
+// Initialise OLED display. You must first activate serial comunication!
+void OLED160G1::init() {
+    resetDisplay();
+
+    wait_ms(OLED_INIT_DELAY);       // wait for initialisation
+
+    s.putc(OLED_DETECT_BAUDRATE);   // send byte for OLED to autodetect baudrate
+    
+    getResponse();
+}
+
+int OLED160G1::toRGB(int red, int green, int blue) {
+    int outR = ((red * 31) / 255);
+    int outG = ((green * 63) / 255);
+    int outB = ((blue * 31) / 255);
+
+    return (outR << 11) | (outG << 5) | outB;
+}
+
+void OLED160G1::eraseScreen() {
+    s.putc(OLED_CLEAR);
+    
+    wait(1);    // TODO: why needed?
+    
+    getResponse();
+    
+    _row = 0;
+    _column = 0;
+}
+
+void OLED160G1::drawPixel(char x, char y, int color) {
+    s.putc(OLED_PUTPIXEL);
+    s.putc(x);
+    s.putc(y);
+
+    s.putc(color >> 8);     // MSB
+    s.putc(color & 0xFF);   // LSB
+
+    getResponse();
+}
+
+void OLED160G1::drawLine(char x1, char y1, char x2, char y2, int color) {
+    s.putc(OLED_LINE);      // Line
+
+    s.putc(x1);
+    s.putc(y1);
+    s.putc(x2);
+    s.putc(y2);
+
+    s.putc(color >> 8);      // MSB          
+    s.putc(color & 0xFF);    // LSB
+
+    getResponse();
+}
+
+void OLED160G1::drawRectangle(char x, char y, char width, char height, int color) {
+    s.putc(OLED_RECTANGLE); 
+
+    s.putc(x);
+    s.putc(y);
+
+    s.putc(x+width);
+    s.putc(y+height);
+
+    s.putc(color >> 8);      // MSB          
+    s.putc(color & 0xFF);    // LSB
+    
+    //
+   // if (filled == 1) { printByte(0x01); }   // Filled
+    //else { printByte(0x00); }               // Outline
+    //
+    getResponse();
+}
+
+void OLED160G1::drawCircle(char x, char y, char radius, int color) {
+    s.putc(OLED_CIRCLE); 
+
+    s.putc(x);
+    s.putc(y);
+    s.putc(radius);
+
+    s.putc(color >> 8);     // MSB          
+    s.putc(color & 0xFF);   // LSB
+
+    getResponse();
+}
+
+void OLED160G1::setFontSize(char fontSize) {
+    s.putc(OLED_SETFONTSIZE);
+    s.putc(fontSize);
+    _fontSize = fontSize;
+    
+    getResponse();
+}
+
+void OLED160G1::setFontColor(int fontColor) {
+    _fontColor = fontColor;
+}
+
+void OLED160G1::setPenSize(char penSize) {
+    s.putc(OLED_PEN_SIZE);
+    
+    s.putc(penSize);
+    
+    _penSize = penSize;
+    
+    getResponse();
+}
+
+void OLED160G1::setTextBackgroundType(char textBackgroundType) {
+    s.putc(OLED_SET_TEXT_BACKGROUND_TYPE);
+    s.putc(textBackgroundType);
+    
+    getResponse();
+}
+
+void OLED160G1::setBackgroundColor(int color) {
+    s.putc(OLED_SET_BACKGROUND_COLOR);
+    
+    s.putc(color >> 8);      // MSB          
+    s.putc(color & 0xFF);    // LSB
+    
+    getResponse();
+}
+
+void OLED160G1::drawText(char column, char row, char font_size, char *mytext, int color) {
+    s.putc(OLED_TEXT);
+    
+    // Adjust to center of the screen (26 Columns at font size 0)
+    //int newCol = 13 - (strlen(mytext)/2);
+    //printByte(newCol); // column
+    s.putc(column); // column
+    
+    s.putc(row); // row
+    s.putc(font_size); // font size (0 = 5x7 font, 1 = 8x8 font, 2 = 8x12 font)
+
+    s.putc(color >> 8);      // MSB          
+    s.putc(color & 0xFF);    // LSB
+
+    for (int i = 0;  i < strlen(mytext); i++) {
+        s.putc(mytext[i]); // character to write
+    }
+    s.putc(0x00);   // string terminator (always 0x00)
+
+    getResponse();
+}
+
+void OLED160G1::drawSingleChar(char column, char row, char theChar, int color) {
+    s.putc(OLED_TEXTFORMATED);
+    
+    s.putc(theChar);
+    s.putc(column);
+    s.putc(row);
+    
+    s.putc(color >> 8);      // MSB          
+    s.putc(color & 0xFF);    // LSB
+
+    getResponse();
+}
+
+void OLED160G1::displayControl(char mode, char value) {
+    s.putc(OLED_COMMAND_CONTROL);
+    
+    s.putc(mode);
+    s.putc(value);
+    
+    getResponse();
+}
+
+char OLED160G1::getPenSize() {
+    return _penSize;
+}
+    
+int OLED160G1::_putc(int value) {
+    if (value == '\n') {
+        _column = 0;
+        _row++;
+        if(_row >= rows()) {
+            _row = 0;
+        }
+    } else {
+        drawSingleChar(_column, _row, value, _fontColor);
+        
+        wait_ms(1);    //TODO: why is this needed?
+        
+        _column++;
+        if (_column >= columns()) {
+            _column = 0;
+            _row++;
+            if(_row >= rows()) {
+                _row = 0;
+            }
+        }
+    }
+    return value;
+}
+
+int OLED160G1::_getc() {
+    return -1;
+}
+
+void OLED160G1::locate(int column, int row) {
+    _column = column;
+    _row = row;
+}
+
+int OLED160G1::rows() { 
+    return 16;
+}
+
+int OLED160G1::columns() { 
+    return 26;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OLED160G1.h	Sun Dec 19 16:42:32 2010 +0000
@@ -0,0 +1,241 @@
+/**
+ * mbed library for 4D Systems uOLED-160-G1
+ *
+ *
+ * Copyright (c) 2010 Steven Blair
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef _OLED160G1_H_
+#define _OLED160G1_H_
+
+
+#define OLED_BAUDRATE                       115200  // 300 baud to 256kbaud supported (256k not always stable?)
+#define OLED_INIT_DELAY                     1100    // milliseconds
+
+// Initialisation routine
+#define OLED_DETECT_BAUDRATE                0x55
+
+// Drawing routines
+#define OLED_CLEAR                          0x45
+#define OLED_BKGCOLOR                       0x42
+#define OLED_COPYPASTE                      0x63
+#define OLED_PEN_SIZE                       0x70
+
+// Graphics
+#define OLED_LINE                           0x4C
+#define OLED_CIRCLE                         0x43
+#define OLED_CIRCLEFILL                     0x69
+#define OLED_PUTPIXEL                       0x50
+#define OLED_READPIXEL                      0x52
+#define OLED_RECTANGLE                      0x72
+
+// Text properties
+#define OLED_TEXT                           0x73
+#define OLED_SETFONTSIZE                    0x46
+#define OLED_FONT5X7                        0x01
+#define OLED_FONT8X8                        0x02
+#define OLED_FONT8X12                       0x03
+#define OLED_TEXTFORMATED                   0x54
+#define OLED_SET_TEXT_BACKGROUND_TYPE       0x4F
+#define OLED_SET_BACKGROUND_COLOR           0x42
+#define OLED_SET_TEXT_TRANSPARENT           0x00
+#define OLED_SET_TEXT_OPAQUE                0x01
+
+// OLED Control
+#define OLED_COMMAND_CONTROL                0x59
+#define OLED_COMMAND_DISPLAY                0x01
+#define OLED_COMMAND_CONTRAST               0x02
+#define OLED_COMMAND_POWER                  0x03
+
+// Responses
+#define OLED_ACK                            0x06  // Ok
+#define OLED_NAK                            0x15  // Error
+
+// Colours
+#define OLED_WHITE                          0xFFFF
+#define OLED_BLACK                          0x0000
+#define OLED_BLUE                           0xA6BF
+#define OLED_RED                            0xF800
+
+#define red_min 0//150
+#define red_max 255
+#define blue_min 0//185
+#define blue_max 255
+#define green_min 0//195
+#define green_max 255
+
+
+class OLED160G1 : public Stream {
+public:
+
+    /**
+     * Default constructor.
+     */
+    OLED160G1(PinName serialTx, PinName serialRx, PinName resetPin);
+    
+    /**
+     * Reset the display using the reset pin (the reset pin is active-low).
+     */
+    void resetDisplay();
+
+    /**
+     * Initialise OLED display.
+     */
+    void init();
+
+    /**
+     * Processes responses (ACK or NAK) from the OLED. A new command cannot be sent to the OLED until a NAK is received, so
+     * this function waits for the minimum time needed.
+     */
+    void getResponse();
+
+    /**
+     * Clear the OLED screen.
+     */
+    void eraseScreen();
+    
+    /**
+     * Calculate 16-bit value from RGB (0 to 63, 565 format)
+     */
+    int toRGB(int red, int green, int blue);
+    
+    /**
+     * 
+     */
+    void drawPixel(char x, char y, int color);
+    
+    /**
+     * 
+     */
+    void drawLine(char x1, char y1, char x2, char y2, int color);
+    
+    /**
+     * 
+     */
+    void drawRectangle(char x, char y, char width, char height, int color);
+    
+    /**
+     * 
+     */
+    void drawCircle(char x, char y, char radius, int color);
+    
+    /**
+     * Set font size, for use with printf() or OLED_DrawSingleChar(); all other functions override this setting.
+     *
+     * @param : fontSize can be: OLED_FONT5X7, OLED_FONT8X8, or OLED_FONT8X12
+     */
+    void setFontSize(char fontSize);
+    
+    /**
+     * Set font color, for use with printf(); all other functions override this setting.
+     */
+    void setFontColor(int fontColor);
+    
+    /**
+     * Set the "pen size".
+     *
+     * @param penSize : 0 to draw solid objects; 1 to draw wireframe objects.
+     */
+    void setPenSize(char penSize);
+    
+    /**
+     * Set whether or not text is drawn with an opaque or transparent background.
+     *
+     * @param : textBackgroundType can be OLED_SET_TEXT_TRANSPARENT or OLED_SET_TEXT_OPAQUE
+     */
+    void setTextBackgroundType(char textBackgroundType);
+    
+    /**
+     * 
+     */
+    void setBackgroundColor(int color);
+    
+    /**
+     * 
+     */
+    void drawText(char column, char row, char font_size, char *mytext, int color);
+    
+    /**
+     * Draw a single ASCII character at the specified location.
+     */
+    void drawSingleChar(char column, char row, char theChar, int color);
+    
+    /**
+     * Display control functions, such as display ON/OFF, contrast and power-up/power-down.
+     *
+     * @param value : can be 0x00 to 0x0F is mode is OLED_COMMAND_CONTRAST. Default contrast value is 0x08.
+     */
+    void displayControl(char mode, char value);
+    
+    /**
+     * 
+     */
+    char getPenSize();
+    
+    /**
+     * Get number of text rows
+     */
+    int rows(); //TODO: must depend on font size
+    
+    /**
+     * Get number of text columns
+     */
+    int columns();
+    
+    /**
+     * Set text cursor location
+     */
+    virtual void locate(int column, int row);
+    
+    /**
+     * 
+     */
+    int lastCount;
+    
+    /**
+     * 
+     */
+    int NAKCount;
+    
+protected:
+    virtual int _putc(int value);
+    virtual int _getc();
+    
+    /**
+     * Text cursor column number
+     */
+    short _column;
+    
+    /**
+     * Text cursor row number
+     */
+    short _row;
+    
+    char _fontSize;
+    char _penSize;
+    int _fontColor;
+    
+private:
+    Serial s;
+    DigitalOut  reset;
+};
+
+#endif
\ No newline at end of file