Hiroshi M / MyLCD

Dependents:   MyLCD_OSARU MyLCD_printf

Files at this revision

API Documentation at this revision

Comitter:
bant62
Date:
Wed Dec 11 11:58:45 2013 +0000
Child:
1:794acd9a0b9c
Commit message:
first commit

Changed in this revision

MyLCD.cpp Show annotated file Show diff for this revision Revisions of this file
MyLCD.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MyLCD.cpp	Wed Dec 11 11:58:45 2013 +0000
@@ -0,0 +1,269 @@
+/**
+ *****************************************************************************
+ * File Name    : MyLCD.cpp
+ *
+ * Title        : ORGINAL I2C LCD Display Claass Source File
+ * Revision     : 0.1
+ * Notes        :
+ * Target Board : mbed NXP LPC1768, mbed LPC1114FN28  etc
+ * Tool Chain   : ????
+ *
+ * Revision History:
+ * When         Who         Description of change
+ * -----------  ----------- -----------------------
+ * 2012/12/11   Hiroshi M   init
+ *****************************************************************************
+ *
+ * Copyright (C) 2013 Hiroshi M, MIT License
+ *
+ * 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.
+ *
+ **/
+
+/* Includes ----------------------------------------------------------------- */
+#include "mbed.h"
+#include "MyLCD.h"
+#include <string.h>
+
+/* Private typedef ---------------------------------------------------------- */
+/* Private define ----------------------------------------------------------- */
+/* Private macro ------------------------------------------------------------ */
+/* Private variables -------------------------------------------------------- */
+
+/* member fanctions --------------------------------------------------------- */
+
+
+// Constractor
+MyLCD::MyLCD(I2C *i2c): _i2c(i2c)
+{
+    _column = 0;
+    _row = 0;
+}
+
+int MyLCD::writeBytes(const char *data, int length, bool repeated)
+{
+    wait_us(i2c_bit_wait_us);
+    _i2c->start();
+
+    wait_us(i2c_bit_wait_us);
+    if (_i2c->write(i2c_addr) != 1)
+    {
+        wait_us(i2c_bit_wait_us);
+        _i2c->stop();
+        return _i2cFAILURE;
+    }
+
+    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;
+}
+
+int MyLCD::gotoCursor( int x, int y )
+{
+    char buff[4] = { ESC, 'L', x, y };
+
+    _column = x;
+    _row = y;
+
+    return writeBytes(buff, sizeof(buff));
+}
+
+
+int MyLCD::home(void)
+{
+    char buff[2] = { ESC, 'H' };
+
+    _column = 0;
+    _row = 0;
+
+    return writeBytes(buff, sizeof(buff));
+}
+
+int MyLCD::clear(void)
+{
+    char buff[2] = { ESC, 'C' };
+
+    return writeBytes(buff, sizeof(buff));
+}
+
+int MyLCD::saveCustomCharacter(int romCharNum, char lcdCharData[])
+{
+    int ret;
+
+    char buff[3] = { ESC, 'S', romCharNum };
+
+    ret = writeBytes(buff, sizeof(buff), true);
+
+    if (ret == _i2cSUCCESS)
+    {
+        ret = writeBytes(lcdCharData, 8, false);
+    }
+    wait_ms(100);
+
+    return ret;
+}
+
+int MyLCD::mapCustomCharacter(int romCharNum, int lcdCharNum)
+{
+    char buff[4] = { ESC, 'M', romCharNum, lcdCharNum };
+
+    return writeBytes(buff, sizeof(buff));
+}
+
+
+int MyLCD::offDisplay(void)
+{
+    char buff[2] = { ESC, 'X' };
+
+    return writeBytes(buff, sizeof(buff));
+}
+
+int MyLCD::onDisplay(void)
+{
+    char buff[2] = { ESC, 'N' };
+
+    return writeBytes(buff, sizeof(buff));
+}
+
+int MyLCD::blinkCharacter(void)
+{
+    char buff[2] = { ESC, 'B' };
+
+    return writeBytes(buff, sizeof(buff));
+}
+
+int MyLCD::dispCursor(void)
+{
+    char buff[2] = { ESC, 'D' };
+
+    return writeBytes(buff, sizeof(buff));
+}
+
+int MyLCD::blinkCursor(void)
+{
+    char buff[2] = { ESC, 'E' };
+
+    return writeBytes(buff, sizeof(buff));
+}
+
+int MyLCD::hideCursor(void)
+{
+    char buff[2] = { ESC, 'H' };
+
+    return writeBytes(buff, sizeof(buff));
+}
+
+int MyLCD::moveLeftCursor(void)
+{
+    char buff[2] = { ESC, '-' };
+
+    return writeBytes(buff, sizeof(buff));
+}
+
+int MyLCD::moveRightCursor(void)
+{
+    char buff[2] = { ESC, '+' };
+
+    return writeBytes(buff, sizeof(buff));
+}
+
+int MyLCD::moveLeftDisplay(void)
+{
+    char buff[2] = { ESC, '<' };
+
+    return writeBytes(buff, sizeof(buff));
+}
+
+int MyLCD::moveRightDisplay(void)
+{
+    char buff[2] = { ESC, '>' };
+
+    return writeBytes(buff, sizeof(buff));
+}
+
+// 一文字表示
+int MyLCD::printChar(char chr)
+{
+    return writeBytes(&chr, 1);
+}
+
+int MyLCD::printStr(const char *s)
+{
+    return writeBytes(s, strlen(s));
+}
+
+
+int MyLCD::_putc(int value)
+{
+    if (value == '\n')
+    {
+        _column = 0;
+        _row++;
+        if (_row >= rows())
+        {
+            _row = 0;
+        }
+    }
+    else
+    {
+        character(_column, _row, value);
+        _column++;
+        if (_column >= columns())
+        {
+            _column = 0;
+            _row++;
+            if (_row >= rows())
+            {
+                _row = 0;
+            }
+        }
+    }
+    return value;
+}
+
+int MyLCD::_getc()
+{
+    return -1;
+}
+
+void MyLCD::character(int column, int row, int c)
+{
+    gotoCursor(_column, row );
+    printChar(c);
+}
+
+int MyLCD::columns()
+{
+    return display_columns;
+}
+
+int MyLCD::rows()
+{
+    return display_rows;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MyLCD.h	Wed Dec 11 11:58:45 2013 +0000
@@ -0,0 +1,100 @@
+/**
+ *****************************************************************************
+ * File Name    : MyLCD.h
+ *
+ * Title        : ORGINAL I2C LCD Display Claass Header File
+ * Revision     : 0.1
+ * Notes        :
+ * Target Board : mbed NXP LPC1768, mbed LPC1114FN28  etc
+ * Tool Chain   : ????
+ *
+ * Revision History:
+ * When         Who         Description of change
+ * -----------  ----------- -----------------------
+ * 2012/12/06   Hiroshi M   init
+ *****************************************************************************
+ *
+ * Copyright (C) 2013 Hiroshi M, MIT License
+ *
+ * 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 __MYLCD_H_
+#define __MYLCD_H_
+
+/* Includes ----------------------------------------------------------------- */
+#include "mbed.h"
+/* typedef ------------------------------------------------------------------ */
+
+/* define ------------------------------------------------------------------- */
+#define ESC             0x1B
+#define I2C_ADDR_MYLCD  0x32
+#define _i2cSUCCESS     0
+#define _i2cFAILURE     1
+
+/* macro -------------------------------------------------------------------- */
+/* variables ---------------------------------------------------------------- */
+/* class -------------------------------------------------------------------- */
+class MyLCD : public Stream
+{
+private:
+    static const int i2c_addr = I2C_ADDR_MYLCD << 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;
+    int writeBytes(const char *data, int length, bool repeated=false);
+
+protected:
+    virtual int _putc(int value);
+    virtual int _getc();
+    
+    void character(int column, int row, int c);
+    int columns();
+    int rows();
+
+public:
+    MyLCD(I2C *i2c);
+#if DOXYGEN_ONLY
+    int putc(int value);
+    int printf(const char* format, ...);
+#endif
+    
+    int gotoCursor( int x, int y );
+    int home(void);
+    int clear(void);
+    int offDisplay(void);
+    int onDisplay(void);
+    int blinkCharacter(void);
+    int dispCursor(void);
+    int blinkCursor(void);
+    int hideCursor(void);
+    int moveLeftCursor(void);
+    int moveRightCursor(void);
+    int moveLeftDisplay(void);
+    int moveRightDisplay(void);
+    int printChar(char chr);
+    int printStr(const char *s);
+    
+    int saveCustomCharacter(int romCharNum, char lcdCharData[]);
+    int mapCustomCharacter(int romCharNum, int lcdCharNum);
+};
+
+#endif /* __MYLCD_H_ */