Graphic OLED 100x16 pixels interface

Dependents:   mbed_nicovideo_search_api mbed_recent_nicovideo_display_pub

/media/uploads/va009039/graphicoled_1.jpg

Revision:
2:337a2655f815
Parent:
1:a104653979bf
Child:
3:a6650dd2dbc8
--- a/GraphicOLED.cpp	Wed Apr 18 10:17:51 2012 +0000
+++ b/GraphicOLED.cpp	Mon Aug 04 06:17:36 2014 +0000
@@ -2,12 +2,28 @@
 #include "GraphicOLED.h"
 #include <ctype.h>
 
-extern void font_4x8(char buf[], int i);
-extern void font_8x8(char buf[], int i);
+extern void font_4x8(uint8_t buf[], int i);
+extern void font_8x8(uint8_t buf[], int i);
+
+GraphicOLED::GraphicOLED(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7) :  _rs(rs), _e(e), _d(d4, d5, d6, d7) {
+    _e  = 1;
+    _rs = 0;            // command mode
+
+    wait_ms(15);        // Wait 15ms to ensure powered up
 
-GraphicOLED::GraphicOLED(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7):
-        TextLCD(rs, e, d4, d5, d6, d7)
-{
+    // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
+    for (int i=0; i<3; i++) {
+        writeByte(0x3);
+        wait_ms(2);     // this command takes 1.64ms, so wait for it
+    }
+    writeByte(0x2);     // 4-bit mode
+    wait_us(40);        // most instructions take 40us
+
+    //writeCommand(0x28); // Function set 001 BW N F - -
+    //writeCommand(0x0C);
+    //writeCommand(0x6);  // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
+    //cls();
+    
     wait_ms(500); // wait 500ms
     writeCommand(0x00); // 0x0 x 5
     writeCommand(0x00);
@@ -19,14 +35,14 @@
     _kanji_flag = false;
 }
 
-void GraphicOLED::g_wrtie(int pat, int x, int y)
+void GraphicOLED::g_write(uint8_t pat, int x, int y)
 {
     writeCommand(0x40+y); // y position
     writeCommand(0x80+x); // x position
     writeData(pat);
 }
 
-void GraphicOLED::g_wrtie(char *buf, int len, int x, int y)
+void GraphicOLED::g_write(uint8_t *buf, int len, int x, int y)
 {
     writeCommand(0x40+y); // y position
     while(len-- > 0) {
@@ -35,19 +51,51 @@
     }
 }
 
+void GraphicOLED::writeCommand(uint8_t command) {
+    _rs = 0;
+    writeByte(command);
+}
+
+void GraphicOLED::writeData(uint8_t data) {
+    _rs = 1;
+    writeByte(data);
+}
+
+void GraphicOLED::writeByte(uint8_t value) {
+    _d = value >> 4;
+    wait_us(40);  // most instructions take 40us
+    _e = 0;
+    wait_us(40);
+    _e = 1;
+    _d = value >> 0;
+    wait_us(40);
+    _e = 0;
+    wait_us(40);  // most instructions take 40us
+    _e = 1;
+}
+
 void GraphicOLED::cls()
 {
     for(int y = 0; y < 2; y++) {
         for(int x = 0; x < 100; x++) {
-            g_wrtie(0x00, x, y);
+            g_write(0x00, x, y);
         }
     }
     locate(0,0);
     _kanji_flag = false;
 }
 
+void GraphicOLED::locate(int column, int row) {
+    _column = column;
+    _row = row;
+}
+
 int GraphicOLED::columns() { return 25; }
 
+int GraphicOLED::rows() {
+    return 2;
+}
+
 int GraphicOLED::_putc(int value)
 {
     if (value == '\n') {
@@ -85,6 +133,10 @@
     return value;
 }
 
+int GraphicOLED::_getc() {
+    return -1;
+}
+
 void GraphicOLED::character(int column, int row, int c)
 {
     int i = 0;
@@ -93,9 +145,9 @@
     } else if (isprint(c)) { // 20 - 7e
         i = c - 32;
     }
-    char buf[4];
+    uint8_t buf[4];
     font_4x8(buf, i);
-    g_wrtie(buf, sizeof(buf), column*4, row);
+    g_write(buf, sizeof(buf), column*4, row);
 }
 
 void _jis(int *ph, int *pl)
@@ -139,7 +191,7 @@
     l = c & 0xff;
     _jis(&h, &l);
     int adrs = _jis2adrs(h<<8 | l);
-    char buf[8];
+    uint8_t buf[8];
     font_8x8(buf, adrs);
-    g_wrtie(buf, sizeof(buf), column*4, row);
+    g_write(buf, sizeof(buf), column*4, row);
 }