Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ILI9340_Driver SDCard_Aitendo_2p2TFT TFT22ok_test_ILI9340_Driver
ILI9340_Driver.cpp
00001 /*************************************************************** 00002 ILI9340_Driver v1.1 01.06.14 Ian Weston 00003 00004 Driver and integrated graphics library for displays that use the 00005 ILI9340 controller in SPI mode. Such as the Adafruit 2.2" display. 00006 00007 This code has been ported from several sources. The driver section 00008 was completely ported from the Adafruits Arduino source code, and 00009 the graphics functions were ported from the Adafruits GFX library 00010 and some elements were ported from code by Elmicros seeduio port. 00011 00012 Future revisions will include more advanced graphics functions. 00013 00014 ***************************************************************/ 00015 00016 00017 #include "mbed.h" 00018 #include "ILI9340_Driver.h" 00019 #include "SimpleFont.cpp" 00020 00021 00022 00023 //////////////////////////////////////////////////////////////////////////////////////////////// 00024 // Constructor, assigns the pins to the SPI object, set orientation, and sets screen dims. 00025 //////////////////////////////////////////////////////////////////////////////////////////////// 00026 ILI9340_Display::ILI9340_Display(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rst, PinName dc) 00027 : spi(mosi, miso, sclk), cs(cs), rst(rst), dc(dc) { 00028 _height = _TFTHEIGHT; 00029 _width = _TFTWIDTH; 00030 orientation = 0; 00031 } 00032 00033 00034 00035 //////////////////////////////////////////////////////////////////////////////////////////////// 00036 // Command writing code 00037 //////////////////////////////////////////////////////////////////////////////////////////////// 00038 void ILI9340_Display::WriteCommand(uint8_t command) { 00039 dc = 0; 00040 cs = 0; 00041 spi.write(command); 00042 cs = 1; 00043 } 00044 00045 00046 00047 //////////////////////////////////////////////////////////////////////////////////////////////// 00048 // Data writing code 00049 //////////////////////////////////////////////////////////////////////////////////////////////// 00050 void ILI9340_Display::WriteData(uint8_t data) { 00051 cs = 0; 00052 dc = 1; 00053 spi.write(data); 00054 cs = 1; 00055 } 00056 00057 00058 00059 //////////////////////////////////////////////////////////////////////////////////////////////// 00060 // Initilise the display 00061 //////////////////////////////////////////////////////////////////////////////////////////////// 00062 void ILI9340_Display::DispInit(void) { 00063 //CtrlOutput(); 00064 00065 rst = 0; 00066 00067 // Setup the spi for 8 bit data, high steady state clock, 00068 // second edge capture, with a 1MHz clock rate 00069 //spi.format(8,3); 00070 spi.frequency(24000000); // actually seems to work up to about 20Mhz... way better than the 8mhz as std. 00071 00072 // Toggle rst to reset 00073 rst = 1; 00074 wait(0.005); 00075 rst = 0; 00076 wait(0.020); 00077 rst = 1; 00078 wait(0.150); 00079 00080 WriteCommand(0xEF); 00081 WriteData(0x03); 00082 WriteData(0x80); 00083 WriteData(0x02); 00084 00085 WriteCommand(0xCF); 00086 WriteData(0x00); 00087 WriteData(0xC1); 00088 WriteData(0x30); 00089 00090 WriteCommand(0xED); 00091 WriteData(0x64); 00092 WriteData(0x03); 00093 WriteData(0x12); 00094 WriteData(0x81); 00095 00096 WriteCommand(0xE8); 00097 WriteData(0x85); 00098 WriteData(0x00); 00099 WriteData(0x78); 00100 00101 WriteCommand(0xCB); 00102 WriteData(0x39); 00103 WriteData(0x2C); 00104 WriteData(0x00); 00105 WriteData(0x34); 00106 WriteData(0x02); 00107 00108 WriteCommand(0xF7); 00109 WriteData(0x20); 00110 00111 WriteCommand(0xEA); 00112 WriteData(0x00); 00113 WriteData(0x00); 00114 00115 WriteCommand(ILI9340_PWCTR1); //Power control 00116 WriteData(0x23); //VRH[5:0] 00117 00118 WriteCommand(ILI9340_PWCTR2); //Power control 00119 WriteData(0x10); //SAP[2:0];BT[3:0] 00120 00121 WriteCommand(ILI9340_VMCTR1); //VCM control 00122 WriteData(0x3e); //�Աȶȵ��� 00123 WriteData(0x28); 00124 00125 WriteCommand(ILI9340_VMCTR2); //VCM control2 00126 WriteData(0x86); //-- 00127 00128 WriteCommand(ILI9340_MADCTL); // Memory Access Control 00129 WriteData(ILI9340_MADCTL_MX | ILI9340_MADCTL_BGR); 00130 00131 WriteCommand(ILI9340_PIXFMT); 00132 WriteData(0x55); 00133 00134 WriteCommand(ILI9340_FRMCTR1); 00135 WriteData(0x00); 00136 WriteData(0x18); 00137 00138 WriteCommand(ILI9340_DFUNCTR); // Display Function Control 00139 WriteData(0x08); 00140 WriteData(0x82); 00141 WriteData(0x27); 00142 00143 WriteCommand(0xF2); // 3Gamma Function Disable 00144 WriteData(0x00); 00145 00146 WriteCommand(ILI9340_GAMMASET); //Gamma curve selected 00147 WriteData(0x01); 00148 00149 WriteCommand(ILI9340_GMCTRP1); //Set Gamma 00150 WriteData(0x0F); 00151 WriteData(0x31); 00152 WriteData(0x2B); 00153 WriteData(0x0C); 00154 WriteData(0x0E); 00155 WriteData(0x08); 00156 WriteData(0x4E); 00157 WriteData(0xF1); 00158 WriteData(0x37); 00159 WriteData(0x07); 00160 WriteData(0x10); 00161 WriteData(0x03); 00162 WriteData(0x0E); 00163 WriteData(0x09); 00164 WriteData(0x00); 00165 00166 WriteCommand(ILI9340_GMCTRN1); //Set Gamma 00167 WriteData(0x00); 00168 WriteData(0x0E); 00169 WriteData(0x14); 00170 WriteData(0x03); 00171 WriteData(0x11); 00172 WriteData(0x07); 00173 WriteData(0x31); 00174 WriteData(0xC1); 00175 WriteData(0x48); 00176 WriteData(0x08); 00177 WriteData(0x0F); 00178 WriteData(0x0C); 00179 WriteData(0x31); 00180 WriteData(0x36); 00181 WriteData(0x0F); 00182 00183 WriteCommand(ILI9340_SLPOUT); //Exit Sleep 00184 wait(0.120); 00185 WriteCommand(ILI9340_DISPON); //Display on 00186 00187 } 00188 00189 00190 00191 //////////////////////////////////////////////////////////////////////////////////////////////// 00192 // Sets the rotation of the display 00193 //////////////////////////////////////////////////////////////////////////////////////////////// 00194 void ILI9340_Display::SetRotation(uint8_t m) { 00195 00196 WriteCommand(ILI9340_MADCTL); 00197 orientation = m % 4; // can't be higher than 3 00198 00199 switch (orientation) { 00200 case 0: 00201 WriteData(ILI9340_MADCTL_MX | ILI9340_MADCTL_BGR); 00202 _width = _TFTWIDTH; 00203 _height = _TFTHEIGHT; 00204 break; 00205 case 1: 00206 WriteData(ILI9340_MADCTL_MV | ILI9340_MADCTL_BGR); 00207 _width = _TFTHEIGHT; 00208 _height = _TFTWIDTH; 00209 break; 00210 case 2: 00211 WriteData(ILI9340_MADCTL_MY | ILI9340_MADCTL_BGR); 00212 _width = _TFTWIDTH; 00213 _height = _TFTHEIGHT; 00214 break; 00215 case 3: 00216 WriteData(ILI9340_MADCTL_MV | ILI9340_MADCTL_MY | ILI9340_MADCTL_MX | ILI9340_MADCTL_BGR); 00217 _width = _TFTHEIGHT; 00218 _height = _TFTWIDTH; 00219 break; 00220 } 00221 } 00222 00223 00224 00225 00226 //////////////////////////////////////////////////////////////////////////////////////////////// 00227 // Invert the colours of the display in hardware 00228 //////////////////////////////////////////////////////////////////////////////////////////////// 00229 void ILI9340_Display::InvertDisplay(bool i) { 00230 WriteCommand(i ? ILI9340_INVON : ILI9340_INVOFF); 00231 } 00232 00233 00234 00235 00236 //////////////////////////////////////////////////////////////////////////////////////////////// 00237 // Set address window for writing data to. 00238 //////////////////////////////////////////////////////////////////////////////////////////////// 00239 void ILI9340_Display::SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { 00240 00241 WriteCommand(ILI9340_CASET); // Column addr set 00242 WriteData(x0 >> 8); 00243 WriteData(x0 & 0xFF); // XSTART 00244 WriteData(x1 >> 8); 00245 WriteData(x1 & 0xFF); // XEND 00246 00247 WriteCommand(ILI9340_PASET); // Row addr set 00248 WriteData(y0>>8); 00249 WriteData(y0); // YSTART 00250 WriteData(y1>>8); 00251 WriteData(y1); // YEND 00252 00253 WriteCommand(ILI9340_RAMWR); // write to RAM 00254 } 00255 00256 00257 00258 00259 //////////////////////////////////////////////////////////////////////////////////////////////// 00260 // To draw the humble pixel 00261 //////////////////////////////////////////////////////////////////////////////////////////////// 00262 void ILI9340_Display::DrawPixel(uint16_t x, uint16_t y, uint16_t colour) { 00263 if((x < 1) ||(x >= _width) || (y < 1) || (y >= _height)) return; 00264 00265 SetAddrWindow(x,y,x+1,y+1); 00266 00267 dc = 1; 00268 cs = 0; 00269 00270 spi.write(colour >> 8); 00271 spi.write(colour); 00272 00273 cs = 1; 00274 } 00275 00276 00277 00278 //////////////////////////////////////////////////////////////////////////////////////////////// 00279 // Fill the screen with a colour 00280 //////////////////////////////////////////////////////////////////////////////////////////////// 00281 void ILI9340_Display::FillScreen(uint16_t colour) { 00282 SetAddrWindow(0,0,_width,_height); 00283 00284 dc = 1; 00285 cs = 0; 00286 00287 unsigned int total = _width * _height; 00288 unsigned int position = 0; 00289 00290 while (position < total) { 00291 spi.write(colour >> 8); 00292 spi.write(colour); 00293 position++; 00294 } 00295 cs = 1; 00296 } 00297 00298 00299 00300 //////////////////////////////////////////////////////////////////////////////////////////////// 00301 // Draws a vertical line fast 00302 //////////////////////////////////////////////////////////////////////////////////////////////// 00303 void ILI9340_Display::DrawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t colour) { 00304 00305 // Rudimentary clipping 00306 if((x >= _width) || (y >= _height)) return; 00307 00308 if((y+h-1) >= _height) 00309 h = _height-y; 00310 00311 SetAddrWindow(x, y, x, y+h-1); 00312 00313 uint8_t hi = colour >> 8, lo = colour; 00314 00315 dc = 1; 00316 cs = 0; 00317 00318 while (h--) { 00319 spi.write(hi); 00320 spi.write(lo); 00321 } 00322 cs = 1; 00323 } 00324 00325 00326 00327 //////////////////////////////////////////////////////////////////////////////////////////////// 00328 // Draws a horizontal line fast 00329 //////////////////////////////////////////////////////////////////////////////////////////////// 00330 void ILI9340_Display::DrawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t colour) { 00331 00332 // Rudimentary clipping 00333 if((x >= _width) || (y >= _height)) return; 00334 if((x+w-1) >= _height) w = _width-x; 00335 SetAddrWindow(x, y, x+w-1, y); 00336 00337 uint8_t hi = colour >> 8, lo = colour; 00338 dc = 1; 00339 cs = 0; 00340 while (w--) { 00341 spi.write(hi); 00342 spi.write(lo); 00343 } 00344 cs = 1; 00345 } 00346 00347 00348 00349 00350 //////////////////////////////////////////////////////////////////////////////////////////////// 00351 // Draws a filled rectangle 00352 //////////////////////////////////////////////////////////////////////////////////////////////// 00353 void ILI9340_Display::FillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t colour) { 00354 00355 // rudimentary clipping (drawChar w/big text requires this) 00356 if((x >= _width) || (y >= _height)) return; 00357 if((x + w - 1) >= _width) w = _width - x; 00358 if((y + h - 1) >= _height) h = _height - y; 00359 00360 SetAddrWindow(x, y, x+w-1, y+h-1); 00361 00362 uint8_t hi = colour >> 8, lo = colour; 00363 00364 dc = 1; 00365 cs = 0; 00366 00367 for(y=h; y>0; y--) { 00368 for(x=w; x>0; x--) { 00369 spi.write(hi); 00370 spi.write(lo); 00371 } 00372 } 00373 cs = 1; 00374 } 00375 00376 00377 00378 00379 //////////////////////////////////////////////////////////////////////////////////////////////// 00380 // Draw an unfilled rectangle 00381 //////////////////////////////////////////////////////////////////////////////////////////////// 00382 void ILI9340_Display::DrawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color){ 00383 DrawFastHLine(x, y, w, color); 00384 DrawFastHLine(x, y+h-1, w, color); 00385 DrawFastVLine(x, y, h, color); 00386 DrawFastVLine(x+w-1, y, h, color); 00387 } 00388 00389 00390 00391 00392 00393 //////////////////////////////////////////////////////////////////////////////////////////////// 00394 // draw an unfilled circle 00395 //////////////////////////////////////////////////////////////////////////////////////////////// 00396 void ILI9340_Display::DrawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t colour){ 00397 int16_t f = 1 - r; 00398 int16_t ddF_x = 1; 00399 int16_t ddF_y = -2 * r; 00400 int16_t x = 0; 00401 int16_t y = r; 00402 00403 DrawPixel(x0 , y0+r, colour); 00404 DrawPixel(x0 , y0-r, colour); 00405 DrawPixel(x0+r, y0 , colour); 00406 DrawPixel(x0-r, y0 , colour); 00407 00408 while (x<y) { 00409 if (f >= 0) { 00410 y--; 00411 ddF_y += 2; 00412 f += ddF_y; 00413 } 00414 x++; 00415 ddF_x += 2; 00416 f += ddF_x; 00417 00418 DrawPixel(x0 + x, y0 + y, colour); 00419 DrawPixel(x0 - x, y0 + y, colour); 00420 DrawPixel(x0 + x, y0 - y, colour); 00421 DrawPixel(x0 - x, y0 - y, colour); 00422 DrawPixel(x0 + y, y0 + x, colour); 00423 DrawPixel(x0 - y, y0 + x, colour); 00424 DrawPixel(x0 + y, y0 - x, colour); 00425 DrawPixel(x0 - y, y0 - x, colour); 00426 } 00427 } 00428 00429 00430 00431 00432 00433 //////////////////////////////////////////////////////////////////////////////////////////////// 00434 // Draw a filled circle 00435 //////////////////////////////////////////////////////////////////////////////////////////////// 00436 void ILI9340_Display::FillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t colour) { 00437 DrawFastVLine(x0, y0-r, 2*r+1, colour); 00438 FillCircleHelper(x0, y0, r, 3, 0, colour); 00439 } 00440 00441 00442 00443 00444 00445 //////////////////////////////////////////////////////////////////////////////////////////////// 00446 // used to draw circles and roundrects! 00447 //////////////////////////////////////////////////////////////////////////////////////////////// 00448 void ILI9340_Display::FillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t colour) { 00449 00450 int16_t f = 1 - r; 00451 int16_t ddF_x = 1; 00452 int16_t ddF_y = -2 * r; 00453 int16_t x = 0; 00454 int16_t y = r; 00455 00456 while (x<y) { 00457 if (f >= 0) { 00458 y--; 00459 ddF_y += 2; 00460 f += ddF_y; 00461 } 00462 x++; 00463 ddF_x += 2; 00464 f += ddF_x; 00465 00466 if (cornername & 0x1) { 00467 DrawFastVLine(x0+x, y0-y, 2*y+1+delta, colour); 00468 DrawFastVLine(x0+y, y0-x, 2*x+1+delta, colour); 00469 } 00470 if (cornername & 0x2) { 00471 DrawFastVLine(x0-x, y0-y, 2*y+1+delta, colour); 00472 DrawFastVLine(x0-y, y0-x, 2*x+1+delta, colour); 00473 } 00474 } 00475 } 00476 00477 00478 00479 00480 00481 //////////////////////////////////////////////////////////////////////////////////////////////// 00482 // used for drawing rounded corner radii 00483 //////////////////////////////////////////////////////////////////////////////////////////////// 00484 void ILI9340_Display::DrawCircleHelper( int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t colour) { 00485 int16_t f = 1 - r; 00486 int16_t ddF_x = 1; 00487 int16_t ddF_y = -2 * r; 00488 int16_t x = 0; 00489 int16_t y = r; 00490 00491 while (x<y) { 00492 if (f >= 0) { 00493 y--; 00494 ddF_y += 2; 00495 f += ddF_y; 00496 } 00497 x++; 00498 ddF_x += 2; 00499 f += ddF_x; 00500 if (cornername & 0x4) { 00501 DrawPixel(x0 + x, y0 + y, colour); 00502 DrawPixel(x0 + y, y0 + x, colour); 00503 } 00504 if (cornername & 0x2) { 00505 DrawPixel(x0 + x, y0 - y, colour); 00506 DrawPixel(x0 + y, y0 - x, colour); 00507 } 00508 if (cornername & 0x8) { 00509 DrawPixel(x0 - y, y0 + x, colour); 00510 DrawPixel(x0 - x, y0 + y, colour); 00511 } 00512 if (cornername & 0x1) { 00513 DrawPixel(x0 - y, y0 - x, colour); 00514 DrawPixel(x0 - x, y0 - y, colour); 00515 } 00516 } 00517 } 00518 00519 00520 00521 00522 //////////////////////////////////////////////////////////////////////////////////////////////// 00523 // draw a rounded rectangle! 00524 //////////////////////////////////////////////////////////////////////////////////////////////// 00525 void ILI9340_Display::DrawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t colour) { 00526 // smarter version 00527 DrawFastHLine(x+r , y , w-2*r, colour); // Top 00528 DrawFastHLine(x+r , y+h-1, w-2*r, colour); // Bottom 00529 DrawFastVLine( x , y+r , h-2*r, colour); // Left 00530 DrawFastVLine( x+w-1, y+r , h-2*r, colour); // Right 00531 // draw four corners 00532 DrawCircleHelper(x+r , y+r , r, 1, colour); 00533 DrawCircleHelper(x+w-r-1, y+r , r, 2, colour); 00534 DrawCircleHelper(x+w-r-1, y+h-r-1, r, 4, colour); 00535 DrawCircleHelper(x+r , y+h-r-1, r, 8, colour); 00536 } 00537 00538 00539 00540 00541 00542 //////////////////////////////////////////////////////////////////////////////////////////////// 00543 // fill a rounded rectangle! 00544 //////////////////////////////////////////////////////////////////////////////////////////////// 00545 void ILI9340_Display::FillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t colour) { 00546 // smarter version 00547 FillRect(x+r, y, w-2*r, h, colour); 00548 00549 // draw four corners 00550 FillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, colour); 00551 FillCircleHelper(x+r , y+r, r, 2, h-2*r-1, colour); 00552 } 00553 00554 00555 00556 00557 00558 //////////////////////////////////////////////////////////////////////////////////////////////// 00559 // Pass 8-bit (each) R,G,B, get back 16-bit packed color 00560 //////////////////////////////////////////////////////////////////////////////////////////////// 00561 uint16_t ILI9340_Display::Colour565(uint8_t r, uint8_t g, uint8_t b) { 00562 return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3); 00563 } 00564 00565 00566 00567 00568 //////////////////////////////////////////////////////////////////////////////////////////////// 00569 // Writes an ascii character to the display 00570 //////////////////////////////////////////////////////////////////////////////////////////////// 00571 void ILI9340_Display::DrawAscii(unsigned char ascii, uint16_t x, uint16_t y, uint16_t size, uint16_t colour) { 00572 SetAddrWindow(x, y, x+size, y+size); 00573 00574 if( (ascii < 0x20) || (ascii > 0x7e) ) //check for valid ASCII char 00575 { 00576 ascii = '?'; //char not supported 00577 } 00578 for(unsigned char i=0; i<8; i++) 00579 { 00580 unsigned char temp = simpleFont[ascii - 0x20][i]; 00581 for(unsigned char f=0; f<8; f++) 00582 { 00583 if( (temp>>f) & 0x01 ) 00584 { 00585 switch(orientation) 00586 { 00587 case '0': 00588 FillRect(x+f*size, y-i*size, size, size, colour); 00589 break; 00590 case '1': 00591 FillRect(x-i*size, x-f*size, size, size, colour); 00592 break; 00593 case '2': 00594 FillRect(x-f*size, y+i*size, size, size, colour); 00595 break; 00596 case '3': 00597 default: 00598 FillRect(x+i*size, y+f*size, size, size, colour); 00599 } 00600 } 00601 } 00602 } 00603 } 00604 00605 00606 00607 00608 00609 //////////////////////////////////////////////////////////////////////////////////////////////// 00610 // Writes a character array to the display 00611 //////////////////////////////////////////////////////////////////////////////////////////////// 00612 void ILI9340_Display::DrawString(char *string, uint16_t x, uint16_t y, uint8_t size, uint16_t colour) 00613 { 00614 while(*string) 00615 { 00616 DrawAscii(*string, x, y, size, colour); 00617 *string++; 00618 switch(orientation) 00619 { 00620 case '0': 00621 if(y > 0) y-=8*size; //Change position to next char 00622 break; 00623 case '1': 00624 if(x > 0) x-=8*size; 00625 break; 00626 case '2': 00627 if(y < _height) y+=8*size; 00628 break; 00629 case '3': 00630 default: 00631 if(x < _width) x+=8*size; 00632 } 00633 } 00634 } 00635 00636 00637 00638 00639 00640 //////////////////////////////////////////////////////////////////////////////////////////////// 00641 // Converts integers into a character array 00642 //////////////////////////////////////////////////////////////////////////////////////////////// 00643 void ILI9340_Display::IntToChars (char* buffer, int value, uint8_t spaceonbuffer, uint8_t countbase, uint16_t x, uint16_t y, uint8_t size, uint16_t colour) { 00644 int workvalue = value; 00645 int i; 00646 int valuetowrite; 00647 int end_i = 0; 00648 00649 if (value < 0) 00650 { 00651 workvalue = -value; 00652 end_i = 1; 00653 buffer[0] = '-'; 00654 } 00655 00656 for (i = spaceonbuffer - 1; i >= end_i; i--) 00657 { 00658 valuetowrite = (workvalue % countbase); 00659 if (workvalue == 0) 00660 { 00661 if (i == (spaceonbuffer - 1)) 00662 { 00663 buffer[i] = 48; // ASCII 0 00664 } else { 00665 buffer[i] = 32; // ASCII SPACE 00666 } 00667 } else { 00668 if (valuetowrite > 9) 00669 { 00670 buffer[i] = valuetowrite + 55; // ASCII A-Z 00671 } else { 00672 buffer[i] = valuetowrite + 48; // ASCII of that character 00673 } 00674 }; 00675 workvalue = (workvalue - valuetowrite) / countbase; 00676 } 00677 00678 DrawString(buffer, x, y, size, colour); 00679 } 00680 00681 00682 00683 00684 00685 //////////////////////////////////////////////////////////////////////////////////////////////// 00686 // Functional code to swap data contents of 16bit registers 00687 //////////////////////////////////////////////////////////////////////////////////////////////// 00688 void ILI9340_Display::Swap(int16_t *a, int16_t *b) { 00689 00690 int16_t x = *a; 00691 *a = *b; 00692 *b = x; 00693 } 00694 00695 00696 00697 00698 //////////////////////////////////////////////////////////////////////////////////////////////// 00699 // Draws a line with any length and orientation 00700 //////////////////////////////////////////////////////////////////////////////////////////////// 00701 void ILI9340_Display::DrawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t colour){ 00702 int16_t steep = abs(y1 - y0) > abs(x1 - x0); 00703 if (steep) { 00704 Swap(&x0, &y0); 00705 Swap(&x1, &y1); 00706 } 00707 00708 if (x0 > x1) { 00709 Swap(&x0, &x1); 00710 Swap(&y0, &y1); 00711 } 00712 00713 int16_t dx, dy; 00714 dx = x1 - x0; 00715 dy = abs(y1 - y0); 00716 00717 int16_t err = dx / 2; 00718 int16_t ystep; 00719 00720 if (y0 < y1) { 00721 ystep = 1; 00722 } else { 00723 ystep = -1; 00724 } 00725 00726 for (; x0<=x1; x0++) { 00727 if (steep) { 00728 DrawPixel(y0, x0, colour); 00729 } else { 00730 DrawPixel(x0, y0, colour); 00731 } 00732 err -= dy; 00733 if (err < 0) { 00734 y0 += ystep; 00735 err += dx; 00736 } 00737 } 00738 } 00739 00740 00741 00742 00743 00744 00745 00746
Generated on Wed Jul 13 2022 11:08:34 by
1.7.2