Adapted from Peter Dresche's original for Waveshare 2.8inch TFT Touch Shield Board and Mbed 6. RGB order reversed by changing reg 16 commands, spi write code adjusted as there is no reset pin but there is data command pin. Wait commands changed for new thread_sleep style, Stream class explicitly included. 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 26:231a28b27a76, committed 2020-07-06
- Comitter:
- jhd25
- Date:
- Mon Jul 06 10:57:10 2020 +0100
- Parent:
- 25:f593b4adb905
- Child:
- 27:8360ab3c19d6
- Commit message:
- Working BMP from SD
Changed in this revision
SPI_TFT.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/SPI_TFT.cpp Sun Jun 28 16:44:22 2020 +0100 +++ b/SPI_TFT.cpp Mon Jul 06 10:57:10 2020 +0100 @@ -128,12 +128,15 @@ // write to a TFT register void SPI_TFT::wr_reg (unsigned char reg, unsigned char val) { + _spi.lock(); + _spi.format(16,3); _cs=0; _reset=0; wr_cmd(reg); _reset=1; wr_dat(val); _cs=1; + _spi.unlock(); } // read from a TFT register @@ -495,9 +498,6 @@ int h = y1 - y0 + 1; int w = x1 - x0 + 1; int pixel = h * w; - #if defined USE_DMA - int dma_count; - #endif window(x0,y0,w,h); _spi.lock(); _cs = 0; @@ -664,4 +664,95 @@ WindowMax(); } +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; + int PixelWidth,PixelHeigh; + unsigned int start_data; + unsigned int i,off; + int padd,j; + unsigned short *line; + + sprintf(&filename[0],"%s",Name_BMP); + + FILE *Image = fopen((const char *)&filename[0], "rb"); // 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 (2 * PixelWidth); // we need a buffer for a line + if (line == NULL) { + return(-4); // error no memory + } + + // the bmp lines are padded to multiple of 4 bytes + padd = -1; + do { + padd ++; + } while ((PixelWidth * 2 + padd)%4 != 0); + int k,l; + + for ( j = PixelHeigh-1 ; j >=0 ; j-- ) { //Lines bottom up + + off = (j * (PixelWidth * 2 + padd)) + (start_data); // start of line + int erra=fseek(Image, off,SEEK_SET); + int errb=fread(line,1,PixelWidth * 2,Image); // read a line - slow ! + window(x, (y+PixelHeigh)-j , PixelWidth ,1); + _spi.lock(); + _cs=0; + _reset=0; + wr_cmd(0x22); + _reset=1; + _spi.format(16,3); + // switch to 16 bit Mode 3 + for (l = PixelWidth - 1; l >= 0 ; l--) { // copy pixel data to TFT + int out = line[(PixelWidth-1)-l]; + #ifdef __DIRECTSPI_H_ + _spi.directWrite(line[(PixelWidth-1)-l]); + #else + _spi.write(ine[(PixelWidth-1)-l]); + #endif // one line + } + + _cs=1; + _spi.unlock(); + + } + + free (line); + fclose(Image); + WindowMax(); + return(1); +} +