k og / HX8347D

Fork of HX8347D by k og

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HX8347D.cpp Source File

HX8347D.cpp

00001 #include "HX8347D.h"
00002 #include "glcdfont.h"
00003 
00004 #define X(x) (x)
00005 #define I_X(x) (TFTWIDTH - x - 1)
00006 #define Y(y) (y)
00007 #define I_Y(y) (TFTHEIGHT - y - 1)
00008 
00009 size_t HX8347D::println(char *c){
00010   while (c[0] != 0) {
00011     write(c[0]);
00012     c++;
00013   }
00014     //drawString(cursor_x, cursor_y, c, textcolor);
00015     return sizeof(c);
00016 }
00017 /*
00018 size_t TFTLCD::println(unsigned long n , int t){
00019     //drawString(cursor_x, cursor_y, n, textcolor);
00020 }
00021 */
00022 
00023 HX8347D::HX8347D(
00024     PinName rd, PinName wr, PinName rs, PinName cs, PinName rst,
00025     PinName d0, PinName d1, PinName d2, PinName d3,
00026     PinName d4, PinName d5, PinName d6, PinName d7
00027     ):
00028     _rd(rd), _wr(wr), _rs(rs), _cs(cs), _rst(rst),
00029     _d(d0, d1, d2, d3, d4, d5, d6, d7)
00030 {
00031     rotation = 0;
00032     _width = TFTWIDTH;
00033     _height = TFTHEIGHT;
00034     
00035   _rd=1; 
00036   _wr=1;
00037   _rs=1;
00038   _cs=1;
00039   _rst = 1;
00040 
00041   cursor_y = cursor_x = 0;
00042   textsize = 1;
00043   textcolor = 0xFFFF;
00044  }
00045 
00046 
00047 void HX8347D::goHome(void) {
00048   goTo(0,0);
00049 }
00050 
00051 uint16_t HX8347D::width(void) {
00052   return _width;
00053 }
00054 uint16_t HX8347D::height(void) {
00055   return _height;
00056 }
00057 
00058 void HX8347D::setCursor(uint16_t x, uint16_t y) {
00059   cursor_x = x;
00060   cursor_y = y;
00061 }
00062 
00063 void HX8347D::setTextSize(uint8_t s) {
00064   textsize = s;
00065 }
00066 
00067 void HX8347D::setTextColor(uint16_t c) {
00068   textcolor = c;
00069 }
00070 
00071 size_t HX8347D::write(uint8_t c) {
00072   if (c == '\n') {
00073     cursor_y += textsize*8;
00074     cursor_x = 0;
00075   } else if (c == '\r') {
00076     // skip em
00077   } else {
00078     drawChar(cursor_x, cursor_y, c, textcolor, textsize);
00079     cursor_x += textsize*6;
00080   }
00081   return 0;
00082 }
00083 
00084 void HX8347D::drawString(uint16_t x, uint16_t y, char *c, 
00085             uint16_t color, uint8_t size) {
00086   while (c[0] != 0) {
00087     drawChar(x, y, c[0], color, size);
00088     x += size*6;
00089     c++;
00090   }
00091 }
00092 // draw a character
00093 void HX8347D::drawChar(uint16_t x, uint16_t y, char c, 
00094               uint16_t color, uint8_t size) {
00095   for (uint8_t i =0; i<5; i++ ) {
00096     uint8_t line = font[(c*5)+i];
00097     for (uint8_t j = 0; j<8; j++) {
00098       if (line & 0x1) {
00099     if (size == 1) // default size
00100       drawPixel(x+i, y+j, color);
00101     else {  // big size
00102       fillRect(x+i*size, y+j*size, size, size, color);
00103     } 
00104       }
00105       line >>= 1;
00106     }
00107   }
00108 }
00109 
00110 
00111 void HX8347D::drawTriangle(uint16_t x0, uint16_t y0,
00112               uint16_t x1, uint16_t y1,
00113               uint16_t x2, uint16_t y2, uint16_t color)
00114 {
00115   drawLine(x0, y0, x1, y1, color);
00116   drawLine(x1, y1, x2, y2, color);
00117   drawLine(x2, y2, x0, y0, color); 
00118 }
00119 
00120 void HX8347D::fillTriangle ( int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint16_t color)
00121 {
00122   if (y0 > y1) {
00123     swap(y0, y1); swap(x0, x1);
00124   }
00125   if (y1 > y2) {
00126     swap(y2, y1); swap(x2, x1);
00127   }
00128   if (y0 > y1) {
00129     swap(y0, y1); swap(x0, x1);
00130   }
00131 
00132   int32_t dx1, dx2, dx3; // Interpolation deltas
00133   int32_t sx1, sx2, sy; // Scanline co-ordinates
00134 
00135   sx2=(int32_t)x0 * (int32_t)1000; // Use fixed point math for x axis values
00136   sx1 = sx2;
00137   sy=y0;
00138 
00139   // Calculate interpolation deltas
00140   if (y1-y0 > 0) dx1=((x1-x0)*1000)/(y1-y0);
00141     else dx1=0;
00142   if (y2-y0 > 0) dx2=((x2-x0)*1000)/(y2-y0);
00143     else dx2=0;
00144   if (y2-y1 > 0) dx3=((x2-x1)*1000)/(y2-y1);
00145     else dx3=0;
00146 
00147   // Render scanlines (horizontal lines are the fastest rendering method)
00148   if (dx1 > dx2)
00149   {
00150     for(; sy<=y1; sy++, sx1+=dx2, sx2+=dx1)
00151     {
00152       drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
00153     }
00154     sx2 = x1*1000;
00155     sy = y1;
00156     for(; sy<=y2; sy++, sx1+=dx2, sx2+=dx3)
00157     {
00158       drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
00159     }
00160   }
00161   else
00162   {
00163     for(; sy<=y1; sy++, sx1+=dx1, sx2+=dx2)
00164     {
00165       drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
00166     }
00167     sx1 = x1*1000;
00168     sy = y1;
00169     for(; sy<=y2; sy++, sx1+=dx3, sx2+=dx2)
00170     {
00171       drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
00172     }
00173   }
00174 }
00175 
00176 uint16_t HX8347D::Color565(uint8_t r, uint8_t g, uint8_t b) {
00177     uint16_t c;
00178     c = r >> 3;
00179     c <<= 6;
00180     c |= g >> 2;
00181     c <<= 5;
00182     c |= b >> 3;
00183 
00184     //printf("%x\r\n", c);
00185     return c;
00186 }
00187 
00188 
00189 // draw a rectangle
00190 void HX8347D::drawRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, 
00191               uint16_t color) {
00192   // smarter version
00193   drawHorizontalLine(x, y, w, color);
00194   drawHorizontalLine(x, y+h-1, w, color);
00195   drawVerticalLine(x, y, h, color);
00196   drawVerticalLine(x+w-1, y, h, color);
00197 }
00198 
00199 // draw a rounded rectangle
00200 void HX8347D::drawRoundRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t r,
00201                uint16_t color) {
00202   // smarter version
00203   drawHorizontalLine(x+r, y, w-2*r, color);
00204   drawHorizontalLine(x+r, y+h-1, w-2*r, color);
00205   drawVerticalLine(x, y+r, h-2*r, color);
00206   drawVerticalLine(x+w-1, y+r, h-2*r, color);
00207   // draw four corners
00208   drawCircleHelper(x+r, y+r, r, 1, color);
00209   drawCircleHelper(x+w-r-1, y+r, r, 2, color);
00210   drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color);
00211   drawCircleHelper(x+r, y+h-r-1, r, 8, color);
00212 }
00213 
00214 
00215 // fill a rounded rectangle
00216 void HX8347D::fillRoundRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t r,
00217                uint16_t color) {
00218   // smarter version
00219   fillRect(x+r, y, w-2*r, h, color);
00220 
00221   // draw four corners
00222   fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color);
00223   fillCircleHelper(x+r, y+r, r, 2, h-2*r-1, color);
00224 }
00225 
00226 // fill a circle
00227 void HX8347D::fillCircle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color) {
00228   //writeRegister(TFTLCD_ENTRY_MOD, 0x1030);
00229   drawVerticalLine(x0, y0-r, 2*r+1, color);
00230   fillCircleHelper(x0, y0, r, 3, 0, color);
00231 }
00232 
00233 
00234 // used to do circles and roundrects!
00235 void HX8347D::fillCircleHelper(uint16_t x0, uint16_t y0, uint16_t r, uint8_t cornername, uint16_t delta,
00236             uint16_t color) {
00237 
00238   int16_t f = 1 - r;
00239   int16_t ddF_x = 1;
00240   int16_t ddF_y = -2 * r;
00241   int16_t x = 0;
00242   int16_t y = r;
00243 
00244   while (x<y) {
00245     if (f >= 0) {
00246       y--;
00247       ddF_y += 2;
00248       f += ddF_y;
00249     }
00250     x++;
00251     ddF_x += 2;
00252     f += ddF_x;
00253   
00254     if (cornername & 0x1) {
00255       drawVerticalLine(x0+x, y0-y, 2*y+1+delta, color);
00256       drawVerticalLine(x0+y, y0-x, 2*x+1+delta, color);
00257     }
00258     if (cornername & 0x2) {
00259       drawVerticalLine(x0-x, y0-y, 2*y+1+delta, color);
00260       drawVerticalLine(x0-y, y0-x, 2*x+1+delta, color);
00261     }
00262   }
00263 }
00264 
00265 
00266 // draw a circle outline
00267 
00268 void HX8347D::drawCircle(uint16_t x0, uint16_t y0, uint16_t r, 
00269             uint16_t color) {
00270   drawPixel(x0, y0+r, color);
00271   drawPixel(x0, y0-r, color);
00272   drawPixel(x0+r, y0, color);
00273   drawPixel(x0-r, y0, color);
00274 
00275   drawCircleHelper(x0, y0, r, 0xF, color);
00276 }
00277 
00278 void HX8347D::drawCircleHelper(uint16_t x0, uint16_t y0, uint16_t r, uint8_t cornername,
00279             uint16_t color) {
00280   int16_t f = 1 - r;
00281   int16_t ddF_x = 1;
00282   int16_t ddF_y = -2 * r;
00283   int16_t x = 0;
00284   int16_t y = r;
00285 
00286 
00287   while (x<y) {
00288     if (f >= 0) {
00289       y--;
00290       ddF_y += 2;
00291       f += ddF_y;
00292     }
00293     x++;
00294     ddF_x += 2;
00295     f += ddF_x;
00296     if (cornername & 0x4) {
00297       drawPixel(x0 + x, y0 + y, color);
00298       drawPixel(x0 + y, y0 + x, color);
00299     } 
00300     if (cornername & 0x2) {
00301       drawPixel(x0 + x, y0 - y, color);
00302       drawPixel(x0 + y, y0 - x, color);
00303     }
00304     if (cornername & 0x8) {
00305       drawPixel(x0 - y, y0 + x, color);
00306       drawPixel(x0 - x, y0 + y, color);
00307     }
00308     if (cornername & 0x1) {
00309       drawPixel(x0 - y, y0 - x, color);
00310       drawPixel(x0 - x, y0 - y, color);
00311     }
00312   }
00313 }
00314 
00315 // fill a rectangle
00316 void HX8347D::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, 
00317               uint16_t fillcolor) {
00318   // smarter version
00319   while (h--)
00320     drawHorizontalLine(x, y++, w, fillcolor);
00321 }
00322 
00323 
00324 void HX8347D::drawVerticalLine(uint16_t x, uint16_t y, uint16_t length, uint16_t color)
00325 {
00326   if (x >= _width) return;
00327 
00328   drawFastLine(x,y,length,color,1);
00329   //drawLine(x, y, x, y+length, color);
00330 }
00331 
00332 void HX8347D::drawHorizontalLine(uint16_t x, uint16_t y, uint16_t length, uint16_t color)
00333 {
00334   if (y >= _height) return;
00335   drawFastLine(x,y,length,color,0);
00336   //drawLine(x, y, x+length, y, color);
00337 }
00338 
00339 
00340 void HX8347D::setRotation(uint8_t x) {
00341 
00342     x %= 4;  // cant be higher than 3
00343     rotation = x;
00344     switch (x) {
00345     case 0:
00346         writeRegister(HX8347D_MEM_ACC_CTRL, 0x08); //0x1030
00347         _width = TFTWIDTH; 
00348         _height = TFTHEIGHT;
00349         break;
00350     case 1:
00351         writeRegister(HX8347D_MEM_ACC_CTRL, 0x68); //0x01028
00352         _width = TFTHEIGHT; 
00353         _height = TFTWIDTH;
00354         break;
00355     case 2:
00356         writeRegister(HX8347D_MEM_ACC_CTRL, 0xC8); //0x1000
00357         _width = TFTWIDTH; 
00358         _height = TFTHEIGHT;
00359         break;
00360     case 3:
00361          writeRegister(HX8347D_MEM_ACC_CTRL, 0xA8 ); //0x1018
00362         _width = TFTHEIGHT; 
00363         _height = TFTWIDTH;
00364         break;
00365     }
00366     //setDefaultViewport();
00367 }
00368 
00369 void HX8347D::drawFastLine(uint16_t x, uint16_t y, uint16_t length, 
00370               uint16_t color, uint8_t rotflag)
00371 {
00372     uint16_t x1 = width() - 1;
00373     uint16_t y1 = height() - 1;
00374     switch (rotation) {
00375         case 0:
00376             x = x;
00377             y = y;
00378             if (rotflag) {
00379                 x1 = x;
00380                 y1 = y + length;
00381             }
00382             else {
00383                 x1 = x + length;
00384                 y1 = y;
00385             }
00386             break;
00387         case 1:
00388             x = x;
00389             y = y;
00390             if (rotflag) {
00391                 x1 = x;
00392                 y1 = y + length;
00393             }
00394             else {
00395                 x1 = x + length;
00396                 y1 = y;
00397             }
00398             break;
00399         case 2:
00400             x = x;
00401             y = y;
00402             if (rotflag) {
00403                 x1 = x;
00404                 y1 = y + length;
00405             }
00406             else {
00407                 x1 = x + length;
00408                 y1 = y;
00409             }
00410             break;
00411         case 3:
00412             x = x;
00413             y = y;
00414             if (rotflag) {
00415                 x1 = x;
00416                 y1 = y + length;
00417             }
00418             else {
00419                 x1 = x + length;
00420                 y1 = y;
00421             }
00422             //swap(x, x1)
00423             break;
00424     }
00425     if (x1 >= width()) {
00426         x1 = width() - 1;
00427     }
00428     if (y1 >= height()) {
00429         y1 = height() - 1;
00430     }
00431 
00432     writeRegister(HX8347D_COL_AD_START2,x>>8);
00433     writeRegister(HX8347D_COL_AD_START1,x);     
00434     writeRegister(HX8347D_COL_AD_END2,x1>>8); 
00435     writeRegister(HX8347D_COL_AD_END1,x1); 
00436     writeRegister(HX8347D_ROW_AD_START2,y>>8);
00437     writeRegister(HX8347D_ROW_AD_START1,y);     
00438     writeRegister(HX8347D_ROW_AD_END2,y1>>8); 
00439     writeRegister(HX8347D_ROW_AD_END1,y1); 
00440 
00441     writeCommand(HX8347D_SRAM_WR_CTRL);  // Write Data to GRAM (R22h)
00442 
00443 
00444   setWriteDir();
00445   _cs = 0;
00446   //digitalWrite(_cs, LOW);
00447   _rs = 1;
00448   //digitalWrite(_cd, HIGH);
00449   _rd = 1;
00450   //digitalWrite(_rd, HIGH);
00451   _wr = 1;
00452   //digitalWrite(_wr, HIGH);
00453 
00454   while (length--) {
00455     writeData_unsafe(color); 
00456   }
00457 
00458   // set back to default
00459   _cs = 1;
00460   //digitalWrite(_cs, HIGH);
00461 
00462 }
00463 
00464 
00465 void HX8347D::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, 
00466               uint16_t color) {
00467   // if you're in rotation 1 or 3, we need to swap the X and Y's
00468 
00469   int16_t steep = abs(y1 - y0) > abs(x1 - x0);
00470   if (steep) {
00471     swap(x0, y0);
00472     swap(x1, y1);
00473   }
00474 
00475   if (x0 > x1) {
00476     swap(x0, x1);
00477     swap(y0, y1);
00478   }
00479 
00480   int16_t dx, dy;
00481   dx = x1 - x0;
00482   //dy = abs(y1 - y0);
00483   dy = abs(y1 - y0);
00484 
00485   int16_t err = dx / 2;
00486   int16_t ystep;
00487 
00488   if (y0 < y1) {
00489     ystep = 1;
00490   } else {
00491     ystep = -1;}
00492 
00493   for (; x0<=x1; x0++) {
00494     if (steep) {
00495       drawPixel(y0, x0, color);
00496     } else {
00497       drawPixel(x0, y0, color);
00498     }
00499     err -= dy;
00500     if (err < 0) {
00501       y0 += ystep;
00502       err += dx;
00503     }
00504   }
00505 }
00506 
00507 
00508 
00509 
00510 void HX8347D::fillScreen(uint16_t color) {
00511     goHome();
00512     uint32_t i;
00513     i = 320;
00514     i *= 240;
00515     _cs = 0;
00516     _rs = 1;
00517     _rd = 1;
00518     _wr = 1;
00519     setWriteDir();
00520     while (i--) {
00521         writeData_unsafe(color); 
00522     }
00523     _cs = 1;
00524 }
00525 
00526 void HX8347D::drawPixel(uint16_t x, uint16_t y, uint16_t color)
00527 {
00528     writeRegister(HX8347D_COL_AD_START2,x>>8);
00529     writeRegister(HX8347D_COL_AD_START1,x);     
00530     writeRegister(HX8347D_COL_AD_END2,x>>8); 
00531     writeRegister(HX8347D_COL_AD_END1,x); 
00532     writeRegister(HX8347D_ROW_AD_START2,y>>8);
00533     writeRegister(HX8347D_ROW_AD_START1,y);     
00534     writeRegister(HX8347D_ROW_AD_END2,y>>8); 
00535     writeRegister(HX8347D_ROW_AD_END1,y); 
00536 
00537     writeCommand(HX8347D_SRAM_WR_CTRL);  // Write Data to GRAM (R22h)
00538     writeData(color);
00539 
00540 }
00541 
00542 
00543 static const uint16_t _regValues[] = {
00544     //HX8347D_CM22()
00545     0x002E,0x0079, //
00546     0x00EE,0x000C, //
00547     //Driving ability Setting
00548     0x00EA,0x0000, //PTBA[15:8]
00549     0x00EB,0x0020, //PTBA[7:0]
00550     0x00EC,0x0008, //STBA[15:8]
00551     0x00ED,0x00C4, //STBA[7:0]
00552     0x00E8,0x0040, //OPON[7:0]
00553     0x00E9,0x0038, //OPON1[7:0]
00554     0x00F1,0x0001, //OTPS1B
00555     0x00F2,0x0010, //GEN
00556     0x0027,0x00A3, //
00557     //Gamma 2.2 Setting
00558     0x0040,0x0000, //
00559     0x0041,0x0000, //
00560     0x0042,0x0001, //
00561     0x0043,0x0012, //
00562     0x0044,0x0010, //
00563     0x0045,0x0026, //
00564     0x0046,0x0008, //
00565     0x0047,0x0053, //
00566     0x0048,0x0002, //
00567     0x0049,0x0015, //
00568     0x004A,0x0019, //
00569     0x004B,0x0019, //
00570     0x004C,0x0016, //
00571     0x0050,0x0019, //
00572     0x0051,0x002F, //
00573     0x0052,0x002D, //
00574     0x0053,0x003E, //
00575     0x0054,0x003F, //
00576     0x0055,0x003F, //
00577     0x0056,0x002C, //
00578     0x0057,0x0077, //
00579     0x0058,0x0009, //
00580     0x0059,0x0006, //
00581     0x005A,0x0006, //
00582     0x005B,0x000A, //
00583     0x005C,0x001D, //
00584     0x005D,0x00CC, //
00585     //Power Voltage Setting
00586     0x001B,0x001B, //VRH=4.65V
00587     0x001A,0x0001, //BT (VGH~15V);(VGL~-10V);(DDVDH~5V)
00588     0x0024,0x002F, //VMH(VCOM High voltage ~3.2V)
00589     0x0025,0x0057, //VML(VCOM Low voltage -1.2V)
00590     //****VCOM offset**///
00591     0x0023,0x0097, //for Flicker adjust //can reload from OTP
00592     //Power on Setting
00593     0x0018,0x0036, //I/P_RADJ);(N/P_RADJ);( Normal mode 75Hz
00594     0x0019,0x0001, //OSC_EN='1');( start Osc
00595     0x0001,0x0000, //DP_STB='0');( out deep sleep
00596     0x001F,0x0088,// GAS=1);( VOMG=00);( PON=0);( DK=1);( XDK=0)
00597     
00598     0xF0,5,
00599     0x001F,0x0080,// GAS=1);( VOMG=00);( PON=0);( DK=0);( XDK=0);( 
00600     
00601     0xF0,5,
00602     0x001F,0x0090,// GAS=1);( VOMG=00);( PON=1);( DK=0);( XDK=0);( 
00603     
00604     0xF0,5,
00605     0x001F,0x00D0,// GAS=1);( VOMG=10);( PON=1);( DK=0);( XDK=0);( 
00606     
00607     0xF0,5,
00608     //262k/65k color selection
00609     0x0017,0x0005, //default 0x0006 262k color // 0x0005 65k color
00610     //SET PANEL
00611     0x0036,0x0000, //SS_P);( GS_P);(REV_P);(BGR_P
00612     //Display ON Setting
00613     0x0028,0x0038, //GON=1);( DTE=1);( D=1000
00614     0xF0,40,
00615     0x0028,0x003C, //GON=1);( DTE=1);( D=1100
00616     //
00617     0x0016, 0x0008,
00618     //Set GRAM Area
00619     0x0002,0x0000,
00620     0x0003,0x0000, //Column Start
00621     0x0004,0x0000,
00622     0x0005,0x00EF, //Column End
00623     0x0006,0x0000,
00624     0x0007,0x0000, //Row Start
00625     0x0008,0x0001,
00626     0x0009,0x003F, //Row End
00627     0xF1, 0x0022, //Start GRAM write
00628 };
00629 
00630 void HX8347D::initDisplay(void) {
00631     uint16_t a, d;
00632     reset();
00633     for (uint8_t i = 0; i < sizeof(_regValues) / 4; i++) {
00634         a = _regValues[i*2];
00635         d = _regValues[i*2 + 1];
00636         if (a == 0xF0) { // _delay
00637             _delay(d);
00638         } else if (a== 0xF1) { // write command
00639             writeCommand(d);
00640         } else
00641         {
00642             writeRegister(a, d);
00643         }
00644     }
00645 }
00646 
00647 uint8_t HX8347D::getRotation(void) {
00648   return rotation;
00649 }
00650 
00651 
00652 void HX8347D::BlockWrite(uint16_t Xstart,uint16_t Xend,uint16_t Ystart,uint16_t Yend)
00653 {
00654     
00655     writeRegister(HX8347D_COL_AD_START2,Xstart>>8);     
00656     writeRegister(HX8347D_COL_AD_START1,Xstart);     
00657     writeRegister(HX8347D_COL_AD_END2,Xend>>8); 
00658     writeRegister(HX8347D_COL_AD_END1,Xend); 
00659     
00660     writeRegister(HX8347D_ROW_AD_START2,Ystart>>8); 
00661     writeRegister(HX8347D_ROW_AD_START1,Ystart);
00662     writeRegister(HX8347D_ROW_AD_END2,Yend>>8); 
00663     writeRegister(HX8347D_ROW_AD_END1,Yend);  
00664 
00665     writeCommand(HX8347D_SRAM_WR_CTRL);  // write ram
00666 }
00667 void HX8347D::DispColor(uint16_t color)
00668 {
00669     uint16_t i,j;
00670 
00671     BlockWrite(0,TFTWIDTH-1,0,TFTHEIGHT-1);// #define ROW  320  #define COL 240
00672 
00673     _cs=0; 
00674     _rs=1;
00675     _rd=1;
00676 
00677     for(i=0;i<TFTHEIGHT;i++)
00678     {
00679         for(j=0;j<TFTWIDTH;j++)
00680         {    
00681             writeData_unsafe(color); 
00682         }
00683     }
00684 
00685     _cs=1; 
00686 }
00687 
00688 void HX8347D::reset(void)
00689 {
00690     _rst=0;
00691     _delay(1000);
00692     _rst=1;
00693     _delay(1000);
00694  }
00695  
00696  void HX8347D::setWriteDir(void) {
00697         _d.output();
00698 }
00699 
00700 void HX8347D::setReadDir(void) {
00701         _d.input();
00702 }
00703 
00704 void HX8347D::write8(uint8_t d) {
00705     _d = d;
00706 }
00707 uint8_t HX8347D::read8(void) {
00708     return _d;
00709 }
00710 
00711 void HX8347D::_write(uint16_t rs, uint16_t d) {
00712     _cs=0;
00713     _rd=1;
00714     _rs=rs;   
00715 
00716     _d.output();
00717     _d=d>>8; 
00718     _wr=0;
00719     _delay(1);
00720     _wr=1;
00721 
00722     _d=d;
00723     _wr=0;
00724     _delay(1);
00725     _wr=1;
00726 
00727     _cs=1; 
00728 }
00729 
00730 void HX8347D::writeData_unsafe(uint16_t d)
00731 {
00732     //_d.output();
00733     _d = d >> 8;
00734 
00735     _wr = 0;
00736     _delay(1);
00737     _wr = 1;
00738     
00739     _d = d;
00740 
00741     _wr = 0;
00742     _delay(1);
00743     _wr = 1;
00744 }
00745 
00746 uint16_t HX8347D::readData(){
00747     uint16_t d = 0;
00748     _d.input();
00749     _cs = 0;
00750     _rs = 1;
00751     _wr = 1;
00752     _rd = 1;
00753 
00754     //wait_us(1);
00755     //_rd = 0;
00756     //wait_us(10);
00757     //d = _d;
00758     //d <<= 8;
00759     //_rd = 1;
00760 
00761     _delay(1);
00762     _rd = 0;
00763     _delay(10);
00764     d = _d;
00765     d <<= 8;
00766     _rd = 1;
00767 
00768     _delay(1);
00769     _rd = 0;
00770     _delay(10);
00771     d |= _d;
00772     _rd = 1;
00773     _cs = 1;
00774     return d;
00775 }
00776 
00777 
00778 /************************************* medium level data reading/writing */
00779 
00780 uint16_t HX8347D::readRegister(uint16_t addr) {
00781    writeCommand(addr);
00782    return readData();
00783 }
00784 void HX8347D::writeRegister(uint16_t addr, uint16_t data) {
00785    writeCommand(addr);
00786    writeData(data);
00787 }
00788 
00789 void HX8347D::goTo(uint16_t x, uint16_t y) {
00790     uint16_t x1 = width() - 1;
00791     uint16_t y1 = height() - 1;
00792     //calcGRAMPosition(&x, &y);
00793     //writeRegister(TFTLCD_GRAM_HOR_AD, x); // GRAM Address Set (Horizontal Address) (R20h)
00794     writeRegister(HX8347D_COL_AD_START2,x>>8);
00795     writeRegister(HX8347D_COL_AD_START1,x);     
00796     writeRegister(HX8347D_COL_AD_END2,x1>>8); 
00797     writeRegister(HX8347D_COL_AD_END1,x1); 
00798     writeRegister(HX8347D_ROW_AD_START2,y>>8);
00799     writeRegister(HX8347D_ROW_AD_START1,y);     
00800     writeRegister(HX8347D_ROW_AD_END2,y1>>8); 
00801     writeRegister(HX8347D_ROW_AD_END1,y1); 
00802     writeCommand(HX8347D_SRAM_WR_CTRL);  // write ram
00803 }
00804 
00805 
00806 void HX8347D::setDefaultViewport()
00807 {
00808     uint16_t x1 = width() - 1;
00809     uint16_t y1 = height() - 1;
00810     writeRegister(HX8347D_COL_AD_START2,0);
00811     writeRegister(HX8347D_COL_AD_START1,0);     
00812     writeRegister(HX8347D_COL_AD_END2,x1>>8); 
00813     writeRegister(HX8347D_COL_AD_END1,x1); 
00814     writeRegister(HX8347D_ROW_AD_START2,0);
00815     writeRegister(HX8347D_ROW_AD_START1,0);     
00816     writeRegister(HX8347D_ROW_AD_END2,y1>>8); 
00817     writeRegister(HX8347D_ROW_AD_END1,y1); 
00818 }
00819 
00820 void HX8347D::getViewport(uint16_t *bx, uint16_t *by, uint16_t *ex, uint16_t *ey)
00821 {
00822     *bx = readRegister(HX8347D_COL_AD_START2)<<8;    
00823     *bx |= readRegister(HX8347D_COL_AD_START1);    
00824     *ex = readRegister(HX8347D_COL_AD_END2)<<8;    
00825     *ex |= readRegister(HX8347D_COL_AD_END1);    
00826     *by = readRegister(HX8347D_ROW_AD_START2)<<8;     
00827     *by |= readRegister(HX8347D_ROW_AD_START1);     
00828     *ey = readRegister(HX8347D_ROW_AD_END2)<<8;     
00829     *ey |= readRegister(HX8347D_ROW_AD_END1);     
00830 }
00831 
00832 void HX8347D::setViewport(uint16_t bx, uint16_t by, uint16_t ex, uint16_t ey)
00833 {
00834     calcGRAMPosition(&bx, &by);
00835     calcGRAMPosition(&ex, &ey);
00836     
00837     // Fix coordinates to be in order
00838     if( ey < by )
00839         swap(ey, by);
00840     if( ex < bx )
00841         swap(ex, bx);
00842     
00843     writeRegister(HX8347D_COL_AD_START2,bx>>8);
00844     writeRegister(HX8347D_COL_AD_START1,bx);     
00845     writeRegister(HX8347D_COL_AD_END2,ex>>8); 
00846     writeRegister(HX8347D_COL_AD_END1,ex); 
00847     writeRegister(HX8347D_ROW_AD_START2,by>>8);
00848     writeRegister(HX8347D_ROW_AD_START1,by);     
00849     writeRegister(HX8347D_ROW_AD_END2,ey>>8); 
00850     writeRegister(HX8347D_ROW_AD_END1,ey); 
00851 }
00852 
00853 void HX8347D::calcGRAMPosition(uint16_t *posx, uint16_t *posy)
00854 {
00855   uint16_t x = *posx;
00856   uint16_t y = *posy;
00857   switch( rotation )
00858   {
00859       case 0:
00860       x = X(x);
00861       y = Y(y);
00862       break;
00863       case 1:  // 90
00864       swap(x, y);
00865       x = I_X(x);
00866       y = Y(y);
00867       break;
00868       case 2:  // 180
00869       x = I_X(x);
00870       y = I_Y(y);
00871       break;
00872       case 3: // 270
00873       swap(x, y);
00874       y = I_Y(y);
00875       break;
00876   }
00877   *posx = x;
00878   *posy = y;
00879 }
00880 
00881 void HX8347D::_delay(uint16_t t)
00882 {
00883 }
00884