Gingax x / AD128160
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers s_Lcd.cpp Source File

s_Lcd.cpp

00001 #include "mbed.h"
00002 
00003 #define  kanji_file    "/local/SHMZN16X.FNT"
00004 //#defin  kanji_file  "/local/k12x10.fnt"
00005     
00006 Serial device(p9, p10);  // tx, rx  LCD
00007 DigitalOut rst(p20);     //P20  --> LCD  RST  (Reset)
00008 
00009 LocalFileSystem local("local");
00010 
00011 int cx = 0;
00012 int cy = 0;
00013 int offsety = 0;
00014 bool kstate = false;
00015 unsigned char kbuf;
00016 
00017     FILE *fp;   //File open buffer
00018 
00019     unsigned short start[92], end[92];
00020     unsigned char font[64];              
00021 
00022     char            Identifier[6+1];   
00023     char            FontName[8+1];    
00024     unsigned char    XSize;            
00025     unsigned char    YSize;            
00026     unsigned char    Size;            
00027     unsigned char    CodeType;        
00028     unsigned char    Tnum;            
00029 
00030 #define FONT_XBYTE        ((XSize + 7) >> 3)
00031 #define FONT_YBYTE        ((YSize + 7) >> 3)
00032 
00033 void lcdspeed()
00034     {
00035     device.putc(0x55);// UART Speed 115200bps
00036     device.putc(0x05);
00037     device.putc(0x8B);
00038     device.putc(0x00);
00039     device.putc(0x01);
00040     device.putc(0xC2);
00041     device.putc(0x00);
00042     device.putc(0x4E);
00043     device.putc(0xAA);
00044     }
00045 void bmp(int x0,int y0,int bmp_no)
00046     {
00047     unsigned char x0H;
00048     unsigned char x0L;
00049     unsigned char y0H;
00050     unsigned char y0L;
00051     unsigned char rH;
00052     unsigned char rL;
00053     unsigned char sum;
00054 
00055     x0H = x0 >> 8;
00056     x0L = x0 & 0xFF;
00057     y0H = y0 >> 8;
00058     y0L = y0 & 0xff;
00059     rH = bmp_no >> 8;
00060     rL = bmp_no & 0xFF;
00061     sum = x0H+x0L+y0H+y0L+rH+rL+0x09;
00062     device.putc(0x55);
00063     device.putc(0x07);
00064     device.putc(0x09);//command
00065     device.putc(x0H);
00066     device.putc(x0L);
00067     device.putc(y0H);
00068     device.putc(y0L);
00069     device.putc(rH);
00070     device.putc(rL);
00071     device.putc(sum);
00072     device.putc(0xAA);
00073     }
00074 
00075 void cls()
00076     {
00077     device.putc(0x55);// Clear
00078     device.putc(0x02);
00079     device.putc(0x80);
00080     device.putc(0x55);
00081     device.putc(0xD5);
00082     device.putc(0xAA);
00083     }
00084 void scroll(void){
00085 
00086 }
00087 
00088 
00089 
00090 
00091 void lcdputs( char data[98],int x,int y)
00092     {
00093     unsigned char s=0;
00094     unsigned char x0H;
00095     unsigned char x0L;
00096     unsigned char y0H;
00097     unsigned char y0L;
00098     unsigned char datalen;
00099     x0H = x >> 8;
00100     x0L = x & 0xFF;
00101     y0H = y >> 8;
00102     y0L = y & 0xFF;
00103     datalen = strlen(data)+5;
00104     device.putc(0x55);
00105     device.putc(datalen);
00106     device.putc(0x0B);  // command ASCII Print
00107     device.putc(x0H);   //x upper 8bit
00108     device.putc(x0L);   //x low 8bit
00109     device.putc(y0H);   //y upper 8bit
00110     device.putc(y0L);   //y low 8bit
00111     for(int a=0;a<strlen(data);a++)
00112         {
00113             device.putc(data[a]);
00114             s = s+data[a];
00115         }
00116     s = s+x0H+x0L+y0H+y0L+0x0B;
00117     device.putc(s);//sumcheck
00118     device.putc(0xAA);
00119     }
00120 
00121 void color(int rgb)
00122     {
00123     int c1;
00124     int c2;
00125     int sum;
00126     
00127     c1=(rgb >> 8) & 0xff;
00128     c2=(rgb & 0xff);
00129     sum=c1+c2+0x84;
00130     device.putc(0x55);
00131     device.putc(0x03);
00132     device.putc(0x84);
00133     device.putc(c1);
00134     device.putc(c2);
00135     device.putc(sum);
00136     device.putc(0xAA);
00137     }
00138 
00139 void newline(void){
00140     cx = 0;
00141     cy += 16;
00142     if(cy > 160){
00143         scroll();
00144         cy = 0;
00145         color(0x001f);
00146         cls();
00147         color(0xffff);
00148     }
00149 }
00150 
00151 void movecorsor(int a)
00152     {
00153     cx+=8*a;
00154     if(cx>=128) newline();
00155     }
00156 
00157 
00158 void pixel(int x0,int y0)
00159     {
00160     unsigned char x0H;
00161     unsigned char x0L;
00162     unsigned char y0H;
00163     unsigned char y0L;
00164     unsigned char sum;
00165 
00166     x0H = x0 >> 8;
00167     x0L = x0 & 0xFF;
00168     y0H = y0 >> 8;
00169     y0L = y0 & 0xff;
00170 
00171     sum = x0H+x0L+y0H+y0L+0x01;
00172     device.putc(0x55);
00173     device.putc(0x05);
00174     device.putc(0x01);//command
00175     device.putc(x0H);
00176     device.putc(x0L);
00177     device.putc(y0H);
00178     device.putc(y0L);
00179     device.putc(sum);
00180     device.putc(0xAA);
00181     } 
00182 
00183 void box(int x0,int y0,int x1,int y1,int paint)
00184     {
00185     unsigned char x0H;
00186     unsigned char x0L;
00187     unsigned char x1H;
00188     unsigned char x1L;
00189     unsigned char y0H;
00190     unsigned char y0L;
00191     unsigned char y1H;
00192     unsigned char y1L;
00193     unsigned char sum;
00194     unsigned char cmd;
00195     switch (paint)
00196     {
00197     case 1:
00198         cmd = 0x04;
00199         break;
00200     default:
00201         cmd =0x03;
00202         break;
00203     }
00204     x0H = x0 >> 8;
00205     x0L = x0 & 0xFF;
00206     y0H = y0 >> 8;
00207     y0L = y0 & 0xff;
00208     x1H = x1 >> 8;
00209     x1L = x1 & 0xFF;
00210     y1H = y1 >> 8;
00211     y1L = y1 & 0xff;
00212     sum = x0H+x0L+y0H+y0L+x1H+x1L+y1H+y1L+cmd;
00213     device.putc(0x55);//Box
00214     device.putc(0x09);
00215     device.putc(cmd);//command
00216     device.putc(x0H);
00217     device.putc(x0L);
00218     device.putc(y0H);
00219     device.putc(y0L);
00220     device.putc(x1H);
00221     device.putc(x1L);
00222     device.putc(y1H);
00223     device.putc(y1L);
00224     device.putc(sum);
00225     device.putc(0xAA);
00226     }
00227 
00228 void circle(int x0,int y0,int r,int paint)
00229     {
00230     unsigned char x0H;
00231     unsigned char x0L;
00232     unsigned char y0H;
00233     unsigned char y0L;
00234     unsigned char rH;
00235     unsigned char rL;
00236     unsigned char sum;
00237     unsigned char cmd;
00238     switch (paint)
00239     {
00240     case 0:
00241         cmd = 0x05;
00242         break;
00243     case 1:
00244         cmd = 0x06;
00245         break;
00246     default:
00247         cmd =0x05;
00248         break;
00249     }
00250     x0H = x0 >> 8;
00251     x0L = x0 & 0xFF;
00252     y0H = y0 >> 8;
00253     y0L = y0 & 0xff;
00254     rH = r >> 8;
00255     rL = r & 0xFF;
00256     sum = x0H+x0L+y0H+y0L+rH+rL+cmd;
00257     device.putc(0x55);
00258     device.putc(0x07);
00259     device.putc(cmd);//command
00260     device.putc(x0H);
00261     device.putc(x0L);
00262     device.putc(y0H);
00263     device.putc(y0L);
00264     device.putc(rH);
00265     device.putc(rL);
00266     device.putc(sum);
00267     device.putc(0xAA);
00268     }
00269 
00270 void line(int x0,int y0,int x1,int y1)
00271     {
00272     unsigned char x0H;
00273     unsigned char x0L;
00274     unsigned char x1H;
00275     unsigned char x1L;
00276     unsigned char y0H;
00277     unsigned char y0L;
00278     unsigned char y1H;
00279     unsigned char y1L;
00280     unsigned char sum;
00281 
00282     x0H = x0 >> 8;
00283     x0L = x0 & 0xFF;
00284     y0H = y0 >> 8;
00285     y0L = y0 & 0xff;
00286     x1H = x1 >> 8;
00287     x1L = x1 & 0xFF;
00288     y1H = y1 >> 8;
00289     y1L = y1 & 0xff;
00290     sum = x0H+x0L+y0H+y0L+x1H+x1L+y1H+y1L+02;
00291     device.putc(0x55);
00292     device.putc(0x09);
00293     device.putc(0x02);//command
00294     device.putc(x0H);
00295     device.putc(x0L);
00296     device.putc(y0H);
00297     device.putc(y0L);
00298     device.putc(x1H);
00299     device.putc(x1L);
00300     device.putc(y1H);
00301     device.putc(y1L);
00302     device.putc(sum);
00303     device.putc(0xAA);
00304     }
00305 void lcdput(unsigned char data,int x,int y)
00306     {
00307     unsigned char s;
00308     unsigned char x0H;
00309     unsigned char x0L;
00310     unsigned char y0H;
00311     unsigned char y0L;
00312     unsigned char datalen;
00313     x0H = x >> 8;
00314     x0L = x & 0xFF;
00315     y0H = y >> 8;
00316     y0L = y & 0xFF;
00317     datalen = 6;
00318     device.putc(0x55);
00319     device.putc(datalen);
00320     device.putc(0x0B);  // command ASCII Print
00321     device.putc(x0H);   //x upper 8bit
00322     device.putc(x0L);   //x low 8bit
00323     device.putc(y0H);   //y upper 8bit
00324     device.putc(y0L);   //y low 8bit
00325     device.putc(data);
00326     s = data+x0H+x0L+y0H+y0L+0x0B;
00327     device.putc(s);//sumcheck
00328     device.putc(0xAA);
00329     }
00330     
00331 
00332 
00333 void kanji_init(){ 
00334     //move cursor 
00335      fp = fopen(kanji_file , "r");
00336 
00337 
00338     if(!fp) {
00339         printf("File could not be opened!\n");
00340         exit(1);
00341     }
00342 
00343     fgets(Identifier,   6+1, fp);    // FONTX2
00344     fgets(FontName,     8+1, fp);        
00345     fread(&XSize,       1, 1, fp);        
00346     fread(&YSize,       1, 1, fp);        
00347     fread(&CodeType,    1, 1, fp);  
00348     fread(&Tnum,        1, 1, fp);
00349 
00350     // Table read
00351     for(int a=0;a< Tnum ;a++){
00352         fread(&start[a],1,2,fp);
00353         fread(&end[a]  ,1,2,fp);
00354         }
00355 }
00356 
00357 //Kanji puts  cr = kanji cord
00358 void k_puts(int cr){
00359     //kanji address
00360     int c;
00361     int adrs;
00362     c=0;
00363     adrs=0;
00364     while(cr>start[c]){
00365         if(cr > end[c]){
00366             adrs += end[c]-start[c]+1;
00367             }
00368         else{
00369         adrs += cr - start[c];
00370         }
00371         c++;
00372     }
00373     c--;
00374                         //printf("C=%d,adrs=%d cr=%X ",c,adrs,cr);
00375     // Kanji image Read
00376     fseek( fp, Tnum*4+18+32*adrs, SEEK_SET );
00377     fread(&font,1,32,fp); 
00378     // kanji put
00379     c=0;
00380     for (int y=0;y<16;y++){
00381         for (int x=0;x<8;x++){
00382             if((font[c] & 0x80)==0x80){
00383                 pixel(cx+x,cy+y);
00384                 }
00385             font[c] = font[c] << 1;
00386             if((font[c+1] & 0x80)==0x80){
00387                 pixel(cx+x+8,cy+y);
00388                 }
00389             font[c+1] = font[c+1] << 1;
00390         }
00391         c+=2;
00392     }
00393     //move cursor 
00394     cx+=16;
00395     if(cx>=128){   //LF control
00396         cx=0;
00397         cy+=16;
00398         }
00399 }
00400 
00401 void kanji_end(){
00402     fclose(fp);
00403 }
00404 
00405 void drawc(unsigned char c){
00406     if(kstate){ // 2nd byte of shift-jis
00407         kstate = false;
00408 
00409        k_puts(kbuf << 8 | c);
00410     } else if((0x81 <= c && c <= 0x9f) || (0xe0 <= c && c <= 0xfc)){ // 1st byte of shift-jis
00411         kstate = true;
00412         kbuf = c;
00413     } else { // Ascii Out
00414         lcdput(c,cx,cy);
00415         movecorsor(1);  // 1byte Move
00416     }
00417 }
00418 
00419 
00420 void s_Lcdinit()
00421     {
00422 //        device.baud(9600);
00423         rst = 0;            //Reset
00424         wait(0.1);
00425         rst = 1;
00426     wait(0.1);
00427     lcdspeed();//speed 115200
00428     wait(0.1);
00429     device.baud(115200);
00430     device.putc(0x55);//Back light On
00431     device.putc(0x03);
00432     device.putc(0x89);
00433     device.putc(0x01);
00434     device.putc(0x2C);
00435     device.putc(0xB6);
00436     device.putc(0xAA);
00437     bmp(0,0,1);
00438     lcdputs(" GingaX ",20,140);
00439     wait(3);
00440     bmp(0,0,0);
00441     wait(3);
00442     cls();
00443     color(0xffff);
00444     kanji_init();
00445     }
00446