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

Files at this revision

API Documentation at this revision

Comitter:
jhd25
Date:
Sun Jun 28 15:58:01 2020 +0100
Parent:
23:469bf5f3c8ac
Child:
25:f593b4adb905
Commit message:
More tidying

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 15:52:02 2020 +0100
+++ b/SPI_TFT.cpp	Sun Jun 28 15:58:01 2020 +0100
@@ -28,7 +28,7 @@
 
 #include "SPI_TFT.h"
 #include "mbed.h"
-#include "stdint.h"
+
 
 #define BPP         16                  // Bits per pixel    
 
@@ -92,10 +92,8 @@
 
 void SPI_TFT::wr_cmd(unsigned char cmd)
 {
-
-    uint16_t spi_d;
+    unsigned short spi_d;
     spi_d =  0x7000 | cmd ;
-
     _spi.write(spi_d);      // mbed lib
 
 }
@@ -104,11 +102,9 @@
 // write data to tft register
 void SPI_TFT::wr_dat(unsigned char dat)
 {
-
     unsigned short spi_d;
     spi_d =  0x7200 | dat;
     _spi.write(spi_d);      
-
 }
 
 
@@ -372,14 +368,10 @@
     _reset=0;
     wr_cmd(0x22);
     _reset=1;   
-    
-                                         // 16 bit SPI  
- //           _spi.format(8,3);                             // 8 bit Mode 3
- //           _spi.write(SPI_START | SPI_WR | SPI_DATA);    // Write : RS = 1, RW = 0
- //           _spi.format(16,3);                            // switch to 16 bit Mode 3
-            for (int y=0; y<h; y++) {
-                _spi.write(color);
-            }
+               // switch to 16 bit Mode 3
+    for (int y=0; y<h; y++) {
+        _spi.write(color);
+    }
 
     _cs = 1;
     _spi.unlock();
@@ -496,14 +488,10 @@
     _reset=0;
     wr_cmd(0x22);
     _reset=1;   
-    
-                                            // 16 bit SPI
-//            _spi.format(8,3);                             // 8 bit Mode 3
-//            _spi.write(SPI_START | SPI_WR | SPI_DATA);    // Write : RS = 1, RW = 0
-//            _spi.format(16,3);                            // switch to 16 bit Mode 3
-            for (int p=0; p<pixel; p++) {
-                _spi.write(color);
-            }
+
+    for (int p=0; p<pixel; p++) {
+        _spi.write(color);
+    }
 
     _cs = 1;
     _spi.unlock();
@@ -569,17 +557,12 @@
         }
     }
     window(char_x, char_y,hor,vert); // char box
-     _spi.lock();
+    _spi.lock();
     _cs = 0;  
     _reset=0;
     wr_cmd(0x22);
     _reset=1;   
-                                         // 16 bit SPI  
-//        _spi.format(8,3);                             // 8 bit Mode 3
-//        _spi.write(SPI_START );    // Write : RS = 1, RW = 0
-//        _spi.format(16,3);                            // switch back to 16 bit Mode 3
-  
-    
+
         zeichen = &font[((c -32) * offset) + 4];    // start of char bitmap
         w = zeichen[0];                             // width of actual char
         for (j=0; j<vert; j++) {  //  vert line
@@ -619,9 +602,7 @@
     int padd;
   
     unsigned short *bitmap_ptr = (unsigned short *)bitmap;
-    #if defined TARGET_KL25Z  // 8 Bit SPI
-        unsigned short pix_temp;
-    #endif
+
   
     // the lines are padded to multiple of 4 bytes in a bitmap
     padd = -1;
@@ -635,11 +616,6 @@
     wr_cmd(0x22);
     _reset=1;   
 
-
-//        _spi.format(8,3);                             // 8 bit Mode 3
- //       _spi.write(SPI_START | SPI_WR | SPI_DATA);    // Write : RS = 1, RW = 0
- //       _spi.format(16,3);                            // switch to 16 bit Mode 3
-
     bitmap_ptr += ((h - 1)* (w + padd));              
     unsigned int i;
     for (j = 0; j < h; j++) {        //Lines
@@ -659,150 +635,3 @@
 }
 
 
-// local filesystem is not implemented in kinetis board
-#if defined TARGET_LPC1768  || defined TARGET_LPC11U24
-
-
-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++;
-    }
-
-    fprintf(stderr, "filename : %s \n\r",filename);
-
-    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);
-
-
-//fseek(Image, 70 ,SEEK_SET);
-    window(x, y,PixelWidth ,PixelHeigh);
-    _spi.lock();
-    _cs = 0;  
-    _reset=0;
-    wr_cmd(0x22);
-    _reset=1;   
-#if defined NO_MBED_LIB
-    if (spi_port == 0) {    // TFT on SSP0
-#if defined USE_DMA
-        LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP0->DR; // we send to SSP0
-        /* Enable SSP0 for DMA. */
-        LPC_SSP0->DMACR = 0x2;
-#endif
-        LPC_SSP0->CR0 &= ~(0x08UL); // set to 8 bit
-        LPC_SSP0->DR = 0x72;        // start Data
-        LPC_SSP0->CR0 |= 0x08UL;    // set to 16 bit
-
-    } else {
-#if defined USE_DMA
-        LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP1->DR; // we send to SSP1
-        /* Enable SSP1 for DMA. */
-        LPC_SSP1->DMACR = 0x2;
-#endif
-        LPC_SSP1->CR0 &= ~(0x08UL); // set to 8 bit
-        LPC_SSP1->DR = 0x72;        // start Data
-        LPC_SSP1->CR0 |= 0x08UL;    // set to 16 bit
-    }
-    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 !
-#if defined USE_DMA
-        LPC_GPDMA->DMACIntTCClear = 0x1;
-        LPC_GPDMA->DMACIntErrClr = 0x1;
-        LPC_GPDMACH0->DMACCSrcAddr = (uint32_t)line;
-        LPC_GPDMACH0->DMACCControl = PixelWidth | (1UL << 18) | (1UL << 21) | (1UL << 31) |  DMA_CHANNEL_SRC_INC ; // 16 bit transfer , address increment, interrupt
-        LPC_GPDMACH0->DMACCConfig  = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P | (spi_port ? DMA_DEST_SSP1_TX : DMA_DEST_SSP0_TX);
-        LPC_GPDMA->DMACSoftSReq = 0x1;
-        do {
-        } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running
-#else
-        for (i = 0; i < PixelWidth; i++) {        // copy pixel data to TFT
-            _spi.write(line[i]);                  // one 16 bit pixel
-        }
-#endif
-    }
-    if (spi_port == 0) {    // TFT on SSP0
-        do {
-        } while ((LPC_SSP0->SR & 0x10) == 0x10); // SPI FIFO not empty
-    } else {
-        do {
-        } while ((LPC_SSP1->SR & 0x10) == 0x10); // SPI FIFO not empty
-    }
-
-#else // use mbed lib
-    _spi.format(8,3);                             // 8 bit Mode 3
-    _spi.write(SPI_START | SPI_WR | SPI_DATA);    // Write : RS = 1, RW = 0
-    _spi.format(16,3);                            // switch to 16 bit Mode 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
-        }
-    }
-#endif
-    _cs = 1;
-    _spi.unlock();
-    free (line);
-    fclose(Image);
-    WindowMax();
-    return(1);
-}
-
-#endif