An I2C text LCD library for Displaytronic ACM1602NI-FLW-FBW-M01

Dependents:   ACM1602NI_Demo ROBOT_v01 ROBOT_v02 Boku-Transmit ... more

Files at this revision

API Documentation at this revision

Comitter:
takuo
Date:
Mon May 13 12:50:42 2013 +0000
Child:
1:84527dcd3fcc
Commit message:
Initial commit

Changed in this revision

ACM1602NI.cpp Show annotated file Show diff for this revision Revisions of this file
ACM1602NI.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ACM1602NI.cpp	Mon May 13 12:50:42 2013 +0000
@@ -0,0 +1,99 @@
+#include "mbed.h"
+#include "ACM1602NI.h"
+
+#define _i2cSUCCESS 0
+#define _i2cFAILURE 1
+
+ACM1602NI::ACM1602NI(I2C &i2c): _i2c(i2c) {
+    init();
+}
+
+void ACM1602NI::init() {
+    writeCommand(0x01);
+    wait_ms(i2c_command_wait_ms);
+    writeCommand(0x38);
+    wait_ms(i2c_command_wait_ms);
+    writeCommand(0x0f);
+    wait_ms(i2c_command_wait_ms);
+    writeCommand(0x06);
+    wait_ms(i2c_command_wait_ms);
+    locate(0, 0);
+}
+
+int ACM1602NI::writeBytes(const char *data, int length, bool repeated) {
+    wait_us(i2c_bit_wait_us);
+    _i2c.start();
+    for (int i = 0; i < length; i++) {
+        wait_us(i2c_bit_wait_us);
+        if (_i2c.write(data[i]) != 1) {
+            wait_us(i2c_bit_wait_us);
+            _i2c.stop();
+            return _i2cFAILURE;
+        }
+    }
+    if (!repeated) {
+        wait_us(i2c_bit_wait_us);
+        _i2c.stop();
+    }
+    return _i2cSUCCESS;
+}
+
+void ACM1602NI::character(int column, int row, int c) {
+    int a = address(column, row);
+    writeCommand(a);
+    writeData(c);
+}
+
+void ACM1602NI::cls() {
+    writeCommand(0x01);
+    wait_ms(i2c_command_wait_ms);
+    locate(0, 0);
+}
+
+void ACM1602NI::locate(int column, int row) {
+    _column = column;
+    _row = row;
+}
+
+int ACM1602NI::_putc(int value) {
+    if (value == '\n') {
+        _column = 0;
+        _row = (_row + 1) % rows();
+    }
+    else {
+        character(_column, _row, value);
+        _column++;
+        if (_column >= columns()) {
+            _column = 0;
+            _row = (_row + 1) % rows();
+        }
+    }
+    return value;
+}
+
+int ACM1602NI::_getc() {
+    return -1;
+}
+
+void ACM1602NI::writeCommand(int command) {
+    char bs[3] = { i2c_addr, 0x00, command };
+    writeBytes(bs, 3);
+}
+
+void ACM1602NI::writeData(int data) {
+    char bs[3] = { i2c_addr, 0x80, data };
+    writeBytes(bs, 3);
+}
+
+int ACM1602NI::address(int column, int row) {
+    return 0x80 + row * 0x40 + column;
+}
+
+int ACM1602NI::columns() {
+    return display_columns;
+}
+
+int ACM1602NI::rows() {
+    return display_rows;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ACM1602NI.h	Mon May 13 12:50:42 2013 +0000
@@ -0,0 +1,87 @@
+/* mbed Text LCD Library for Displaytronic ACM1602NI-FLW-FBW-M01
+ * Copyright (c) 2013, wtakuo
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+ 
+#ifndef ACM1602NI_H
+#define ACM1602NI_H
+
+#include "mbed.h"
+
+/** An I2C Text LCD interface for driving Displaytronic ACM1602NI-FLW-FBW-M01
+ * The interface is basically the same as TextLCD by Simon Ford.
+ *
+ * @code
+ * #include "mbed.h"
+ * #include "ACM1602NI.h"
+ * 
+ * I2C i2c(p9, p10);
+ * ACM1602NI lcd(i2c);
+ *
+ * int main() {
+ *     lcd.printf("Hello, World!\n");
+ * }
+ * @endcode
+ */
+class ACM1602NI : public Stream
+{
+public:
+    /** Create an ACM1602NI object connected to the specified I2C port.
+     *
+     * @param i2c     The I2C port to connect to
+     */
+    ACM1602NI(I2C &i2c);
+
+    /** Locate to a screen column and row
+     *
+     * @param column  The horizontal position from the left, indexed from 0
+     * @param row     The vertical position from the top, indexed from 0
+     */
+    void locate(int column, int row);
+    
+    /** Clear the screen and locate to 0,0 */
+    void cls();
+
+    int rows();
+    int columns();
+
+protected:
+    virtual int _putc(int value);
+    virtual int _getc();
+
+    void init();
+    int address(int column, int raw);
+    void character(int column, int row, int c);
+    int writeBytes(const char *data, int length, bool repeated = false);
+    void writeCommand(int command);
+    void writeData(int data);
+
+    static const int i2c_addr = 0x50 << 1;
+    static const int i2c_bit_wait_us = 20;
+    static const int i2c_command_wait_ms = 4;
+
+    static const int display_columns = 16;
+    static const int display_rows = 2;
+
+    I2C &_i2c;
+    int _column, _row;
+};
+
+#endif