nlgplay for mbed

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 I2C i2c(dp5, dp27); // sda, scl
00017 
00018 const int AQM0802_addr = 0x7C;
00019 
00020 void lcd_cmd(char x)
00021 {
00022   char data[2];
00023   data[0] = 0x00; // CO = 0,RS = 0
00024   data[1] = x;
00025   i2c.write(AQM0802_addr, data, 2);
00026 }
00027 
00028 void lcd_contdata(char x)
00029 {
00030   char data[2];
00031   data[0] = 0xC0; //0b11000000 CO = 1, RS = 1
00032   data[1] = x;
00033   i2c.write(AQM0802_addr, data, 2);
00034 }
00035 
00036 void lcd_lastdata(char x)
00037 {
00038   char data[2];
00039   data[0] = 0x40; //0b11000000 CO = 0, RS = 1
00040   data[1] = x;
00041   i2c.write(AQM0802_addr, data, 2);
00042 }
00043 
00044 void lcd_printStrFill(const char *s)
00045 {
00046   int idx = 0;
00047   for(idx = 0; idx < 8; idx++)
00048   {
00049     if(idx < 7) {
00050       lcd_contdata(*s);
00051     } else {
00052       lcd_lastdata(*s);
00053     }
00054     if (*s) s++;
00055   }
00056 }
00057 
00058 
00059 void lcd_printStr(const char *s)
00060 {
00061   int idx = 0;
00062   while(*s && idx < 8) {
00063     if(*(s + 1)) {
00064       lcd_contdata(*s);
00065     } else {
00066       lcd_lastdata(*s);
00067     }
00068     s++;
00069     idx++;
00070   }
00071 }
00072 
00073 void lcd_setContrast(unsigned char c) {
00074   lcd_cmd(0x39);
00075   lcd_cmd(0x70 | (c & 0x0f)); // contrast Low
00076   lcd_cmd(0x5C | ((c >> 4) & 0x03)); // contast High/icon/power
00077   lcd_cmd(0x38);
00078 }
00079 
00080 void lcd_printHex(unsigned char num)
00081 {
00082   lcd_contdata(num);
00083 }
00084 
00085 void lcd_init() {
00086   int cont = 0;
00087   wait(0.04);
00088   // LCD initialize
00089   lcd_cmd(0x38); // function set
00090   lcd_cmd(0x39); // function set
00091   lcd_cmd(0x04); // EntryModeSet
00092   lcd_cmd(0x14); // interval osc
00093   lcd_cmd(0x70 | (cont & 0xF)); // contrast Low
00094   lcd_cmd(0x5C | ((cont >> 4) & 0x3)); // contast High/icon/power
00095   lcd_cmd(0x6C); // follower control
00096   wait(0.2);
00097   lcd_cmd(0x38); // function set
00098   lcd_cmd(0x0C); // Display On
00099   lcd_cmd(0x01); // Clear Display
00100   wait(0.2); // need additional wait to Clear Display
00101   
00102   lcd_setContrast(36);
00103 
00104 }
00105 
00106 void lcd_setCursor(unsigned char x,unsigned char y) {
00107   lcd_cmd(0x80 | (y * 0x40 + x));
00108 }
00109 
00110 
00111 void lcd_printStrY(int y,const char *s)
00112 {
00113   lcd_setCursor(0, y);
00114   lcd_printStrFill(s);
00115 }
00116 
00117 void lcd_printStrYscr(int y,const char *s)
00118 {
00119   int cnt = strlen(s) - 7;
00120   if (cnt <= 0)
00121     cnt = 1;
00122     
00123   for(int i = 0; i < cnt; i++)
00124   {   
00125     lcd_setCursor(0, y);
00126     lcd_printStr(s + i);
00127     wait(0.5);
00128   }
00129 }
00130 
00131 
00132 void lcd_printStr2(const char *s, const char *s2)
00133 {
00134   lcd_setCursor(0,0);
00135   lcd_printStrFill(s);
00136   lcd_setCursor(0,1);
00137   lcd_printStrFill(s2);
00138 }
00139 
00140 void lcd_cls(void)
00141 {
00142     lcd_setCursor(0, 0);
00143     lcd_printStrFill("");
00144     lcd_setCursor(0, 1);
00145     lcd_printStrFill("");
00146 }
00147