From 73c1831e7b6dc9394129c9bf1f7f7b51b8cf41af Mon Sep 17 00:00:00 2001 From: Bodmer Date: Sun, 29 Jul 2018 15:00:09 +0100 Subject: [PATCH] Add option for a different TFT read SPI frequency #define SPI_READ_FREQUENCY 20000000 // Optional reduced SPI frequency for reading TFT Also weeded out some compiler warnings --- Extensions/Smooth_font.cpp | 5 +++- Extensions/Sprite.cpp | 12 +++++----- TFT_eSPI.cpp | 48 +++++++++++++++++++++++++++----------- TFT_eSPI.h | 13 +++++++++-- User_Setup.h | 2 ++ library.json | 2 +- library.properties | 2 +- 7 files changed, 60 insertions(+), 24 deletions(-) diff --git a/Extensions/Smooth_font.cpp b/Extensions/Smooth_font.cpp index 14ed7960..7ec23406 100644 --- a/Extensions/Smooth_font.cpp +++ b/Extensions/Smooth_font.cpp @@ -256,7 +256,10 @@ uint16_t TFT_eSPI::decodeUTF8(uint8_t *buf, uint16_t *index, uint16_t remaining) // 16 bit Unicode if (((c & 0xF0) == 0xE0) && (remaining > 2)) - return ((c & 0x0F)<<12) | ((buf[(*index)++]&0x3F)<<6) | ((buf[(*index)++]&0x3F)); + { + c = ((c & 0x0F)<<12) | ((buf[(*index)++]&0x3F)<<6); + return c | ((buf[(*index)++]&0x3F)); + } // 21 bit Unicode not supported so fall-back to extended ASCII // if ((c & 0xF8) == 0xF0) return c; diff --git a/Extensions/Sprite.cpp b/Extensions/Sprite.cpp index a7443e9e..fb94747e 100644 --- a/Extensions/Sprite.cpp +++ b/Extensions/Sprite.cpp @@ -517,13 +517,14 @@ void TFT_eSprite::pushColor(uint32_t color, uint16_t len) if (!_created ) return; uint16_t pixelColor; + if (_bpp == 16) pixelColor = (uint16_t) (color >> 8) | (color << 8); else if (_bpp == 8) pixelColor = (color & 0xE000)>>8 | (color & 0x0700)>>6 | (color & 0x0018)>>3; - // else Nothing to do for 1bpp + else pixelColor = (uint16_t) color; // for 1bpp while(len--) writeColor(pixelColor); } @@ -1213,11 +1214,11 @@ void TFT_eSprite::drawChar(int32_t x, int32_t y, unsigned char c, uint32_t color uint16_t bo = pgm_read_word(&glyph->bitmapOffset); uint8_t w = pgm_read_byte(&glyph->width), - h = pgm_read_byte(&glyph->height), - xa = pgm_read_byte(&glyph->xAdvance); + h = pgm_read_byte(&glyph->height); + //xa = pgm_read_byte(&glyph->xAdvance); int8_t xo = pgm_read_byte(&glyph->xOffset), yo = pgm_read_byte(&glyph->yOffset); - uint8_t xx, yy, bits, bit=0; + uint8_t xx, yy, bits=0, bit=0; int16_t xo16 = 0, yo16 = 0; if(size > 1) { @@ -1609,7 +1610,6 @@ void TFT_eSprite::printToSprite(char *cbuffer, int len) //String string) } createSprite(sWidth, this->gFont.yAdvance); - uint16_t transparent = TFT_BLACK; if (this->textbgcolor != TFT_BLACK) fillSprite(this->textbgcolor); } @@ -1646,7 +1646,7 @@ int16_t TFT_eSprite::printToSprite(int16_t x, int16_t y, uint16_t index) if (newSprite) { createSprite(sWidth, this->gFont.yAdvance); - uint16_t transparent = TFT_BLACK; + if (this->textbgcolor != TFT_BLACK) fillSprite(this->textbgcolor); drawGlyph(this->gUnicode[index]); diff --git a/TFT_eSPI.cpp b/TFT_eSPI.cpp index 97701633..d19d0c62 100644 --- a/TFT_eSPI.cpp +++ b/TFT_eSPI.cpp @@ -58,6 +58,26 @@ inline void TFT_eSPI::spi_end(void){ #endif } +inline void TFT_eSPI::spi_begin_read(void){ +#if defined (SPI_HAS_TRANSACTION) && defined (SUPPORT_TRANSACTIONS) && !defined(ESP32_PARALLEL) + if (locked) {locked = false; SPI.beginTransaction(SPISettings(SPI_READ_FREQUENCY, MSBFIRST, TFT_SPI_MODE));} +#else + #if !defined(ESP32_PARALLEL) + SPI.setFrequency(SPI_READ_FREQUENCY); + #endif +#endif +} + +inline void TFT_eSPI::spi_end_read(void){ +#if defined (SPI_HAS_TRANSACTION) && defined (SUPPORT_TRANSACTIONS) && !defined(ESP32_PARALLEL) + if(!inTransaction) {if (!locked) {locked = true; SPI.endTransaction();}} +#else + #if !defined(ESP32_PARALLEL) + SPI.setFrequency(SPI_FREQUENCY); + #endif +#endif +} + #if defined (TOUCH_CS) && defined (SPI_TOUCH_FREQUENCY) // && !defined(ESP32_PARALLEL) inline void TFT_eSPI::spi_begin_touch(void){ @@ -495,7 +515,7 @@ uint8_t TFT_eSPI::readcommand8(uint8_t cmd_function, uint8_t index) #else // for ILI9341 Interface II i.e. IM [3:0] = "1101" - spi_begin(); + spi_begin_read(); index = 0x10 + (index & 0x0F); DC_C; @@ -512,7 +532,7 @@ uint8_t TFT_eSPI::readcommand8(uint8_t cmd_function, uint8_t index) reg = tft_Write_8(0); CS_H; - spi_end(); + spi_end_read(); #endif return reg; } @@ -526,7 +546,7 @@ uint16_t TFT_eSPI::readcommand16(uint8_t cmd_function, uint8_t index) { uint32_t reg; - reg |= (readcommand8(cmd_function, index + 0) << 8); + reg = (readcommand8(cmd_function, index + 0) << 8); reg |= (readcommand8(cmd_function, index + 1) << 0); return reg; @@ -597,7 +617,7 @@ uint16_t TFT_eSPI::readPixel(int32_t x0, int32_t y0) #else // Not ESP32_PARALLEL - spi_begin(); + spi_begin_read(); readAddrWindow(x0, y0, x0, y0); // Sets CS low @@ -620,7 +640,7 @@ uint16_t TFT_eSPI::readPixel(int32_t x0, int32_t y0) CS_H; - spi_end(); + spi_end_read(); return color565(r, g, b); @@ -733,7 +753,7 @@ void TFT_eSPI::readRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint16_t #else // Not ESP32_PARALLEL - spi_begin(); + spi_begin_read(); readAddrWindow(x, y, x + w - 1, y + h - 1); // Sets CS low @@ -764,7 +784,7 @@ void TFT_eSPI::readRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint16_t CS_H; - spi_end(); + spi_end_read(); #endif } @@ -1183,7 +1203,7 @@ void TFT_eSPI::pushImage(int32_t x, int32_t y, uint32_t w, uint32_t h, uint8_t * uint8_t msbColor = 0; uint8_t lsbColor = 0; - int32_t spx = x, spy = y; + //int32_t spx = x, spy = y; while (dh--) { @@ -1317,7 +1337,7 @@ bool TFT_eSPI::getSwapBytes(void) void TFT_eSPI::readRectRGB(int32_t x0, int32_t y0, int32_t w, int32_t h, uint8_t *data) { #if !defined(ESP32_PARALLEL) - spi_begin(); + spi_begin_read(); readAddrWindow(x0, y0, x0 + w - 1, y0 + h - 1); // Sets CS low @@ -1347,7 +1367,7 @@ void TFT_eSPI::readRectRGB(int32_t x0, int32_t y0, int32_t w, int32_t h, uint8_ } CS_H; - spi_end(); + spi_end_read(); #endif } @@ -2301,11 +2321,11 @@ void TFT_eSPI::drawChar(int32_t x, int32_t y, unsigned char c, uint32_t color, u uint16_t bo = pgm_read_word(&glyph->bitmapOffset); uint8_t w = pgm_read_byte(&glyph->width), - h = pgm_read_byte(&glyph->height), - xa = pgm_read_byte(&glyph->xAdvance); + h = pgm_read_byte(&glyph->height); + //xa = pgm_read_byte(&glyph->xAdvance); int8_t xo = pgm_read_byte(&glyph->xOffset), yo = pgm_read_byte(&glyph->yOffset); - uint8_t xx, yy, bits, bit=0; + uint8_t xx, yy, bits=0, bit=0; int16_t xo16 = 0, yo16 = 0; if(size > 1) { @@ -4054,8 +4074,10 @@ int16_t TFT_eSPI::drawChar(unsigned int uniCode, int x, int y, int font) //spi_begin(); setAddrWindow(x, y, x + width - 1, y + height - 1); +#ifdef RPI_WRITE_STROBE uint8_t textcolorBin[] = { (uint8_t) (textcolor >> 8), (uint8_t) textcolor }; uint8_t textbgcolorBin[] = { (uint8_t) (textbgcolor >> 8), (uint8_t) textbgcolor }; +#endif // Maximum font size is equivalent to 180x180 pixels in area while (w > 0) diff --git a/TFT_eSPI.h b/TFT_eSPI.h index fdb39e76..5729a37c 100644 --- a/TFT_eSPI.h +++ b/TFT_eSPI.h @@ -30,6 +30,11 @@ #define SPI_FREQUENCY 20000000 #endif +// If the frequency is not defined, set a default +#ifndef SPI_READ_FREQUENCY + #define SPI_READ_FREQUENCY SPI_FREQUENCY +#endif + #ifdef ST7789_DRIVER #define TFT_SPI_MODE SPI_MODE3 #else @@ -213,8 +218,9 @@ #define set_mask(C) xset_mask[C] // 63fps Sprite rendering test 33% faster, graphicstest only 1.8% faster than shifting in real time // Real-time shifting alternative to above to save 1KByte RAM, 47 fps Sprite rendering test - //#define set_mask(C) ((C&0x80)>>7)<>6)<>5)<>4)<>7)<>6)<>5)<>4)<>3)<>2)<>1)<>0)<