Kazushi Mukaiyama / AD128160_kazushi_branch

Fork of AD128160 by Gingax x

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