Jacob Johnson / Adafruit_ST7735

Fork of Adafruit_ST7735 by justin kim

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adafruit_ST7735.cpp Source File

Adafruit_ST7735.cpp

00001 /***************************************************
00002   This is a library for the Adafruit 1.8" SPI display.
00003   This library works with the Adafruit 1.8" TFT Breakout w/SD card
00004   ----> http://www.adafruit.com/products/358
00005   as well as Adafruit raw 1.8" TFT display
00006   ----> http://www.adafruit.com/products/618
00007 
00008   Check out the links above for our tutorials and wiring diagrams
00009   These displays use SPI to communicate, 4 or 5 pins are required to
00010   interface (RST is optional)
00011   Adafruit invests time and resources providing this open source code,
00012   please support Adafruit and open-source hardware by purchasing
00013   products from Adafruit!
00014 
00015   Written by Limor Fried/Ladyada for Adafruit Industries.
00016   MIT license, all text above must be included in any redistribution
00017  ****************************************************/
00018 
00019 #include "mbed.h"
00020 #include "Adafruit_ST7735.h"
00021 
00022 //#include <string>
00023 
00024 
00025 
00026 // Constructor 
00027 Adafruit_ST7735::Adafruit_ST7735(PinName mosi, PinName miso, PinName sck, PinName cs, PinName rs, PinName rst) 
00028         : lcdPort(mosi, miso, sck), _cs(cs), _rs(rs), _rst(rst), Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT) 
00029 { }
00030 
00031 Serial pc(USBTX, USBRX);
00032 
00033 void Adafruit_ST7735::writecommand(uint8_t c)
00034 {
00035     _rs = 0;
00036     _cs = 0;
00037     lcdPort.write( c );
00038     _cs = 1;
00039 }
00040 
00041 
00042 void Adafruit_ST7735::writedata(uint8_t c)
00043 {
00044     _rs = 1;
00045     _cs = 0;
00046     lcdPort.write( c );
00047 
00048     _cs = 1;
00049 }
00050 
00051 
00052 // Rather than a bazillion writecommand() and writedata() calls, screen
00053 // initialization commands and arguments are organized in these tables
00054 // stored in PROGMEM.  The table may look bulky, but that's mostly the
00055 // formatting -- storage-wise this is hundreds of bytes more compact
00056 // than the equivalent code.  Companion function follows.
00057 #define DELAY 0x80
00058 static unsigned char
00059 Bcmd[] = {                  // Initialization commands for 7735B screens
00060     18,                       // 18 commands in list:
00061     ST7735_SWRESET,   DELAY,  //  1: Software reset, no args, w/delay
00062     50,                     //     50 ms delay
00063     ST7735_SLPOUT ,   DELAY,  //  2: Out of sleep mode, no args, w/delay
00064     255,                    //     255 = 500 ms delay
00065     ST7735_COLMOD , 1+DELAY,  //  3: Set color mode, 1 arg + delay:
00066     0x05,                   //     16-bit color
00067     10,                     //     10 ms delay
00068     ST7735_FRMCTR1, 3+DELAY,  //  4: Frame rate control, 3 args + delay:
00069     0x00,                   //     fastest refresh
00070     0x06,                   //     6 lines front porch
00071     0x03,                   //     3 lines back porch
00072     10,                     //     10 ms delay
00073     ST7735_MADCTL , 1      ,  //  5: Memory access ctrl (directions), 1 arg:
00074     0x08,                   //     Row addr/col addr, bottom to top refresh
00075     ST7735_DISSET5, 2      ,  //  6: Display settings #5, 2 args, no delay:
00076     0x15,                   //     1 clk cycle nonoverlap, 2 cycle gate
00077     //     rise, 3 cycle osc equalize
00078     0x02,                   //     Fix on VTL
00079     ST7735_INVCTR , 1      ,  //  7: Display inversion control, 1 arg:
00080     0x0,                    //     Line inversion
00081     ST7735_PWCTR1 , 2+DELAY,  //  8: Power control, 2 args + delay:
00082     0x02,                   //     GVDD = 4.7V
00083     0x70,                   //     1.0uA
00084     10,                     //     10 ms delay
00085     ST7735_PWCTR2 , 1      ,  //  9: Power control, 1 arg, no delay:
00086     0x05,                   //     VGH = 14.7V, VGL = -7.35V
00087     ST7735_PWCTR3 , 2      ,  // 10: Power control, 2 args, no delay:
00088     0x01,                   //     Opamp current small
00089     0x02,                   //     Boost frequency
00090     ST7735_VMCTR1 , 2+DELAY,  // 11: Power control, 2 args + delay:
00091     0x3C,                   //     VCOMH = 4V
00092     0x38,                   //     VCOML = -1.1V
00093     10,                     //     10 ms delay
00094     ST7735_PWCTR6 , 2      ,  // 12: Power control, 2 args, no delay:
00095     0x11, 0x15,
00096     ST7735_GMCTRP1,16      ,  // 13: Magical unicorn dust, 16 args, no delay:
00097     0x09, 0x16, 0x09, 0x20, //     (seriously though, not sure what
00098     0x21, 0x1B, 0x13, 0x19, //      these config values represent)
00099     0x17, 0x15, 0x1E, 0x2B,
00100     0x04, 0x05, 0x02, 0x0E,
00101     ST7735_GMCTRN1,16+DELAY,  // 14: Sparkles and rainbows, 16 args + delay:
00102     0x0B, 0x14, 0x08, 0x1E, //     (ditto)
00103     0x22, 0x1D, 0x18, 0x1E,
00104     0x1B, 0x1A, 0x24, 0x2B,
00105     0x06, 0x06, 0x02, 0x0F,
00106     10,                     //     10 ms delay
00107     ST7735_CASET  , 4      ,  // 15: Column addr set, 4 args, no delay:
00108     0x00, 0x02,             //     XSTART = 2
00109     0x00, 0x81,             //     XEND = 129
00110     ST7735_RASET  , 4      ,  // 16: Row addr set, 4 args, no delay:
00111     0x00, 0x02,             //     XSTART = 1
00112     0x00, 0x81,             //     XEND = 160
00113     ST7735_NORON  ,   DELAY,  // 17: Normal display on, no args, w/delay
00114     10,                     //     10 ms delay
00115     ST7735_DISPON ,   DELAY,  // 18: Main screen turn on, no args, w/delay
00116     255
00117 },                  //     255 = 500 ms delay
00118 
00119 Rcmd1[] = {                 // Init for 7735R, part 1 (red or green tab)
00120     15,                       // 16 commands in list:
00121     ST7735_SWRESET,   DELAY,  //  1: Software reset, 0 args, w/delay
00122     150,                    //     150 ms delay
00123     ST7735_SLPOUT ,   DELAY,  //  2: Out of sleep mode, 0 args, w/delay
00124     255,                    //     500 ms delay
00125     ST7735_FRMCTR1, 3      ,  //  3: Frame rate ctrl - normal mode, 3 args:
00126     0x01, 0x2C, 0x2D,       //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
00127     ST7735_FRMCTR2, 3      ,  //  4: Frame rate control - idle mode, 3 args:
00128     0x01, 0x2C, 0x2D,       //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
00129     ST7735_FRMCTR3, 6      ,  //  5: Frame rate ctrl - partial mode, 6 args:
00130     0x01, 0x2C, 0x2D,       //     Dot inversion mode
00131     0x01, 0x2C, 0x2D,       //     Line inversion mode
00132     ST7735_INVCTR , 1      ,  //  6: Display inversion ctrl, 1 arg, no delay:
00133     0x07,                   //     No inversion
00134     ST7735_PWCTR1 , 3      ,  //  7: Power control, 3 args, no delay:
00135     0xA2,
00136     0x02,                   //     -4.6V
00137     0x84,                   //     AUTO mode
00138     ST7735_PWCTR2 , 1      ,  //  8: Power control, 1 arg, no delay:
00139     0xC5,                   //     VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
00140     ST7735_PWCTR3 , 2      ,  //  9: Power control, 2 args, no delay:
00141     0x0A,                   //     Opamp current small
00142     0x00,                   //     Boost frequency
00143     ST7735_PWCTR4 , 2      ,  // 10: Power control, 2 args, no delay:
00144     0x8A,                   //     BCLK/2, Opamp current small & Medium low
00145     0x2A,
00146     ST7735_PWCTR5 , 2      ,  // 11: Power control, 2 args, no delay:
00147     0x8A, 0xEE,
00148     ST7735_VMCTR1 , 1      ,  // 12: Power control, 1 arg, no delay:
00149     0x0E,
00150     ST7735_INVOFF , 0      ,  // 13: Don't invert display, no args, no delay
00151     ST7735_MADCTL , 1      ,  // 14: Memory access control (directions), 1 arg:
00152     0xC0,                   //     row addr/col addr, bottom to top refresh
00153     ST7735_COLMOD , 1      ,  // 15: set color mode, 1 arg, no delay:
00154     0x05
00155 },                 //     16-bit color
00156 
00157 Rcmd2green[] = {            // Init for 7735R, part 2 (green tab only)
00158     2,                        //  2 commands in list:
00159     ST7735_CASET  , 4      ,  //  1: Column addr set, 4 args, no delay:
00160     0x00, 0x02,             //     XSTART = 0
00161     0x00, 0x7F+0x02,        //     XEND = 127
00162     ST7735_RASET  , 4      ,  //  2: Row addr set, 4 args, no delay:
00163     0x00, 0x01,             //     XSTART = 0
00164     0x00, 0x9F+0x01
00165 },      //     XEND = 159
00166 Rcmd2red[] = {              // Init for 7735R, part 2 (red tab only)
00167     2,                        //  2 commands in list:
00168     ST7735_CASET  , 4      ,  //  1: Column addr set, 4 args, no delay:
00169     0x00, 0x00,             //     XSTART = 0
00170     0x00, 0x7F,             //     XEND = 127
00171     ST7735_RASET  , 4      ,  //  2: Row addr set, 4 args, no delay:
00172     0x00, 0x00,             //     XSTART = 0
00173     0x00, 0x9F
00174 },           //     XEND = 159
00175 
00176 Rcmd3[] = {                 // Init for 7735R, part 3 (red or green tab)
00177     4,                        //  4 commands in list:
00178     ST7735_GMCTRP1, 16      , //  1: Magical unicorn dust, 16 args, no delay:
00179     0x02, 0x1c, 0x07, 0x12,
00180     0x37, 0x32, 0x29, 0x2d,
00181     0x29, 0x25, 0x2B, 0x39,
00182     0x00, 0x01, 0x03, 0x10,
00183     ST7735_GMCTRN1, 16      , //  2: Sparkles and rainbows, 16 args, no delay:
00184     0x03, 0x1d, 0x07, 0x06,
00185     0x2E, 0x2C, 0x29, 0x2D,
00186     0x2E, 0x2E, 0x37, 0x3F,
00187     0x00, 0x00, 0x02, 0x10,
00188     ST7735_NORON  ,    DELAY, //  3: Normal display on, no args, w/delay
00189     10,                     //     10 ms delay
00190     ST7735_DISPON ,    DELAY, //  4: Main screen turn on, no args w/delay
00191     100
00192 };                  //     100 ms delay
00193 
00194 
00195 // Companion code to the above tables.  Reads and issues
00196 // a series of LCD commands stored in byte array.
00197 void Adafruit_ST7735::commandList(uint8_t *addr)
00198 {
00199 
00200     uint8_t  numCommands, numArgs;
00201     uint16_t ms;
00202 
00203     numCommands = *addr++;   // Number of commands to follow
00204     while(numCommands--) {                 // For each command...
00205         writecommand(*addr++); //   Read, issue command
00206         numArgs  = *addr++;    //   Number of args to follow
00207         ms       = numArgs & DELAY;          //   If hibit set, delay follows args
00208         numArgs &= ~DELAY;                   //   Mask out delay bit
00209         while(numArgs--) {                   //   For each argument...
00210             writedata(*addr++);  //     Read, issue argument
00211         }
00212 
00213         if(ms) {
00214             ms = *addr++; // Read post-command delay time (ms)
00215             if(ms == 255) ms = 500;     // If 255, delay for 500 ms
00216             wait_ms(ms);
00217         }
00218     }
00219 }
00220 
00221 
00222 // Initialization code common to both 'B' and 'R' type displays
00223 void Adafruit_ST7735::commonInit(uint8_t *cmdList)
00224 {
00225 
00226     colstart  = rowstart = 0; // May be overridden in init func
00227 
00228     _rs = 1;
00229     _cs = 1;
00230 
00231     // use default SPI format
00232     lcdPort.format(8,0);
00233     lcdPort.frequency(4000000);     // Lets try 4MHz
00234 
00235     // toggle RST low to reset; CS low so it'll listen to us
00236     _cs = 0;
00237     _rst = 1;
00238     wait_ms(500);
00239     _rst = 0;
00240     wait_ms(500);
00241     _rst = 1;
00242     wait_ms(500);
00243 
00244     if(cmdList) commandList(cmdList);
00245 }
00246 
00247 
00248 // Initialization for ST7735B screens
00249 void Adafruit_ST7735::initB(void)
00250 {
00251     commonInit(Bcmd);
00252 }
00253 
00254 
00255 // Initialization for ST7735R screens (green or red tabs)
00256 void Adafruit_ST7735::initR(uint8_t options)
00257 {
00258     commonInit(Rcmd1);
00259     if(options == INITR_GREENTAB) {
00260         commandList(Rcmd2green);
00261         colstart = 2;
00262         rowstart = 1;
00263     } else {
00264         // colstart, rowstart left at default '0' values
00265         commandList(Rcmd2red);
00266     }
00267     commandList(Rcmd3);
00268 }
00269 
00270 
00271 void Adafruit_ST7735::setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1,
00272                                     uint8_t y1)
00273 {
00274 
00275     writecommand(ST7735_CASET); // Column addr set
00276     writedata(0x00);
00277     writedata(x0+colstart);     // XSTART
00278     writedata(0x00);
00279     writedata(x1+colstart);     // XEND
00280 
00281     writecommand(ST7735_RASET); // Row addr set
00282     writedata(0x00);
00283     writedata(y0+rowstart);     // YSTART
00284     writedata(0x00);
00285     writedata(y1+rowstart);     // YEND
00286 
00287     writecommand(ST7735_RAMWR); // write to RAM
00288 }
00289 
00290 
00291 void Adafruit_ST7735::fillScreen(uint16_t color)
00292 {
00293 
00294     uint8_t x, y, hi = color >> 8, lo = color;
00295 
00296     setAddrWindow(0, 0, _width-1, _height-1);
00297 
00298     _rs = 1;
00299     _cs = 0;
00300 
00301     for(y=_height; y>0; y--) {
00302         for(x=_width; x>0; x--) {
00303             lcdPort.write( hi );
00304             lcdPort.write( lo );
00305         }
00306     }
00307 
00308     _cs = 1;
00309 }
00310 
00311 
00312 void Adafruit_ST7735::pushColor(uint16_t color)
00313 {
00314     _rs = 1;
00315     _cs = 0;
00316 
00317     lcdPort.write( color >> 8 );
00318     lcdPort.write( color );
00319     _cs = 1;
00320 }
00321 
00322 
00323 void Adafruit_ST7735::drawPixel(int16_t x, int16_t y, uint16_t color)
00324 {
00325 
00326     if((x < 0) ||(x >= _width) || (y < 0) || (y >= _height)) return;
00327 
00328     setAddrWindow(x,y,x+1,y+1);
00329 
00330     _rs = 1;
00331     _cs = 0;
00332     
00333     lcdPort.write( color >> 8 );
00334     lcdPort.write( color );
00335 
00336     _cs = 1;
00337 }
00338 
00339 
00340 void Adafruit_ST7735::drawFastVLine(int16_t x, int16_t y, int16_t h,
00341                                     uint16_t color)
00342 {
00343 
00344     // Rudimentary clipping
00345     if((x >= _width) || (y >= _height)) return;
00346     if((y+h-1) >= _height) h = _height-y;
00347     setAddrWindow(x, y, x, y+h-1);
00348 
00349     uint8_t hi = color >> 8, lo = color;
00350     _rs = 1;
00351     _cs = 0;
00352     while (h--) {
00353         lcdPort.write( hi );
00354         lcdPort.write( lo );
00355     }
00356     _cs = 1;
00357 }
00358 
00359 
00360 void Adafruit_ST7735::drawFastHLine(int16_t x, int16_t y, int16_t w,
00361                                     uint16_t color)
00362 {
00363 
00364     // Rudimentary clipping
00365     if((x >= _width) || (y >= _height)) return;
00366     if((x+w-1) >= _width)  w = _width-x;
00367     setAddrWindow(x, y, x+w-1, y);
00368 
00369     uint8_t hi = color >> 8, lo = color;
00370     _rs = 1;
00371     _cs = 0;
00372     while (w--) {
00373         lcdPort.write( hi );
00374         lcdPort.write( lo );
00375     }
00376     _cs = 1;
00377 }
00378 
00379 
00380 // fill a rectangle
00381 void Adafruit_ST7735::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
00382                                uint16_t color)
00383 {
00384 
00385     // rudimentary clipping (drawChar w/big text requires this)
00386     if((x >= _width) || (y >= _height)) return;
00387     if((x + w - 1) >= _width)  w = _width  - x;
00388     if((y + h - 1) >= _height) h = _height - y;
00389 
00390     setAddrWindow(x, y, x+w-1, y+h-1);
00391 
00392     uint8_t hi = color >> 8, lo = color;
00393     _rs = 1;
00394     _cs = 0;
00395     for(y=h; y>0; y--) {
00396         for(x=w; x>0; x--) {
00397             lcdPort.write( hi );
00398             lcdPort.write( lo );
00399         }
00400     }
00401 
00402     _cs = 1;
00403 }
00404 
00405 
00406 // Pass 8-bit (each) R,G,B, get back 16-bit packed color
00407 uint16_t Adafruit_ST7735::Color565(uint8_t r, uint8_t g, uint8_t b)
00408 {
00409     return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
00410 }
00411 
00412 
00413 #define MADCTL_MY  0x80
00414 #define MADCTL_MX  0x40
00415 #define MADCTL_MV  0x20
00416 #define MADCTL_ML  0x10
00417 #define MADCTL_RGB 0x00
00418 #define MADCTL_MH  0x04
00419 
00420 void Adafruit_ST7735::setRotation(uint8_t m)
00421 {
00422 
00423     writecommand(ST7735_MADCTL);
00424     rotation = m % 4; // can't be higher than 3
00425     switch (rotation) {
00426         case 0:
00427             writedata(MADCTL_MX | MADCTL_MY | MADCTL_RGB);
00428             _width  = ST7735_TFTWIDTH;
00429             _height = ST7735_TFTHEIGHT;
00430             break;
00431         case 1:
00432             writedata(MADCTL_MY | MADCTL_MV | MADCTL_RGB);
00433             _width  = ST7735_TFTHEIGHT;
00434             _height = ST7735_TFTWIDTH;
00435             break;
00436         case 2:
00437             writedata(MADCTL_RGB);
00438             _width  = ST7735_TFTWIDTH;
00439             _height = ST7735_TFTHEIGHT;
00440             break;
00441         case 3:
00442             writedata(MADCTL_MX | MADCTL_MV | MADCTL_RGB);
00443             _width  = ST7735_TFTHEIGHT;
00444             _height = ST7735_TFTWIDTH;
00445             break;
00446     }
00447 }
00448 
00449 
00450 void Adafruit_ST7735::invertDisplay(boolean i)
00451 {
00452     writecommand(i ? ST7735_INVON : ST7735_INVOFF);
00453 }
00454 
00455 
00456 int Adafruit_ST7735::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP)
00457 {
00458  
00459  
00460  
00461 #define OffsetPixelWidth    18
00462 #define OffsetPixelHeigh    22
00463 #define OffsetFileSize      34
00464 #define OffsetPixData       10
00465 #define OffsetBPP           28
00466     pc.printf("TEST START\r\n");
00467     char filename[50];
00468     unsigned char BMP_Header[54];
00469     unsigned short BPP_t;
00470     unsigned int PixelWidth,PixelHeigh,start_data;
00471     unsigned int    i,off;
00472     int padd,j;
00473     unsigned short *line;
00474  
00475     pc.printf("TEST START1\r\n");
00476     // get the filename
00477     i=0;
00478     while (*Name_BMP!='\0') {
00479         filename[i++]=*Name_BMP++;
00480     }
00481     pc.printf("TEST START2\r\n");
00482     filename[i] = 0;  
00483     
00484     pc.printf("TEST START3\r\n");
00485     FILE *Image = fopen((const char *)&filename[0], "rb");  // open the bmp file
00486     pc.printf("TEST FILEOPEN\r\n");
00487     if (!Image) {
00488         pc.printf("TEST error file not found");
00489         return(0);      // error file not found !
00490     }
00491  
00492     fread(&BMP_Header[0],1,54,Image);      // get the BMP Header
00493  
00494     if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) {  // check magic byte
00495         pc.printf("TEST error no BMP file");
00496         fclose(Image);
00497         return(-1);     // error no BMP file
00498     }
00499  
00500     BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8);
00501     if (BPP_t != 0x0010) {
00502         pc.printf("TEST error no 16 bit BMP\r\n");
00503         fclose(Image);
00504         return(-2);     // error no 16 bit BMP
00505     }
00506  
00507     PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24);
00508     PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24);
00509     if (PixelHeigh > _height + y || PixelWidth > _width + x) {
00510         pc.printf("TEST to big\r\n");
00511         fclose(Image);
00512         return(-3);      // to big
00513     }
00514  
00515     start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24);
00516  
00517     line = (unsigned short *) malloc (2 * PixelHeigh); // we need a buffer for a line
00518     if (line == NULL) {
00519         pc.printf("TEST error no memory\r\n");
00520         return(-4);         // error no memory
00521     }
00522     pc.printf("TEST 506");
00523     // the bmp lines are padded to multiple of 4 bytes
00524     padd = -1;
00525     do {pc.printf("TEST padd : %d\r\n",padd);
00526         padd ++;
00527     } while ((PixelHeigh * 2 + padd)%4 != 0);
00528  
00529     setAddrWindow(x, y,PixelWidth ,PixelHeigh);
00530     writecommand(0x2C);  // send pixel
00531     
00532     pc.printf("TEST 518");
00533     for (j = PixelWidth - 1; j >= 0; j--) {               //Lines bottom up
00534         off = j * (PixelHeigh  * 2 + padd) + start_data;   // start of line
00535         fseek(Image, off ,SEEK_SET);
00536         fread(line,1,PixelHeigh * 2,Image);       // read a line - slow 
00537         for (i = 0; i < PixelHeigh; i++) {        // copy pixel data to TFT
00538             _rs = 1;
00539             _cs = 0;
00540             lcdPort.write(line[i]);
00541         }
00542      }
00543     _cs = 1;
00544     lcdPort.format(8,3);
00545     free (line);
00546     fclose(Image);
00547     setAddrWindow(0,0,_width,_height);
00548     pc.printf("TEST END");
00549     return(1);
00550 }
00551 
00552 #define RGB(r,g,b)  (((r&0xF8)<<8)|((g&0xFC)<<3)|((b&0xF8)>>3)) //5 red | 6 green | 5 blue
00553 #define TFT_DEBUG
00554 
00555 int Adafruit_ST7735::DrawBitmapFile(const char *Name_BMP)
00556 {
00557    
00558     char img[3*240];
00559     uint32_t imgsize = 0;
00560     uint32_t offset = 0;
00561     uint32_t imgw = 0;
00562     uint32_t imgh = 0;
00563     char colbits = 0;
00564     char compress = 0;
00565     uint16_t col;
00566 
00567     int i, j;
00568     
00569     char filename[50];
00570     
00571     pc.printf("TEST START1\r\n");
00572     // get the filename
00573     i=0;
00574     while (*Name_BMP!='\0') {
00575         filename[i++]=*Name_BMP++;
00576     }
00577     pc.printf("TEST START2\r\n");
00578     filename[i] = 0;  
00579     
00580     pc.printf("TEST START3\r\n");
00581     FILE *Image = fopen((const char *)&filename[0], "rb");  // open the bmp file
00582     
00583     
00584     if(Image == NULL) return -1;
00585     if(fgetc(Image) != 0x42) return -2;
00586     if(fgetc(Image) != 0x4D) return -2;
00587 
00588     for(i = 0; i < 4; i++)
00589     {
00590         imgsize += (((uint32_t)fgetc(Image)) << i*8);
00591     }
00592 #ifdef TFT_DEBUG
00593     pc.printf("BMP SIZE:%d\r\n",imgsize);
00594 #endif
00595     fseek(Image,4,SEEK_CUR);
00596     for(i = 0; i < 4; i++)
00597     {
00598         offset += (((uint32_t)fgetc(Image)) << i*8);
00599     }
00600 #ifdef TFT_DEBUG    
00601     pc.printf("BMP OFFSET:%d\r\n",offset);
00602 #endif
00603     fseek(Image,4,SEEK_CUR);
00604     for(i = 0; i < 4; i++)
00605     {
00606         imgw += (((uint32_t)fgetc(Image)) << i*8);
00607     }
00608     if(imgw > 240) return -3;
00609     
00610     for(i = 0; i < 4; i++)
00611     {
00612         imgh += (((uint32_t)fgetc(Image)) << i*8);
00613     }
00614     if(imgh > 320) return -3;
00615     
00616     fseek(Image,2,SEEK_CUR);
00617     colbits = fgetc(Image);
00618     //if(colbits != 16 || colbits != 24) return -4;
00619     fgetc(Image);
00620     if((compress=fgetc(Image)) != 0)
00621     {
00622     #ifdef TFT_DEBUG    
00623         pc.printf("Not supported compress : %d\r\n",compress);
00624     #endif
00625         return -4;    
00626     }
00627     
00628 
00629 #ifdef TFT_DEBUG    
00630     pc.printf("RESOL : %d col, %d X %d",colbits,imgw,imgh);
00631 #endif    
00632     
00633     fseek(Image, offset, SEEK_SET);
00634     for (j = imgh; j >= 0; j--)        //Lines
00635     {  
00636         fread(img,sizeof(char),imgw*3,Image);
00637         _cs = 1;
00638         setAddrWindow(0, j, imgw ,1);
00639         writecommand(0x2C);  // send pixel
00640         #ifdef TARGET_WIZWIKI_W7500
00641         lcdPort.format(16,3);
00642         #endif
00643 
00644         for(i = 0; i < imgw; i++)
00645         {
00646 /*            if(colbits == 16)
00647             {
00648                 col = (uint16_t)img[2*i+1];
00649                 col <<= 8;
00650                 col += (uint16_t)img[2*i];
00651             }
00652             else if(colbits == 24) */
00653             //{
00654                 col = RGB((uint16_t)img[3*i+2],(uint16_t)img[3*i+1], (uint16_t)img[3*i]);
00655             //}
00656         #ifdef TFT_DEBUG    
00657         /*
00658         
00659            pc.printf("RGB(%d): ",i);
00660            pc.printf("(%d,",img[3*i+2]);
00661            pc.printf("%d,", img[3*i+1]);
00662            pc.printf("%d),->", img[3*i]);
00663            pc.printf("%04x\r\n",col);
00664         */
00665            //pc.printf("RGB(%d): (%d,%d,%d) -> %04X\r\n ",i,img[3*i+2],img[3*i+1],img[3*i],col);
00666         #endif    
00667             _rs = 1;
00668             _cs = 0;
00669 
00670             lcdPort.write(col);
00671         }
00672         _cs = 1;
00673         #ifdef TARGET_WIZWIKI_W7500
00674         lcdPort.format(8,3);
00675         #endif
00676     }
00677     setAddrWindow(0,0,_width,_height);
00678     
00679     return 0;
00680 }
00681 
00682