SO1602A Lib. SO1602A is Organic LED, has 16 chars and 2 lines. This lib supports printf() of C-Language. http://akizukidenshi.com/catalog/g/gP-08276/

Dependents:   NEW_LineTraceHub NEW_LineTraceHub_2 ColorSensorTest

Committer:
AkinoriHashimoto
Date:
Fri Nov 06 06:23:52 2015 +0000
Revision:
4:e17943b827d8
Parent:
3:eaaedd09fa6b
Adj. Constructer, Refer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AkinoriHashimoto 0:d8b95544d238 1 #include "SO1602A.h"
AkinoriHashimoto 0:d8b95544d238 2
AkinoriHashimoto 0:d8b95544d238 3 SO1602A::SO1602A (PinName sda, PinName scl, char address)
AkinoriHashimoto 4:e17943b827d8 4 : p_i2c(new I2C(sda, scl)), i2c(*p_i2c), addr(address)
AkinoriHashimoto 0:d8b95544d238 5 {
AkinoriHashimoto 0:d8b95544d238 6 init();
AkinoriHashimoto 0:d8b95544d238 7 }
AkinoriHashimoto 0:d8b95544d238 8 SO1602A::SO1602A (I2C &_i2c, char address)
AkinoriHashimoto 4:e17943b827d8 9 : p_i2c(NULL), i2c(_i2c), addr(address)
AkinoriHashimoto 0:d8b95544d238 10 {
AkinoriHashimoto 0:d8b95544d238 11 init();
AkinoriHashimoto 0:d8b95544d238 12 }
AkinoriHashimoto 4:e17943b827d8 13 SO1602A::~SO1602A()
AkinoriHashimoto 4:e17943b827d8 14 {
AkinoriHashimoto 4:e17943b827d8 15 if(p_i2c != NULL)
AkinoriHashimoto 4:e17943b827d8 16 delete p_i2c;
AkinoriHashimoto 4:e17943b827d8 17 }
AkinoriHashimoto 0:d8b95544d238 18
AkinoriHashimoto 0:d8b95544d238 19 bool SO1602A::cmd(char chr)
AkinoriHashimoto 0:d8b95544d238 20 {
AkinoriHashimoto 0:d8b95544d238 21 buf[0]= 0x00;
AkinoriHashimoto 0:d8b95544d238 22 buf[1]= chr;
AkinoriHashimoto 0:d8b95544d238 23 // if write success:0, other: err code.
AkinoriHashimoto 0:d8b95544d238 24 int status= i2c.write(addr, buf, 2);
AkinoriHashimoto 0:d8b95544d238 25 wait_ms(3);
AkinoriHashimoto 0:d8b95544d238 26
AkinoriHashimoto 0:d8b95544d238 27 if(status == 0)
AkinoriHashimoto 0:d8b95544d238 28 return true;
AkinoriHashimoto 0:d8b95544d238 29 else
AkinoriHashimoto 0:d8b95544d238 30 return false;
AkinoriHashimoto 0:d8b95544d238 31 }
AkinoriHashimoto 0:d8b95544d238 32
AkinoriHashimoto 0:d8b95544d238 33 int SO1602A::_putc(int val) // for printf()
AkinoriHashimoto 0:d8b95544d238 34 {
AkinoriHashimoto 0:d8b95544d238 35 if (val == '\n') {
AkinoriHashimoto 3:eaaedd09fa6b 36 col = 0;
AkinoriHashimoto 3:eaaedd09fa6b 37 row = (row + 1) % 2;
AkinoriHashimoto 0:d8b95544d238 38 } else {
AkinoriHashimoto 3:eaaedd09fa6b 39 locate(col, row);
AkinoriHashimoto 0:d8b95544d238 40 buf[0]= 0x40;
AkinoriHashimoto 0:d8b95544d238 41 buf[1]= val;
AkinoriHashimoto 0:d8b95544d238 42 i2c.write(addr, buf, 2);
AkinoriHashimoto 0:d8b95544d238 43
AkinoriHashimoto 3:eaaedd09fa6b 44 col++;
AkinoriHashimoto 3:eaaedd09fa6b 45 if (col >= 16) {
AkinoriHashimoto 3:eaaedd09fa6b 46 col = 0;
AkinoriHashimoto 3:eaaedd09fa6b 47 row = (row + 1) % 2;
AkinoriHashimoto 0:d8b95544d238 48 }
AkinoriHashimoto 0:d8b95544d238 49 }
AkinoriHashimoto 0:d8b95544d238 50 wait_ms(1);
AkinoriHashimoto 0:d8b95544d238 51 return val;
AkinoriHashimoto 0:d8b95544d238 52 }
AkinoriHashimoto 0:d8b95544d238 53
AkinoriHashimoto 0:d8b95544d238 54 // for "Stream"
AkinoriHashimoto 0:d8b95544d238 55 int SO1602A::_getc()
AkinoriHashimoto 0:d8b95544d238 56 {
AkinoriHashimoto 0:d8b95544d238 57 return -1;
AkinoriHashimoto 0:d8b95544d238 58 }
AkinoriHashimoto 0:d8b95544d238 59
AkinoriHashimoto 3:eaaedd09fa6b 60 void SO1602A::locate(int _col, int _row)
AkinoriHashimoto 0:d8b95544d238 61 {
AkinoriHashimoto 3:eaaedd09fa6b 62 col= _col;
AkinoriHashimoto 3:eaaedd09fa6b 63 row= _row;
AkinoriHashimoto 0:d8b95544d238 64 cmd(0x80 + row * 0x20 + col);
AkinoriHashimoto 0:d8b95544d238 65 return;
AkinoriHashimoto 0:d8b95544d238 66 }
AkinoriHashimoto 0:d8b95544d238 67
AkinoriHashimoto 0:d8b95544d238 68 void SO1602A::init()
AkinoriHashimoto 0:d8b95544d238 69 {
AkinoriHashimoto 3:eaaedd09fa6b 70 col= row= 0;
AkinoriHashimoto 0:d8b95544d238 71 buf[0]= 0x00;
AkinoriHashimoto 0:d8b95544d238 72 buf[1]= 0x00;
AkinoriHashimoto 0:d8b95544d238 73 buf[2]= 0x00;
AkinoriHashimoto 0:d8b95544d238 74
AkinoriHashimoto 0:d8b95544d238 75 wait_ms(10);
AkinoriHashimoto 0:d8b95544d238 76 this->clear();
AkinoriHashimoto 0:d8b95544d238 77 this->cmd(0x02); //Return Home.
AkinoriHashimoto 1:eef15a16fe7a 78 this->setDispFlag(true, true, true);
AkinoriHashimoto 1:eef15a16fe7a 79
AkinoriHashimoto 0:d8b95544d238 80 return;
AkinoriHashimoto 0:d8b95544d238 81 }
AkinoriHashimoto 0:d8b95544d238 82
AkinoriHashimoto 0:d8b95544d238 83 void SO1602A::setContrast(char val)
AkinoriHashimoto 0:d8b95544d238 84 {
AkinoriHashimoto 0:d8b95544d238 85 // Cmd of Contrast-setting must be setted Ext-Func-Register RE & SD.
AkinoriHashimoto 0:d8b95544d238 86 this->setRE();
AkinoriHashimoto 0:d8b95544d238 87 this->setSD();
AkinoriHashimoto 0:d8b95544d238 88 // Double Byte Command. The contrast has 256 steps, and increase as the value.
AkinoriHashimoto 0:d8b95544d238 89 this->cmd(0x81);
AkinoriHashimoto 0:d8b95544d238 90 this->cmd(val);
AkinoriHashimoto 0:d8b95544d238 91 this->clearSD();
AkinoriHashimoto 0:d8b95544d238 92 this->clearRE();
AkinoriHashimoto 0:d8b95544d238 93 wait_ms(100);
AkinoriHashimoto 0:d8b95544d238 94 return;
AkinoriHashimoto 0:d8b95544d238 95 }
AkinoriHashimoto 0:d8b95544d238 96
AkinoriHashimoto 2:45791c1064f6 97 void SO1602A::setDispFlag(bool disp, bool cursor, bool blink)
AkinoriHashimoto 1:eef15a16fe7a 98 {
AkinoriHashimoto 1:eef15a16fe7a 99 // set On/Off. b3=1, b2:Disp, b1:Cursor, b0:blink.
AkinoriHashimoto 1:eef15a16fe7a 100 char tmp= 0x08;
AkinoriHashimoto 1:eef15a16fe7a 101 if(disp)
AkinoriHashimoto 1:eef15a16fe7a 102 tmp += 0x04;
AkinoriHashimoto 1:eef15a16fe7a 103 if(cursor)
AkinoriHashimoto 1:eef15a16fe7a 104 tmp += 0x02;
AkinoriHashimoto 1:eef15a16fe7a 105 if(blink)
AkinoriHashimoto 1:eef15a16fe7a 106 tmp += 0x01;
AkinoriHashimoto 1:eef15a16fe7a 107 this->cmd(tmp);
AkinoriHashimoto 1:eef15a16fe7a 108 this->cmd(0x01); //Clear Disp.
AkinoriHashimoto 1:eef15a16fe7a 109 wait_ms(20);
AkinoriHashimoto 1:eef15a16fe7a 110 return;
AkinoriHashimoto 1:eef15a16fe7a 111 }
AkinoriHashimoto 1:eef15a16fe7a 112
AkinoriHashimoto 0:d8b95544d238 113 void SO1602A::clear()
AkinoriHashimoto 0:d8b95544d238 114 {
AkinoriHashimoto 0:d8b95544d238 115 this->cmd(0x01);
AkinoriHashimoto 0:d8b95544d238 116 locate(0, 0);
AkinoriHashimoto 0:d8b95544d238 117 wait_ms(5);
AkinoriHashimoto 0:d8b95544d238 118 return;
AkinoriHashimoto 0:d8b95544d238 119 }
AkinoriHashimoto 0:d8b95544d238 120
AkinoriHashimoto 0:d8b95544d238 121 // ******************** FUNCTION SET **********************
AkinoriHashimoto 0:d8b95544d238 122 // RE & IS func-set -> b7-4: 001*.
AkinoriHashimoto 0:d8b95544d238 123 // b3: dispLine; 1(2&4), 0(1&3).
AkinoriHashimoto 0:d8b95544d238 124 // RE: b1, IS: b0.
AkinoriHashimoto 0:d8b95544d238 125 void SO1602A::setRE()
AkinoriHashimoto 0:d8b95544d238 126 {
AkinoriHashimoto 0:d8b95544d238 127 this->cmd(0x2a);
AkinoriHashimoto 0:d8b95544d238 128 return;
AkinoriHashimoto 0:d8b95544d238 129 }
AkinoriHashimoto 0:d8b95544d238 130 void SO1602A::clearRE()
AkinoriHashimoto 0:d8b95544d238 131 {
AkinoriHashimoto 0:d8b95544d238 132 this->cmd(0x28);
AkinoriHashimoto 0:d8b95544d238 133 return;
AkinoriHashimoto 0:d8b95544d238 134 }
AkinoriHashimoto 0:d8b95544d238 135 // Extention Register; SD.
AkinoriHashimoto 0:d8b95544d238 136 // RE setted, 0b 0111 100F. F= Flag; 0: OLED-cmd is disable.
AkinoriHashimoto 0:d8b95544d238 137 // 1: enable.
AkinoriHashimoto 0:d8b95544d238 138 void SO1602A::setSD()
AkinoriHashimoto 0:d8b95544d238 139 {
AkinoriHashimoto 0:d8b95544d238 140 this->cmd(0x79);
AkinoriHashimoto 0:d8b95544d238 141 return;
AkinoriHashimoto 0:d8b95544d238 142 }
AkinoriHashimoto 0:d8b95544d238 143 void SO1602A::clearSD()
AkinoriHashimoto 0:d8b95544d238 144 {
AkinoriHashimoto 0:d8b95544d238 145 this->cmd(0x78);
AkinoriHashimoto 0:d8b95544d238 146 return;
AkinoriHashimoto 0:d8b95544d238 147 }
AkinoriHashimoto 0:d8b95544d238 148
AkinoriHashimoto 0:d8b95544d238 149 // EOF