I2C 接続の LCD AQM1602XA-RN-GBW 用のライブラリ. Library for LCD 1602XA-RN-GBW connected using I2C interface.

Dependents:   UIT2_MovingAv_Intr UIT2_VariableFIR UIT2_VowelSynthesizer UIT2_ALE_LeakyLMS ... more

Revision:
8:854c244a7863
Parent:
7:5375acc9a74a
Child:
9:74a845df6e7a
--- a/AQM1602.cpp	Sun Nov 15 06:32:29 2015 +0000
+++ b/AQM1602.cpp	Thu Jan 14 05:55:06 2016 +0000
@@ -2,7 +2,7 @@
 //  Class for LCD, AQM1602XA-RN-GBW
 //      Do not use mbed Rev.109, using Nucleo-F401/441
 //
-//  2015/11/15, Copyright (c) 2015 MIKAMI, Naoki
+//  2016/01/14, Copyright (c) 2015 MIKAMI, Naoki
 //-------------------------------------------------------
 
 #include "AQM1602.hpp"
@@ -12,34 +12,20 @@
     // Constructor
     Aqm1602::Aqm1602(PinName sda, PinName scl, uint32_t clock,
                      bool cursor, bool blink)
-        : i2c_(sda, scl)
-    {
-        if (clock != 100000) i2c_.frequency(clock);
+        : i2cPtr_(new I2C(sda, scl)), i2c_(*i2cPtr_)
+    {   Init(clock, cursor, blink); }
 
-        wait_ms(50);
-        connected_ = WriteCmd(0x38);
-        if (!connected_)
-        {
-            fprintf(stderr, "\r\nLCD AQM1602 not connected\r\n");
-            return;
-        }
-        WriteCmd(0x39);         // To extended command
-        WriteCmd(0x14);         // Internal OSC frequency
-        WriteCmd(0x70 | 0x00);  // Contrast set
-        WriteCmd(0x54 | 0x02);  // Power/ICON/Contrast control
-        WriteCmd(0x6C);         // Follower control
-        wait_ms(200);
-
-        WriteCmd(0x01);
-        WriteCmd(0x38); // data length:8-bit, 2-line, 5×8 dots
-        WriteCmd(0x0C | (cursor << 1) | blink);
-    }
+    Aqm1602::Aqm1602(I2C &i2c, uint32_t clock,
+                     bool cursor, bool blink)
+            : i2cPtr_(NULL), i2c_(i2c)
+    {   Init(clock, cursor, blink); }
 
     // All clear
-    void Aqm1602::Clear()
+    bool Aqm1602::Clear()
     {
-        WriteCmd(0x01);
-        wait_ms(2);
+        bool ok = WriteCmd(0x01);
+        wait_ms(50);
+        return ok;
     }
 
     // Write string
@@ -78,6 +64,30 @@
     //---------------------------------------------------
     // Following functions: private
 
+    // Initialize
+    void Aqm1602::Init(uint32_t clock, bool cursor, bool blink)
+    {
+        if (clock != 100000) i2c_.frequency(clock);
+
+        wait_ms(40);
+        connected_ = Clear();   // Clear display
+        if (!connected_)
+        {
+            fprintf(stderr, "\r\nLCD AQM1602 not connected\r\n");
+            return;
+        }
+
+        WriteCmd(0x39);         // To extended command
+        WriteCmd(0x14);         // Internal OSC frequency
+        WriteCmd(0x70 | 0x00);  // Contrast set
+        WriteCmd(0x54 | 0x02);  // Power/ICON/Contrast control
+        WriteCmd(0x6C);         // Follower control
+        wait_ms(200);
+
+        WriteCmd(0x38); // data length:8-bit, 2-line, 5×8 dots
+        WriteCmd(0x0C | (cursor << 1) | blink);
+    }
+
     // Send command and data
     bool Aqm1602::LcdTx(uint8_t cmdData, uint8_t data)
     {