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, committed 2011-07-30
- Comitter:
- dreschpe
- Date:
- Sat Jul 30 22:15:35 2011 +0000
- Parent:
- 4:e1e45f8a7664
- Child:
- 6:fc33e4a5713e
- Commit message:
- 0.2
Changed in this revision
SPI_TFT.cpp | Show annotated file Show diff for this revision Revisions of this file |
SPI_TFT.h | Show annotated file Show diff for this revision Revisions of this file |
--- 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
--- a/SPI_TFT.h Thu Jul 14 20:13:07 2011 +0000 +++ b/SPI_TFT.h Sat Jul 30 22:15:35 2011 +0000 @@ -225,6 +225,28 @@ */ void Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap); + + /** paint a 16 bit BMP from local filesytem on the TFT (slow) + * + * @param x,y : upper left corner + * @param *Name_BMP name of the BMP file + * @returns 1 if bmp file was found and painted + * @returns -1 if bmp file was found not found + * @returns -2 if bmp file is not 16bit + * @returns -3 if bmp file is to big for screen + * @returns -4 if buffer malloc go wrong + * + * bitmap format: 16 bit R5 G6 B5 + * + * use Gimp to create / load , save as BMP, option 16 bit R5 G6 B5 + * copy to internal file system + * + */ + + int BMP_16(unsigned int x, unsigned int y, const char *Name_BMP); + + + /** select the font to use * * @param f pointer to font array @@ -237,7 +259,7 @@ * - the number of byte per vertical line * you also have to change the array to char[] * - */ + */ void set_font(unsigned char* f); /** Set the orientation of the screen @@ -347,6 +369,7 @@ unsigned int orientation; unsigned int char_x; + };