Sample Sketch

PieChart

// This sketch includes a function to draw circle segments
// for pie charts in 1 degree increments

#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>

TFT_eSPI tft = TFT_eSPI(); // Invoke custom library

#define DEG2RAD 0.0174532925

byte inc = 0;
unsigned int col = 0;


void setup(void) {
    tft.begin();

    tft.setRotation(1);

    tft.fillScreen(TFT_BLACK);
}

void loop() {

    // Draw 4 pie chart segments
    fillSegment(160, 120, 0, 60, 100, TFT_RED);
    fillSegment(160, 120, 60, 30, 100, TFT_GREEN);
    fillSegment(160, 120, 60 + 30, 120, 100, TFT_BLUE);
    fillSegment(160, 120, 60 + 30 + 120, 150, 100, TFT_YELLOW);

    delay(4000);

    // Erase old chart with 360 degree black plot
    fillSegment(160, 120, 0, 360, 100, TFT_BLACK);
}


// #########################################################################
// Draw circle segments
// #########################################################################

// x,y == coords of centre of circle
// start_angle = 0 - 359
// sub_angle   = 0 - 360 = subtended angle
// r = radius
// color = 16 bit color value

int fillSegment(int x, int y, int start_angle, int sub_angle, int r, unsigned int color) {
    // Calculate first pair of coordinates for segment start
    float sx = cos((start_angle - 90) * DEG2RAD);
    float sy = sin((start_angle - 90) * DEG2RAD);
    uint16_t x1 = sx * r + x;
    uint16_t y1 = sy * r + y;

    // Draw color blocks every inc degrees
    for (int i = start_angle; i < start_angle + sub_angle; i++) {

        // Calculate pair of coordinates for segment end
        int x2 = cos((i + 1 - 90) * DEG2RAD) * r + x;
        int y2 = sin((i + 1 - 90) * DEG2RAD) * r + y;

        tft.fillTriangle(x1, y1, x2, y2, x, y, color);

        // Copy segment end to sgement start for next segment
        x1 = x2;
        y1 = y2;
    }
}


// #########################################################################
// Return the 16 bit color with brightness 0-100%
// #########################################################################
unsigned int brightness(unsigned int color, int brightness) {
    byte red   = colour >> 11;
    byte green = (color & 0x7E0) >> 5;
    byte blue  = color & 0x1F;

    blue = (blue * brightness) / 100;
    green = (green * brightness) / 100;
    red = (red * brightness) / 100;

    return (red << 11) + (green << 5) + blue;
}

AllFreeFontsDemo

/*
    Example for TFT_eSPI library

    Created by Bodmer 31/12/16

    This example draws all fonts (as used by the Adafruit_GFX library) onto the
    screen. These fonts are called the GFX Free Fonts (GFXFF) in this library.

    The fonts are referenced by a short name, see the Free_Fonts.h file
    attached to this sketch.

    Other True Type fonts could be converted using the utility within the
    "fontconvert" folder inside the library. This converted has also been
    copied from the Adafruit_GFX library.

    Since these fonts are a recent addition Adafruit do not have a tutorial
    available yet on how to use the fontconvert utility.   Linux users will
    no doubt figure it out!  In the meantime there are 48 font files to use
    in sizes from 9 point to 24 point, and in normal, bold, and italic or
    oblique styles.

    This example sketch uses both the print class and drawString() functions
    to plot text to the screen.

    Make sure LOAD_GFXFF is defined in the User_Setup.h file within the
    library folder.

    --------------------------- NOTE ----------------------------------------
    The free font encoding format does not lend itself easily to plotting
    the background without flicker. For values that changes on screen it is
    better to use Fonts 1- 8 which are encoded specifically for rapid
    drawing with background.
    -------------------------------------------------------------------------

    #########################################################################
    ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
    ######       TO SELECT YOUR DISPLAY TYPE AND ENABLE FONTS          ######
    #########################################################################
*/

#define TEXT "aA MWyz~12" // Text that will be printed on screen in any font

#include "Free_Fonts.h" // Include the header file attached to this sketch

#include "SPI.h"
#include "TFT_eSPI.h"

// Use hardware SPI
TFT_eSPI tft = TFT_eSPI();

unsigned long drawTime = 0;

void setup(void) {

    tft.begin();

    tft.setRotation(1);

}

void loop() {

    // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    // Show all 48 fonts in centre of screen ( x,y coordinate 160,120)
    // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    // Where font sizes increase the screen is not cleared as the larger fonts overwrite
    // the smaller one with the background color.

    // Set text datum to middle centre
    tft.setTextDatum(MC_DATUM);

    // Set text colour to orange with black background
    tft.setTextColor(TFT_WHITE, TFT_BLACK);

    tft.fillScreen(TFT_BLACK);            // Clear screen
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF1, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF1);                 // Select the font
    tft.drawString(TEXT, 160, 120, GFXFF);// Print the string name of the font
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF2, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF2);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF3, 160, 60, GFXFF);// Print the string name of the font

    tft.setFreeFont(FF3);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF4, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF4);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);

    tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF5, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF5);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF6, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF6);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF7, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF7);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF8, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF8);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);

    tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF9, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF9);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF10, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF10);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF11, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF11);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF12, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF12);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);

    tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF13, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF13);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF14, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF14);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF15, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF15);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF16, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF16);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);

    tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF17, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF17);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF18, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF18);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF19, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF19);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF20, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF20);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);

    tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF21, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF21);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF22, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF22);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF23, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF23);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF24, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF24);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);

    tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF25, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF25);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF26, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF26);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF27, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF27);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF28, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF28);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);

    tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF29, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF29);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF30, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF30);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF31, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF31);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF32, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF32);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);

    tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF33, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF33);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF34, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF34);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF35, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF35);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF36, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF36);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);

    tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF37, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF37);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF38, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF38);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF39, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF39);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF40, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF40);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);

    tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF41, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF41);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF42, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF42);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF43, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF43);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF44, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF44);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);

    tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF45, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF45);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF46, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF46);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF47, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF47);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);
    //tft.fillScreen(TFT_BLACK);
    tft.setFreeFont(FF18);                 // Select the font
    tft.drawString(sFF48, 160, 60, GFXFF);// Print the string name of the font
    tft.setFreeFont(FF48);
    tft.drawString(TEXT, 160, 120, GFXFF);
    delay(1000);

}

// There follows a crude way of flagging that this example sketch needs fonts which
// have not been enbabled in the User_Setup.h file inside the TFT_HX8357 library.
//
// These lines produce errors during compile time if settings in User_Setup are not correct
//
// The error will be "does not name a type" but ignore this and read the text between ''
// it will indicate which font or feature needs to be enabled
//
// Either delete all the following lines if you do not want warnings, or change the lines
// to suit your sketch modifications.

#ifndef LOAD_GLCD
    //ERROR_Please_enable_LOAD_GLCD_in_User_Setup
#endif

#ifndef LOAD_GFXFF
    ERROR_Please_enable_LOAD_GFXFF_in_User_Setup!
#endif

ColorTest

//   Diagnostic test for the displayed color order
//
// Writen by Bodmer 17/2/19 for the TFT_eSPI library:
// https://github.com/Bodmer/TFT_eSPI

/*
    Different hardware manufacturers use different color order
    configurations at the hardware level.  This may result in
    incorrect colors being displayed.

    Incorrectly displayed colours could also be the result of
    using the wrong display driver in the library setup file.

    Typically displays have a control register (MADCTL) that can
    be used to set the Red Green Blue (RGB) color order to RGB
    or BRG so that red and blue are swapped on the display.

    This control register is also used to manage the display
    rotation and coordinate mirroring. The control register
    typically has 8 bits, for the ILI9341 these are:

    Bit Function
    7   Mirror Y coordinate (row address order)
    6   Mirror X coordinate (column address order)
    5   Row/column exchange (for rotation)
    4   Refresh direction (top to bottom or bottom to top in portrait orientation)
    3   RGB order (swaps red and blue)
    2   Refresh direction (top to bottom or bottom to top in landscape orientation)
    1   Not used
    0   Not used

    The control register bits can be written with this example command sequence:

    tft.writecommand(TFT_MADCTL);
    tft.writedata(0x48);          // Bits 6 and 3 set

    0x48 is the default value for ILI9341 (0xA8 for ESP32 M5STACK)
    in rotation 0 orientation.

    Another control register can be used to "invert" colors,
    this swaps black and white as well as other colors (e.g.
    green to magenta, red to cyan, blue to yellow).

    To invert colours insert this line after tft.init() or tft.begin():

    tft.invertDisplay( invert ); // Where invert is true or false

*/

#include <SPI.h>

#include <TFT_eSPI.h>       // Hardware-specific library

TFT_eSPI tft = TFT_eSPI();  // Invoke custom library

void setup(void) {
    tft.init();

    tft.fillScreen(TFT_BLACK);

    // Set "cursor" at top left corner of display (0,0) and select font 4
    tft.setCursor(0, 0, 4);

    // Set the font colour to be white with a black background
    tft.setTextColor(TFT_WHITE, TFT_BLACK);

    // We can now plot text on screen using the "print" class
    tft.println("Intialised default\n");
    tft.println("White text");

    tft.setTextColor(TFT_RED, TFT_BLACK);
    tft.println("Red text");

    tft.setTextColor(TFT_GREEN, TFT_BLACK);
    tft.println("Green text");

    tft.setTextColor(TFT_BLUE, TFT_BLACK);
    tft.println("Blue text");

    delay(5000);

}

void loop() {

    tft.invertDisplay(false);   // Where i is true or false

    tft.fillScreen(TFT_BLACK);

    tft.setCursor(0, 0, 4);

    tft.setTextColor(TFT_WHITE, TFT_BLACK);
    tft.println("Invert OFF\n");

    tft.println("White text");

    tft.setTextColor(TFT_RED, TFT_BLACK);
    tft.println("Red text");

    tft.setTextColor(TFT_GREEN, TFT_BLACK);
    tft.println("Green text");

    tft.setTextColor(TFT_BLUE, TFT_BLACK);
    tft.println("Blue text");

    delay(5000);


    // Binary inversion of colours
    tft.invertDisplay(true);   // Where i is true or false

    tft.fillScreen(TFT_BLACK);

    tft.setCursor(0, 0, 4);

    tft.setTextColor(TFT_WHITE, TFT_BLACK);
    tft.println("Invert ON\n");

    tft.println("White text");

    tft.setTextColor(TFT_RED, TFT_BLACK);
    tft.println("Red text");

    tft.setTextColor(TFT_GREEN, TFT_BLACK);
    tft.println("Green text");

    tft.setTextColor(TFT_BLUE, TFT_BLACK);
    tft.println("Blue text");

    delay(5000);
}

SpriteDrawPixel

/*

    Sketch to show how a Sprite is created, how to draw pixels
    and text within the Sprite and then push the Sprite onto
    the display screen.

    Example for library:
    https://github.com/Bodmer/TFT_eSPI

    A Sprite is notionally an invisible graphics screen that is
    kept in the processors RAM. Graphics can be drawn into the
    Sprite just as it can be drawn directly to the screen. Once
    the Sprite is completed it can be plotted onto the screen in
    any position. If there is sufficient RAM then the Sprite can
    be the same size as the screen and used as a frame buffer.

    A 16 bit Sprite occupies (2 * width * height) bytes in RAM.

    On a ESP8266 Sprite sizes up to 126 x 160 can be accomodated,
    this size requires 40kBytes of RAM for a 16 bit colour depth.

    When 8 bit colour depth sprites are created they occupy
    (width * height) bytes in RAM, so larger sprites can be
    created, or the RAM required is halved.

*/

// Set delay after plotting the sprite
#define DELAY 1000

// Width and height of sprite
#define WIDTH  128
#define HEIGHT 128

#include <TFT_eSPI.h>                 // Include the graphics library (this includes the sprite functions)

TFT_eSPI    tft = TFT_eSPI();         // Declare object "tft"

TFT_eSprite spr = TFT_eSprite(&tft);  // Declare Sprite object "spr" with pointer to "tft" object

void setup() {
    Serial.begin(250000);
    Serial.println();

    // Initialise the TFT registers
    tft.init();

    // Optionally set colour depth to 8 or 16 bits, default is 16 if not spedified
    // spr.setColorDepth(8);

    // Create a sprite of defined size
    spr.createSprite(WIDTH, HEIGHT);

    // Clear the TFT screen to blue
    tft.fillScreen(TFT_BLUE);
}

void loop(void) {
    // Fill the whole sprite with black (Sprite is in memory so not visible yet)
    spr.fillSprite(TFT_BLACK);

    // Number of pixels to draw
    uint16_t n = 100;

    // Draw 100 random color pixels at random positions in sprite
    while (n--) {
        uint16_t color = random(0x10000); // Returns color 0 - 0xFFFF
        int16_t x = random(WIDTH);        // Random x coordinate
        int16_t y = random(HEIGHT);       // Random y coordinate
        spr.drawPixel(x, y, color);       // Draw pixel in sprite
    }

    // Draw some lines
    spr.drawLine(1, 0, WIDTH, HEIGHT - 1, TFT_GREEN);
    spr.drawLine(0, 0, WIDTH, HEIGHT, TFT_GREEN);
    spr.drawLine(0, 1, WIDTH - 1, HEIGHT, TFT_GREEN);
    spr.drawLine(0, HEIGHT - 1, WIDTH - 1, 0, TFT_RED);
    spr.drawLine(0, HEIGHT, WIDTH, 0, TFT_RED);
    spr.drawLine(1, HEIGHT, WIDTH, 1, TFT_RED);

    // Draw some text with Middle Centre datum
    spr.setTextDatum(MC_DATUM);
    spr.drawString("Sprite", WIDTH / 2, HEIGHT / 2, 4);

    // Now push the sprite to the TFT at position 0,0 on screen
    spr.pushSprite(-40, -40);
    spr.pushSprite(tft.width() / 2 - WIDTH / 2, tft.height() / 2 - HEIGHT / 2);
    spr.pushSprite(tft.width() - WIDTH + 40, tft.height() - HEIGHT + 40);

    delay(DELAY);

    // Fill TFT screen with blue
    tft.fillScreen(TFT_BLUE);

    // Draw a blue rectangle in sprite so when we move it 1 pixel it does not leave a trail
    // on the blue screen background
    spr.drawRect(0, 0, WIDTH, HEIGHT, TFT_BLUE);

    int x = tft.width() / 2  -  WIDTH / 2;
    int y = tft.height() / 2 - HEIGHT / 2;

    uint32_t updateTime = 0;       // time for next update

    while (true) {
        // Random movement direction
        int dx = 1;
        if (random(2)) {
            dx = -1;
        }
        int dy = 1;
        if (random(2)) {
            dy = -1;
        }

        // Pull it back onto screen if it wanders off
        if (x < -WIDTH / 2) {
            dx = 1;
        }
        if (x >= tft.width() - WIDTH / 2) {
            dx = -1;
        }
        if (y < -HEIGHT / 2) {
            dy = 1;
        }
        if (y >= tft.height() - HEIGHT / 2) {
            dy = -1;
        }

        // Draw it 50 time, moving in random direct or staying still
        n = 50;
        int wait = random(50);
        while (n) {
            if (updateTime <= millis()) {
                // Use time delay so sprtie does not move fast when not all on screen
                updateTime = millis() + wait;

                // Push the sprite to the TFT screen
                spr.pushSprite(x, y);

                // Change coord for next loop
                x += dx;
                y += dy;
                n--;
                yield(); // Stop watchdog reset
            }
        }
    } // Infinite while, will not exit!
}

SpriteScroll1bit

/*
    Sketch to show scrolling of the graphics in sprites.

    This sketch scrolls a 1 bit per pixel (1 bpp) Sprite.

    In a 1 bit Sprite any color except TFT_BLACK turns a pixel "ON"
    TFT_BLACK turns a pixel "OFF".

    The 1 bpp Sprite has a unique property that other bit depth Sprites
    do not have, you can set the rotation of the coordinate frame e.g.:
    spr.setRotation(1);

    This is similar to screen rotations, so for example text can
    be drawn rotated:
    Rotation 0: Normal orientation
    Rotation 1: Coordinate frame rotated clockwise 90 degrees
    Rotation 2: Coordinate frame rotated clockwise 180 degrees (upside down)
    Rotation 3: Coordinate frame rotated clockwise 270 degrees

    When pushSprite is used the sprite is drawn with the width and height
    staying as created, so the created Sprite itself is not rotated during
    rendering. See stext2 sprite example below at line 83.

    ON and OFF pixels can be set to any two colors before
    rendering to the screen with pushSprite, for example:
    tft.setBitmapColor(ON_COLOR, OFF_COLOR);

    Scrolling moves the pixels in a defined rectangle within
    the Sprite. By defalt the whole sprite is scrolled.
    The gap left by scrolling is filled with a defined color.

    Example for library:
    https://github.com/Bodmer/TFT_eSPI

    A Sprite is notionally an invisible graphics screen that is
    kept in the processors RAM. Graphics can be drawn into the
    Sprite just as it can be drawn directly to the screen. Once
    the Sprite is completed it can be plotted onto the screen in
    any position. If there is sufficient RAM then the Sprite can
    be the same size as the screen and used as a frame buffer.

    A 1 bit Sprite occupies (width * height)/8 bytes in RAM.

*/

#include <TFT_eSPI.h>

TFT_eSPI tft = TFT_eSPI();

TFT_eSprite graph1 = TFT_eSprite(&tft); // Sprite object graph1

TFT_eSprite stext1 = TFT_eSprite(&tft); // Sprite object stext1

TFT_eSprite stext2 = TFT_eSprite(&tft); // Sprite object stext2

int graphVal = 1;
int delta = 1;
int grid = 0;
int tcount = 0;

//==========================================================================================
void setup() {
    tft.init();
    tft.fillScreen(TFT_BLACK);

    // Create a sprite for the graph
    graph1.setColorDepth(1);
    graph1.createSprite(128, 61);
    graph1.fillSprite(TFT_BLACK); // Note: Sprite is filled with black when created

    // The scroll area is set to the full sprite size upon creation of the sprite
    // but we can change that by defining a smaller area using "setScrollRect()"if needed
    // parameters are x,y,w,h,color as in drawRect(), the color fills the gap left by scrolling
    //graph1.setScrollRect(64, 0, 64, 61, TFT_BLACK);  // Try this line to change the graph scroll area

    // Create a sprite for the scrolling numbers
    stext1.setColorDepth(1);
    stext1.createSprite(32, 64);
    stext1.fillSprite(TFT_BLACK); // Fill sprite with blue
    stext1.setScrollRect(0, 0, 32, 64, TFT_BLACK);     // here we set scroll gap fill color to blue
    stext1.setTextColor(TFT_WHITE); // White text, no background
    stext1.setTextDatum(BR_DATUM);  // Bottom right coordinate datum

    // Create a sprite for Hello World
    stext2.setColorDepth(1);
    stext2.createSprite(16, 80); // Narrow and tall
    stext2.setRotation(1);       // Plot with 90 deg. clockwise rotation
    stext2.fillSprite(TFT_BLACK);
    stext2.setScrollRect(0, 0, 40, 16, TFT_BLACK); // Scroll the "Hello" in the first 40 pixels
    stext2.setTextColor(TFT_WHITE); // White text, no background
}

//==========================================================================================
void loop() {
    // Draw point in graph1 sprite at far right edge (this will scroll left later)
    graph1.drawFastVLine(127, 60 - graphVal, 2, TFT_WHITE); // draw 2 pixel point on graph

    // Draw number in stext1 sprite at 31,63 (bottom right datum set)
    stext1.drawNumber(graphVal, 31, 63, 2); // plot value in font 2

    // Push the sprites onto the TFT at specied coordinates
    tft.setBitmapColor(TFT_WHITE, TFT_BLUE); // Specify the colors of the ON and OFF pixels
    graph1.pushSprite(0, 0);

    tft.setBitmapColor(TFT_GREEN, TFT_BLACK);
    stext1.pushSprite(0, 64);

    tft.setBitmapColor(TFT_BLACK, TFT_YELLOW);
    stext2.pushSprite(60, 70);

    // Change the value to plot
    graphVal += delta;

    // If the value reaches a limit, then change delta of value
    if (graphVal >= 60) {
        delta = -1;    // ramp down value
    } else if (graphVal <= 1) {
        delta = +1;    // ramp up value
    }

    delay(50); // wait so things do not scroll too fast

    // Now scroll the sprites scroll(dt, dy) where:
    // dx is pixels to scroll, left = negative value, right = positive value
    // dy is pixels to scroll, up = negative value, down = positive value
    graph1.scroll(-1, 0); // scroll graph 1 pixel left, 0 up/down
    stext1.scroll(0, -16); // scroll stext 0 pixels left/right, 16 up
    stext2.scroll(1);     // scroll stext 1 pixel right, up/down default is 0

    // Draw the grid on far right edge of sprite as graph has now moved 1 pixel left
    grid++;
    if (grid >= 10) {
        // Draw a vertical line if we have scrolled 10 times (10 pixels)
        grid = 0;
        graph1.drawFastVLine(127, 0, 61, TFT_WHITE); // draw line on graph
    } else {
        // Otherwise draw points spaced 10 pixels for the horizontal grid lines
        for (int p = 0; p <= 60; p += 10) {
            graph1.drawPixel(127, p, TFT_WHITE);
        }
    }

    tcount--;
    if (tcount <= 0) {
        // If we have scrolled 40 pixels the redraw text
        tcount = 40;
        stext2.drawString("Hello World", 6, 0, 2); // draw at 6,0 in sprite, font 2
    }

} // Loop back and do it all again
//==========================================================================================

追加エリア


rem *********** 追加エリア ***********