AD-128160-UART制御用のライブラリ http://www.aitendo.co.jp/product/3119 gingaxさんのプログラムを参考に作らせてもらっています。 http://mbed.org/users/akira/libraries/AD128160/m159hi

Dependents:   AD128160_HelloWorld

Revision:
3:d15cda2a5e91
Parent:
2:6f2db745808e
diff -r 6f2db745808e -r d15cda2a5e91 AD128160.cpp
--- a/AD128160.cpp	Mon Dec 12 02:38:49 2011 +0000
+++ b/AD128160.cpp	Tue Dec 13 15:37:06 2011 +0000
@@ -1,76 +1,107 @@
 #include "AD128160.h"
 
-
 AD128160::AD128160(PinName tx,PinName reset):_device(tx,NC),_rst(reset) {
     init();
 }
 
-int AD128160::_putc(int c) {
-    if (c == '\n') {
-        newline();
-    } else {
-        int x = _column * 8;  // FIXME: Char sizes
-        int y = _row * 16;
-
-        unsigned char sum;
-        unsigned char x0H;
-        unsigned char x0L;
-        unsigned char y0H;
-        unsigned char y0L;
-        unsigned char datalen;
-        x0H = x >> 8;
-        x0L = x & 0xFF;
-        y0H = y >> 8;
-        y0L = y & 0xFF;
-        datalen = 6;
-        _device.putc(0x55);
-        _device.putc(datalen);
-        _device.putc(0x0B);  // command ASCII Print
-        _device.putc(x0H);   //x upper 8bit
-        _device.putc(x0L);   //x low 8bit
-        _device.putc(y0H);   //y upper 8bit
-        _device.putc(y0L);   //y low 8bit
-        _device.putc(c);
-        sum = c+x0H+x0L+y0H+y0L+0x0B;
-        _device.putc(sum);//sumch
-        _device.putc(0xAA);
-
-        _column++;
-        if (_column >= LCD_COLS) {
-            _row++;
-            _column = 0;
-        }
-        if (_row >= LCD_ROWS) {
-            _row = 0;
-        }
-    }
-    return c;
-}
-
 void AD128160::init() {
     reset();
     brightness(300);
     cls();
     locate(0,0);
     color(0xffff);
+
+    _font_x=8;
+    _font_y=16;
+    _max_columns = LCD_WIDTH/_font_x;
+    _max_rows = LCD_HEIGHT/_font_y;
+}
+
+int AD128160::_putc(int c) {
+    if (c == '\n') {
+        newline();
+    } else {
+        int x = _column * _font_x;  // FIXME: Char sizes
+        int y = _row * _font_y;
+        unsigned char data[5];
+
+        data[0]= x >> 8;//x upper 8bit
+        data[1]= x & 0xFF;//x low 8bit
+        data[2]= y >> 8; //y upper 8bit
+        data[3]=y & 0xFF; //y low 8bit
+        data[4]=c;
+        cwrite(0x0B,data,5);
+
+        _column++;
+        if (_column >= _max_columns) {
+            _row++;
+            _column = 0;
+        }
+        if (_row >= _max_rows) {
+            _row = 0;
+        }
+    }
+    return c;
+}
+void AD128160::textSetting(int size,int rgb) {
+    unsigned char data[4];
+
+    data[0]= size >> 8;//x upper 8bit
+    data[1]= size & 0xFF;//x low 8bit
+    data[2]= rgb >> 8; //y upper 8bit
+    data[3]= rgb & 0xFF; //y low 8bit
+
+    cwrite(0x81,data,4);
+
+    switch (size) {
+        case 0:
+            _font_x=6;
+            _font_y=10;
+            break;
+        case 1:
+            _font_x=7;
+            _font_y=13;
+            break;
+        case 3:
+            _font_x=10;
+            _font_y=20;
+            break;
+        case 4:
+            _font_x=16;
+            _font_y=32;
+            break;
+        default:
+            _font_x=8;
+            _font_y=16;
+            break;
+    }
+    _max_columns = LCD_WIDTH/_font_x;
+    _max_rows = LCD_HEIGHT/_font_y;
+    
+    locate(0,0);
+}
+
+
+void AD128160::cwrite(int command,unsigned char* data,int length) {
+    unsigned char sum=0;
+    _device.putc(0x55);//start code
+    _device.putc(length+1);
+    _device.putc(command);
+    for (int i=0; i<length; i++) {
+        _device.putc(data[i]);
+        sum = sum+data[i];
+    }
+    sum += command;
+    _device.putc(sum);//check sum
+    _device.putc(0xAA);//end core
 }
 
 void AD128160::brightness(int value) {
-    unsigned char sum=0;
-    unsigned char byte[2];
-
-    byte[0] = (value & 0xff00)>>8;
-    byte[1] = value & 0x00ff;
-
-    sum+=byte[0]+byte[1]+0x89;
+    unsigned char data[2];
+    data[0] = (value & 0xff00)>>8;
+    data[1] = value & 0x00ff;
 
-    _device.putc(0x55);//Back light On
-    _device.putc(0x03);
-    _device.putc(0x89);
-    _device.putc(byte[0]);
-    _device.putc(byte[1]);
-    _device.putc(sum);
-    _device.putc(0xAA);
+    cwrite(0x89,data,2);
 }
 
 void AD128160::reset() {
@@ -82,83 +113,53 @@
 
 
 void AD128160::speed(int baud) {
-    unsigned char sum=0;
-
-    unsigned char byte[4];
-    byte[0] = (baud&0xff000000)>>24;
-    byte[1] = (baud&0x00ff0000)>>16;
-    byte[2] = (baud&0x0000ff00)>>8;
-    byte[3] = baud&0x000000ff;
-
-    sum += byte[0]+byte[1]+byte[2]+byte[3]+0x8B;
+    unsigned char data[4];
 
-    _device.putc(0x55);
-    _device.putc(0x05);
-    _device.putc(0x8B);
-    _device.putc(byte[0]);
-    _device.putc(byte[1]);
-    _device.putc(byte[2]);
-    _device.putc(byte[3]);
+    data[0]=(baud&0xff000000)>>24;
+    data[1]=(baud&0x00ff0000)>>16;
+    data[2]=(baud&0x0000ff00)>>8;
+    data[3]=baud&0x000000ff;
 
-    _device.putc(sum);
-    _device.putc(0xAA);
+    cwrite(0x8B,data,4);
+
     wait(0.1);
     _device.baud(baud);
 
 }
 
-int AD128160::width(){
+int AD128160::width() {
     return LCD_WIDTH;
 }
 
-int AD128160::height(){
+int AD128160::height() {
     return LCD_HEIGHT;
 }
 
-int AD128160::columns(){
-    return LCD_COLS;
+int AD128160::columns() {
+    return _max_columns;
 }
 
-int AD128160::rows(){
-    return LCD_ROWS;
+int AD128160::rows() {
+    return _max_rows;
 }
 
 void AD128160::bmp(int x0,int y0,int bmp_no) {
-    unsigned char x0H;
-    unsigned char x0L;
-    unsigned char y0H;
-    unsigned char y0L;
-    unsigned char rH;
-    unsigned char rL;
-    unsigned char sum;
+    unsigned char data[6];
 
-    x0H = x0 >> 8;
-    x0L = x0 & 0xFF;
-    y0H = y0 >> 8;
-    y0L = y0 & 0xff;
-    rH = bmp_no >> 8;
-    rL = bmp_no & 0xFF;
-    sum = x0H+x0L+y0H+y0L+rH+rL+0x09;
-    _device.putc(0x55);
-    _device.putc(0x07);
-    _device.putc(0x09);//command
-    _device.putc(x0H);
-    _device.putc(x0L);
-    _device.putc(y0H);
-    _device.putc(y0L);
-    _device.putc(rH);
-    _device.putc(rL);
-    _device.putc(sum);
-    _device.putc(0xAA);
+    data[0] = x0 >> 8;
+    data[1] = x0 & 0xFF;
+    data[2] = y0 >> 8;
+    data[3] = y0 & 0xff;
+    data[4] = bmp_no >> 8;
+    data[5] = bmp_no & 0xFF;
+
+    cwrite(0x09,data,6);
 }
 
 void AD128160::cls() {
-    _device.putc(0x55);// Clear
-    _device.putc(0x02);
-    _device.putc(0x80);
-    _device.putc(0x55);
-    _device.putc(0xD5);
-    _device.putc(0xAA);
+    unsigned char data[1];
+    data[0]=0x55;
+    cwrite(0x80,data,1);
 
     locate(0,0);
 }
@@ -168,217 +169,100 @@
     _row = row;
 }
 
-void AD128160::puts( char s[98]) {
-    unsigned char sum=0;
-    unsigned char x0H;
-    unsigned char x0L;
-    unsigned char y0H;
-    unsigned char y0L;
-    unsigned char datalen;
-    x0H = _column >> 8;
-    x0L = _column & 0xFF;
-    y0H = _row >> 8;
-    y0L = _row & 0xFF;
-    datalen = strlen(s)+5;
-    _device.putc(0x55);
-    _device.putc(datalen);
-    _device.putc(0x0B);  // command ASCII Print
-    _device.putc(x0H);   //x upper 8bit
-    _device.putc(x0L);   //x low 8bit
-    _device.putc(y0H);   //y upper 8bit
-    _device.putc(y0L);   //y low 8bit
-    for (int a=0; a<strlen(s); a++) {
-        _device.putc(s[a]);
-        sum = sum+s[a];
-    }
-    sum = sum+x0H+x0L+y0H+y0L+0x0B;
-    _device.putc(sum);//sumcheck
-    _device.putc(0xAA);
+void AD128160::puts(char* s) {
+    int data_len=strlen(s)+4;
+    unsigned char data[data_len];
+    int x = _column * _font_x;  // FIXME: Char sizes
+    int y = _row * _font_y;
+
+    data[0] = x >> 8;
+    data[1] = x & 0xFF;
+    data[2] = y >> 8;
+    data[3] = y & 0xFF;
+
+    strcpy((char*)(data+4),s);
+    cwrite(0x0B,data,data_len);
 }
 
-void AD128160::backgroudColor(int rgb) {
-    int c1;
-    int c2;
-    int sum;
-    int mode=1;
+void AD128160::textBackground(bool mode,int rgb) {
+    unsigned char data[3];
 
-    c1=(rgb >> 8) & 0xff;
-    c2=(rgb & 0xff);
-    sum=mode+c1+c2+0x85;
-    _device.putc(0x55);
-    _device.putc(0x04);
-    _device.putc(0x85);
-    _device.putc(mode);
-    _device.putc(c1);
-    _device.putc(c2);
-    _device.putc(sum);
-    _device.putc(0xAA);
+    if (mode) data[0]=1;
+    else     data[0]=0;
+    data[1]=(rgb >> 8) & 0xff;
+    data[2]=(rgb & 0xff);
+    cwrite(0x85,data,3);
 }
 
 void AD128160::color(int rgb) {
-    int c1;
-    int c2;
-    int sum;
+    unsigned char data[2];
 
-    c1=(rgb >> 8) & 0xff;
-    c2=(rgb & 0xff);
-    sum=c1+c2+0x84;
-    _device.putc(0x55);
-    _device.putc(0x03);
-    _device.putc(0x84);
-    _device.putc(c1);
-    _device.putc(c2);
-    _device.putc(sum);
-    _device.putc(0xAA);
+    data[0]=(rgb >> 8) & 0xff;
+    data[1]=(rgb & 0xff);
+    cwrite(0x84,data,2);
 }
 
 void AD128160::newline(void) {
     _column = 0;
     _row++;
 
-    if (_row >= LCD_ROWS) {
+    if (_row >= _max_rows) {
         _row = 0;
     }
 }
 
 void AD128160::pixel(int x0,int y0) {
-    unsigned char x0H;
-    unsigned char x0L;
-    unsigned char y0H;
-    unsigned char y0L;
-    unsigned char sum;
-
-    x0H = x0 >> 8;
-    x0L = x0 & 0xFF;
-    y0H = y0 >> 8;
-    y0L = y0 & 0xff;
+    unsigned char data[4];
 
-    sum = x0H+x0L+y0H+y0L+0x01;
-    _device.putc(0x55);
-    _device.putc(0x05);
-    _device.putc(0x01);//command
-    _device.putc(x0H);
-    _device.putc(x0L);
-    _device.putc(y0H);
-    _device.putc(y0L);
-    _device.putc(sum);
-    _device.putc(0xAA);
+    data[0] = x0 >> 8;
+    data[1] = x0 & 0xFF;
+    data[2] = y0 >> 8;
+    data[3] = y0 & 0xff;
+
+    cwrite(0x01,data,4);
 }
 
 void AD128160::box(int x0,int y0,int x1,int y1,int paint) {
-    unsigned char x0H;
-    unsigned char x0L;
-    unsigned char x1H;
-    unsigned char x1L;
-    unsigned char y0H;
-    unsigned char y0L;
-    unsigned char y1H;
-    unsigned char y1L;
-    unsigned char sum;
-    unsigned char cmd;
-    switch (paint) {
-        case 1:
-            cmd = 0x04;
-            break;
-        default:
-            cmd =0x03;
-            break;
-    }
-    x0H = x0 >> 8;
-    x0L = x0 & 0xFF;
-    y0H = y0 >> 8;
-    y0L = y0 & 0xff;
-    x1H = x1 >> 8;
-    x1L = x1 & 0xFF;
-    y1H = y1 >> 8;
-    y1L = y1 & 0xff;
-    sum = x0H+x0L+y0H+y0L+x1H+x1L+y1H+y1L+cmd;
-    _device.putc(0x55);//Box
-    _device.putc(0x09);
-    _device.putc(cmd);//command
-    _device.putc(x0H);
-    _device.putc(x0L);
-    _device.putc(y0H);
-    _device.putc(y0L);
-    _device.putc(x1H);
-    _device.putc(x1L);
-    _device.putc(y1H);
-    _device.putc(y1L);
-    _device.putc(sum);
-    _device.putc(0xAA);
+    unsigned char data[8];
+
+    data[0] = x0 >> 8;
+    data[1] = x0 & 0xFF;
+    data[2] = y0 >> 8;
+    data[3] = y0 & 0xff;
+    data[4] = x1 >> 8;
+    data[5] = x1 & 0xFF;
+    data[6] = y1 >> 8;
+    data[7] = y1 & 0xff;
+
+    if (paint)   cwrite(0x04,data,8);
+    else        cwrite(0x03,data,8);
 }
 
 void AD128160::circle(int x0,int y0,int r,int paint) {
-    unsigned char x0H;
-    unsigned char x0L;
-    unsigned char y0H;
-    unsigned char y0L;
-    unsigned char rH;
-    unsigned char rL;
-    unsigned char sum;
-    unsigned char cmd;
-    switch (paint) {
-        case 0:
-            cmd = 0x05;
-            break;
-        case 1:
-            cmd = 0x06;
-            break;
-        default:
-            cmd =0x05;
-            break;
-    }
-    x0H = x0 >> 8;
-    x0L = x0 & 0xFF;
-    y0H = y0 >> 8;
-    y0L = y0 & 0xff;
-    rH = r >> 8;
-    rL = r & 0xFF;
-    sum = x0H+x0L+y0H+y0L+rH+rL+cmd;
-    _device.putc(0x55);
-    _device.putc(0x07);
-    _device.putc(cmd);//command
-    _device.putc(x0H);
-    _device.putc(x0L);
-    _device.putc(y0H);
-    _device.putc(y0L);
-    _device.putc(rH);
-    _device.putc(rL);
-    _device.putc(sum);
-    _device.putc(0xAA);
+    unsigned char data[6];
+
+    data[0] = x0 >> 8;
+    data[1] = x0 & 0xFF;
+    data[2] = y0 >> 8;
+    data[3] = y0 & 0xff;
+    data[4] = r >> 8;
+    data[5] = r & 0xFF;
+
+    if (paint)   cwrite(0x06,data,6);
+    else        cwrite(0x05,data,6);
 }
 
 void AD128160::line(int x0,int y0,int x1,int y1) {
-    unsigned char x0H;
-    unsigned char x0L;
-    unsigned char x1H;
-    unsigned char x1L;
-    unsigned char y0H;
-    unsigned char y0L;
-    unsigned char y1H;
-    unsigned char y1L;
-    unsigned char sum;
+    unsigned char data[8];
 
-    x0H = x0 >> 8;
-    x0L = x0 & 0xFF;
-    y0H = y0 >> 8;
-    y0L = y0 & 0xff;
-    x1H = x1 >> 8;
-    x1L = x1 & 0xFF;
-    y1H = y1 >> 8;
-    y1L = y1 & 0xff;
-    sum = x0H+x0L+y0H+y0L+x1H+x1L+y1H+y1L+02;
-    _device.putc(0x55);
-    _device.putc(0x09);
-    _device.putc(0x02);//command
-    _device.putc(x0H);
-    _device.putc(x0L);
-    _device.putc(y0H);
-    _device.putc(y0L);
-    _device.putc(x1H);
-    _device.putc(x1L);
-    _device.putc(y1H);
-    _device.putc(y1L);
-    _device.putc(sum);
-    _device.putc(0xAA);
+    data[0] = x0 >> 8;
+    data[1] = x0 & 0xFF;
+    data[2] = y0 >> 8;
+    data[3] = y0 & 0xff;
+    data[4] = x1 >> 8;
+    data[5] = x1 & 0xFF;
+    data[6] = y1 >> 8;
+    data[7] = y1 & 0xff;
+
+    cwrite(0x02,data,8);
 }