Library to drive an ILI9225 2.2" SPI TFT.

Dependents:   ILI9225_TFT_Sample

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TFT_22_ILI9225.cpp Source File

TFT_22_ILI9225.cpp

00001 /*
00002 ****************                               ******************
00003 
00004 Ported to mbed platform 
00005 Added Persian Support 
00006 
00007 by Arman Safikhani  31 - 08-2015
00008 
00009 * ***************                               ******************
00010 */
00011 
00012 
00013 
00014 
00015 #include "TFT_22_ILI9225.h"
00016 #define bitRead(value, bit) (((value) >> (bit)) & 0x01)
00017 
00018 int font_color, x_font, y_font;
00019 char highlight, fa_num;
00020 int effect = 0, italic = 0;
00021 unsigned int highlight_color;
00022 /* Global Variables */
00023 int x_text = 0, y_text = 0;
00024 int x_base = 0;
00025 
00026 // Constructor when using software SPI.  All output pins are configurable.
00027 TFT_22_ILI9225::TFT_22_ILI9225(PinName rst, PinName rs, PinName cs, PinName sdi, PinName clk, PinName led) :
00028     _rst(rst), _rs(rs), _cs(cs), _sdi(sdi), _clk(clk), spi(sdi, NC, clk), _led(led),
00029     _rstInOut(_rst), _rsInOut(_rs), _csInOut(_cs), _ledInOut(_led),
00030     hwSPI(true) 
00031 {
00032 }
00033 
00034 // Constructor when using hardware SPI.  Faster, but must use SPI pins
00035 // specific to each board type (e.g. 11,13 for Uno, 51,52 for Mega, etc.)
00036 //TFT_22_ILI9225::TFT_22_ILI9225(PinName rst, PinName rs, PinName cs, PinName led) :
00037 //  _rst(rst), _rs(rs), _cs(cs), _sdi(NC), _clk(NC), _led(led),
00038 //  _rstInOut(_rst), _rsInOut(_rs), _csInOut(_cs), _ledInOut(_led),
00039 //  hwSPI(true), spi(SPI_MOSI, NC, SPI_SCK)
00040 //{
00041 //}
00042 
00043 
00044 
00045 
00046 void TFT_22_ILI9225::_orientCoordinates(uint16_t &x1, uint16_t &y1) {
00047 
00048     switch (_orientation) {
00049     case 0:  // ok
00050         break;
00051     case 1: // ok
00052         y1 = _maxY - y1 - 1;
00053         _swap(x1, y1);
00054         break;
00055     case 2: // ok
00056         x1 = _maxX - x1 - 1;
00057         y1 = _maxY - y1 - 1;
00058         break;
00059     case 3: // ok
00060         x1 = _maxX - x1 - 1;
00061         _swap(x1, y1);
00062         break;
00063     }
00064 }
00065 
00066 
00067 void TFT_22_ILI9225::_setWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
00068     _orientCoordinates(x0, y0);
00069     _orientCoordinates(x1, y1);
00070 
00071     if (x1 < x0) _swap(x0, x1);
00072     if (y1 < y0) _swap(y0, y1);
00073 
00074     _writeRegister(ILI9225_HORIZONTAL_WINDOW_ADDR1, x1);
00075     _writeRegister(ILI9225_HORIZONTAL_WINDOW_ADDR2, x0);
00076 
00077     _writeRegister(ILI9225_VERTICAL_WINDOW_ADDR1, y1);
00078     _writeRegister(ILI9225_VERTICAL_WINDOW_ADDR2, y0);
00079 
00080     _writeRegister(ILI9225_RAM_ADDR_SET1, x0);
00081     _writeRegister(ILI9225_RAM_ADDR_SET2, y0);
00082 
00083     _writeCommand(0x00, 0x22);
00084 }
00085 
00086 
00087 void TFT_22_ILI9225::begin() {
00088 
00089     // Set up pins
00090 
00091 
00092     _rsInOut.output();
00093     _csInOut.output();
00094     _rstInOut.output();
00095 
00096     if (_led) _ledInOut.output();
00097 
00098     if (hwSPI) { // Using hardware SPI
00099         spi.frequency(16000000);
00100         spi.format(8, 0);
00101 
00102     }
00103     else {
00104         DigitalInOut _clkInOut(_clk);
00105         _clkInOut.output();
00106         DigitalInOut _sdiInOut(_sdi);
00107         _sdiInOut.output();
00108     }
00109 
00110     // Turn on backlight
00111     if (_led) _ledInOut = 1;
00112 
00113     // Initialization Code
00114     _rstInOut = 1; // Pull the reset pin high to release the ILI9225C from the reset status
00115     wait_ms(1);
00116     _rstInOut = 0;// Pull the reset pin low to reset ILI9225
00117     wait_ms(10);
00118     _rstInOut = 1;// Pull the reset pin high to release the ILI9225C from the reset status
00119     wait_ms(50);
00120 
00121     /* Start Initial Sequence */
00122     /* Set SS bit and direction output from S528 to S1 */
00123     _writeRegister(ILI9225_POWER_CTRL1, 0x0000); // Set SAP,DSTB,STB
00124     _writeRegister(ILI9225_POWER_CTRL2, 0x0000); // Set APON,PON,AON,VCI1EN,VC
00125     _writeRegister(ILI9225_POWER_CTRL3, 0x0000); // Set BT,DC1,DC2,DC3
00126     _writeRegister(ILI9225_POWER_CTRL4, 0x0000); // Set GVDD
00127     _writeRegister(ILI9225_POWER_CTRL5, 0x0000); // Set VCOMH/VCOML voltage
00128     wait_ms(40);
00129 
00130     // Power-on sequence
00131     _writeRegister(ILI9225_POWER_CTRL2, 0x0018); // Set APON,PON,AON,VCI1EN,VC
00132     _writeRegister(ILI9225_POWER_CTRL3, 0x6121); // Set BT,DC1,DC2,DC3
00133     _writeRegister(ILI9225_POWER_CTRL4, 0x006F); // Set GVDD   /*007F 0088 */
00134     _writeRegister(ILI9225_POWER_CTRL5, 0x495F); // Set VCOMH/VCOML voltage
00135     _writeRegister(ILI9225_POWER_CTRL1, 0x0800); // Set SAP,DSTB,STB
00136     wait_ms(10);
00137     _writeRegister(ILI9225_POWER_CTRL2, 0x103B); // Set APON,PON,AON,VCI1EN,VC
00138     wait_ms(50);
00139 
00140     _writeRegister(ILI9225_DRIVER_OUTPUT_CTRL, 0x011C); // set the display line number and display direction
00141     _writeRegister(ILI9225_LCD_AC_DRIVING_CTRL, 0x0100); // set 1 line inversion
00142     _writeRegister(ILI9225_ENTRY_MODE, 0x1030); // set GRAM write direction and BGR=1.
00143     _writeRegister(ILI9225_DISP_CTRL1, 0x0000); // Display off
00144     _writeRegister(ILI9225_BLANK_PERIOD_CTRL1, 0x0808); // set the back porch and front porch
00145     _writeRegister(ILI9225_FRAME_CYCLE_CTRL, 0x1100); // set the clocks number per line
00146     _writeRegister(ILI9225_INTERFACE_CTRL, 0x0000); // CPU interface
00147     _writeRegister(ILI9225_OSC_CTRL, 0x0D01); // Set Osc  /*0e01*/
00148     _writeRegister(ILI9225_VCI_RECYCLING, 0x0020); // Set VCI recycling
00149     _writeRegister(ILI9225_RAM_ADDR_SET1, 0x0000); // RAM Address
00150     _writeRegister(ILI9225_RAM_ADDR_SET2, 0x0000); // RAM Address
00151 
00152                                                    /* Set GRAM area */
00153     _writeRegister(ILI9225_GATE_SCAN_CTRL, 0x0000);
00154     _writeRegister(ILI9225_VERTICAL_SCROLL_CTRL1, 0x00DB);
00155     _writeRegister(ILI9225_VERTICAL_SCROLL_CTRL2, 0x0000);
00156     _writeRegister(ILI9225_VERTICAL_SCROLL_CTRL3, 0x0000);
00157     _writeRegister(ILI9225_PARTIAL_DRIVING_POS1, 0x00DB);
00158     _writeRegister(ILI9225_PARTIAL_DRIVING_POS2, 0x0000);
00159     _writeRegister(ILI9225_HORIZONTAL_WINDOW_ADDR1, 0x00AF);
00160     _writeRegister(ILI9225_HORIZONTAL_WINDOW_ADDR2, 0x0000);
00161     _writeRegister(ILI9225_VERTICAL_WINDOW_ADDR1, 0x00DB);
00162     _writeRegister(ILI9225_VERTICAL_WINDOW_ADDR2, 0x0000);
00163 
00164     /* Set GAMMA curve */
00165     _writeRegister(ILI9225_GAMMA_CTRL1, 0x0000);
00166     _writeRegister(ILI9225_GAMMA_CTRL2, 0x0808);
00167     _writeRegister(ILI9225_GAMMA_CTRL3, 0x080A);
00168     _writeRegister(ILI9225_GAMMA_CTRL4, 0x000A);
00169     _writeRegister(ILI9225_GAMMA_CTRL5, 0x0A08);
00170     _writeRegister(ILI9225_GAMMA_CTRL6, 0x0808);
00171     _writeRegister(ILI9225_GAMMA_CTRL7, 0x0000);
00172     _writeRegister(ILI9225_GAMMA_CTRL8, 0x0A00);
00173     _writeRegister(ILI9225_GAMMA_CTRL9, 0x0710);
00174     _writeRegister(ILI9225_GAMMA_CTRL10, 0x0710);
00175 
00176     _writeRegister(ILI9225_DISP_CTRL1, 0x0012);
00177     wait_ms(50);
00178     _writeRegister(ILI9225_DISP_CTRL1, 0x1017);
00179 
00180     setBacklight(true);
00181     setOrientation(0);
00182 
00183     // Initialize variables
00184     setBackgroundColor(COLOR_BLACK);
00185 
00186     clear();
00187 }
00188 
00189 
00190 void TFT_22_ILI9225::clear() {
00191     uint8_t old = _orientation;
00192     setOrientation(0);
00193     fillRectangle(0, 0, _maxX - 1, _maxY - 1, COLOR_BLACK);
00194     setOrientation(old);
00195     wait_ms(10);
00196 }
00197 
00198 void TFT_22_ILI9225::fill(uint16_t color)
00199 {
00200     fillRectangle(0, 0, _maxX - 1, _maxY - 1, color);
00201 }
00202 
00203 
00204 void TFT_22_ILI9225::invert(bool flag) {
00205     _writeCommand(0x00, flag ? ILI9225C_INVON : ILI9225C_INVOFF);
00206 }
00207 
00208 
00209 void TFT_22_ILI9225::setBacklight(bool flag) {
00210     if (_led) _ledInOut = flag ? 1 : 0;
00211 }
00212 
00213 
00214 void TFT_22_ILI9225::setDisplay(bool flag) {
00215     if (flag) {
00216         _writeRegister(0x00ff, 0x0000);
00217         _writeRegister(ILI9225_POWER_CTRL1, 0x0000);
00218         wait_ms(50);
00219         _writeRegister(ILI9225_DISP_CTRL1, 0x1017);
00220         wait_ms(200);
00221     }
00222     else {
00223         _writeRegister(0x00ff, 0x0000);
00224         _writeRegister(ILI9225_DISP_CTRL1, 0x0000);
00225         wait_ms(50);
00226         _writeRegister(ILI9225_POWER_CTRL1, 0x0003);
00227         wait_ms(200);
00228     }
00229 }
00230 
00231 
00232 void TFT_22_ILI9225::setOrientation(uint8_t orientation) {
00233 
00234     _orientation = orientation % 4;
00235 
00236     switch (_orientation) {
00237     case 0:
00238         _maxX = ILI9225_LCD_WIDTH;
00239         _maxY = ILI9225_LCD_HEIGHT;
00240         break;
00241     case 1:
00242         _maxX = ILI9225_LCD_HEIGHT;
00243         _maxY = ILI9225_LCD_WIDTH;
00244         break;
00245     case 2:
00246         _maxX = ILI9225_LCD_WIDTH;
00247         _maxY = ILI9225_LCD_HEIGHT;
00248         break;
00249     case 3:
00250         _maxX = ILI9225_LCD_HEIGHT;
00251         _maxY = ILI9225_LCD_WIDTH;
00252         break;
00253     }
00254 }
00255 
00256 
00257 uint8_t TFT_22_ILI9225::getOrientation() {
00258     return _orientation;
00259 }
00260 
00261 
00262 void TFT_22_ILI9225::drawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) {
00263     drawLine(x1, y1, x1, y2, color);
00264     drawLine(x1, y1, x2, y1, color);
00265     drawLine(x1, y2, x2, y2, color);
00266     drawLine(x2, y1, x2, y2, color);
00267 }
00268 
00269 
00270 void TFT_22_ILI9225::fillRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) {
00271 
00272     _setWindow(x1, y1, x2, y2);
00273 
00274     for (uint16_t t = (y2 - y1 + 1) * (x2 - x1 + 1); t > 0; t--)
00275         _writeData(color >> 8, color);
00276 }
00277 
00278 
00279 void TFT_22_ILI9225::drawCircle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color) {
00280 
00281     int16_t f = 1 - r;
00282     int16_t ddF_x = 1;
00283     int16_t ddF_y = -2 * r;
00284     int16_t x = 0;
00285     int16_t y = r;
00286 
00287     drawPixel(x0, y0 + r, color);
00288     drawPixel(x0, y0 - r, color);
00289     drawPixel(x0 + r, y0, color);
00290     drawPixel(x0 - r, y0, color);
00291 
00292     while (x < y) {
00293         if (f >= 0) {
00294             y--;
00295             ddF_y += 2;
00296             f += ddF_y;
00297         }
00298         x++;
00299         ddF_x += 2;
00300         f += ddF_x;
00301 
00302         drawPixel(x0 + x, y0 + y, color);
00303         drawPixel(x0 - x, y0 + y, color);
00304         drawPixel(x0 + x, y0 - y, color);
00305         drawPixel(x0 - x, y0 - y, color);
00306         drawPixel(x0 + y, y0 + x, color);
00307         drawPixel(x0 - y, y0 + x, color);
00308         drawPixel(x0 + y, y0 - x, color);
00309         drawPixel(x0 - y, y0 - x, color);
00310     }
00311 }
00312 
00313 
00314 void TFT_22_ILI9225::fillCircle(uint8_t x0, uint8_t y0, uint8_t radius, uint16_t color) {
00315 
00316     int16_t f = 1 - radius;
00317     int16_t ddF_x = 1;
00318     int16_t ddF_y = -2 * radius;
00319     int16_t x = 0;
00320     int16_t y = radius;
00321 
00322     while (x < y) {
00323         if (f >= 0) {
00324             y--;
00325             ddF_y += 2;
00326             f += ddF_y;
00327         }
00328         x++;
00329         ddF_x += 2;
00330         f += ddF_x;
00331 
00332         drawLine(x0 + x, y0 + y, x0 - x, y0 + y, color); // bottom
00333         drawLine(x0 + x, y0 - y, x0 - x, y0 - y, color); // top
00334         drawLine(x0 + y, y0 - x, x0 + y, y0 + x, color); // right
00335         drawLine(x0 - y, y0 - x, x0 - y, y0 + x, color); // left
00336     }
00337     fillRectangle(x0 - x, y0 - y, x0 + x, y0 + y, color);
00338 }
00339 
00340 
00341 void TFT_22_ILI9225::drawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) {
00342 
00343     // Classic Bresenham algorithm
00344     int16_t steep = abs(y2 - y1) > abs(x2 - x1);
00345     int16_t dx, dy;
00346 
00347     if (steep) {
00348         _swap(x1, y1);
00349         _swap(x2, y2);
00350     }
00351 
00352     if (x1 > x2) {
00353         _swap(x1, x2);
00354         _swap(y1, y2);
00355     }
00356 
00357     dx = x2 - x1;
00358     dy = abs(y2 - y1);
00359 
00360     int16_t err = dx / 2;
00361     int16_t ystep;
00362 
00363     if (y1 < y2) ystep = 1;
00364     else ystep = -1;
00365 
00366 
00367     for (; x1 <= x2; x1++) {
00368         if (steep) drawPixel(y1, x1, color);
00369         else       drawPixel(x1, y1, color);
00370 
00371         err -= dy;
00372         if (err < 0) {
00373             y1 += ystep;
00374             err += dx;
00375         }
00376     }
00377 }
00378 
00379 
00380 void TFT_22_ILI9225::drawPixel(uint16_t x1, uint16_t y1, uint16_t color) {
00381 
00382     if ((x1 >= _maxX) || (y1 >= _maxY)) return;
00383 
00384     _setWindow(x1, y1, x1 + 1, y1 + 1);
00385     _orientCoordinates(x1, y1);
00386     _writeData(color >> 8, color);
00387 }
00388 
00389 
00390 uint16_t TFT_22_ILI9225::maxX() {
00391     return _maxX;
00392 }
00393 
00394 
00395 uint16_t TFT_22_ILI9225::maxY() {
00396     return _maxY;
00397 }
00398 
00399 
00400 uint16_t TFT_22_ILI9225::setColor(uint8_t red8, uint8_t green8, uint8_t blue8) {
00401     // rgb16 = red5 green6 blue5
00402     return (red8 >> 3) << 11 | (green8 >> 2) << 5 | (blue8 >> 3);
00403 }
00404 
00405 
00406 void TFT_22_ILI9225::splitColor(uint16_t rgb, uint8_t &red, uint8_t &green, uint8_t &blue) {
00407     // rgb16 = red5 green6 blue5
00408     
00409     red = (rgb & 0xF800) >> 11 << 3;
00410     green = (rgb & 0x7E0) >> 5 << 2;
00411     blue = (rgb & 0x1F) << 3;
00412 }
00413 
00414 
00415 void TFT_22_ILI9225::_swap(uint16_t &a, uint16_t &b) {
00416     uint16_t w = a;
00417     a = b;
00418     b = w;
00419 }
00420 
00421 // Utilities
00422 void TFT_22_ILI9225::_writeCommand(uint8_t HI, uint8_t LO) {
00423     _rsInOut = 0;
00424     _csInOut = 0;
00425     spi.write(HI);
00426     spi.write(LO);
00427     _csInOut = 1;
00428 }
00429 
00430 
00431 void TFT_22_ILI9225::_writeData(uint8_t HI, uint8_t LO) {
00432     _rsInOut = 1;
00433     _csInOut = 0;
00434     spi.write(HI);
00435     spi.write(LO);
00436     _csInOut = 1;
00437 }
00438 
00439 
00440 void TFT_22_ILI9225::_writeRegister(uint16_t reg, uint16_t data) {
00441     _writeCommand(reg >> 8, reg & 255);
00442     _writeData(data >> 8, data & 255);
00443 }
00444 
00445 
00446 void TFT_22_ILI9225::drawTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint16_t color) {
00447     drawLine(x1, y1, x2, y2, color);
00448     drawLine(x2, y2, x3, y3, color);
00449     drawLine(x3, y3, x1, y1, color);
00450 }
00451 
00452 
00453 void TFT_22_ILI9225::fillTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint16_t color) {
00454 
00455     uint16_t a, b, y, last;
00456 
00457     // Sort coordinates by Y order (y3 >= y2 >= y1)
00458     if (y1 > y2) {
00459         _swap(y1, y2); _swap(x1, x2);
00460     }
00461     if (y2 > y3) {
00462         _swap(y3, y2); _swap(x3, x2);
00463     }
00464     if (y1 > y2) {
00465         _swap(y1, y2); _swap(x1, x2);
00466     }
00467 
00468     if (y1 == y3) { // Handle awkward all-on-same-line case as its own thing
00469         a = b = x1;
00470         if (x2 < a)      a = x2;
00471         else if (x2 > b) b = x2;
00472         if (x3 < a)      a = x3;
00473         else if (x3 > b) b = x3;
00474         drawLine(a, y1, b, y1, color);
00475         return;
00476     }
00477 
00478     uint16_t    dx11 = x2 - x1,
00479         dy11 = y2 - y1,
00480         dx12 = x3 - x1,
00481         dy12 = y3 - y1,
00482         dx22 = x3 - x2,
00483         dy22 = y3 - y2,
00484         sa = 0,
00485         sb = 0;
00486 
00487     // For upper part of triangle, find scanline crossings for segments
00488     // 0-1 and 0-2.  If y2=y3 (flat-bottomed triangle), the scanline y2
00489     // is included here (and second loop will be skipped, avoiding a /0
00490     // error there), otherwise scanline y2 is skipped here and handled
00491     // in the second loop...which also avoids a /0 error here if y1=y2
00492     // (flat-topped triangle).
00493     if (y2 == y3) last = y2;   // Include y2 scanline
00494     else          last = y2 - 1; // Skip it
00495 
00496     for (y = y1; y <= last; y++) {
00497         a = x1 + sa / dy11;
00498         b = x1 + sb / dy12;
00499         sa += dx11;
00500         sb += dx12;
00501         /* longhand:
00502         a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
00503         b = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
00504         */
00505         if (a > b) _swap(a, b);
00506         drawLine(a, y, b, y, color);
00507     }
00508 
00509     // For lower part of triangle, find scanline crossings for segments
00510     // 0-2 and 1-2.  This loop is skipped if y2=y3.
00511     sa = dx22 * (y - y2);
00512     sb = dx12 * (y - y1);
00513     for (; y <= y3; y++) {
00514         a = x2 + sa / dy22;
00515         b = x1 + sb / dy12;
00516         sa += dx22;
00517         sb += dx12;
00518         /* longhand:
00519         a = x2 + (x3 - x2) * (y - y2) / (y3 - y2);
00520         b = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
00521         */
00522         if (a > b) _swap(a, b);
00523         drawLine(a, y, b, y, color);
00524     }
00525 }
00526 
00527 
00528 void TFT_22_ILI9225::setBackgroundColor(uint16_t color) {
00529     _bgColor = color;
00530 }
00531 
00532 
00533 void TFT_22_ILI9225::setFont(uint8_t* font) {
00534 
00535     cfont.font = font;
00536     cfont.width = font[0];
00537     cfont.height = font[1];
00538     cfont.offset = font[2];
00539     cfont.numchars = font[3];
00540     cfont.nbrows = cfont.height / 8;
00541 
00542     if (cfont.height % 8) cfont.nbrows++;  // Set number of bytes used by height of font in multiples of 8
00543 }
00544 
00545 
00546 void TFT_22_ILI9225::drawText(uint16_t x, uint16_t y, char *s, uint16_t color) {
00547 
00548     uint16_t currx = x;
00549 
00550     // Print every character in string
00551     for (uint8_t k = 0; k < strlen(s); k++) {
00552         currx += drawChar(currx, y, s[k], color) + 1;
00553     }
00554 }
00555 
00556 
00557 uint16_t TFT_22_ILI9225::drawChar(uint16_t x, uint16_t y, uint16_t ch, uint16_t color) {
00558 
00559     uint8_t charData, charWidth;
00560     uint8_t h, i, j;
00561     uint16_t charOffset;
00562 
00563     charOffset = (cfont.width * cfont.nbrows) + 1;  // bytes used by each character
00564     charOffset = (charOffset * (ch - cfont.offset)) + FONT_HEADER_SIZE;  // char offset (add 4 for font header)
00565     charWidth = cfont.font[charOffset];  // get font width from 1st byte
00566     charOffset++;  // increment pointer to first character data byte
00567 
00568     for (i = 0; i <= charWidth; i++) {  // each font "column" (+1 blank column for spacing)
00569         h = 0;  // keep track of char height
00570         for (j = 0; j < cfont.nbrows; j++) {  // each column byte
00571             if (i == charWidth) charData = (uint8_t)0x0; // Insert blank column
00572             else                charData = cfont.font[charOffset];
00573             charOffset++;
00574 
00575             // Process every row in font character
00576             for (uint8_t k = 0; k < 8; k++) {
00577                 if (h >= cfont.height) break;  // No need to process excess bits
00578                 if (bitRead(charData, k)) drawPixel(x + i, y + (j * 8) + k, color);
00579                 else                      drawPixel(x + i, y + (j * 8) + k, _bgColor);
00580                 h++;
00581             };
00582         };
00583     };
00584     return charWidth;
00585 }
00586 
00587 
00588 //----------------------------------------------------------------------------------------------------
00589 //************************************* ECA 2.8 inch LCD Module **************************************
00590 //----------------------------------------------------------------------------------------------------
00591 // Set Farsi font color
00592 //----------------------------------------------------------------------------------------------------
00593 void TFT_22_ILI9225::setFontColor(int color)
00594 {
00595     font_color = color;
00596 }
00597 //----------------------------------------------------------------------------------------------------
00598 //************************************* ECA 2.8 inch LCD Module **************************************
00599 //----------------------------------------------------------------------------------------------------
00600 // Set number format to farsi
00601 //----------------------------------------------------------------------------------------------------
00602 void TFT_22_ILI9225::setNumberFormatToFa(void)
00603 {
00604     fa_num = 1;
00605 }
00606 
00607 //----------------------------------------------------------------------------------------------------
00608 //************************************* ECA 2.8 inch LCD Module **************************************
00609 //----------------------------------------------------------------------------------------------------
00610 // Set number format to english
00611 //----------------------------------------------------------------------------------------------------
00612 void TFT_22_ILI9225::setNumberFormatToEn(void)
00613 {
00614     fa_num = 0;
00615 }
00616 
00617 //----------------------------------------------------------------------------------------------------
00618 //************************************* ECA 2.8 inch LCD Module **************************************
00619 //----------------------------------------------------------------------------------------------------
00620 // Set efect of the farsi font
00621 // ef : effect number           ef:1,2,...,5
00622 //----------------------------------------------------------------------------------------------------
00623 void TFT_22_ILI9225::setFontEffect(int ef)
00624 {
00625     if (ef >= 0 && ef <= 5)
00626         effect = ef;
00627 }
00628 
00629 //----------------------------------------------------------------------------------------------------
00630 //************************************* ECA 2.8 inch LCD Module **************************************
00631 //----------------------------------------------------------------------------------------------------
00632 // Enable Italic effect for farsi font 
00633 //----------------------------------------------------------------------------------------------------
00634 void TFT_22_ILI9225::setItalicOn(void)
00635 {
00636     italic = 1;
00637 }
00638 
00639 //----------------------------------------------------------------------------------------------------
00640 //************************************* ECA 2.8 inch LCD Module **************************************
00641 //----------------------------------------------------------------------------------------------------
00642 // Disable Italic effect for farsi font 
00643 //----------------------------------------------------------------------------------------------------
00644 void TFT_22_ILI9225::setItalicOff(void)
00645 {
00646     italic = 0;
00647 }
00648 
00649 //----------------------------------------------------------------------------------------------------
00650 //************************************* ECA 2.8 inch LCD Module **************************************
00651 //----------------------------------------------------------------------------------------------------
00652 // Puts a Farsi Character On The LCD Screen      simlpe
00653 //----------------------------------------------------------------------------------------------------
00654 void TFT_22_ILI9225::putChar(char character)
00655 {
00656     //x_text
00657     int counter_x, counter_y;
00658     char letter_length, flag_p;
00659 
00660     letter_length = font12x16_FA[character][0] >> 12;
00661     for (counter_y = 0; counter_y<16; counter_y++)
00662         for (counter_x = 0; counter_x<letter_length; counter_x++)
00663         {
00664             flag_p = font12x16_FA[character][counter_y] >> (counter_x);
00665             flag_p = flag_p & 0x01;
00666             if (flag_p)
00667                 drawPixel(_maxX - 3 - (x_font + (counter_x)), y_font + counter_y, font_color);
00668             else if (highlight)
00669                 drawPixel(_maxX - 3 - (x_font + (counter_x)), y_font + counter_y, highlight_color);
00670         }
00671 
00672     x_font += letter_length;
00673     if (x_font > _maxX - 1)
00674     {
00675         x_font = x_base;
00676         y_font += 16;
00677         if (y_font > _maxY - 0)
00678             y_font = 0;
00679     }
00680 
00681 }
00682 
00683 //----------------------------------------------------------------------------------------------------
00684 //************************************* ECA 2.8 inch LCD Module **************************************
00685 //----------------------------------------------------------------------------------------------------
00686 // Description  : Draws a beveled figure on the screen. 
00687 // Input        : x0, y0 - coordinate position of the upper left center
00688 //                  : x1, y1 - coordinate position of the lower right center
00689 //              : rad    - defines the redius of the circle,
00690 //              : fill   - fill yes or no
00691 //----------------------------------------------------------------------------------------------------
00692 void TFT_22_ILI9225::roundRectangle(int x0, int y0, int x1, int y1, int rad, bool fill, int color)
00693 {
00694     signed int a, b, P;
00695 
00696     a = 0;                      // increment by 1
00697     b = rad;                // decrement by 1 using P
00698     P = 1 - rad;
00699 
00700 
00701     if (fill)
00702     {
00703         fillRectangle(x0, y0 + rad, x1, y1 - rad, color);
00704 
00705         do
00706         {
00707             fillRectangle(x0 - a + rad, y0 - b + rad, a + x1 - rad, y0 - b + rad, color);   // 8 --> 1
00708             fillRectangle(x0 - b + rad, y0 - a + rad, b + x1 - rad, y0 - a + rad, color);   // 7 --> 2
00709             fillRectangle(x0 - b + rad, a + y1 - rad, b + x1 - rad, a + y1 - rad, color);   // 6 --> 3
00710             fillRectangle(x0 - a + rad, b + y1 - rad, a + x1 - rad, b + y1 - rad, color);   // 5 --> 4
00711 
00712             if (P < 0)
00713                 P += 3 + 2 * a++;
00714             else
00715                 P += 5 + 2 * (a++ - b--);
00716 
00717         } while (a <= b);
00718     } //fill
00719     else
00720     {
00721         fillRectangle(x0 + rad, y0, x1 - rad, y0, color);   // top
00722         fillRectangle(x0 + rad, y1, x1 - rad, y1, color);   // bottom
00723         fillRectangle(x0, y0 + rad, x0, y1 - rad, color);   // left
00724         fillRectangle(x1, y0 + rad, x1, y1 - rad, color);   // right
00725 
00726         do
00727         {
00728             drawPixel(a + x1 - rad, y0 - b + rad, color);   // `````` Segment 1
00729             drawPixel(b + x1 - rad, y0 - a + rad, color);   // `````` Segment 2
00730 
00731             drawPixel(b + x1 - rad, a + y1 - rad, color);   // `````` Segment 3
00732             drawPixel(a + x1 - rad, b + y1 - rad, color);   // `````` Segment 4
00733 
00734             drawPixel(x0 - a + rad, b + y1 - rad, color);   // `````` Segment 5
00735             drawPixel(x0 - b + rad, a + y1 - rad, color);   // `````` Segment 6
00736 
00737             drawPixel(x0 - b + rad, y0 - a + rad, color);   // `````` Segment 7
00738             drawPixel(x0 - a + rad, y0 - b + rad, color);   // `````` Segment 8
00739 
00740             if (P < 0)
00741                 P += 3 + 2 * a++;
00742             else
00743                 P += 5 + 2 * (a++ - b--);
00744         } while (a <= b);
00745     } //no fill
00746 }   //RoundRectangle
00747 
00748 
00749 
00750 //----------------------------------------------------------------------------------------------------
00751 //************************************* ECA 2.8 inch LCD Module **************************************
00752 //----------------------------------------------------------------------------------------------------
00753 // Puts a Farsi Character On The LCD Screen with scaling
00754 // size : Scale factor       size:1,2,3,...
00755 //----------------------------------------------------------------------------------------------------
00756 void TFT_22_ILI9225::putBoldChar(char character, int size)
00757 {
00758     int counter_x, counter_y, i, j;
00759     char letter_length, flag_p;
00760 
00761 
00762     letter_length = font12x16_FA[character][0] >> 12;
00763     for (counter_y = 0; counter_y<16; counter_y++)
00764         for (counter_x = 0; counter_x<letter_length; counter_x++)
00765         {
00766             flag_p = font12x16_FA[character][counter_y] >> (counter_x);
00767             flag_p = flag_p & 0x01;
00768 
00769 
00770             if (flag_p)
00771             {
00772 
00773                 if (effect == 0)
00774                     for (i = 0; i<size; i++)
00775                         for (j = 0; j<size; j++)
00776                             drawPixel(_maxX - 3 + italic*(-counter_y + 7) + i - (x_font + (size*counter_x)), y_font + size*counter_y + j, font_color);
00777                 // Very good effect (1)
00778                 else if (effect == 1)
00779                 {
00780                     // good for size = 2
00781                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00782                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y - 1, font_color);
00783                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y + 1, font_color);
00784                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - 1 - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00785                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00786                 }
00787                 else if (effect == 2)
00788                 {
00789                     // good for size = 2
00790                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00791                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y - 1, font_color);
00792                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y + 1, font_color);
00793                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - 1 - (x_font + (size*counter_x)), y_font + size*counter_y + 1, font_color);
00794                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y + 1, font_color);
00795                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - 1 - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00796                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00797                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y + 2, font_color);
00798                 }
00799                 else if (effect == 3)
00800                 {
00801                     // good for size = 3
00802                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00803                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y - 1, font_color);
00804                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y - 1, font_color);
00805                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - 1 - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00806                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00807                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 2 - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00808                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y + 1, font_color);
00809                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y + 1, font_color);
00810                 }
00811                 else if (effect == 4)
00812                 {
00813                     // good for size = 3
00814                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00815                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y - 1, font_color);
00816                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y - 1, font_color);
00817                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y + 1, font_color);
00818                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - 1 - (x_font + (size*counter_x)), y_font + size*counter_y + 1, font_color);
00819                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y + 1, font_color);
00820                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 2 - (x_font + (size*counter_x)), y_font + size*counter_y + 1, font_color);
00821                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - 1 - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00822                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00823                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 2 - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00824                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y + 2, font_color);
00825                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y + 2, font_color);
00826                 }
00827                 else if (effect == 5)
00828                 {
00829                     // good for size = 1,3
00830                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00831                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y + 1, font_color);
00832                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00833                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y + 1, font_color);
00834                 }
00835 
00836                 //tftlcd_draw_circle(_maxX - 3 -(x_font+(size*counter_x)),y_font+size*counter_y,size,1,font_color);
00837 
00838             }
00839             else if (highlight)
00840             {
00841 
00842 
00843                 if (effect == 0)
00844                     for (i = 0; i<size; i++)
00845                         for (j = 0; j<size; j++)
00846                             drawPixel(_maxX - 3 + italic*(-counter_y + 7) + i - (x_font + (size*counter_x)), y_font + size*counter_y + j, highlight_color);
00847                 // Very good effect (1)
00848                 else if (effect == 1)
00849                 {
00850                     // good for size = 2
00851                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y, font_color);
00852                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y - 1, highlight_color);
00853                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y + 1, highlight_color);
00854                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - 1 - (x_font + (size*counter_x)), y_font + size*counter_y, highlight_color);
00855                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y, highlight_color);
00856                 }
00857                 else if (effect == 2)
00858                 {
00859                     // good for size = 2
00860                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y, highlight_color);
00861                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y - 1, highlight_color);
00862                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y + 1, highlight_color);
00863                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - 1 - (x_font + (size*counter_x)), y_font + size*counter_y + 1, highlight_color);
00864                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y + 1, highlight_color);
00865                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - 1 - (x_font + (size*counter_x)), y_font + size*counter_y, highlight_color);
00866                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y, highlight_color);
00867                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y + 2, highlight_color);
00868                 }
00869                 else if (effect == 3)
00870                 {
00871                     // good for size = 3
00872                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y, highlight_color);
00873                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y - 1, highlight_color);
00874                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y - 1, highlight_color);
00875                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - 1 - (x_font + (size*counter_x)), y_font + size*counter_y, highlight_color);
00876                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y, highlight_color);
00877                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 2 - (x_font + (size*counter_x)), y_font + size*counter_y, highlight_color);
00878                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y + 1, highlight_color);
00879                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y + 1, highlight_color);
00880                 }
00881                 else if (effect == 4)
00882                 {
00883                     // good for size = 3
00884                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y, highlight_color);
00885                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y - 1, highlight_color);
00886                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y - 1, highlight_color);
00887                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y + 1, highlight_color);
00888                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - 1 - (x_font + (size*counter_x)), y_font + size*counter_y + 1, highlight_color);
00889                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y + 1, highlight_color);
00890                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 2 - (x_font + (size*counter_x)), y_font + size*counter_y + 1, highlight_color);
00891                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - 1 - (x_font + (size*counter_x)), y_font + size*counter_y, highlight_color);
00892                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y, highlight_color);
00893                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 2 - (x_font + (size*counter_x)), y_font + size*counter_y, highlight_color);
00894                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y + 2, highlight_color);
00895                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y + 2, highlight_color);
00896                 }
00897                 else if (effect == 5)
00898                 {
00899                     // good for size = 1,3
00900                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y, highlight_color);
00901                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) - (x_font + (size*counter_x)), y_font + size*counter_y + 1, highlight_color);
00902                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y, highlight_color);
00903                     drawPixel(_maxX - 3 + italic*(-counter_y + 7) + 1 - (x_font + (size*counter_x)), y_font + size*counter_y + 1, highlight_color);
00904                 }
00905 
00906 
00907 
00908                 //tftlcd_draw_circle(_maxX - 3 + italic*(-counter_y+7) -(x_font+(size*counter_x)),y_font+size*counter_y,size,1,highlight_color);
00909 
00910 
00911             }
00912         }
00913 
00914     x_font += size*letter_length;
00915     if (x_font > _maxX - 1)
00916     {
00917         x_font = x_base;
00918         y_font += size * 16;
00919         if (y_font > _maxY - 0)
00920             y_font = 0;
00921     }
00922 
00923 }
00924 
00925 
00926 //----------------------------------------------------------------------------------------------------
00927 //************************************* ECA 2.8 inch LCD Module **************************************
00928 //----------------------------------------------------------------------------------------------------
00929 // Go to a specific pont for farsi font (x:0..TS_SIZE_X , y:0..TS_SIZE_Y)
00930 //----------------------------------------------------------------------------------------------------
00931 void TFT_22_ILI9225::goToXY(int x, int y)
00932 {
00933     if ((x >= _maxX) || (x < 0))
00934     {
00935         x_font = 0;
00936         x_base = 0;
00937     }
00938     else
00939     {
00940         x_font = x;
00941         x_base = x;
00942     }
00943 
00944     if ((y >= _maxY) || (y < 0))
00945         y_font = 0;
00946     else
00947         y_font = y;
00948 }
00949 
00950 //----------------------------------------------------------------------------------------------------
00951 //************************************* ECA 2.8 inch LCD Module **************************************
00952 //----------------------------------------------------------------------------------------------------
00953 // Enable Farsi font Highlight
00954 //----------------------------------------------------------------------------------------------------
00955 void TFT_22_ILI9225::setTextHighlightOn(void)
00956 {
00957     highlight = 1;
00958 }
00959 
00960 //----------------------------------------------------------------------------------------------------
00961 //************************************* ECA 2.8 inch LCD Module **************************************
00962 //----------------------------------------------------------------------------------------------------
00963 // Disable Farsi font Highlight
00964 //----------------------------------------------------------------------------------------------------
00965 void TFT_22_ILI9225::setTextHighlightOff(void)
00966 {
00967     highlight = 0;
00968 }
00969 
00970 //----------------------------------------------------------------------------------------------------
00971 //************************************* ECA 2.8 inch LCD Module **************************************
00972 //----------------------------------------------------------------------------------------------------
00973 // Set Farsi font Highlight color
00974 //----------------------------------------------------------------------------------------------------
00975 void TFT_22_ILI9225::setTextHighlightColor(unsigned int color)
00976 {
00977     highlight_color = color;
00978 }
00979 
00980 //----------------------------------------------------------------------------------------------------
00981 //************************************* ECA 2.8 inch LCD Module **************************************
00982 //----------------------------------------------------------------------------------------------------
00983 // Show a farsi-englisg string on the LCD with specific size
00984 // size : Scale factor       size:1,2,3,...
00985 //----------------------------------------------------------------------------------------------------
00986 void TFT_22_ILI9225::putMixedEnAndFaString(const unsigned char *string, int size)
00987 {
00988     unsigned char letter, nt_letter, pr_letter;
00989     unsigned char CN = 0, CP = 0, pr_CN = 0, nt_CP = 0, place = 0, flag1 = 0;
00990     unsigned char letter_counter = 0, letter_count = 0, length = 0, enter = 0;
00991     unsigned char text_buffer[32];
00992 
00993 
00994     flag1 = 0;
00995 
00996     while (*string)
00997     {
00998         if (*string > 0x80)
00999         {
01000             while (*string > 0x80)
01001             {
01002                 if (flag1 == 0)
01003                     pr_letter = 0x20;
01004                 else
01005                     pr_letter = *(string - 1);
01006 
01007                 letter = *string++;
01008 
01009                 if (*string == 0)
01010                     nt_letter = 0x20;
01011                 else
01012                     nt_letter = *string;
01013 
01014                 flag1 = 1;
01015 
01016                 if (letter > 0x98) letter = letter;
01017                 else if (letter == 0x98) letter = 0xBC;
01018                 else if (letter == 0x90) letter = 0xC0;
01019                 else if (letter == 0x8D) letter = 0xBE;
01020                 else if (letter == 0x81) letter = 0xBD;
01021                 else if (letter == 0x8E) letter = 0xBF;
01022 
01023                 if (pr_letter > 0x98) pr_letter = pr_letter;
01024                 else if (pr_letter == 0x98) pr_letter = 0xBC;
01025                 else if (pr_letter == 0x90) pr_letter = 0xC0;
01026                 else if (pr_letter == 0x8D) pr_letter = 0xBE;
01027                 else if (pr_letter == 0x81) pr_letter = 0xBD;
01028                 else if (pr_letter == 0x8E) pr_letter = 0xBF;
01029 
01030                 if (nt_letter > 0x98) nt_letter = nt_letter;
01031                 else if (nt_letter == 0x98) nt_letter = 0xBC;
01032                 else if (nt_letter == 0x90) nt_letter = 0xC0;
01033                 else if (nt_letter == 0x8D) nt_letter = 0xBE;
01034                 else if (nt_letter == 0x81) nt_letter = 0xBD;
01035                 else if (nt_letter == 0x8E) nt_letter = 0xBF;
01036 
01037 
01038 
01039                 if (pr_letter > 0x80)
01040                     pr_CN = ((FAmap[(pr_letter - 0xBC)][5]) == 1);
01041                 else
01042                     pr_CN = 0;
01043 
01044                 if (nt_letter > 0x80)
01045                     nt_CP = ((FAmap[(nt_letter - 0xBC)][4]) == 1);
01046                 else
01047                     nt_CP = 0;
01048 
01049                 if (letter > 0x80)
01050                     CP = ((FAmap[(letter - 0xBC)][4]) == 1);
01051                 else
01052                     CP = 0;
01053 
01054                 if (letter > 0x80)
01055                     CN = ((FAmap[(letter - 0xBC)][5]) == 1);
01056                 else
01057                     CN = 0;
01058 
01059                 CP = pr_CN && CP;
01060                 CN = CN && nt_CP;
01061                 place = (CP << 1) | CN;
01062 
01063                 text_buffer[letter_counter++] = FAmap[(letter - 0xBC)][place];
01064 
01065                 length += size*(font12x16_FA[FAmap[(letter - 0xBC)][place]][1] >> 12);
01066             }
01067 
01068 
01069 
01070             if (length < _maxX - x_font - 3)
01071                 for (letter_count = 0; letter_count<letter_counter; letter_count++)
01072                     putBoldChar(text_buffer[letter_count], size);
01073             else
01074             {
01075 
01076                 x_font = x_base;
01077                 y_font += size * 16;
01078                 if (y_font > _maxY - size * 16)
01079                     y_font = 0;
01080 
01081                 for (letter_count = 0; letter_count<letter_counter; letter_count++)
01082                     putBoldChar(text_buffer[letter_count], size);
01083             }
01084             letter_counter = 0;
01085             length = 0;
01086         }
01087         else if (*string == 0x20)   //Space detect
01088             putBoldChar(*string++, size);
01089         else   //English letter & Number & Enter detect
01090         {
01091             if (*string == 0x0D)   //Enter detect
01092             {
01093                 x_font = x_base;
01094                 y_font += size * 16;
01095                 if (y_font > _maxY - size * 16)
01096                     y_font = 0;
01097 
01098                 string += 2;
01099                 goto p1;
01100             }
01101             //letter_counter = 0;
01102             while ((*string < 0x81) && (*(string + 1) < 0x81) && (*string != 0x00))
01103             {
01104 
01105                 if (fa_num)
01106                 {
01107                     if ((*string > 0x2F) && (*string < 0x3A) && (*string != 0x00))
01108                     {
01109                         letter = (*string) - 0x20;
01110                         text_buffer[letter_counter++] = letter;
01111                         string++;
01112                         goto P2;
01113                     }
01114                 }
01115                 text_buffer[letter_counter++] = *string++;
01116             P2:
01117                 if ((*string == 0x20) && ((letter_counter * size * 8) < (_maxX - x_font - 1)))
01118                     flag1 = letter_counter;
01119 
01120                 if ((letter_counter * size * 8) > (_maxX - x_font - 1))
01121                 {
01122                     string -= (letter_counter - flag1 + 1);
01123                     letter_counter = flag1 - 1;
01124                     enter = 1;
01125 
01126                     break;
01127                 }
01128 
01129             }
01130             if ((letter_counter * size * 8) < (_maxX - x_font - 1))
01131             {
01132                 for (letter_count = (letter_counter); letter_count>0; letter_count--)
01133                     putBoldChar(text_buffer[letter_count - 1], size);
01134 
01135                 if (enter)
01136                 {
01137                     enter = 0;
01138                     x_font = x_base;
01139                     y_font += size * 16;
01140                     if (y_font > _maxY - size * 16)
01141                         y_font = 0;
01142                 }
01143             }
01144         p1:
01145             letter_counter = 0;
01146             flag1 = 0;
01147 
01148         }
01149     }
01150     flag1 = 0;
01151 
01152 }
01153 
01154 //----------------------------------------------------------------------------------------------------
01155 //************************************* ECA 2.8 inch LCD Module **************************************
01156 //----------------------------------------------------------------------------------------------------
01157 // Make an ascii string from an unicode string 
01158 //----------------------------------------------------------------------------------------------------
01159 
01160 void TFT_22_ILI9225::unicode2ascii(char *uni_str, char *ascii_str)
01161 {
01162     int counter = 0;
01163     int Uch = 0;
01164     char chl, chh;
01165 
01166 
01167     while (*uni_str)
01168     {
01169         chl = *uni_str++;
01170         chh = *uni_str++;
01171 
01172         Uch = 0;
01173         Uch = ((Uch | chh) << 8) | chl;
01174 
01175         if (Uch > 1574 && Uch < 1591)
01176             *(ascii_str + counter) = (char)(Uch - 1376);
01177         else if (Uch > 1590 && Uch < 1595)
01178             *(ascii_str + counter) = (char)(Uch - 1375);
01179         else if (Uch > 1600 && Uch < 1603)
01180             *(ascii_str + counter) = (char)(Uch - 1380);
01181         else if (Uch == 1705)
01182             *(ascii_str + counter) = (char)(Uch - 1482);
01183         else if (Uch == 1604)
01184             *(ascii_str + counter) = (char)(Uch - 1379);
01185         else if (Uch > 1604 && Uch < 1609)
01186             *(ascii_str + counter) = (char)(Uch - 1378);
01187         else if (Uch == 1740)
01188             *(ascii_str + counter) = (char)(Uch - 1503);
01189         else if (Uch == 1574)
01190             *(ascii_str + counter) = (char)(Uch - 1381);
01191         else if (Uch == 1662)
01192             *(ascii_str + counter) = (char)(Uch - 1533);
01193         else if (Uch == 1670)
01194             *(ascii_str + counter) = (char)(Uch - 1529);
01195         else if (Uch == 1688)
01196             *(ascii_str + counter) = (char)(Uch - 1546);
01197         else if (Uch == 1711)
01198             *(ascii_str + counter) = (char)(Uch - 1567);
01199         else if (Uch == 1570)
01200             *(ascii_str + counter) = (char)(Uch - 1376);
01201         else if (Uch > 1631 && Uch < 1642)
01202             *(ascii_str + counter) = (char)(Uch - 1584);
01203         else if (Uch == 65536)
01204             *(ascii_str + counter) = NULL;
01205         else
01206             *(ascii_str + counter) = (char)Uch;
01207 
01208 
01209         counter++;
01210 
01211     }
01212     *(ascii_str + counter) = NULL;
01213 
01214 }
01215