Akinori Hashimoto / SO1602A

Dependents:   NEW_LineTraceHub NEW_LineTraceHub_2 ColorSensorTest

Files at this revision

API Documentation at this revision

Comitter:
AkinoriHashimoto
Date:
Tue Sep 08 08:35:42 2015 +0000
Child:
1:eef15a16fe7a
Commit message:
Published.

Changed in this revision

SO1602A.cpp Show annotated file Show diff for this revision Revisions of this file
SO1602A.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SO1602A.cpp	Tue Sep 08 08:35:42 2015 +0000
@@ -0,0 +1,127 @@
+#include "SO1602A.h"
+
+SO1602A::SO1602A (PinName sda, PinName scl, char address)
+    : i2c(sda, scl),  addr(address)
+{
+    init();
+}
+SO1602A::SO1602A (I2C &_i2c, char address)
+    : i2c(_i2c), addr(address)
+{
+    init();
+}
+
+bool SO1602A::cmd(char chr)
+{
+    buf[0]= 0x00;
+    buf[1]= chr;
+    // if write success:0, other: err code.
+    int status= i2c.write(addr, buf, 2);
+    wait_ms(3);
+
+    if(status == 0)
+        return true;
+    else
+        return false;
+}
+
+int SO1602A::_putc(int val)     // for printf()
+{
+    if (val == '\n') {
+        _column = 0;
+        _row = (_row + 1) % 2;
+    } else {
+        locate(_column, _row);
+        buf[0]= 0x40;
+        buf[1]= val;
+        i2c.write(addr, buf, 2);
+
+        _column++;
+        if (_column >= 16) {
+            _column = 0;
+            _row = (_row + 1) % 2;
+        }
+    }
+    wait_ms(1);
+    return val;
+}
+
+// for "Stream"
+int SO1602A::_getc()
+{
+    return -1;
+}
+
+void SO1602A::locate(int col, int row)
+{
+    cmd(0x80 + row * 0x20 + col);
+    return;
+}
+
+void SO1602A::init()
+{
+    _column= _row= 0;
+    buf[0]= 0x00;
+    buf[1]= 0x00;
+    buf[2]= 0x00;
+
+    wait_ms(10);
+    this->clear();
+    this->cmd(0x02);    //Return Home.
+    this->cmd(0x0f);    // set On/Off. b3=1, b2:Disp, b1:Cursor, b0:blink.
+    this->cmd(0x01);    //Clear Disp.
+    wait_ms(20);
+    return;
+}
+
+void SO1602A::setContrast(char val)
+{
+    // Cmd of Contrast-setting must be setted Ext-Func-Register RE & SD.
+    this->setRE();
+    this->setSD();
+    // Double Byte Command. The contrast has 256 steps, and increase as the value.
+    this->cmd(0x81);
+    this->cmd(val);
+    this->clearSD();
+    this->clearRE();
+    wait_ms(100);
+    return;
+}
+
+void SO1602A::clear()
+{
+    this->cmd(0x01);
+    locate(0, 0);
+    wait_ms(5);
+    return;
+}
+
+// ******************** FUNCTION SET **********************
+// RE & IS func-set -> b7-4: 001*.
+//                      b3: dispLine; 1(2&4), 0(1&3).
+//                      RE: b1, IS: b0.
+void SO1602A::setRE()
+{
+    this->cmd(0x2a);
+    return;
+}
+void SO1602A::clearRE()
+{
+    this->cmd(0x28);
+    return;
+}
+// Extention Register; SD.
+// RE setted, 0b 0111 100F. F= Flag; 0: OLED-cmd is disable.
+//                                   1:             enable.
+void SO1602A::setSD()
+{
+    this->cmd(0x79);
+    return;
+}
+void SO1602A::clearSD()
+{
+    this->cmd(0x78);
+    return;
+}
+
+// EOF
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SO1602A.h	Tue Sep 08 08:35:42 2015 +0000
@@ -0,0 +1,53 @@
+#pragma once
+
+#include "mbed.h"
+
+// SA0(Pin4)=Low: i2c_addr= 0x78, High: 0x7a;
+// DDRAM addr. Line1: 0x00 ~ 0x0f. Line2: 0x20 ~ 0x2f.
+
+class SO1602A : public Stream{
+public:
+    SO1602A (PinName sda, PinName scl, char address= 0x78);
+    SO1602A (I2C &_i2c, char address= 0x78);
+    
+//  ******************** printf() future of C-language. ***************************
+
+    /** Initialize
+     */
+    void init();
+
+    /** Clear Display.
+     */
+    void clear();
+    
+    /** Set Position of char.
+     *  @param col: column, row: rows.
+     */
+    void locate(int col, int row);
+    
+    /** Set Contrast.
+     *  @param val; 256steps, 0x00 ~ 0xff. Contrast increas as the value.
+     */
+    void setContrast(char val);
+
+private:    
+    I2C i2c;
+    char addr;
+    char buf[3];
+    int _column, _row;
+    
+    bool cmd(char chr);
+    
+    // virtual func for printf() in Stream-class.
+    virtual int _putc(int val);
+    virtual int _getc();
+    
+// Function SET
+    void setRE();
+    void clearRE();
+    void setSD();
+    void clearSD();
+
+};
+
+// EOF
\ No newline at end of file