firmware of NBCTRLV1 / AYC01

Dependencies:   SDFileSystemEx mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lcd.cpp Source File

lcd.cpp

00001 /*
00002  * lcd.cpp : AQM0802A mini library
00003  *
00004  *  Based on LCD module "AQM0802A-RN-GBW" sample program
00005  *  Copyright (c) 2013 Yoshihiro TSUBOI
00006  *
00007  *  Original Arduino version was developed by
00008  *  Copyright (c) 2013 Masahiro WAKAYAMA at SWITCH SCIENCE
00009  *
00010  *  Released under the MIT License: http://mbed.org/license/mit
00011  *
00012  */
00013 
00014 #include "mbed.h"
00015 
00016 #include "lcd.h"
00017 #include "pindef.h"
00018 
00019 LCD::LCD()
00020 {
00021   i2c = new I2C(DP_SDA, DP_SCL); // sda, scl
00022 }
00023 
00024 #define LCD_ADDR 0x7C
00025 
00026 void LCD::cmd(char x)
00027 {
00028   char data[2];
00029   data[0] = 0x00; // CO = 0,RS = 0
00030   data[1] = x;
00031   i2c->write(LCD_ADDR, data, 2);
00032 }
00033 
00034 void LCD::contdata(char x)
00035 {
00036   char data[2];
00037   data[0] = 0xC0; //0b11000000 CO = 1, RS = 1
00038   data[1] = x;
00039   i2c->write(LCD_ADDR, data, 2);
00040 }
00041 
00042 void LCD::lastdata(char x)
00043 {
00044   char data[2];
00045   data[0] = 0x40; //0b11000000 CO = 0, RS = 1
00046   data[1] = x;
00047   i2c->write(LCD_ADDR, data, 2);
00048 }
00049 
00050 
00051 void LCD::setContrast(unsigned char c) {
00052   cmd(0x39);
00053   cmd(0x70 | (c & 0x0f)); // contrast Low
00054   cmd(0x5C | ((c >> 4) & 0x03)); // contast High/icon/power
00055   cmd(0x38);
00056 }
00057 
00058 void LCD::init() {
00059   int cont = 0;
00060   wait(0.04);
00061   // LCD initialize
00062   cmd(0x38); // function set
00063   cmd(0x39); // function set
00064   cmd(0x04); // EntryModeSet
00065   cmd(0x14); // interval osc
00066   cmd(0x70 | (cont & 0xF)); // contrast Low
00067   cmd(0x5C | ((cont >> 4) & 0x3)); // contast High/icon/power
00068   cmd(0x6C); // follower control
00069   wait(0.2);
00070   cmd(0x38); // function set
00071   cmd(0x0C); // Display On
00072   cmd(0x01); // Clear Display
00073   wait(0.2); // need additional wait to Clear Display
00074 
00075   setContrast(36);
00076 }
00077 
00078 
00079 // 文字と空白で1行を埋める
00080 void LCD::printStrFill(const char *s)
00081 {
00082   int idx = 0;
00083   for(idx = 0; idx < 8; idx++)
00084   {
00085     if(idx < 7) {
00086       contdata(*s);
00087     } else {
00088       lastdata(*s);
00089     }
00090     if (*s) s++;
00091   }
00092 }
00093 
00094 void LCD::printStr(const char *s)
00095 {
00096   int idx = 0;
00097   while(*s && idx < 8) {
00098     if(*(s + 1)) {
00099       contdata(*s);
00100     } else {
00101       lastdata(*s);
00102     }
00103     s++;
00104     idx++;
00105   }
00106 }
00107 
00108 void LCD::setCursor(unsigned char x,unsigned char y)
00109 {
00110   cmd(0x80 | (y * 0x40 + x));
00111 }
00112 
00113 void LCD::printStrY(int y,const char *s)
00114 {
00115   setCursor(0, y);
00116   printStrFill(s);
00117 }
00118 
00119 void LCD::printStrYscr(int y,const char *s)
00120 {
00121   int cnt = strlen(s) - 7;
00122   if (cnt <= 0)
00123     cnt = 1;
00124 
00125   for(int i = 0; i < cnt; i++)
00126   {
00127     setCursor(0, y);
00128     printStr(s + i);
00129     wait(0.5);
00130   }
00131 }
00132 
00133 
00134 void LCD::printStr2(const char *s, const char *s2)
00135 {
00136   setCursor(0,0);
00137   printStrFill(s);
00138   setCursor(0,1);
00139   printStrFill(s2);
00140 }
00141 
00142 void LCD::cls(void)
00143 {
00144     setCursor(0, 0);
00145     printStrFill("");
00146     setCursor(0, 1);
00147     printStrFill("");
00148 }