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
Diff: SPI_TFT.cpp
- Revision:
- 25:f593b4adb905
- Parent:
- 24:b986bedb0a73
- Child:
- 26:231a28b27a76
diff -r b986bedb0a73 -r f593b4adb905 SPI_TFT.cpp --- a/SPI_TFT.cpp Sun Jun 28 15:58:01 2020 +0100 +++ b/SPI_TFT.cpp Sun Jun 28 16:44:22 2020 +0100 @@ -28,6 +28,9 @@ #include "SPI_TFT.h" #include "mbed.h" +// for fast performance on some STM boards + + #define BPP 16 // Bits per pixel @@ -252,7 +255,11 @@ // _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 - _spi.write(color); // Write D0..D15 + #ifdef __DIRECTSPI_H_ + _spi.directWrite(color); + #else + _spi.write(color); + #endif // Write D0..D15 _cs = 1; _spi.unlock(); } @@ -293,7 +300,11 @@ // _spi.format(16,3); // switch back to 16 bit Mode 3 unsigned int i; for (i = 0; i < ( width() * height()); i++) - _spi.write(_background); + #ifdef __DIRECTSPI_H_ + _spi.directWrite(_background); + #else + _spi.write(_background); + #endif _cs = 1; _spi.unlock(); } @@ -343,12 +354,13 @@ _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 back to 16 bit Mode 3 + // switch back to 16 bit Mode 3 for (int j=0; j<w; j++) { - _spi.write(color); + #ifdef __DIRECTSPI_H_ + _spi.directWrite(color); + #else + _spi.write(color); + #endif // one line } _cs = 1; @@ -370,7 +382,11 @@ _reset=1; // switch to 16 bit Mode 3 for (int y=0; y<h; y++) { - _spi.write(color); + #ifdef __DIRECTSPI_H_ + _spi.directWrite(color); + #else + _spi.write(color); + #endif // one line } _cs = 1; @@ -490,7 +506,11 @@ _reset=1; for (int p=0; p<pixel; p++) { - _spi.write(color); + #ifdef __DIRECTSPI_H_ + _spi.directWrite(color); + #else + _spi.write(color); + #endif // one line } _cs = 1; @@ -571,12 +591,18 @@ b = 1 << (j & 0x07); if (( z & b ) == 0x00) { - _spi.write(_background); + #ifdef __DIRECTSPI_H_ + _spi.directWrite(_background); + #else + _spi.write(_background); + #endif // one line } else { - - _spi.write(_foreground); - + #ifdef __DIRECTSPI_H_ + _spi.directWrite(_foreground); + #else + _spi.write(_foreground); + #endif // one line } } } @@ -621,7 +647,11 @@ for (j = 0; j < h; j++) { //Lines for (i = 0; i < w; i++) { // copy pixel data to TFT - _spi.write(*bitmap_ptr); // one line + #ifdef __DIRECTSPI_H_ + _spi.directWrite(*bitmap_ptr); + #else + _spi.write(*bitmap_ptr); + #endif // one line bitmap_ptr++; }