Digital Photo Frame using FTP Client

Dependencies:   FTPClient SDFileSystem SeeedStudioTFTv2 TFT_fonts WIZnetInterface mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MySeeedStudioTFTv2.cpp Source File

MySeeedStudioTFTv2.cpp

00001 /* mbed library for resistive touch pads
00002  * uses 4 pins - 2 IO and 2 Analog
00003 
00004  * c 2011 Peter Drescher - DC2PD
00005  *
00006  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00007  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00008  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00009  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00010  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00011  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00012  * THE SOFTWARE.
00013  */
00014 
00015 
00016 #include "mbed.h"
00017 #include "MySeeedStudioTFTv2.h"
00018 
00019 #define TFT_DEBUG
00020 
00021 #ifdef TFT_DEBUG
00022 extern Serial uart;
00023 #endif
00024 
00025 
00026 MySeeedStudioTFTv2::MySeeedStudioTFTv2(PinName xp, PinName xm, PinName yp, PinName ym,
00027                      PinName mosi, PinName miso, PinName sclk,
00028                      PinName csTft, PinName dcTft, PinName blTft,
00029                      PinName csSd) : SeeedStudioTFTv2(xp,xm,yp,ym,mosi,miso,sclk,csTft,dcTft,blTft,csSd)
00030 {
00031     SeeedStudioTFTv2::SPI_TFT_ILI9341::_spi.frequency(18000000);
00032     SeeedStudioTFTv2::SDFileSystem::_spi.frequency(24000000);     
00033 }
00034 
00035 int MySeeedStudioTFTv2::DrawBitmapFile(FILE * fp)
00036 {
00037    
00038     char img[3*240];
00039     uint32_t imgsize = 0;
00040     uint32_t offset = 0;
00041     uint32_t imgw = 0;
00042     uint32_t imgh = 0;
00043     char colbits = 0;
00044     char compress = 0;
00045     uint16_t col;
00046 
00047     int i, j;
00048  
00049     if(fp == NULL) return -1;
00050     if(fgetc(fp) != 0x42) return -2;
00051     if(fgetc(fp) != 0x4D) return -2;
00052 
00053     for(i = 0; i < 4; i++)
00054     {
00055         imgsize += (((uint32_t)fgetc(fp)) << i*8);
00056     }
00057 #ifdef TFT_DEBUG
00058     uart.printf("BMP SIZE:%d\r\n",imgsize);
00059 #endif
00060     fseek(fp,4,SEEK_CUR);
00061     for(i = 0; i < 4; i++)
00062     {
00063         offset += (((uint32_t)fgetc(fp)) << i*8);
00064     }
00065 #ifdef TFT_DEBUG    
00066     uart.printf("BMP OFFSET:%d\r\n",offset);
00067 #endif
00068     fseek(fp,4,SEEK_CUR);
00069     for(i = 0; i < 4; i++)
00070     {
00071         imgw += (((uint32_t)fgetc(fp)) << i*8);
00072     }
00073     if(imgw > 240) return -3;
00074     
00075     for(i = 0; i < 4; i++)
00076     {
00077         imgh += (((uint32_t)fgetc(fp)) << i*8);
00078     }
00079     if(imgh > 320) return -3;
00080     
00081     fseek(fp,2,SEEK_CUR);
00082     colbits = fgetc(fp);
00083     //if(colbits != 16 || colbits != 24) return -4;
00084     fgetc(fp);
00085     if((compress=fgetc(fp)) != 0)
00086     {
00087     #ifdef TFT_DEBUG    
00088         uart.printf("Not supported compress : %d\r\n",compress);
00089     #endif
00090         return -4;    
00091     }
00092     
00093 
00094 #ifdef TFT_DEBUG    
00095     uart.printf("RESOL : %d col, %d X %d",colbits,imgw,imgh);
00096 #endif    
00097     
00098     fseek(fp, offset, SEEK_SET);
00099     for (j = imgh-1; j >= 0; j--)        //Lines
00100     {  
00101         fread(img,sizeof(char),imgw*3,fp);
00102         window(0, j, imgw, 1);    
00103         wr_cmd(0x2C);  // send pixel
00104     #ifndef TARGET_KL25Z  // 16 Bit SPI 
00105         SeeedStudioTFTv2::SPI_TFT_ILI9341::_spi.format(16,3);
00106     #endif                            // switch to 16 bit Mode 3
00107 
00108         for(i = 0; i < imgw; i++)
00109         {
00110 /*            if(colbits == 16)
00111             {
00112                 col = (uint16_t)img[2*i+1];
00113                 col <<= 8;
00114                 col += (uint16_t)img[2*i];
00115             }
00116             else if(colbits == 24) */
00117             {
00118                 col = RGB((uint16_t)img[3*i+2],(uint16_t)img[3*i+1], (uint16_t)img[3*i]);
00119             }
00120         #ifdef TFT_DEBUG    
00121             //uart.printf("RGB(%d): (%d,%d,%d) -> %04X\r\n ",i,img[3*i+2],img[3*i+1],img[3*i],col);
00122         #endif    
00123 
00124         #ifdef TAGET_KL25Z
00125             SeeedStudioTFTv2::SPI_TFT_ILI9341::_spi.write((char)(col>>8));
00126             SeeedStudioTFTv2::SPI_TFT_ILI9341::_spi.write((char)col);
00127         #else
00128            SeeedStudioTFTv2::SPI_TFT_ILI9341::_spi.write(col);
00129         #endif
00130         }
00131         SeeedStudioTFTv2::SPI_TFT_ILI9341::_cs = 1;
00132         #ifndef TARGET_KL25Z  // 16 Bit SPI 
00133             SeeedStudioTFTv2::SPI_TFT_ILI9341::_spi.format(8,3);
00134         #endif
00135  
00136     }
00137     WindowMax();
00138     
00139     return 0;
00140 }