alterações para funcionar com o display

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPI_TFT_ILI9341.cpp Source File

SPI_TFT_ILI9341.cpp

00001 /* mbed library for 240*320 pixel display TFT based on ILI9341 LCD Controller
00002  * Copyright (c) 2013 Peter Drescher - DC2PD
00003  *
00004  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00005  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00006  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00007  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00008  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00009  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00010  * THE SOFTWARE.
00011  */
00012  
00013 // 12.06.13 fork from SPI_TFT code because controller is different ...
00014 // 14.07.13 Test with real display and bugfix 
00015 // 18.10.13 Better Circle function from Michael Ammann
00016 // 22.10.13 Fixes for Kinetis Board - 8 bit spi
00017 
00018 #include "SPI_TFT_ILI9341.h"
00019 #include "mbed.h"
00020 
00021 #define BPP         16                  // Bits per pixel    
00022             
00023 
00024 //extern Serial pc;
00025 //extern DigitalOut xx;     // debug !!
00026 
00027 SPI_TFT_ILI9341::SPI_TFT_ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char *name)
00028     : GraphicsDisplay(name), _spi(mosi, miso, sclk), _cs(cs), _dc(dc)
00029 {
00030     orientation = 0;
00031     char_x = 0;
00032     _reset = reset;
00033     tft_reset();
00034 }
00035 
00036 int SPI_TFT_ILI9341::width()
00037 {
00038     if (orientation == 0 || orientation == 2) return 240;
00039     else return 320;
00040 }
00041 
00042 
00043 int SPI_TFT_ILI9341::height()
00044 {
00045     if (orientation == 0 || orientation == 2) return 320;
00046     else return 240;
00047 }
00048 
00049 
00050 void SPI_TFT_ILI9341::set_orientation(unsigned int o)
00051 {
00052     orientation = o;
00053     wr_cmd(0x36);                     // MEMORY_ACCESS_CONTROL
00054     switch (orientation) {
00055         case 0:
00056             _spi.write(0x48);
00057             break;
00058         case 1:
00059             _spi.write(0x28);
00060             break;
00061         case 2:
00062             _spi.write(0x88);
00063             break;
00064         case 3:
00065             _spi.write(0xE8);
00066             break;
00067     }
00068     _cs = 1; 
00069     WindowMax();
00070 } 
00071 
00072 
00073 // write command to tft register
00074 
00075 void SPI_TFT_ILI9341::wr_cmd(unsigned char cmd)
00076 {
00077     _dc = 0;
00078     _cs = 0;
00079     _spi.write(cmd);      // mbed lib
00080     _dc = 1;
00081 }
00082 
00083 
00084 
00085 void SPI_TFT_ILI9341::wr_dat(unsigned char dat)
00086 {
00087    _spi.write(dat);      // mbed lib
00088 }
00089 
00090 
00091 
00092 // the ILI9341 can read - has to be implemented later
00093 // A read will return 0 at the moment
00094 
00095 //unsigned short SPI_TFT_ILI9341::rd_dat (void)
00096 //{
00097 //    unsigned short val = 0;
00098 
00099     //val = _spi.write(0x73ff);                /* Dummy read 1           */
00100     //val   = _spi.write(0x0000);              /* Read D8..D15           */
00101 //    return (val);
00102 //}
00103 
00104 
00105 
00106 // Init code based on MI0283QT datasheet
00107 
00108 void SPI_TFT_ILI9341::tft_reset()
00109 {
00110     _spi.format(8,3);                  // 8 bit spi mode 3
00111     _spi.frequency(10000000);          // 10 Mhz SPI clock
00112     _cs = 1;                           // cs high
00113     _dc = 1;                           // dc high 
00114     if (_reset != NC)
00115     {
00116         DigitalOut rst(_reset);
00117         rst = 0;                       // display reset
00118         wait_us(50);
00119         rst = 1;                       // end hardware reset
00120     }
00121     wait_ms(5);
00122      
00123     wr_cmd(0x01);                     // SW reset  
00124     wait_ms(5);
00125     wr_cmd(0x28);                     // display off  
00126 
00127     /* Start Initial Sequence ----------------------------------------------------*/
00128      wr_cmd(0xCF);                     //copiado para 11-2
00129      _spi.write(0x00);
00130      _spi.write(0x83);
00131      _spi.write(0x30);
00132      _cs = 1;
00133      
00134      wr_cmd(0xED);                     //igual
00135      _spi.write(0x64);
00136      _spi.write(0x03);
00137      _spi.write(0x12);
00138      _spi.write(0x81);
00139      _cs = 1;
00140      
00141      wr_cmd(0xE8);                     //copiado para 11-2
00142      _spi.write(0x85);
00143      _spi.write(0x01);
00144      _spi.write(0x79);
00145      _cs = 1;
00146      
00147      wr_cmd(0xCB);                     //igual
00148      _spi.write(0x39);
00149      _spi.write(0x2C);
00150      _spi.write(0x00);
00151      _spi.write(0x34);
00152      _spi.write(0x02);
00153      _cs = 1;
00154            
00155      wr_cmd(0xF7);                     //copiado para 11-2
00156      _spi.write(0x20);
00157      _cs = 1;
00158            
00159      wr_cmd(0xEA);                     //igual
00160      _spi.write(0x00);
00161      _spi.write(0x00);
00162      _cs = 1;
00163      
00164      wr_cmd(0xC0);                     // POWER_CONTROL_1       //copiado para 11-2
00165      _spi.write(0x26);
00166      _cs = 1;
00167  
00168      wr_cmd(0xC1);                     // POWER_CONTROL_2       //copiado para 11-2
00169      _spi.write(0x11);
00170      _cs = 1;
00171      
00172      wr_cmd(0xC5);                     // VCOM_CONTROL_1       //copiado para 11-2
00173      _spi.write(0x35);
00174      _spi.write(0x3E);
00175      _cs = 1;
00176      
00177      wr_cmd(0xC7);                     // VCOM_CONTROL_2       //copiado para 11-2
00178      _spi.write(0xBE);
00179      _cs = 1; 
00180      
00181      wr_cmd(0x36);                     // MEMORY_ACCESS_CONTROL //igual
00182      _spi.write(0x48);
00183      _cs = 1; 
00184      
00185      wr_cmd(0x3A);                     // COLMOD_PIXEL_FORMAT_SET   //igual
00186      _spi.write(0x55);                 // 16 bit pixel 
00187      _cs = 1;
00188      
00189      wr_cmd(0xB1);                     // Frame Rate    //copiado para 11-2
00190      _spi.write(0x00);
00191      _spi.write(0x1B);               
00192      _cs = 1;
00193      
00194      wr_cmd(0xF2);                     // Gamma Function Disable    //copiado para 11-2
00195      _spi.write(0x08);
00196      _cs = 1; 
00197      
00198      wr_cmd(0x26);                     
00199      _spi.write(0x01);                 // gamma set for curve 01/2/04/08    //igual
00200      _cs = 1; 
00201      
00202      wr_cmd(0xE0);                     // positive gamma correction    //copiado para 11-2
00203      _spi.write(0x1F); 
00204      _spi.write(0x1A); 
00205      _spi.write(0x18); 
00206      _spi.write(0x0A); 
00207      _spi.write(0x0F); 
00208      _spi.write(0x06); 
00209      _spi.write(0x45); 
00210      _spi.write(0x87); 
00211      _spi.write(0x32); 
00212      _spi.write(0x0A); 
00213      _spi.write(0x07); 
00214      _spi.write(0x02); 
00215      _spi.write(0x07);
00216      _spi.write(0x05); 
00217      _spi.write(0x00);
00218      _cs = 1;
00219      
00220      wr_cmd(0xE1);                     // negativ gamma correction    //copiado para 11-2
00221      _spi.write(0x00); 
00222      _spi.write(0x25); 
00223      _spi.write(0x27); 
00224      _spi.write(0x05); 
00225      _spi.write(0x10); 
00226      _spi.write(0x09); 
00227      _spi.write(0x3A); 
00228      _spi.write(0x78); 
00229      _spi.write(0x4D); 
00230      _spi.write(0x05); 
00231      _spi.write(0x18); 
00232      _spi.write(0x0D); 
00233      _spi.write(0x38);
00234      _spi.write(0x3A); 
00235      _spi.write(0x1F);
00236      _cs = 1;
00237      
00238      WindowMax ();
00239      
00240      //wr_cmd(0x34);                     // tearing effect off
00241      //_cs = 1;
00242      
00243      //wr_cmd(0x35);                     // tearing effect on
00244      //_cs = 1;
00245       
00246      wr_cmd(0xB7);                       // entry mode    //copiado para 11-2  
00247      _spi.write(0x07);
00248      _cs = 1;
00249      
00250      wr_cmd(0xB6);                       // display function control    //copiado para 11-2  
00251      _spi.write(0x0A);
00252      _spi.write(0x82);
00253      _spi.write(0x27);
00254      _spi.write(0x00);
00255      _cs = 1;
00256      
00257      wr_cmd(0x11);                     // sleep out    //copiado para 11-2 
00258      _cs = 1;
00259      
00260      wait_ms(100);
00261      
00262      wr_cmd(0x29);                     // display on    //copiado para 11-2 (igual)
00263      _cs = 1;
00264      
00265      wait_ms(100);
00266      
00267  }
00268 
00269 
00270 void SPI_TFT_ILI9341::pixel(int x, int y, int color)
00271 {
00272     wr_cmd(0x2A);
00273     _spi.write(x >> 8);
00274     _spi.write(x);
00275     _cs = 1;
00276     wr_cmd(0x2B);
00277     _spi.write(y >> 8);
00278     _spi.write(y);
00279     _cs = 1;
00280     wr_cmd(0x2C);  // send pixel
00281     #if defined TARGET_KL25Z  // 8 Bit SPI
00282     _spi.write(color >> 8);
00283     _spi.write(color & 0xff);
00284     #else 
00285     _spi.format(16,3);                            // switch to 16 bit Mode 3
00286     _spi.write(color);                              // Write D0..D15
00287     _spi.format(8,3);
00288     #endif
00289     _cs = 1;
00290 }
00291 
00292 
00293 void SPI_TFT_ILI9341::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h)
00294 {
00295     wr_cmd(0x2A);
00296     _spi.write(x >> 8);
00297     _spi.write(x);
00298     _spi.write((x+w-1) >> 8);
00299     _spi.write(x+w-1);
00300     
00301     _cs = 1;
00302     wr_cmd(0x2B);
00303     _spi.write(y >> 8);
00304     _spi.write(y);
00305     _spi.write((y+h-1) >> 8);
00306     _spi.write(y+h-1);
00307     _cs = 1;
00308 }
00309 
00310 
00311 void SPI_TFT_ILI9341::WindowMax (void)
00312 {
00313     window (0, 0, width(),  height());
00314 }
00315 
00316 
00317 
00318 void SPI_TFT_ILI9341::cls (void)
00319 {
00320     int pixel = ( width() * height());
00321     WindowMax();
00322     wr_cmd(0x2C);  // send pixel
00323     #if defined TARGET_KL25Z  // 8 Bit SPI
00324     unsigned int i;
00325     for (i = 0; i < ( width() * height()); i++){
00326         _spi.write(_background >> 8);
00327         _spi.write(_background & 0xff);
00328         }
00329     
00330     #else 
00331     _spi.format(16,3);                            // switch to 16 bit Mode 3
00332     unsigned int i;
00333     for (i = 0; i < ( width() * height()); i++)
00334         _spi.write(_background);
00335     _spi.format(8,3);    
00336     #endif                         
00337     _cs = 1; 
00338 }
00339 
00340 
00341 void SPI_TFT_ILI9341::circle(int x0, int y0, int r, int color)
00342 {
00343 
00344     int x = -r, y = 0, err = 2-2*r, e2;
00345     do {
00346         pixel(x0-x, y0+y,color);
00347         pixel(x0+x, y0+y,color);
00348         pixel(x0+x, y0-y,color);
00349         pixel(x0-x, y0-y,color);
00350         e2 = err;
00351         if (e2 <= y) {
00352             err += ++y*2+1;
00353             if (-x == y && e2 <= x) e2 = 0;
00354         }
00355         if (e2 > x) err += ++x*2+1;
00356     } while (x <= 0);
00357 
00358 }
00359 
00360 void SPI_TFT_ILI9341::fillcircle(int x0, int y0, int r, int color)
00361 {
00362     int x = -r, y = 0, err = 2-2*r, e2;
00363     do {
00364         vline(x0-x, y0-y, y0+y, color);
00365         vline(x0+x, y0-y, y0+y, color);
00366         e2 = err;
00367         if (e2 <= y) {
00368             err += ++y*2+1;
00369             if (-x == y && e2 <= x) e2 = 0;
00370         }
00371         if (e2 > x) err += ++x*2+1;
00372     } while (x <= 0);
00373 }
00374 
00375 
00376 void SPI_TFT_ILI9341::hline(int x0, int x1, int y, int color)
00377 {
00378     int w;
00379     w = x1 - x0 + 1;
00380     window(x0,y,w,1);
00381     wr_cmd(0x2C);  // send pixel
00382     #if defined TARGET_KL25Z  // 8 Bit SPI
00383     int j;
00384     for (j=0; j<w; j++) {
00385         _spi.write(color >> 8);
00386         _spi.write(color & 0xff);
00387     } 
00388     #else 
00389     _spi.format(16,3);                            // switch to 16 bit Mode 3
00390     int j;
00391     for (j=0; j<w; j++) {
00392         _spi.write(color);
00393     }
00394     _spi.format(8,3);
00395     #endif
00396     _cs = 1;
00397     WindowMax();
00398     return;
00399 }
00400 
00401 void SPI_TFT_ILI9341::vline(int x, int y0, int y1, int color)
00402 {
00403     int h;
00404     h = y1 - y0 + 1;
00405     window(x,y0,1,h);
00406     wr_cmd(0x2C);  // send pixel
00407     #if defined TARGET_KL25Z  // 8 Bit SPI
00408     for (int y=0; y<h; y++) {
00409         _spi.write(color >> 8);
00410         _spi.write(color & 0xff);
00411     } 
00412     #else 
00413     _spi.format(16,3);                            // switch to 16 bit Mode 3
00414     for (int y=0; y<h; y++) {
00415         _spi.write(color);
00416     }
00417     _spi.format(8,3);
00418     #endif
00419     _cs = 1;
00420     WindowMax();
00421     return;
00422 }
00423 
00424 
00425 
00426 void SPI_TFT_ILI9341::line(int x0, int y0, int x1, int y1, int color)
00427 {
00428     //WindowMax();
00429     int   dx = 0, dy = 0;
00430     int   dx_sym = 0, dy_sym = 0;
00431     int   dx_x2 = 0, dy_x2 = 0;
00432     int   di = 0;
00433 
00434     dx = x1-x0;
00435     dy = y1-y0;
00436 
00437     if (dx == 0) {        /* vertical line */
00438         if (y1 > y0) vline(x0,y0,y1,color);
00439         else vline(x0,y1,y0,color);
00440         return;
00441     }
00442 
00443     if (dx > 0) {
00444         dx_sym = 1;
00445     } else {
00446         dx_sym = -1;
00447     }
00448     if (dy == 0) {        /* horizontal line */
00449         if (x1 > x0) hline(x0,x1,y0,color);
00450         else  hline(x1,x0,y0,color);
00451         return;
00452     }
00453 
00454     if (dy > 0) {
00455         dy_sym = 1;
00456     } else {
00457         dy_sym = -1;
00458     }
00459 
00460     dx = dx_sym*dx;
00461     dy = dy_sym*dy;
00462 
00463     dx_x2 = dx*2;
00464     dy_x2 = dy*2;
00465 
00466     if (dx >= dy) {
00467         di = dy_x2 - dx;
00468         while (x0 != x1) {
00469 
00470             pixel(x0, y0, color);
00471             x0 += dx_sym;
00472             if (di<0) {
00473                 di += dy_x2;
00474             } else {
00475                 di += dy_x2 - dx_x2;
00476                 y0 += dy_sym;
00477             }
00478         }
00479         pixel(x0, y0, color);
00480     } else {
00481         di = dx_x2 - dy;
00482         while (y0 != y1) {
00483             pixel(x0, y0, color);
00484             y0 += dy_sym;
00485             if (di < 0) {
00486                 di += dx_x2;
00487             } else {
00488                 di += dx_x2 - dy_x2;
00489                 x0 += dx_sym;
00490             }
00491         }
00492         pixel(x0, y0, color);
00493     }
00494     return;
00495 }
00496 
00497 
00498 void SPI_TFT_ILI9341::rect(int x0, int y0, int x1, int y1, int color)
00499 {
00500 
00501     if (x1 > x0) hline(x0,x1,y0,color);
00502     else  hline(x1,x0,y0,color);
00503 
00504     if (y1 > y0) vline(x0,y0,y1,color);
00505     else vline(x0,y1,y0,color);
00506 
00507     if (x1 > x0) hline(x0,x1,y1,color);
00508     else  hline(x1,x0,y1,color);
00509 
00510     if (y1 > y0) vline(x1,y0,y1,color);
00511     else vline(x1,y1,y0,color);
00512 
00513     return;
00514 }
00515 
00516 
00517 
00518 void SPI_TFT_ILI9341::fillrect(int x0, int y0, int x1, int y1, int color)
00519 {
00520 
00521     int h = y1 - y0 + 1;
00522     int w = x1 - x0 + 1;
00523     int pixel = h * w;
00524     window(x0,y0,w,h);
00525     wr_cmd(0x2C);  // send pixel 
00526     #if defined TARGET_KL25Z  // 8 Bit SPI
00527     for (int p=0; p<pixel; p++) {
00528         _spi.write(color >> 8);
00529         _spi.write(color & 0xff);
00530     }
00531    #else
00532     _spi.format(16,3);                            // switch to 16 bit Mode 3
00533     for (int p=0; p<pixel; p++) {
00534         _spi.write(color);
00535     }
00536     _spi.format(8,3);
00537     #endif
00538     _cs = 1;
00539     WindowMax();
00540     return;
00541 }
00542 
00543 
00544 void SPI_TFT_ILI9341::locate(int x, int y)
00545 {
00546     char_x = x;
00547     char_y = y;
00548 }
00549 
00550 
00551 
00552 int SPI_TFT_ILI9341::columns()
00553 {
00554     return width() / font[1];
00555 }
00556 
00557 
00558 
00559 int SPI_TFT_ILI9341::rows()
00560 {
00561     return height() / font[2];
00562 }
00563 
00564 
00565 
00566 int SPI_TFT_ILI9341::_putc(int value)
00567 {
00568     if (value == '\n') {    // new line
00569         char_x = 0;
00570         char_y = char_y + font[2];
00571         if (char_y >= height() - font[2]) {
00572             char_y = 0;
00573         }
00574     } else {
00575         character(char_x, char_y, value);
00576     }
00577     return value;
00578 }
00579 
00580 
00581 void SPI_TFT_ILI9341::character(int x, int y, int c)
00582 {
00583     unsigned int hor,vert,offset,bpl,j,i,b;
00584     unsigned char* zeichen;
00585     unsigned char z,w;
00586 
00587     if ((c < 31) || (c > 127)) return;   // test char range
00588 
00589     // read font parameter from start of array
00590     offset = font[0];                    // bytes / char
00591     hor = font[1];                       // get hor size of font
00592     vert = font[2];                      // get vert size of font
00593     bpl = font[3];                       // bytes per line
00594 
00595     if (char_x + hor > width()) {
00596         char_x = 0;
00597         char_y = char_y + vert;
00598         if (char_y >= height() - font[2]) {
00599             char_y = 0;
00600         }
00601     }
00602     window(char_x, char_y,hor,vert); // char box
00603     wr_cmd(0x2C);  // send pixel
00604     #ifndef TARGET_KL25Z  // 16 Bit SPI 
00605     _spi.format(16,3);   
00606     #endif                         // switch to 16 bit Mode 3
00607     zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap
00608     w = zeichen[0];                          // width of actual char
00609      for (j=0; j<vert; j++) {  //  vert line
00610         for (i=0; i<hor; i++) {   //  horz line
00611             z =  zeichen[bpl * i + ((j & 0xF8) >> 3)+1];
00612             b = 1 << (j & 0x07);
00613             if (( z & b ) == 0x00) {
00614                #ifndef TARGET_KL25Z  // 16 Bit SPI 
00615                 _spi.write(_background);
00616                #else
00617                 _spi.write(_background >> 8);
00618                 _spi.write(_background & 0xff);
00619                 #endif
00620             } else {
00621                 #ifndef TARGET_KL25Z  // 16 Bit SPI
00622                 _spi.write(_foreground);
00623                 #else
00624                 _spi.write(_foreground >> 8);
00625                 _spi.write(_foreground & 0xff);
00626                 #endif
00627             }
00628         }
00629     }
00630     _cs = 1;
00631     #ifndef TARGET_KL25Z  // 16 Bit SPI
00632     _spi.format(8,3);
00633     #endif
00634     WindowMax();
00635     if ((w + 2) < hor) {                   // x offset to next char
00636         char_x += w + 2;
00637     } else char_x += hor;
00638 }
00639 
00640 
00641 void SPI_TFT_ILI9341::set_font(unsigned char* f)
00642 {
00643     font = f;
00644 }
00645 
00646 
00647 
00648 void SPI_TFT_ILI9341::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap)
00649 {
00650     unsigned int  j;
00651     int padd;
00652     unsigned short *bitmap_ptr = (unsigned short *)bitmap;
00653     #if defined TARGET_KL25Z  // 8 Bit SPI
00654         unsigned short pix_temp;
00655     #endif
00656     
00657     unsigned int i;
00658     
00659     // the lines are padded to multiple of 4 bytes in a bitmap
00660     padd = -1;
00661     do {
00662         padd ++;
00663     } while (2*(w + padd)%4 != 0);
00664     window(x, y, w, h);
00665     bitmap_ptr += ((h - 1)* (w + padd));
00666     wr_cmd(0x2C);  // send pixel
00667     #ifndef TARGET_KL25Z  // 16 Bit SPI 
00668     _spi.format(16,3);
00669     #endif                            // switch to 16 bit Mode 3
00670     for (j = 0; j < h; j++) {         //Lines
00671         for (i = 0; i < w; i++) {     // one line
00672             #if defined TARGET_KL25Z  // 8 Bit SPI
00673                 pix_temp = *bitmap_ptr;
00674                 _spi.write(pix_temp >> 8);
00675                 _spi.write(pix_temp);
00676                 bitmap_ptr++;
00677             #else
00678                 _spi.write(*bitmap_ptr);    // one line
00679                 bitmap_ptr++;
00680             #endif
00681         }
00682         bitmap_ptr -= 2*w;
00683         bitmap_ptr -= padd;
00684     }
00685     _cs = 1;
00686     #ifndef TARGET_KL25Z  // 16 Bit SPI 
00687     _spi.format(8,3);
00688     #endif
00689     WindowMax();
00690 }
00691 
00692 
00693 // local filesystem is not implemented in kinetis board
00694 #if DEVICE_LOCALFILESYSTEM
00695 
00696 int SPI_TFT_ILI9341::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP)
00697 {
00698 
00699 #define OffsetPixelWidth    18
00700 #define OffsetPixelHeigh    22
00701 #define OffsetFileSize      34
00702 #define OffsetPixData       10
00703 #define OffsetBPP           28
00704 
00705     char filename[50];
00706     unsigned char BMP_Header[54];
00707     unsigned short BPP_t;
00708     unsigned int PixelWidth,PixelHeigh,start_data;
00709     unsigned int    i,off;
00710     int padd,j;
00711     unsigned short *line;
00712 
00713     // get the filename
00714     LocalFileSystem local("local");
00715     sprintf(&filename[0],"/local/");
00716     i=7;
00717     while (*Name_BMP!='\0') {
00718         filename[i++]=*Name_BMP++;
00719     }
00720 
00721     fprintf(stderr, "filename : %s \n\r",filename);
00722 
00723     FILE *Image = fopen((const char *)&filename[0], "rb");  // open the bmp file
00724     if (!Image) {
00725         return(0);      // error file not found !
00726     }
00727 
00728     fread(&BMP_Header[0],1,54,Image);      // get the BMP Header
00729 
00730     if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) {  // check magic byte
00731         fclose(Image);
00732         return(-1);     // error no BMP file
00733     }
00734 
00735     BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8);
00736     if (BPP_t != 0x0010) {
00737         fclose(Image);
00738         return(-2);     // error no 16 bit BMP
00739     }
00740 
00741     PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24);
00742     PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24);
00743     if (PixelHeigh > height() + y || PixelWidth > width() + x) {
00744         fclose(Image);
00745         return(-3);      // to big
00746     }
00747 
00748     start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24);
00749 
00750     line = (unsigned short *) malloc (2 * PixelWidth); // we need a buffer for a line
00751     if (line == NULL) {
00752         return(-4);         // error no memory
00753     }
00754 
00755     // the bmp lines are padded to multiple of 4 bytes
00756     padd = -1;
00757     do {
00758         padd ++;
00759     } while ((PixelWidth * 2 + padd)%4 != 0);
00760 
00761 
00762 //fseek(Image, 70 ,SEEK_SET);
00763     window(x, y,PixelWidth ,PixelHeigh);
00764     wr_cmd(0x2C);  // send pixel 
00765     _spi.format(16,3);                            // switch to 16 bit Mode 3
00766     for (j = PixelHeigh - 1; j >= 0; j--) {               //Lines bottom up
00767         off = j * (PixelWidth  * 2 + padd) + start_data;   // start of line
00768         fseek(Image, off ,SEEK_SET);
00769         fread(line,1,PixelWidth * 2,Image);       // read a line - slow !
00770         for (i = 0; i < PixelWidth; i++) {        // copy pixel data to TFT
00771             _spi.write(line[i]);                  // one 16 bit pixel
00772         } 
00773      }
00774     _cs = 1;
00775     _spi.format(8,3);
00776     free (line);
00777     fclose(Image);
00778     WindowMax();
00779     return(1);
00780 }
00781 #endif