Graphic OLED 100x16 pixels interface

Dependents:   mbed_nicovideo_search_api mbed_recent_nicovideo_display_pub

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GraphicOLED.cpp Source File

GraphicOLED.cpp

00001 // GraphicOLED.cpp
00002 #include "GraphicOLED.h"
00003 
00004 extern void font_4x8(uint8_t buf[], int c); // misaki_4x8_jis201.cpp
00005 extern void font_8x8(uint8_t buf[], uint32_t unicode); // misaki_8x8_unicode.cpp
00006 
00007 GraphicOLED::GraphicOLED(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7) :  _rs(rs), _e(e), _d(d4, d5, d6, d7) {
00008     _e  = 1;
00009     _rs = 0;            // command mode
00010 
00011     wait_ms(500);       // Wait For Stabilization 500ms
00012 
00013     writeCommand(0x00); // 0x0 x 5, sync 4bit-mode
00014     writeCommand(0x00);
00015     writeCommand(0x02); // function set
00016     writeCommand(0x28); // N=1 2-line display
00017     writeCommand(0x0c); // D=1 display on
00018     writeCommand(0x14); // S/C=0 R/L=1
00019     writeCommand(0x1f); // G/C=1 graphic mode
00020 
00021     _uni_len = 0;
00022 }
00023 
00024 void GraphicOLED::g_write(uint8_t pat, int x, int y) {
00025     writeCommand(0x80 + (x&0x7f)); // x position
00026     writeCommand(0x40 + (y&0x01)); //y position
00027     writeData(pat);
00028 }
00029 
00030 void GraphicOLED::g_write(uint8_t *buf, int len, int x, int y) {
00031     for(int i = 0; i < len; i++) {
00032         g_write(*buf++, x++, y);
00033     }
00034 }
00035 
00036 void GraphicOLED::writeCommand(uint8_t command) {
00037     _rs = 0;
00038     writeByte(command);
00039 }
00040 
00041 void GraphicOLED::writeData(uint8_t data) {
00042     _rs = 1;
00043     writeByte(data);
00044 }
00045 
00046 void GraphicOLED::writeByte(uint8_t value) {
00047     _d = value >> 4;
00048     wait_us(40);  // most instructions take 40us
00049     _e = 0;
00050     wait_us(40);
00051     _e = 1;
00052     _d = value >> 0;
00053     wait_us(40);
00054     _e = 0;
00055     wait_us(40);  // most instructions take 40us
00056     _e = 1;
00057 }
00058 
00059 void GraphicOLED::cls() {
00060     for(int y = 0; y < rows(); y++) {
00061         for(int x = 0; x < 100; x++) {
00062             g_write(0x00, x, y);
00063         }
00064     }
00065     locate(0,0);
00066     _uni_len = 0;
00067 }
00068 
00069 void GraphicOLED::locate(int column, int row) {
00070     _column = column;
00071     _row = row;
00072 }
00073 
00074 int GraphicOLED::columns() {
00075      return 25;
00076 }
00077 
00078 int GraphicOLED::rows() {
00079     return 2;
00080 }
00081 
00082 int GraphicOLED::_putc(int value)
00083 {
00084     int step = 0;
00085     if (value == '\n') {
00086         _column = 0;
00087         _row++;
00088         if (_row >= rows()) {
00089             _row = 0;
00090         }
00091         _uni_len = 0;
00092     } else if (value <= 0x7f) {
00093         step = character(_column, _row, value);
00094     } else if (value >= 0xc2 && value <= 0xdf) {
00095         _unicode = value & 0x1f;
00096         _uni_len = 1;
00097     } else if (value >= 0xe0 && value <= 0xef) {
00098         _unicode = value & 0x0f;
00099         _uni_len = 2;
00100     } else if (value >= 0xf0 && value <= 0xf7) {
00101         _unicode = value & 0x07;
00102         _uni_len = 3;
00103     } else if (value >= 0xf8 && value <= 0xfb) {
00104         _unicode = value & 0x03;
00105         _uni_len = 4;
00106     } else if (value >= 0xfc && value <= 0xfd) {
00107         _unicode = value & 0x01;
00108         _uni_len = 5;
00109     } else if (_uni_len >= 1 && value >= 0x80 && value <= 0xbf) {
00110         _unicode = (_unicode<<6) | (value&0x3f);
00111         if (--_uni_len == 0) {
00112             step = character(_column, _row, _unicode);
00113         }
00114     } else {
00115         _uni_len = 0;
00116     }
00117     if (step > 0) {
00118         _column += step;
00119         if (_column >= columns()) {
00120             _column = 0;
00121             if (++_row >= rows()) {
00122                 _row = 0;
00123             }
00124         }
00125         _uni_len = 0;
00126     }
00127     return value;
00128 }
00129 
00130 int GraphicOLED::_getc() {
00131     return -1;
00132 }
00133 
00134 int GraphicOLED::character(int column, int row, int c) {
00135     if (c <= 0x7f) { // 半角
00136         uint8_t buf[4];
00137         font_4x8(buf, c);
00138         g_write(buf, sizeof(buf), column*4, row);
00139         return 1;
00140     } else if (c >= 0xff61 && c <= 0xff9f) { // 半角カタカナ
00141         int i = c - 0xff61 + 0xa1;
00142         uint8_t buf[4];
00143         font_4x8(buf, i);
00144         g_write(buf, sizeof(buf), column*4, row);
00145         return 1;
00146     } else { // 全角
00147         uint8_t buf[8];
00148         font_8x8(buf, c);
00149         g_write(buf, sizeof(buf), column*4, row);
00150         return 2;
00151     }
00152 }
00153