Revision:
4:1f72df1a88e0
Parent:
3:f5576943e216
Child:
5:39b01a6f0f75
--- a/s_Lcd.cpp	Sat Oct 08 23:51:33 2011 +0000
+++ b/s_Lcd.cpp	Sun Oct 23 02:06:34 2011 +0000
@@ -1,9 +1,35 @@
 #include "mbed.h"
 
-Serial device(p9, p10);  // tx, rx
-//Serial pc(USBTX, USBRX); // tx, rx
+#define  kanji_file    "/local/SHMZN16X.FNT"
+//#defin  kanji_file  "/local/k12x10.fnt"
+    
+Serial device(p9, p10);  // tx, rx  LCD
 DigitalOut rst(p20);     //P20  --> LCD  RST  (Reset)
 
+LocalFileSystem local("local");
+
+int cx = 0;
+int cy = 0;
+int offsety = 0;
+bool kstate = false;
+unsigned char kbuf;
+
+    FILE *fp;   //File open buffer
+
+    unsigned short start[92], end[92];
+    unsigned char font[64];              
+
+    char            Identifier[6+1];   
+    char            FontName[8+1];    
+    unsigned char    XSize;            
+    unsigned char    YSize;            
+    unsigned char    Size;            
+    unsigned char    CodeType;        
+    unsigned char    Tnum;            
+
+#define FONT_XBYTE        ((XSize + 7) >> 3)
+#define FONT_YBYTE        ((YSize + 7) >> 3)
+
 void lcdspeed()
     {
     device.putc(0x55);// UART Speed 115200bps
@@ -16,6 +42,35 @@
     device.putc(0x4E);
     device.putc(0xAA);
     }
+void 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;
+
+    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);
+    }
 
 void cls()
     {
@@ -25,37 +80,44 @@
     device.putc(0x55);
     device.putc(0xD5);
     device.putc(0xAA);
-    wait(0.1);
     }
+void scroll(void){
 
-void s_Lcdinit()
+}
+
+
+
+
+void lcdputs( char data[98],int x,int y)
     {
-        device.baud(9600);
-        rst = 0;            //Reset
-        wait(0.1);
-        rst = 1;
-    wait(0.1); 
-    device.putc(0x55);//Back light On
-    device.putc(0x03);
-    device.putc(0x89);
-    device.putc(0x01);
-    device.putc(0x2C);
-    device.putc(0xB6);
+    unsigned char s=0;
+    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 = strlen(data)+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(data);a++)
+        {
+            device.putc(data[a]);
+            s = s+data[a];
+        }
+    s = s+x0H+x0L+y0H+y0L+0x0B;
+    device.putc(s);//sumcheck
     device.putc(0xAA);
-    wait(0.1); 
-    device.putc(0x55);// Clear
-    device.putc(0x02);
-    device.putc(0x80);
-    device.putc(0x55);
-    device.putc(0xD5);
-    device.putc(0xAA);
-    wait(0.1);
-    lcdspeed();
-    wait(0.1);
-    device.baud(115200);
     }
 
-
 void color(int rgb)
     {
     int c1;
@@ -72,9 +134,27 @@
     device.putc(c2);
     device.putc(sum);
     device.putc(0xAA);
-    //pc.printf("color %x %x %x ",c1,c2,sum);
     }
 
+void newline(void){
+    cx = 0;
+    cy += 16;
+    if(cy > 160){
+        scroll();
+        cy = 0;
+        color(0x001f);
+        cls();
+        color(0xffff);
+    }
+}
+
+void movecorsor(int a)
+    {
+    cx+=8*a;
+    if(cx>=128) newline();
+    }
+
+
 void pixel(int x0,int y0)
     {
     unsigned char x0H;
@@ -222,8 +302,7 @@
     device.putc(sum);
     device.putc(0xAA);
     }
-    
-void lcdprint(char data[98],int x,int y)
+void lcdput(unsigned char data,int x,int y)
     {
     unsigned char s;
     unsigned char x0H;
@@ -235,7 +314,7 @@
     x0L = x & 0xFF;
     y0H = y >> 8;
     y0L = y & 0xFF;
-    datalen = strlen(data)+5;
+    datalen = 6;
     device.putc(0x55);
     device.putc(datalen);
     device.putc(0x0B);  // command ASCII Print
@@ -243,14 +322,125 @@
     device.putc(x0L);   //x low 8bit
     device.putc(y0H);   //y upper 8bit
     device.putc(y0L);   //y low 8bit
-    for(int a=0;a<strlen(data);a++)
-        {
-            device.putc(data[a]);
-            s = s+data[a];
-//            pc.printf("%x:",data[a]);
-        }
-    s = s+x0H+x0L+y0H+y0L+0x0B;
+    device.putc(data);
+    s = data+x0H+x0L+y0H+y0L+0x0B;
     device.putc(s);//sumcheck
     device.putc(0xAA);
-//    pc.printf("print %x %x %x %x %x %d   ",x0H,x0L,y0H,y0L,s,datalen);
+    }
+    
+
+
+void kanji_init(){ 
+    //move cursor 
+     fp = fopen(kanji_file , "r");
+
+
+    if(!fp) {
+        printf("File could not be opened!\n");
+        exit(1);
+    }
+
+    fgets(Identifier,   6+1, fp);    // FONTX2
+    fgets(FontName,     8+1, fp);        
+    fread(&XSize,       1, 1, fp);        
+    fread(&YSize,       1, 1, fp);        
+    fread(&CodeType,    1, 1, fp);  
+    fread(&Tnum,        1, 1, fp);
+
+    // Table read
+    for(int a=0;a< Tnum ;a++){
+        fread(&start[a],1,2,fp);
+        fread(&end[a]  ,1,2,fp);
+        }
+}
+
+//Kanji puts  cr = kanji cord
+void k_puts(int cr){
+    //kanji address
+    int c;
+    int adrs;
+    c=0;
+    adrs=0;
+    while(cr>start[c]){
+        if(cr > end[c]){
+            adrs += end[c]-start[c]+1;
+            }
+        else{
+        adrs += cr - start[c];
+        }
+        c++;
     }
+    c--;
+                        //printf("C=%d,adrs=%d cr=%X ",c,adrs,cr);
+    // Kanji image Read
+    fseek( fp, Tnum*4+18+32*adrs, SEEK_SET );
+    fread(&font,1,32,fp); 
+    // kanji put
+    c=0;
+    for (int y=0;y<16;y++){
+        for (int x=0;x<8;x++){
+            if((font[c] & 0x80)==0x80){
+                pixel(cx+x,cy+y);
+                }
+            font[c] = font[c] << 1;
+            if((font[c+1] & 0x80)==0x80){
+                pixel(cx+x+8,cy+y);
+                }
+            font[c+1] = font[c+1] << 1;
+        }
+        c+=2;
+    }
+    //move cursor 
+    cx+=16;
+    if(cx>=128){   //LF control
+        cx=0;
+        cy+=16;
+        }
+}
+
+void kanji_end(){
+    fclose(fp);
+}
+
+void drawc(unsigned char c){
+    if(kstate){ // 2nd byte of shift-jis
+        kstate = false;
+
+       k_puts(kbuf << 8 | c);
+    } else if((0x81 <= c && c <= 0x9f) || (0xe0 <= c && c <= 0xfc)){ // 1st byte of shift-jis
+        kstate = true;
+        kbuf = c;
+    } else { // Ascii Out
+        lcdput(c,cx,cy);
+        movecorsor(1);  // 1byte Move
+    }
+}
+
+
+void s_Lcdinit()
+    {
+//        device.baud(9600);
+        rst = 0;            //Reset
+        wait(0.1);
+        rst = 1;
+    wait(0.1);
+    lcdspeed();//speed 115200
+    wait(0.1);
+    device.baud(115200);
+    device.putc(0x55);//Back light On
+    device.putc(0x03);
+    device.putc(0x89);
+    device.putc(0x01);
+    device.putc(0x2C);
+    device.putc(0xB6);
+    device.putc(0xAA);
+    bmp(0,0,1);
+    lcdputs(" GingaX ",20,140);
+    wait(3);
+    bmp(0,0,0);
+    wait(3);
+    cls();
+    color(0xffff);
+    kanji_init();
+    }
+