LCD TFT for ssd0139 driver 8 bit mode - using portout for max speed
Dependents: TFT_CJS_ssd01399_portver_prettyfont TFT_CJS_ssd01399_portver_prettyfont snake_main_game ServerLatest
Fork of LCDTFT_ssd01399 by
LCDTFT.cpp
00001 /* 00002 @file LCDTFT.cpp 00003 @version: 1.0 00004 00005 @web www.micros-designs.com.ar 00006 @date 30/01/11 00007 00008 *- Version Log --------------------------------------------------------------* 00009 * Fecha Autor Comentarios * 00010 *----------------------------------------------------------------------------* 00011 * 30/01/11 Suky Original * 00012 *----------------------------------------------------------------------------*/ 00013 /////////////////////////////////////////////////////////////////////////// 00014 //// //// 00015 //// //// 00016 //// (C) Copyright 2011 www.micros-designs.com.ar //// 00017 //// Este c�digo puede ser usado, modificado y distribuido libremente //// 00018 //// sin eliminar esta cabecera y sin garant�a de ning�n tipo. //// 00019 //// //// 00020 //// //// 00021 /////////////////////////////////////////////////////////////////////////// 00022 #include "LCDTFT.h " 00023 00024 LCDTFT::LCDTFT(PinName PIN_RD,PinName PIN_WR,PinName PIN_RS,PinName PIN_CS,PinName PIN_RESET, PortOut *PORTLCD) 00025 : LCD_PIN_RD(PIN_RD),LCD_PIN_WR(PIN_WR),LCD_PIN_RS(PIN_RS),LCD_PIN_CS(PIN_CS),LCD_PIN_RESET(PIN_RESET){ 00026 LCD_PORT=PORTLCD; 00027 X=0; 00028 Y=0; 00029 X_min=0; 00030 X_max=LCD_X_MAX; 00031 _Alto=1; 00032 _Color=0x0000; 00033 } 00034 // global variable for screen orientation set by init 00035 bool orient=0; 00036 00037 static const unsigned short regValues[] = { 00038 0x0000, 0x0001, // start oscillation 00039 0x00FF, 0x0010, // wait 16 ms 00040 0x0007, 0x0000, //display control - zeros everything ??? 00041 0x0013, 0x0000, // power control 3 setting = all zero 00042 0x0011, 0x2604, //power control 2 seting = gvd voltage 0x26 vci1 voltage 0x04 00043 0x0014, 0x0015, // power contrlol 4 setting vcmr=0 vcomh=0 vml=13 = amplitude of vcom voltage 00044 0x0010, 0x3C00, // power control 1 bt3=0 sap=7 (0b111) bt=4 (0b100) 00045 0x0013, 0x0040, //power control 3 PON=1 PON1=0 AON=0 00046 0x00FF,0x0010, // wait 16ms 00047 0x0013, 0x0060, // power control 3 PON=1 PON1=1 AON=0 00048 0x00FF, 0x0032, // wait 50ms 00049 0x0013, 0x0070, //power control 3 PON=1 PON1=1 AON=1 00050 0x00FF, 0x0028, // wait 40ms 00051 00052 0x0001, 0x0127, // driver ouptut control 00053 0x0002, 0x0700, //LCD driving waveform settings 00054 0x0003, 0x1030, // entry mode settingtri=0 dfm=0 bgr=1 id1:id0=11 00055 0x0007, 0x0000, // display control 1 pt1-0=0 vle2-1=0 spt=0 gon=0 rev=0 d1-0=0 00056 0x0008, 0x0404, //black period control fp3-0=4 bp3-0=4 00057 0x000B, 0x0200, //frame cycle setting nd1-0=0 sdt1-0=2 ecs2-0=0 div1-0=0 dcr_ex=0 dcr2-0=0 rtn1-0=0 00058 0x000C, 0x0000, //external interface controlrm=0 dm1-0=0 rim1-0=0 00059 0x00015,0x0000, //sub panel control sub_im1-0=0 stn_en=0 mpu_mode=0 fcv_en=0 00060 00061 //gamma setting 00062 0x0030, 0x0000, 00063 0x0031, 0x0606, 00064 0x0032, 0x0006, 00065 0x0033, 0x0403, 00066 0x0034, 0x0107, 00067 0x0035, 0x0101, 00068 0x0036, 0x0707, 00069 0x0037, 0x0304, 00070 0x0038, 0x0A00, 00071 0x0039, 0x0706, 00072 // end of gamma settings 00073 0x0040, 0x0000, // gate scan position (start g1 scan) scn5-0=0 00074 0x0041, 0x0000, // vertical scroll setting vl8-0=0 00075 0x0042, 0x013F, // screen end position se18-10=0x13F 00076 0x0043, 0x0000, // screen_start position ss28-20=0 00077 0x0044, 0x0000, // 2nd screen driving position end 00 00078 0x0045, 0x0000, //2nd screen driving position start =00 00079 0x0046, 0xEF00, //window addre horizontal ram for x0,x1 HEA=0xEF HSA=00 ie x=0 and x=239 00080 0x0047, 0x013F, //vertical ram address end vea=0x13F ie y=319 00081 0x0048, 0x0000, //vertical ram address start=00 ie y=0 00082 0x0007, 0x0011, //dispaly control 1 00083 0x00FF, 0x0028, //wait 40ms 00084 0x0007, 0x0017, //display control 1 00085 00086 }; 00087 00088 00089 void LCDTFT::vLCDTFTSetParametersPrintf(unsigned short Xo,unsigned short Yo,unsigned short Xmin,unsigned short Xmax,unsigned char Alto, unsigned short Color,unsigned short BackColor){ 00090 00091 X=Xo; 00092 Y=Yo; 00093 X_min=Xmin; 00094 X_max=Xmax; 00095 _Alto=Alto; 00096 _Color=Color; 00097 _Background=BackColor; 00098 } 00099 00100 int LCDTFT::_putc(int value){ 00101 char Fmt[2]={value,0}; 00102 00103 if(value=='\n'){ 00104 X=X_min; 00105 Y+=8*_Alto + 1; 00106 }else{ 00107 vLCDTFTText(X,Y,(const char *)&Fmt[0],&ARIAL[0],_Alto,_Color,_Background); 00108 X+=5*_Alto+1; 00109 if(X >= X_max){ 00110 X = X_min; 00111 Y += 8*_Alto + 1; 00112 } 00113 } 00114 return(value); 00115 } 00116 00117 int LCDTFT::_getc(){ 00118 return(-1); 00119 } 00120 00121 void LCDTFT::vLCDTFTWriteCommand(unsigned short Data){ 00122 00123 LCD_PIN_RS=0; 00124 LCD_PIN_CS=0; 00125 LCD_PORT->write(Data>>8); // MSB so with an 8 bit port we need to write here the high and ,low bytes successively 00126 LCD_PIN_WR=0; 00127 LCD_PIN_WR=1; 00128 LCD_PORT->write(Data & 0x00FF); // LSB 00129 LCD_PIN_WR=0; 00130 LCD_PIN_WR=1; 00131 00132 LCD_PIN_CS=1; 00133 } 00134 00135 void LCDTFT::vLCDTFTWriteData(unsigned short Data){ 00136 LCD_PIN_RS=1; 00137 LCD_PIN_CS=0; 00138 LCD_PORT->write(Data>>8); // MSB so with an 8 bit port we need to write here the high and ,low bytes successively 00139 LCD_PIN_WR=0; 00140 LCD_PIN_WR=1; 00141 LCD_PORT->write(Data & 0x00FF); // LSB 00142 LCD_PIN_WR=0; 00143 LCD_PIN_WR=1; 00144 LCD_PIN_CS =1; 00145 } 00146 00147 00148 void LCDTFT::vLCDTFTWriteCommandData(unsigned short CMD,unsigned short Data){ 00149 vLCDTFTWriteCommand(CMD); 00150 vLCDTFTWriteData(Data); 00151 } 00152 00153 void LCDTFT::vLCDTFTAddressSet(unsigned short x1,unsigned short y1,unsigned short x2,unsigned short y2){ 00154 00155 vLCDTFTWriteCommandData(0x0044,(x2<<8)+x1); 00156 vLCDTFTWriteCommandData(0x0045,y1); 00157 vLCDTFTWriteCommandData(0x0046,y2); 00158 vLCDTFTWriteCommandData(0x004e,x1); 00159 vLCDTFTWriteCommandData(0x004f,y1); 00160 vLCDTFTWriteCommand(0x0022); 00161 } 00162 00163 void LCDTFT::vLCDTFTAddressSetPoint(unsigned short x,unsigned short y){ 00164 00165 // CGRAM addresses are given by (x+y*256) - for some reason the y cordinate 00166 // addresses require 17 bits 00167 // equiv if y is given by 0xYYY and x by 0xXX 00168 // then address of point x,y is 0xYYYXXX 00169 int add; 00170 int adl,adh; 00171 if(orient==0){ // portrait 00172 add=x+y*256; //240 x 320 y but 256 bytes for each x row 00173 00174 } 00175 else{ 00176 add=(239-y)+x*256; 00177 } 00178 adl=add & 0x00FF; // selects the 8 low bits 00179 adh=(add >>8); // the 9 high bits 00180 vLCDTFTWriteCommandData(0x0020,adl); // 8 lsb of address 00181 vLCDTFTWriteCommandData(0x0021,adh); //9 msb of address 00182 vLCDTFTWriteCommand(0x0022); // prepare to send rgb data 00183 } 00184 00185 void LCDTFT::vLCDTFTInit(bool format){ 00186 00187 int i; 00188 unsigned short address,data; 00189 orient=format; 00190 LCD_PIN_RESET=1; 00191 wait_ms(5); // must hold for at least 1ms after reset 00192 LCD_PIN_RESET=0; 00193 wait_ms(10); // wait for stable R-C oscillation 00194 LCD_PIN_RESET=1; 00195 wait_ms(5); // final wait for rest to be acitvated 00196 LCD_PIN_CS=1; 00197 LCD_PIN_RD=1; 00198 LCD_PIN_WR=1; 00199 wait_ms(20); 00200 00201 for(i=0;i<sizeof(regValues)/4;i++) 00202 { 00203 address=regValues[i*2]; 00204 data=regValues[i*2+1]; 00205 00206 if(address==0xFF) 00207 { 00208 wait_ms(data); 00209 } 00210 else { 00211 vLCDTFTWriteCommandData(address,data); 00212 } 00213 } 00214 wait_ms(1); 00215 00216 00217 /* 00218 vLCDTFTWriteCommandData(0x0000,0x0001); wait_ms(1); 00219 vLCDTFTWriteCommandData(0x0003,0xA8A4); wait_ms(1); 00220 vLCDTFTWriteCommandData(0x000C,0x0000); wait_ms(1); 00221 vLCDTFTWriteCommandData(0x000D,0x080C); wait_ms(1); 00222 vLCDTFTWriteCommandData(0x000E,0x2B00); wait_ms(1); 00223 vLCDTFTWriteCommandData(0x001E,0x00B0); wait_ms(1); 00224 vLCDTFTWriteCommandData(0x0001,0x2B3F); wait_ms(1); 00225 vLCDTFTWriteCommandData(0x0002,0x0600); wait_ms(1); 00226 vLCDTFTWriteCommandData(0x0010,0x0000); wait_ms(1); 00227 vLCDTFTWriteCommandData(0x0011,0x6070); wait_ms(1); 00228 vLCDTFTWriteCommandData(0x0005,0x0000); wait_ms(1); 00229 vLCDTFTWriteCommandData(0x0006,0x0000); wait_ms(1); 00230 vLCDTFTWriteCommandData(0x0016,0xEF1C); wait_ms(1); 00231 vLCDTFTWriteCommandData(0x0017,0x0003); wait_ms(1); 00232 vLCDTFTWriteCommandData(0x0007,0x0233); wait_ms(1); 00233 vLCDTFTWriteCommandData(0x000B,0x0000); wait_ms(1); 00234 vLCDTFTWriteCommandData(0x000F,0x0000); wait_ms(1); 00235 vLCDTFTWriteCommandData(0x0041,0x0000); wait_ms(1); 00236 vLCDTFTWriteCommandData(0x0042,0x0000); wait_ms(1); 00237 vLCDTFTWriteCommandData(0x0048,0x0000); wait_ms(1); 00238 vLCDTFTWriteCommandData(0x0049,0x013F); wait_ms(1); 00239 vLCDTFTWriteCommandData(0x004A,0x0000); wait_ms(1); 00240 vLCDTFTWriteCommandData(0x004B,0x0000); wait_ms(1); 00241 vLCDTFTWriteCommandData(0x0044,0xEF00); wait_ms(1); 00242 vLCDTFTWriteCommandData(0x0045,0x0000); wait_ms(1); 00243 vLCDTFTWriteCommandData(0x0046,0x013F); wait_ms(1); 00244 vLCDTFTWriteCommandData(0x0030,0x0707); wait_ms(1); 00245 vLCDTFTWriteCommandData(0x0031,0x0204); wait_ms(1); 00246 vLCDTFTWriteCommandData(0x0032,0x0204); wait_ms(1); 00247 vLCDTFTWriteCommandData(0x0033,0x0502); wait_ms(1); 00248 vLCDTFTWriteCommandData(0x0034,0x0507); wait_ms(1); 00249 vLCDTFTWriteCommandData(0x0035,0x0204); wait_ms(1); 00250 vLCDTFTWriteCommandData(0x0036,0x0204); wait_ms(1); 00251 vLCDTFTWriteCommandData(0x0037,0x0502); wait_ms(1); 00252 vLCDTFTWriteCommandData(0x003A,0x0302); wait_ms(1); 00253 vLCDTFTWriteCommandData(0x003B,0x0302); wait_ms(1); 00254 vLCDTFTWriteCommandData(0x0023,0x0000); wait_ms(1); 00255 vLCDTFTWriteCommandData(0x0024,0x0000); wait_ms(1); 00256 vLCDTFTWriteCommandData(0x0025,0x8000); wait_ms(1); 00257 vLCDTFTWriteCommandData(0x004f,0); 00258 vLCDTFTWriteCommandData(0x004e,0); 00259 vLCDTFTWriteCommand(0x0022); 00260 */ 00261 } 00262 00263 void LCDTFT::vLCDTFTFillScreen(unsigned short Color){ 00264 unsigned short i,j; 00265 00266 vLCDTFTWriteCommandData(0x0020,00); //set x=0 00267 vLCDTFTWriteCommandData(0x0021,00); // set y=0 00268 vLCDTFTWriteCommand(0x0022); 00269 00270 for(i=0;i<320;i++){ 00271 for (j=0;j<240;j++){ 00272 vLCDTFTWriteData(Color); 00273 } 00274 } 00275 } 00276 00277 void LCDTFT::vLCDTFTPoint(unsigned short x,unsigned short y,unsigned short Color){ 00278 00279 vLCDTFTAddressSetPoint(x,y); 00280 vLCDTFTWriteData(Color); 00281 } 00282 // write a text into the display using abasic 5x7 font defined in fuentes.h 00283 void LCDTFT::vLCDTFTText(unsigned short x,unsigned short y,const char *PtrText,const char (*Fuente)[5],unsigned char Alto,unsigned short Color,unsigned short BackColor){ 00284 unsigned short i, j, k, l, m, temp,lmax; 00285 char DataPunto[5]; 00286 const char *Ptr; 00287 if(orient==0) lmax=LCD_X_MAX; 00288 else lmax=LCD_Y_MAX; 00289 00290 while(*PtrText!='\0'){ 00291 Ptr=(Fuente+*PtrText-' ')[0]; 00292 for(i=0;i<5;i++){DataPunto[i]=*Ptr++;} 00293 switch(*PtrText){ 00294 case '\n': 00295 y += 7*Alto + 1; 00296 break; 00297 case '\r': 00298 x = 0; 00299 break; 00300 default: 00301 if(x+5*Alto >= lmax){ 00302 x = 0; 00303 y += 7*Alto + 1; 00304 } 00305 for(j=0; j<5; ++j, x+=Alto){ 00306 for(k=0; k < 7; k++){ 00307 temp=(0x01<<k); 00308 if((DataPunto[j]&temp)==temp){ 00309 for(l=0; l < Alto; ++l){ 00310 for(m=0; m < Alto; ++m){ 00311 vLCDTFTPoint(x+m,y+k*Alto+l,Color); 00312 }//endform 00313 }//endforl 00314 }//endif 00315 else{ 00316 for(l=0; l < Alto; ++l){ 00317 for(m=0; m < Alto; ++m){ 00318 vLCDTFTPoint(x+m,y+k*Alto+l,BackColor); 00319 }//endform 00320 }//endforl 00321 } 00322 } 00323 } 00324 x++; 00325 break; 00326 } 00327 *PtrText++; 00328 } 00329 } 00330 00331 void LCDTFT::vLCDTFTLine(unsigned short x1,unsigned short y1,unsigned short x2,unsigned short y2,unsigned short Color){ 00332 00333 unsigned short dy, dx; 00334 short addx=1, addy=1; 00335 short P, diff; 00336 unsigned short i=0; 00337 00338 diff=((short)x2-x1); 00339 if(diff<0) diff*=-1; 00340 dx=diff; 00341 diff=((short)y2-y1); 00342 if(diff<0) diff*=-1; 00343 dy=diff; 00344 00345 00346 if(x1 > x2)addx = -1; 00347 if(y1 > y2)addy = -1; 00348 if(dx >= dy){ 00349 dy *= 2; 00350 P = dy - dx; 00351 diff = P - dx; 00352 for(;i<=dx;++i){ 00353 vLCDTFTPoint(x1, y1, Color); 00354 if(P < 0){ 00355 P += dy; 00356 x1 += addx; 00357 }else{ 00358 P += diff; 00359 x1 += addx; 00360 y1 += addy; 00361 } 00362 } 00363 }else{ 00364 dx *= 2; 00365 P = dx - dy; 00366 diff = P - dy; 00367 for(; i<=dy; ++i){ 00368 vLCDTFTPoint(x1, y1, Color); 00369 if(P < 0){ 00370 P += dx; 00371 y1 += addy; 00372 }else{ 00373 P += diff; 00374 x1 += addx; 00375 y1 += addy; 00376 } 00377 } 00378 } 00379 } 00380 00381 00382 void LCDTFT::vLCDTFTRectangle(unsigned short x1,unsigned short y1,unsigned short x2,unsigned short y2,bool Filled,unsigned short Color){ 00383 00384 if(Filled){ 00385 int Lenght=(int)(y2-y1); 00386 00387 00388 for(int i=0;i<Lenght;i++){ 00389 vLCDTFTLine(x1,y1+i,x2,y1+i,Color); 00390 } 00391 }else{ 00392 vLCDTFTLine(x1, y1, x2, y1, Color); 00393 vLCDTFTLine(x1, y2, x2, y2, Color); 00394 vLCDTFTLine(x1, y1, x1, y2, Color); 00395 vLCDTFTLine(x2, y1, x2, y2, Color); 00396 } 00397 } 00398 00399 00400 void LCDTFT::vLCDTFTCircle(unsigned short x,unsigned short y,unsigned short Radius,bool Filled,unsigned short Color){ 00401 short a, b, P; 00402 00403 a = 0; 00404 b = Radius; 00405 P = 1 - Radius; 00406 00407 do{ 00408 if(Filled){ 00409 vLCDTFTLine(x-a, y+b, x+a, y+b, Color); 00410 vLCDTFTLine(x-a, y-b, x+a, y-b, Color); 00411 vLCDTFTLine(x-b, y+a, x+b, y+a, Color); 00412 vLCDTFTLine(x-b, y-a, x+b, y-a, Color); 00413 }else{ 00414 vLCDTFTPoint(a+x, b+y, Color); 00415 vLCDTFTPoint(b+x, a+y, Color); 00416 vLCDTFTPoint(x-a, b+y, Color); 00417 vLCDTFTPoint(x-b, a+y, Color); 00418 vLCDTFTPoint(b+x, y-a, Color); 00419 vLCDTFTPoint(a+x, y-b, Color); 00420 vLCDTFTPoint(x-a, y-b, Color); 00421 vLCDTFTPoint(x-b, y-a, Color); 00422 } 00423 if(P < 0) P += 3 + 2 * a++; 00424 else P += 5 + 2 * (a++ - b--); 00425 }while(a <= b); 00426 } 00427 /* 00428 void LCDTFT::vDrawImageBMP24Bits(const char *NameImagen){ 00429 00430 #define OffsetWidthPixel 18 00431 #define OffsetHeighPixel 22 00432 #define OffsetSizeFile 34 00433 #define OffsetInitData 10 00434 #define OffserTipeFile 28 00435 char Nombre[80],k; 00436 unsigned short PosXImagen,PosYImagen; 00437 unsigned char Temp,BufferHeader[54],BufferTemp[3],Adicional; 00438 unsigned int WidthPixel,HeighPixel; 00439 unsigned short TipeFile,InitData,Temp16bits; 00440 union{ 00441 unsigned short Val; 00442 struct{ 00443 unsigned Blue:5; 00444 unsigned Green:6; 00445 unsigned Red:5; 00446 }; 00447 }Color; 00448 00449 LocalFileSystem local("mbedMemory"); 00450 sprintf(&Nombre[0],"/mbedMemory/"); 00451 k=12; 00452 while(*NameImagen!='\0'){ 00453 Nombre[k++]=*NameImagen++; 00454 } 00455 00456 FILE *Imagen = fopen((const char *)&Nombre[0], "r"); // Abrimos archivo para lectura. 00457 // Si no se pudo encontrar archivo: 00458 if(!Imagen) { 00459 vLCDTFTFillScreen(ColorBlack); 00460 return; 00461 } 00462 // Leemos cabecera de archivo 00463 fread(&BufferHeader[0],1,54,Imagen); 00464 00465 WidthPixel = ((unsigned int)BufferHeader[OffsetWidthPixel + 3]*16777216+BufferHeader[OffsetWidthPixel + 2]*65536+BufferHeader[OffsetWidthPixel + 1]*256+BufferHeader[OffsetWidthPixel]); 00466 HeighPixel = ((unsigned int)BufferHeader[OffsetHeighPixel + 3]*16777216+BufferHeader[OffsetHeighPixel + 2]*65536+BufferHeader[OffsetHeighPixel + 1]*256+BufferHeader[OffsetHeighPixel]); 00467 InitData = ((unsigned short)BufferHeader[OffsetInitData]); 00468 TipeFile = ((unsigned short)BufferHeader[OffserTipeFile + 1]*256 + BufferHeader[OffserTipeFile]); 00469 00470 if((WidthPixel>LCD_X_MAX) || (HeighPixel>LCD_Y_MAX) || (TipeFile!=24)){ 00471 fclose(Imagen); 00472 return; 00473 } 00474 00475 if(InitData!=54){ 00476 for(int k=54;k<InitData;k++){ 00477 fread(&Temp,1,1,Imagen); 00478 } 00479 } 00480 00481 PosXImagen=(LCD_X_MAX/2)-(WidthPixel/2); 00482 PosYImagen=(LCD_Y_MAX/2)+(HeighPixel/2); 00483 00484 Temp16bits=WidthPixel*3; 00485 Adicional=0; 00486 while(((Temp16bits)%4)!=0){ 00487 Adicional++; 00488 Temp16bits++; 00489 } 00490 00491 for(int k=0,y=PosYImagen;k<HeighPixel;k++,y--){ 00492 vLCDTFTAddressSet(PosXImagen,y,PosXImagen+WidthPixel-1,y); 00493 for(int i=0;i<WidthPixel;i++){ 00494 fread(&BufferTemp[0],1,3,Imagen); // Leemos 3 bytes (R,G,B) 00495 Color.Blue=BufferTemp[0]>>3;Color.Green=BufferTemp[1]>>2;Color.Red=BufferTemp[2]>>3; // Conversion de 24-bits a 16-bits.- 00496 vLCDTFTWriteData(Color.Val); 00497 } 00498 // Bytes adicionales para que linea sea multiplo de 4.- 00499 for(int p=0;p<Adicional;p++){ 00500 fread(&Temp,1,1,Imagen); 00501 } 00502 } 00503 fclose(Imagen); 00504 } 00505 */ 00506 void LCDTFT::vLCDTFTDrawImage(unsigned short x,unsigned short y, unsigned short Width, unsigned short Heigh, unsigned int Lenght, const unsigned short *Imagen){ 00507 00508 vLCDTFTAddressSet(x,y,x+Width-1,y+Heigh-1); 00509 for(int i=0;i<Lenght;i++){ 00510 vLCDTFTWriteData(*Imagen++); 00511 } 00512 }
Generated on Wed Jul 13 2022 02:54:41 by
![doxygen](doxygen.png)