Library to control a QVGA TFT connected to SPI. You can use printf to print text The lib can handle different fonts, draw lines, circles, rect and bmp

Revision:
5:2db1b8070d94
Parent:
4:e1e45f8a7664
Child:
6:fc33e4a5713e
--- a/SPI_TFT.cpp	Thu Jul 14 20:13:07 2011 +0000
+++ b/SPI_TFT.cpp	Sat Jul 30 22:15:35 2011 +0000
@@ -16,7 +16,7 @@
 
 #define BPP         16                  // Bits per pixel                
 
-
+DigitalOut led(LED1);
 
 SPI_TFT::SPI_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, const char *name)
         : _spi(mosi, miso, sclk), _cs(cs), _reset(reset),GraphicsDisplay(name) {
@@ -381,9 +381,9 @@
     return;
 }
 
-void SPI_TFT::fillcircle(int x, int y, int r, int color){
+void SPI_TFT::fillcircle(int x, int y, int r, int color) {
     int i;
-    for (i = 0;i <= r; i++)
+    for (i = 0; i <= r; i++)
         circle(x,y,i,color);
 }
 
@@ -648,4 +648,85 @@
     }
     _spi.format(8,3);
     wr_dat_stop();
+}
+
+
+int SPI_TFT::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP) {
+
+#define OffsetPixelWidth    18
+#define OffsetPixelHeigh    22
+#define OffsetFileSize      34
+#define OffsetPixData       10
+#define OffsetBPP           28
+
+    char filename[50];
+    unsigned char BMP_Header[54];
+    unsigned short BPP_t;
+    unsigned int PixelWidth,PixelHeigh,start_data;
+    unsigned int    i,off;
+    int padd,j;
+    unsigned short *line;
+
+    // get the filename
+    LocalFileSystem local("local");
+    sprintf(&filename[0],"/local/");
+    i=7;
+    while (*Name_BMP!='\0') {
+        filename[i++]=*Name_BMP++;
+    }
+    FILE *Image = fopen((const char *)&filename[0], "r");  // open the bmp file
+    if (!Image) {
+        return(0);      // error file not found !
+    }
+
+    fread(&BMP_Header[0],1,54,Image);      // get the BMP Header
+
+    if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) {  // check magic byte
+        fclose(Image);
+        return(-1);     // error no BMP file
+    }
+
+    BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8);
+    if (BPP_t != 0x0010) {
+        fclose(Image);
+        return(-2);     // error no 16 bit BMP
+    }
+
+    PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24);
+    PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24);
+    if (PixelHeigh > height() + y || PixelWidth > width() + x) {
+        fclose(Image);
+        return(-3);      // to big
+    }
+
+    start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24);
+
+    line = (unsigned short *) malloc (PixelWidth); // we need a buffer for a line
+    if (line == NULL) {
+        return(-4);         // error no memory
+    }
+
+    // the lines are padded to multiple of 4 bytes
+    padd = -1;
+    do {
+        padd ++;
+    } while ((PixelWidth * 2 + padd)%4 != 0);
+
+    window(x, y,PixelWidth,PixelHeigh);
+    wr_cmd(0x22);
+    wr_dat_start();
+    _spi.format(16,3);    
+    for (j = PixelHeigh - 1; j >= 0; j--) {               //Lines bottom up
+        off = j * (PixelWidth * 2 + padd) + start_data;   // start of line
+        fseek(Image, off ,SEEK_SET);
+        fread(line,1,PixelWidth * 2,Image);       // read a line - slow !
+        for (i = 0; i < PixelWidth; i++) {        // copy pixel data to TFT
+            _spi.write(line[i]);                  // one 16 bit pixel
+        } 
+    }
+    _spi.format(8,3);
+    wr_dat_stop();
+    free (line);
+    fclose(Image);
+    return(1);
 }
\ No newline at end of file