diff --git a/Extensions/Smooth_font.cpp b/Extensions/Smooth_font.cpp index ca1cb63..c835b48 100644 --- a/Extensions/Smooth_font.cpp +++ b/Extensions/Smooth_font.cpp @@ -370,7 +370,6 @@ uint16_t TFT_eSPI::decodeUTF8(uint8_t c) */ - /*************************************************************************************** ** Function name: alphaBlend ** Description: Blend foreground and background and return new colour @@ -509,6 +508,7 @@ void TFT_eSPI::drawGlyph(uint16_t code) else drawFastHLine( xs, y + cy, dl, fg); dl = 0; } + if (getColor) bg = getColor(x + cx, y + cy); drawPixel(x + cx, y + cy, alphaBlend(pixel, fg, bg)); } else diff --git a/Extensions/Sprite.cpp b/Extensions/Sprite.cpp index 2c8573e..6984d38 100644 --- a/Extensions/Sprite.cpp +++ b/Extensions/Sprite.cpp @@ -566,21 +566,21 @@ void TFT_eSprite::pushImage(int32_t x, int32_t y, int32_t w, int32_t h, uint16_ int32_t xs = x; int32_t ys = y; - uint32_t ws = w; - uint32_t hs = h; + int32_t ws = w; + int32_t hs = h; if (x < 0) { xo = -x; ws += x; xs = 0; } if (y < 0) { yo = -y; hs += y; ys = 0; } - if (xs + ws >= _iwidth) ws = _iwidth - xs; - if (ys + hs >= _iheight) hs = _iheight - ys; + if (xs + ws >= (int32_t)_iwidth) ws = _iwidth - xs; + if (ys + hs >= (int32_t)_iheight) hs = _iheight - ys; if (_bpp == 16) // Plot a 16 bpp image into a 16 bpp Sprite { - for (uint32_t yp = yo; yp < yo + hs; yp++) + for (int32_t yp = yo; yp < yo + hs; yp++) { x = xs; - for (uint32_t xp = xo; xp < xo + ws; xp++) + for (int32_t xp = xo; xp < xo + ws; xp++) { uint16_t color = data[xp + yp * w]; if(!_iswapBytes) color = color<<8 | color>>8; @@ -592,10 +592,10 @@ void TFT_eSprite::pushImage(int32_t x, int32_t y, int32_t w, int32_t h, uint16_ } else if (_bpp == 8) // Plot a 16 bpp image into a 8 bpp Sprite { - for (uint32_t yp = yo; yp < yo + hs; yp++) + for (int32_t yp = yo; yp < yo + hs; yp++) { x = xs; - for (uint32_t xp = xo; xp < xo + ws; xp++) + for (int32_t xp = xo; xp < xo + ws; xp++) { uint16_t color = data[xp + yp * w]; if(_iswapBytes) color = color<<8 | color>>8; @@ -662,21 +662,21 @@ void TFT_eSprite::pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const u int32_t xs = x; int32_t ys = y; - uint32_t ws = w; - uint32_t hs = h; + int32_t ws = w; + int32_t hs = h; if (x < 0) { xo = -x; ws += x; xs = 0; } if (y < 0) { yo = -y; hs += y; ys = 0; } - if (xs + ws >= _iwidth) ws = _iwidth - xs; - if (ys + hs >= _iheight) hs = _iheight - ys; + if (xs + ws >= (int32_t)_iwidth) ws = _iwidth - xs; + if (ys + hs >= (int32_t)_iheight) hs = _iheight - ys; if (_bpp == 16) // Plot a 16 bpp image into a 16 bpp Sprite { - for (uint32_t yp = yo; yp < yo + hs; yp++) + for (int32_t yp = yo; yp < yo + hs; yp++) { x = xs; - for (uint32_t xp = xo; xp < xo + ws; xp++) + for (int32_t xp = xo; xp < xo + ws; xp++) { uint16_t color = pgm_read_word(data + xp + yp * w); if(!_iswapBytes) color = color<<8 | color>>8; @@ -689,10 +689,10 @@ void TFT_eSprite::pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const u else if (_bpp == 8) // Plot a 16 bpp image into a 8 bpp Sprite { - for (uint32_t yp = yo; yp < yo + hs; yp++) + for (int32_t yp = yo; yp < yo + hs; yp++) { x = xs; - for (uint32_t xp = xo; xp < xo + ws; xp++) + for (int32_t xp = xo; xp < xo + ws; xp++) { uint16_t color = pgm_read_word(data + xp + yp * w); if(_iswapBytes) color = color<<8 | color>>8; @@ -733,7 +733,7 @@ void TFT_eSprite::pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const u uint8_t pbyte = pgm_read_byte(pdata++); for (uint8_t xc = 0; xc < 8; xc++) { - if (xp+xc // Hardware-specific library #include +#include "Free_Fonts.h" // Include the header file attached to this sketch + TFT_eSPI tft = TFT_eSPI(); // Invoke custom library with default width and height unsigned long drawTime = 0; diff --git a/examples/480 x 320/Free_Font_Demo/Free_Fonts.h b/examples/480 x 320/Free_Font_Demo/Free_Fonts.h index a87be36..7c0a264 100644 --- a/examples/480 x 320/Free_Font_Demo/Free_Fonts.h +++ b/examples/480 x 320/Free_Font_Demo/Free_Fonts.h @@ -39,7 +39,6 @@ // // tft.setFreeFont(NULL); // Set font to GLCD -#define LOAD_GFXFF #ifdef LOAD_GFXFF // Only include the fonts if LOAD_GFXFF is defined in User_Setup.h @@ -265,6 +264,8 @@ #define FONT7 7 #define FONT8 8 +#define TT1 1 + #define FF0 1 #define FF1 1 #define FF2 1 diff --git a/examples/Smooth Fonts/Smooth_font_gradient/Smooth_font_gradient.ino b/examples/Smooth Fonts/Smooth_font_gradient/Smooth_font_gradient.ino new file mode 100644 index 0000000..b545ab3 --- /dev/null +++ b/examples/Smooth Fonts/Smooth_font_gradient/Smooth_font_gradient.ino @@ -0,0 +1,121 @@ +/* + This sketch is based on Font Demo 1. It introduces a method for rendering + anti-aliased fonts on a graded background. This is acheived by telling the + TFT_eSPI library the pixel color at each point on the screen. In this sketch + a graded background is drawn, the color of each pixel can therefore be + determined. The TFT does not need to support reading of the graphics memory. + The sketch could be adapted so only part of the screen is gas a color gradient. + + The TFT_eSPI library must be given the name of the function in the sketch + that will return the pixel xolor at a position x,y on the TFT. In this + sketch that function is called gradientColor, so this line is included: + + tft.setCallback(gradientColor); + + TFT_eSPI will call this function during the rendering of the anti-aliased + font to blend the edges of each character with the returned color. + + If the TFT supports reading the screen RAM then the returned value can be + tft.readPixel(x,y) and anti-aliased text can the be drawn over any screen + image. See "Smooth_font_over_image" example. +*/ +// The fonts used are in the sketch data folder, press Ctrl+K to view. + +// Upload the fonts and icons to SPIFFS (must set at least 1M for SPIFFS) using the +// "Tools" "ESP8266 (or ESP32) Sketch Data Upload" menu option in the IDE. +// To add this option follow instructions here for the ESP8266: +// https://github.com/esp8266/arduino-esp8266fs-plugin +// or for the ESP32: +// https://github.com/me-no-dev/arduino-esp32fs-plugin + +// Close the IDE and open again to see the new menu option. + +// A processing sketch to create new fonts can be found in the Tools folder of TFT_eSPI +// https://github.com/Bodmer/TFT_eSPI/tree/master/Tools/Create_Smooth_Font/Create_font + +// This sketch uses font files created from the Noto family of fonts: +// https://www.google.com/get/noto/ + +#define AA_FONT_SMALL "NotoSansBold15" +#define AA_FONT_LARGE "NotoSansBold36" + +// Font files are stored in SPIFFS, so load the library +#include + +#include +#include // Hardware-specific library + +TFT_eSPI tft = TFT_eSPI(); + +#define TOP_COLOR TFT_RED +#define BOTTOM_COLOR TFT_BLACK + +#define GRADIENT_HEIGHT (9 + tft.fontHeight() * 5) // Gradient over 5 lines +#define OUTSIDE_GRADIENT TFT_BLUE + +uint16_t gradientColor(uint16_t x, uint16_t y) +{ + if (y > GRADIENT_HEIGHT) return OUTSIDE_GRADIENT; // Outside gradient area + uint8_t alpha = (255 * y) / GRADIENT_HEIGHT; // alpha is a value in the range 0-255 + return tft.alphaBlend(alpha, BOTTOM_COLOR, TOP_COLOR); +} + +void fillGradient() { + uint16_t w = tft.width(); + for (uint16_t y = 0; y < tft.height(); ++y) { + uint16_t color = gradientColor(0, y); // x not used here + tft.drawFastHLine(0, y, w, color); + } +} + +void setup(void) { + Serial.begin(115200); + + tft.begin(); + + tft.setCallback(gradientColor); // Switch on color callback for anti-aliased fonts + //tft.setCallback(nullptr); // Switch off callback (off by default) + + tft.setRotation(1); + + if (!SPIFFS.begin()) { + Serial.println("SPIFFS initialisation failed!"); + while (1) yield(); // Stay here twiddling thumbs waiting + } + Serial.println("\r\nSPIFFS available!"); + + // ESP32 will crash if any of the fonts are missing + bool font_missing = false; + if (SPIFFS.exists("/NotoSansBold15.vlw") == false) font_missing = true; + if (SPIFFS.exists("/NotoSansBold36.vlw") == false) font_missing = true; + + if (font_missing) + { + Serial.println("\r\nFont missing in SPIFFS, did you upload it?"); + while (1) yield(); + } + else Serial.println("\r\nFonts found OK."); +} + + +void loop() { + + // Select a font size comensurate with screen size + if (tft.width()>= 320) + tft.loadFont(AA_FONT_LARGE); + else + tft.loadFont(AA_FONT_SMALL); + + fillGradient(); // Put here after selecting the font so fontHeight() is already set + + tft.setTextColor(TFT_WHITE); // Background color is ignored in gradient area + tft.setCursor(0, 10); // Set cursor at top left of screen + + uint32_t t = millis(); + tft.println(" Ode to a small\n lump of green\n putty I found\n in my armpit\n one midsummer\n morning "); + Serial.println(t = millis()-t); + + tft.unloadFont(); // Remove the font to recover memory used + + delay(2000); +} diff --git a/examples/Smooth Fonts/Smooth_font_gradient/data/NotoSansBold15.vlw b/examples/Smooth Fonts/Smooth_font_gradient/data/NotoSansBold15.vlw new file mode 100644 index 0000000..803a1bd Binary files /dev/null and b/examples/Smooth Fonts/Smooth_font_gradient/data/NotoSansBold15.vlw differ diff --git a/examples/Smooth Fonts/Smooth_font_gradient/data/NotoSansBold36.vlw b/examples/Smooth Fonts/Smooth_font_gradient/data/NotoSansBold36.vlw new file mode 100644 index 0000000..66003f6 Binary files /dev/null and b/examples/Smooth Fonts/Smooth_font_gradient/data/NotoSansBold36.vlw differ diff --git a/examples/Smooth Fonts/Smooth_font_reading_TFT/Smooth_font_reading_TFT.ino b/examples/Smooth Fonts/Smooth_font_reading_TFT/Smooth_font_reading_TFT.ino new file mode 100644 index 0000000..c4d499c --- /dev/null +++ b/examples/Smooth Fonts/Smooth_font_reading_TFT/Smooth_font_reading_TFT.ino @@ -0,0 +1,165 @@ +/* + This sketch is based on Font Demo 1. It introduces a method for rendering + anti-aliased fonts on an arbitrary background. This is acheived by reading + the pixel color at each point on the screen. The TFT must support reading + the graphics RAM of the screen memory. This sketch has been tested with + ILI9241 and ILI9481 serial and parallel screens. Other screens may or may + not work! + + The TFT_eSPI library must be given the name of the function in the sketch + that will return the pixel color at a position x,y on the TFT. In this + sketch that function is called pixelColor, so this line is included: + + tft.setCallback(pixelColor); + + TFT_eSPI will call this function during the rendering of the anti-aliased + font and use it to blend the edges of each character with the screen color. +*/ +// The fonts used are in the sketch data folder, press Ctrl+K to view. + +// Upload the fonts and icons to SPIFFS (must set at least 1M for SPIFFS) using the +// "Tools" "ESP8266 (or ESP32) Sketch Data Upload" menu option in the IDE. +// To add this option follow instructions here for the ESP8266: +// https://github.com/esp8266/arduino-esp8266fs-plugin +// or for the ESP32: +// https://github.com/me-no-dev/arduino-esp32fs-plugin + +// Close the IDE and open again to see the new menu option. + +// A processing sketch to create new fonts can be found in the Tools folder of TFT_eSPI +// https://github.com/Bodmer/TFT_eSPI/tree/master/Tools/Create_Smooth_Font/Create_font + +// This sketch uses font files created from the Noto family of fonts: +// https://www.google.com/get/noto/ + +#define AA_FONT_SMALL "NotoSansBold15" +#define AA_FONT_LARGE "NotoSansBold36" + +// Font files are stored in SPIFFS, so load the library +#include + +#include +#include // Hardware-specific library + +TFT_eSPI tft = TFT_eSPI(); + +// Callback function to provide the pixel color at x,y +uint16_t pixelColor(uint16_t x, uint16_t y) { return tft.readPixel(x, y); } + + +void setup(void) { + Serial.begin(115200); + + tft.begin(); + + tft.setCallback(pixelColor); // The callback is only used durung font rendering + //tft.setCallback(nullptr); // Switch off callback (off by default) + + tft.setRotation(1); + + if (!SPIFFS.begin()) { + Serial.println("SPIFFS initialisation failed!"); + while (1) yield(); // Stay here twiddling thumbs waiting + } + Serial.println("\r\nSPIFFS available!"); + + // ESP32 will crash if any of the fonts are missing, so check + bool font_missing = false; + if (SPIFFS.exists("/NotoSansBold15.vlw") == false) font_missing = true; + if (SPIFFS.exists("/NotoSansBold36.vlw") == false) font_missing = true; + + if (font_missing) + { + Serial.println("\r\nFont missing in SPIFFS, did you upload it?"); + while (1) yield(); + } + else Serial.println("\r\nFonts found OK."); +} + + +void loop() { + + rainbow_fill(); // Fill the screen with rainbow colours + + // Select a font size comensurate with screen size + if (tft.width()>= 320) + tft.loadFont(AA_FONT_LARGE); + else + tft.loadFont(AA_FONT_SMALL); + + tft.setTextColor(TFT_BLACK, TFT_WHITE); // Background color is ignored if callback is set + tft.setCursor(0, 10); // Set cursor at top left of screen + + uint32_t t = millis(); + tft.println(" Ode to a small\n lump of green\n putty I found\n in my armpit\n one midsummer\n morning "); + Serial.println(t = millis()-t); + + tft.unloadFont(); // Remove the font to recover memory used + + delay(2000); +} + +// ######################################################################### +// Fill screen with a rainbow pattern +// ######################################################################### +byte red = 31; +byte green = 0; +byte blue = 0; +byte state = 0; +unsigned int colour = red << 11; // Colour order is RGB 5+6+5 bits each + +void rainbow_fill() +{ + // The colours and state are not initialised so the start colour changes each time the funtion is called + + for (int i = 319; i >= 0; i--) { + // Draw a vertical line 1 pixel wide in the selected colour + tft.drawFastHLine(0, i, tft.width(), colour); // in this example tft.width() returns the pixel width of the display + // This is a "state machine" that ramps up/down the colour brightnesses in sequence + switch (state) { + case 0: + green ++; + if (green == 64) { + green = 63; + state = 1; + } + break; + case 1: + red--; + if (red == 255) { + red = 0; + state = 2; + } + break; + case 2: + blue ++; + if (blue == 32) { + blue = 31; + state = 3; + } + break; + case 3: + green --; + if (green == 255) { + green = 0; + state = 4; + } + break; + case 4: + red ++; + if (red == 32) { + red = 31; + state = 5; + } + break; + case 5: + blue --; + if (blue == 255) { + blue = 0; + state = 0; + } + break; + } + colour = red << 11 | green << 5 | blue; + } +} diff --git a/examples/Smooth Fonts/Smooth_font_reading_TFT/data/NotoSansBold15.vlw b/examples/Smooth Fonts/Smooth_font_reading_TFT/data/NotoSansBold15.vlw new file mode 100644 index 0000000..803a1bd Binary files /dev/null and b/examples/Smooth Fonts/Smooth_font_reading_TFT/data/NotoSansBold15.vlw differ diff --git a/examples/Smooth Fonts/Smooth_font_reading_TFT/data/NotoSansBold36.vlw b/examples/Smooth Fonts/Smooth_font_reading_TFT/data/NotoSansBold36.vlw new file mode 100644 index 0000000..66003f6 Binary files /dev/null and b/examples/Smooth Fonts/Smooth_font_reading_TFT/data/NotoSansBold36.vlw differ diff --git a/library.json b/library.json index ac9e8b9..474ebcb 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "TFT_eSPI", - "version": "1.4.20", + "version": "1.4.21", "keywords": "tft, ePaper, display, ESP8266, NodeMCU, ESP32, M5Stack, ILI9341, ST7735, ILI9163, S6D02A1, ILI9486, ST7789, RM68140", "description": "A TFT and ePaper SPI graphics library for ESP8266 and ESP32", "repository": diff --git a/library.properties b/library.properties index aea4881..d7f2e39 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=TFT_eSPI -version=1.4.20 +version=1.4.21 author=Bodmer maintainer=Bodmer sentence=A fast TFT graphics library for ESP8266 and ESP32 processors for the Arduino IDE