Tsukuni Koji / SO1602A

Fork of SO1602A by Akinori Hashimoto

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SO1602A.cpp Source File

SO1602A.cpp

00001 #include "SO1602A.h"
00002 
00003 SO1602A::SO1602A (PinName sda, PinName scl, char address)
00004     : p_i2c(new I2C(sda, scl)), i2c(*p_i2c), addr(address)
00005 {
00006     init();
00007 }
00008 SO1602A::SO1602A (I2C &_i2c, char address)
00009     : p_i2c(NULL), i2c(_i2c), addr(address)
00010 {
00011     init();
00012 }
00013 SO1602A::~SO1602A()
00014 {
00015     if(p_i2c != NULL)
00016     delete p_i2c;
00017 }
00018 
00019 bool SO1602A::cmd(char chr)
00020 {
00021     buf[0]= 0x00;
00022     buf[1]= chr;
00023     // if write success:0, other: err code.
00024     int status= i2c.write(addr, buf, 2);
00025 //    wait_ms(3);
00026 
00027     if(status == 0)
00028         return true;
00029     else
00030         return false;
00031 }
00032 
00033 int SO1602A::_putc(int val)     // for printf()
00034 {
00035     if (val == '\n') {
00036         col = 0;
00037         row = (row + 1) % 2;
00038     } else {
00039         locate(col, row);
00040         buf[0]= 0x40;
00041         buf[1]= val;
00042         i2c.write(addr, buf, 2);
00043 
00044         col++;
00045         if (col >= 16) {
00046             col = 0;
00047             row = (row + 1) % 2;
00048         }
00049     }
00050     wait_ms(1);
00051     return val;
00052 }
00053 
00054 // for "Stream"
00055 int SO1602A::_getc()
00056 {
00057     return -1;
00058 }
00059 
00060 void SO1602A::locate(int _col, int _row)
00061 {
00062     col= _col;
00063     row= _row;
00064     cmd(0x80 + row * 0x20 + col);
00065     return;
00066 }
00067 
00068 void SO1602A::init()
00069 {
00070     i2c.frequency(400000);
00071     col= row= 0;
00072     buf[0]= 0x00;
00073     buf[1]= 0x00;
00074     buf[2]= 0x00;
00075 
00076     wait_ms(10);
00077     this->clear();
00078     this->cmd(0x02);    //Return Home.
00079     this->setDispFlag(true, true, true);
00080     
00081     return;
00082 }
00083 
00084 void SO1602A::setContrast(char val)
00085 {
00086     // Cmd of Contrast-setting must be setted Ext-Func-Register RE & SD.
00087     this->setRE();
00088     this->setSD();
00089     // Double Byte Command. The contrast has 256 steps, and increase as the value.
00090     this->cmd(0x81);
00091     this->cmd(val);
00092     this->clearSD();
00093     this->clearRE();
00094     wait_ms(100);
00095     return;
00096 }
00097 
00098 void SO1602A::setDispFlag(bool disp, bool cursor, bool blink)
00099 {
00100     // set On/Off. b3=1, b2:Disp, b1:Cursor, b0:blink.
00101     char tmp=  0x08;
00102     if(disp)
00103         tmp += 0x04;
00104     if(cursor)
00105         tmp += 0x02;
00106     if(blink)
00107         tmp += 0x01;
00108     this->cmd(tmp);
00109     this->cmd(0x01);    //Clear Disp.
00110     wait_ms(20);
00111     return;
00112 }
00113 
00114 void SO1602A::clear()
00115 {
00116     this->cmd(0x01);
00117     locate(0, 0);
00118     wait_ms(5);
00119     return;
00120 }
00121 
00122 // ******************** FUNCTION SET **********************
00123 // RE & IS func-set -> b7-4: 001*.
00124 //                      b3: dispLine; 1(2&4), 0(1&3).
00125 //                      RE: b1, IS: b0.
00126 void SO1602A::setRE()
00127 {
00128     this->cmd(0x2a);
00129     return;
00130 }
00131 void SO1602A::clearRE()
00132 {
00133     this->cmd(0x28);
00134     return;
00135 }
00136 // Extention Register; SD.
00137 // RE setted, 0b 0111 100F. F= Flag; 0: OLED-cmd is disable.
00138 //                                   1:             enable.
00139 void SO1602A::setSD()
00140 {
00141     this->cmd(0x79);
00142     return;
00143 }
00144 void SO1602A::clearSD()
00145 {
00146     this->cmd(0x78);
00147     return;
00148 }
00149 
00150 // EOF