### Arduino TFT_22_ILI9225 Initialization and Drawing Example Source: https://context7.com/nkawu/tft_22_ili9225/llms.txt This Arduino sketch demonstrates initializing the TFT_22_ILI9225 display, setting its orientation, background color, and drawing basic UI elements like rectangles, text, and lines. It also shows how to display dynamic sensor readings with different colors and a status indicator. Dependencies include SPI.h and TFT_22_ILI9225.h. ```cpp #include "SPI.h" #include "TFT_22_ILI9225.h" #define TFT_RST 8 #define TFT_RS 9 #define TFT_CS 10 #define TFT_LED 3 TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_LED); void setup() { // Initialize display tft.begin(); tft.clear(); tft.setOrientation(0); tft.setBackgroundColor(COLOR_BLACK); // Draw UI frame tft.drawRectangle(0, 0, tft.maxX() - 1, tft.maxY() - 1, COLOR_WHITE); // Display title tft.setFont(Terminal12x16); tft.drawText(20, 10, "Sensor Monitor", COLOR_CYAN); // Draw separator line tft.drawLine(10, 30, tft.maxX() - 10, 30, COLOR_GRAY); // Display sensor labels tft.setFont(Terminal6x8); tft.drawText(10, 40, "Temperature:", COLOR_WHITE); tft.drawText(10, 60, "Humidity:", COLOR_WHITE); tft.drawText(10, 80, "Pressure:", COLOR_WHITE); } void loop() { // Simulate sensor readings float temperature = 23.5; float humidity = 65.2; int pressure = 1013; // Display sensor values tft.setFont(Terminal12x16); // Temperature value with colored background tft.setBackgroundColor(COLOR_BLACK); String tempStr = String(temperature, 1) + "C"; tft.drawText(100, 35, tempStr, COLOR_RED); // Humidity value String humidStr = String(humidity, 1) + "%"; tft.drawText(100, 55, humidStr, COLOR_BLUE); // Pressure value String pressStr = String(pressure) + "hPa"; tft.drawText(100, 75, pressStr, COLOR_GREEN); // Draw status indicator static bool toggle = false; tft.fillCircle(tft.maxX() - 20, 15, 5, toggle ? COLOR_GREEN : COLOR_RED); toggle = !toggle; delay(1000); } ``` -------------------------------- ### Screen Orientation and Size Methods Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home Functions to set and retrieve the screen's orientation, and to get the maximum dimensions (width and height) of the display in pixels. These are crucial for correctly rendering graphics and text. ```C++ /// Set orientation /// @param orientation orientation, 0=portrait, 1=right rotated landscape, 2=reverse portrait, 3=left rotated landscape void setOrientation(uint8_t orientation); ``` ```C++ /// Get orientation /// @return orientation orientation, 0=portrait, 1=right rotated landscape, 2=reverse portrait, 3=left rotated landscape uint8_t getOrientation(void); ``` ```C++ /// Screen size, x-axis /// @return horizontal size of the screen, in pixels /// @note 240 means 240 pixels and thus 0..239 coordinates (decimal) uint16_t maxX(void); ``` ```C++ /// Screen size, y-axis /// @return vertical size of the screen, in pixels /// @note 220 means 220 pixels and thus 0..219 coordinates (decimal) uint16_t maxY(void); ``` -------------------------------- ### Include and Select GFX Font in Arduino Sketch Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home Demonstrates how to include a GFX font file in an Arduino sketch and select it for use with the library. It shows the necessary include directive and the function call to set the desired font. It also provides an example of adjusting the Y coordinate for text rendering based on text dimensions. ```c++ #include <../fonts/FreeSans12pt7b.h> ``` ```c++ tft.setGFXFont(&FreeSans12pt7b); ``` ```c++ tft.getGFXTextExtent("AbCd", x, y, &w, &h); y = h; // Move the origin point down the height of the printed string tft.drawGFXText(x, y, "AbCd", COLOR_RED); ``` -------------------------------- ### Drawing Text and Fonts Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home Methods for drawing text on the screen. This includes drawing individual characters, ASCII strings, and strings using Adafruit GFX compatible fonts. Functionality also includes setting the current font and getting text extent. ```C++ /// Draw pixel /// @param x pixel coordinate, x-axis /// @param y pixel coordinate, y-axis /// @param color 16-bit color void drawPixel(uint16_t x, uint16_t y, uint16_t color); ``` ```C++ /// Draw ASCII Text (pixel coordinates) /// @param x pixel coordinate, x-axis /// @param y pixel coordinate, y-axis /// @param string text to draw /// @param color 16-bit color void drawText(uint16_t x, uint16_t y, const char* string, uint16_t color); ``` ```C++ /// Draw triangle, triangle coordinates /// @param x1 vertex 1, x-axis /// @param y1 vertex 1, y-axis /// @param x2 vertex 2, x-axis /// @param y2 vertex 2, y-axis /// @param x3 vertex 3, x-axis /// @param y3 vertex 3, y-axis /// @param color 16-bit color void drawTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint16_t color); ``` ```C++ /// Draw solid triangle, triangle coordinates /// @param x1 vertex 1, x-axis /// @param y1 vertex 1, y-axis /// @param x2 vertex 2, x-axis /// @param y2 vertex 2, y-axis /// @param x3 vertex 3, x-axis /// @param y3 vertex 3, y-axis /// @param color 16-bit color void fillTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint16_t color); ``` ```C++ /// Set font /// @param font pointer to font structure void setFont(const GFXfont *font); ``` ```C++ /// Draw single character (pixel coordinates) /// @param x pixel coordinate, x-axis /// @param y pixel coordinate, y-axis /// @param character to draw /// @param color 16-bit color void drawChar(uint16_t x, uint16_t y, char character, uint16_t color); ``` ```C++ /// Set current GFX font /// @param font pointer to GFX font structure void setGFXFont(const GFXfont *font); ``` ```C++ /// Draw a string with the current GFX font /// @param x starting pixel coordinate, x-axis /// @param y starting pixel coordinate, y-axis /// @param string text to draw /// @param color 16-bit color void drawGFXText(uint16_t x, uint16_t y, const char* string, uint16_t color); ``` ```C++ /// Get the width & height of a text string with the current GFX font /// @param string text to measure /// @param width output width in pixels /// @param height output height in pixels void getGFXTextExtent(const char* string, uint16_t* width, uint16_t* height); ``` ```C++ /// Draw a single character with the current GFX font /// @param x starting pixel coordinate, x-axis /// @param y starting pixel coordinate, y-axis /// @param character to draw /// @param color 16-bit color void drawGFXChar(uint16_t x, uint16_t y, char character, uint16_t color); ``` -------------------------------- ### Initialization and Screen Control Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home Methods for initializing the display, clearing the screen, controlling backlight, display on/off, and setting screen orientation. ```APIDOC ## Initialization and Screen Control ### begin #### Description Initializes the display. #### Method `void begin(void);` ### clear #### Description Clears the entire screen. #### Method `void clear(void);` ### invert #### Description Inverts or normalizes the screen colors. #### Method `void invert(boolean flag);` #### Parameters - **flag** (boolean) - `true` to invert, `false` for normal screen. ### setBacklight #### Description Switches the backlight on or off. #### Method `void setBacklight(boolean flag);` #### Parameters - **flag** (boolean) - `true` to turn on, `false` to turn off. ### setBacklightBrightness #### Description Sets the backlight brightness level. #### Method `void setBacklightBrightness(uint8_t brightness);` #### Parameters - **brightness** (uint8_t) - Brightness value from 0 (off) to 255 (maximum). ### setDisplay #### Description Switches the display on or off. #### Method `void setDisplay(boolean flag);` #### Parameters - **flag** (boolean) - `true` to turn on, `false` to turn off. ### setOrientation #### Description Sets the screen orientation. #### Method `void setOrientation(uint8_t orientation);` #### Parameters - **orientation** (uint8_t) - Orientation setting: `0` (portrait), `1` (right rotated landscape), `2` (reverse portrait), `3` (left rotated landscape). ### getOrientation #### Description Gets the current screen orientation. #### Method `uint8_t getOrientation(void);` #### Returns - uint8_t: The current orientation value (0-3). ``` -------------------------------- ### Initialization and Screen Control Methods Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home Methods for initializing the display, clearing the screen, inverting colors, controlling the backlight, and setting display on/off states. These functions are fundamental for basic screen operation. ```C++ /// Initialization void begin(void); ``` ```C++ /// Clear the screen void clear(void); ``` ```C++ /// Invert screen /// @param flag true to invert, false for normal screen void invert(boolean flag); ``` ```C++ /// Switch backlight on or off /// @param flag true=on, false=off void setBacklight(boolean flag); ``` ```C++ /// Set backlight brightness /// @param brightness sets backlight brightness 0-255 void setBacklightBrightness(uint8_t brightness); ``` ```C++ /// Switch display on or off /// @param flag true=on, false=off void setDisplay(boolean flag); ``` -------------------------------- ### TFT Display Initialization with Arduino Source: https://context7.com/nkawu/tft_22_ili9225/llms.txt Initializes the TFT display hardware using either hardware or software SPI. Sets up essential pins, brightness, and configures the display orientation. Requires the SPI library and the TFT_22_ILI9225 library. ```cpp #include "SPI.h" #include "TFT_22_ILI9225.h" // Hardware SPI pins (faster) #define TFT_RST 8 #define TFT_RS 9 #define TFT_CS 10 #define TFT_LED 3 #define TFT_BRIGHTNESS 200 // Create display object using hardware SPI TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_LED, TFT_BRIGHTNESS); // Alternative: Software SPI (slower but flexible pins) // TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_SDI, TFT_CLK, TFT_LED, TFT_BRIGHTNESS); void setup() { tft.begin(); // Initialize display tft.clear(); // Clear screen to black tft.setOrientation(0); // 0=portrait, 1=landscape right, 2=reverse portrait, 3=landscape left } ``` -------------------------------- ### Color and Background Setting Methods Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home Functions to set the background color of the display and to convert between RGB components and a 16-bit color format. The `setColor` function takes 8-bit R, G, B values, while `splitColor` does the reverse. ```C++ /// Set background color /// @param color background color, default=black void setBackgroundColor(uint16_t color = COLOR_BLACK); ``` ```C++ /// Calculate 16-bit color from 8-bit Red-Green-Blue components /// @param red 8-bit red component /// @param green 8-bit green component /// @param blue 8-bit blue component /// @return 16-bit color value uint16_t setColor(uint8_t red, uint8_t green, uint8_t blue); ``` ```C++ /// Calculate 8-bit Red-Green-Blue components from 16-bit color /// @param color 16-bit color value /// @param red output 8-bit red component /// @param green output 8-bit green component /// @param blue output 8-bit blue component void splitColor(uint16_t color, uint8_t& red, uint8_t& green, uint8_t& blue); ``` -------------------------------- ### Screen Dimensions and Color Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home Methods for retrieving screen dimensions and handling color values. ```APIDOC ## Screen Dimensions and Color ### maxX #### Description Gets the maximum X-axis dimension (width) of the screen in pixels. #### Method `uint16_t maxX(void);` #### Returns - uint16_t: The horizontal size of the screen in pixels. For a 240-pixel width, coordinates range from 0 to 239. ### maxY #### Description Gets the maximum Y-axis dimension (height) of the screen in pixels. #### Method `uint16_t maxY(void);` #### Returns - uint16_t: The vertical size of the screen in pixels. For a 220-pixel height, coordinates range from 0 to 219. ### setColor #### Description Calculates a 16-bit color value from 8-bit Red, Green, and Blue components. #### Method `void setColor(uint8_t red, uint8_t green, uint8_t blue);` #### Parameters - **red** (uint8_t) - 8-bit red component (0-255). - **green** (uint8_t) - 8-bit green component (0-255). - **blue** (uint8_t) - 8-bit blue component (0-255). ### splitColor #### Description Calculates the 8-bit Red, Green, and Blue components from a 16-bit color value. #### Method `void splitColor(uint16_t color, uint8_t &red, uint8_t &green, uint8_t &blue);` #### Parameters - **color** (uint16_t) - The 16-bit color value. - **red** (uint8_t&) - Reference to store the 8-bit red component. - **green** (uint8_t&) - Reference to store the 8-bit green component. - **blue** (uint8_t&) - Reference to store the 8-bit blue component. ``` -------------------------------- ### Bitmap Rendering Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home Methods for drawing bitmaps, including standard bitmaps and XBM format. ```APIDOC ## Bitmap Rendering ### drawBitmap #### Description Draws a bitmap image at the specified coordinates. #### Method `void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], uint16_t w, uint16_t h, uint16_t color);` #### Parameters - **x** (int16_t) - The x-coordinate for the top-left corner of the bitmap. - **y** (int16_t) - The y-coordinate for the top-left corner of the bitmap. - **bitmap[]** (const uint8_t[]) - Pointer to the byte array containing the bitmap data. - **w** (uint16_t) - The width of the bitmap in pixels. - **h** (uint16_t) - The height of the bitmap in pixels. - **color** (uint16_t) - The 16-bit color to draw the bitmap with. ### drawXBitmap #### Description Draws an XBM (X BitMap) file format image at the specified coordinates. #### Method `void drawXBitmap(int16_t x, int16_t y, const uint8_t bitmap[], uint16_t w, uint16_t h, uint16_t color);` #### Parameters - **x** (int16_t) - The x-coordinate for the top-left corner of the bitmap. - **y** (int16_t) - The y-coordinate for the top-left corner of the bitmap. - **bitmap[]** (const uint8_t[]) - Pointer to the byte array containing the XBM bitmap data. - **w** (uint16_t) - The width of the bitmap in pixels. - **h** (uint16_t) - The height of the bitmap in pixels. - **color** (uint16_t) - The 16-bit color to draw the bitmap with. ``` -------------------------------- ### Text Rendering Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home Methods for setting fonts and drawing text, including ASCII and GFX fonts. ```APIDOC ## Text Rendering ### setBackgroundColor #### Description Sets the background color for subsequent text drawing operations. Defaults to black. #### Method `void setBackgroundColor(uint16_t color = COLOR_BLACK);` #### Parameters - **color** (uint16_t) - The 16-bit background color. `COLOR_BLACK` is the default. ### setFont #### Description Sets the current font for ASCII text rendering. #### Method `void setFont(const GFXfont *f = NULL);` #### Parameters - **f** (const GFXfont *) - Pointer to the GFXfont structure. If `NULL`, the default font is used. ### drawText #### Description Draws ASCII text at the specified pixel coordinates using the current font. #### Method `void drawText(uint16_t x, uint16_t y, const char *text, uint16_t color);` #### Parameters - **x** (uint16_t) - The x-coordinate for the start of the text. - **y** (uint16_t) - The y-coordinate for the start of the text. - **text** (const char *) - Pointer to the null-terminated string to draw. - **color** (uint16_t) - The 16-bit color of the text. ### drawChar #### Description Draws a single ASCII character at the specified pixel coordinates using the current font. #### Method `void drawChar(uint16_t x, uint16_t y, char c, uint16_t color);` #### Parameters - **x** (uint16_t) - The x-coordinate for the character. - **y** (uint16_t) - The y-coordinate for the character. - **c** (char) - The character to draw. - **color** (uint16_t) - The 16-bit color of the character. ### setGFXFont #### Description Sets the current font for GFX text rendering. #### Method `void setGFXFont(const GFXfont *f);` #### Parameters - **f** (const GFXfont *) - Pointer to the GFXfont structure. ### drawGFXText #### Description Draws a string using the currently set GFX font. #### Method `void drawGFXText(int16_t x, int16_t y, const char *text, uint16_t color);` #### Parameters - **x** (int16_t) - The x-coordinate for the start of the text. - **y** (int16_t) - The y-coordinate for the start of the text. - **text** (const char *) - Pointer to the null-terminated string to draw. - **color** (uint16_t) - The 16-bit color of the text. ### getGFXTextExtent #### Description Gets the width and height of a text string when rendered with the current GFX font. #### Method `void getGFXTextExtent(const char *text, int16_t &width, int16_t &height);` #### Parameters - **text** (const char *) - Pointer to the null-terminated string. - **width** (int16_t&) - Reference to store the calculated width of the text. - **height** (int16_t&) - Reference to store the calculated height of the text. ### drawGFXChar #### Description Draws a single character using the currently set GFX font. #### Method `void drawGFXChar(int16_t x, int16_t y, unsigned char c, uint16_t color);` #### Parameters - **x** (int16_t) - The x-coordinate for the character. - **y** (int16_t) - The y-coordinate for the character. - **c** (unsigned char) - The character to draw. - **color** (uint16_t) - The 16-bit color of the character. ``` -------------------------------- ### Display Monochrome and Color Bitmaps Source: https://context7.com/nkawu/tft_22_ili9225/llms.txt Renders monochrome (1-bit) and color (16-bit RGB565) bitmaps from program memory or RAM. Supports transparent backgrounds and different bitmap formats like XBitmap. Requires the TFT library and PROGMEM. ```cpp // Define bitmap in PROGMEM (1-bit monochrome) static const uint8_t PROGMEM logo[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; // Define color bitmap (16-bit RGB565) static const uint16_t PROGMEM colorBitmap[] = { COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_YELLOW, COLOR_CYAN, COLOR_MAGENTA, COLOR_WHITE, COLOR_BLACK }; void displayBitmaps() { // Draw monochrome bitmap (transparent background) // Bitmap: 32 pixels wide x 4 pixels high tft.drawBitmap(10, 10, logo, 32, 4, COLOR_WHITE); // Draw monochrome bitmap with background color tft.drawBitmap(50, 10, logo, 32, 4, COLOR_RED, COLOR_BLUE); // Draw XBitmap (format from GIMP export) // XBitmap format has reversed bit order tft.drawXBitmap(10, 30, logo, 32, 4, COLOR_GREEN); // Draw color bitmap (1D array) // 4 pixels wide x 2 pixels high tft.drawBitmap(10, 50, colorBitmap, 4, 2); // Draw large color bitmap (2D array for row-based access) const uint16_t* rows[2] = { &colorBitmap[0], &colorBitmap[4] }; tft.drawBitmap(50, 50, rows, 4, 2); } ``` -------------------------------- ### Control Display Orientation and Power Source: https://context7.com/nkawu/tft_22_ili9225/llms.txt Enables control over display orientation, backlight, screen inversion, and power state. Supports setting brightness and retrieving current screen dimensions. Requires the TFT library. ```cpp void displayControl() { // Set display orientation // 0 = portrait (176x220) // 1 = landscape right (220x176) // 2 = reverse portrait (176x220) // 3 = landscape left (220x176) tft.setOrientation(1); // Get current orientation uint8_t currentOrientation = tft.getOrientation(); // Get screen dimensions based on current orientation uint16_t width = tft.maxX(); // Returns 220 in landscape uint16_t height = tft.maxY(); // Returns 176 in landscape // Control backlight tft.setBacklight(true); // Turn on backlight tft.setBacklight(false); // Turn off backlight // Set backlight brightness (0-255) tft.setBacklightBrightness(128); // 50% brightness // Turn display on/off (preserves content) tft.setDisplay(true); // Display on tft.setDisplay(false); // Display off (low power) // Invert display colors tft.invert(true); // White becomes black, black becomes white tft.invert(false); // Normal display // Clear entire screen to black tft.clear(); } ``` -------------------------------- ### Bitmap Drawing Methods Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home Functions for drawing bitmap images onto the display. Supports standard bitmaps and XBM (X BitMap) files, which are often used for monochrome images. These methods require coordinates and color information. ```C++ /// Draw bitmap /// @param x starting pixel coordinate, x-axis /// @param y starting pixel coordinate, y-axis /// @param bitmap pointer to bitmap data /// @param width width of the bitmap in pixels /// @param height height of the bitmap in pixels /// @param color 16-bit color void drawBitmap(uint16_t x, uint16_t y, const uint8_t *bitmap, uint16_t width, uint16_t height, uint16_t color); ``` ```C++ /// Draw XBitMap Files (*.xbm), exported from GIMP /// @param x starting pixel coordinate, x-axis /// @param y starting pixel coordinate, y-axis /// @param xbm pointer to XBM data /// @param width width of the bitmap in pixels /// @param height height of the bitmap in pixels /// @param color 16-bit color void drawXBitmap(uint16_t x, uint16_t y, const uint8_t *xbm, uint16_t width, uint16_t height, uint16_t color); ``` -------------------------------- ### Manage 16-bit RGB565 Colors Source: https://context7.com/nkawu/tft_22_ili9225/llms.txt Demonstrates creating, manipulating, and splitting 16-bit RGB565 color values. Supports custom RGB inputs and utilizes pre-defined color constants. Requires the TFT library. ```cpp void colorOperations() { // Create custom color from RGB components (0-255) uint16_t purple = tft.setColor(128, 0, 128); tft.fillRectangle(10, 10, 60, 60, purple); // Create orange color uint16_t orange = tft.setColor(255, 165, 0); tft.fillCircle(88, 110, 30, orange); // Split color back to components uint8_t red, green, blue; tft.splitColor(COLOR_CYAN, red, green, blue); // Use extracted values: red, green, blue (0-255 range) // Pre-defined colors available: // COLOR_BLACK, COLOR_WHITE, COLOR_RED, COLOR_GREEN, COLOR_BLUE // COLOR_CYAN, COLOR_MAGENTA, COLOR_YELLOW, COLOR_ORANGE // COLOR_PURPLE, COLOR_NAVY, COLOR_DARKGREEN, COLOR_GRAY, etc. } ``` -------------------------------- ### TFT Text Rendering with GFX Fonts Source: https://context7.com/nkawu/tft_22_ili9225/llms.txt Renders text using Adafruit GFX compatible fonts, offering anti-aliasing and variable character sizing. This requires including specific GFX font header files. Supports measuring text extents for precise placement. ```cpp #include "fonts/FreeSansBold12pt7b.h" #include "fonts/FreeSerif18pt7b.h" void displayGFXText() { // Set GFX font tft.setGFXFont(&FreeSansBold12pt7b); // Draw text with GFX font (note: y coordinate is baseline) tft.drawGFXText(10, 30, "Hello GFX!", COLOR_YELLOW); // Change to different GFX font tft.setGFXFont(&FreeSerif18pt7b); tft.drawGFXText(10, 60, "Serif Font", COLOR_CYAN); // Get text dimensions for positioning int16_t w, h; tft.getGFXTextExtent("Measure", 0, 0, &w, &h); // Draw text centered using dimensions uint16_t centerX = (tft.maxX() - w) / 2; tft.drawGFXText(centerX, 100, "Measure", COLOR_WHITE); // Reset to built-in fonts tft.setGFXFont(NULL); } ``` -------------------------------- ### Define and Use Custom C Fonts Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home This section explains how to create and use custom fonts compatible with the library, typically generated using GLCD Font Creator. It details the required data format for the font array, including dimensions, offset, and character data, and how to declare it using PROGMEM for Arduino. ```c++ const uint8_t FontName[] PROGMEM = { 0x06, 0x08, 0x20, 0x60, // width = 6, height = 8, offset = 32 (space), characters = 96 (32 - 127)*** 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Code for char }; ``` ```c++ extern uint8_t FontName[]; ``` -------------------------------- ### Drawing Primitives Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home Methods for drawing basic shapes like circles, lines, rectangles, and pixels. ```APIDOC ## Drawing Primitives ### drawPixel #### Description Draws a single pixel at the specified coordinates. #### Method `void drawPixel(uint16_t x, uint16_t y, uint16_t color);` #### Parameters - **x** (uint16_t) - The x-coordinate of the pixel. - **y** (uint16_t) - The y-coordinate of the pixel. - **color** (uint16_t) - The 16-bit color of the pixel. ### drawLine #### Description Draws a line between two points. #### Method `void drawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);` #### Parameters - **x1** (uint16_t) - The x-coordinate of the first point. - **y1** (uint16_t) - The y-coordinate of the first point. - **x2** (uint16_t) - The x-coordinate of the second point. - **y2** (uint16_t) - The y-coordinate of the second point. - **color** (uint16_t) - The 16-bit color of the line. ### drawRectangle #### Description Draws a rectangle outline. #### Method `void drawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);` #### Parameters - **x1** (uint16_t) - The x-coordinate of the top-left corner. - **y1** (uint16_t) - The y-coordinate of the top-left corner. - **x2** (uint16_t) - The x-coordinate of the bottom-right corner. - **y2** (uint16_t) - The y-coordinate of the bottom-right corner. - **color** (uint16_t) - The 16-bit color of the rectangle outline. ### fillRectangle #### Description Draws a solid (filled) rectangle. #### Method `void fillRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);` #### Parameters - **x1** (uint16_t) - The x-coordinate of the top-left corner. - **y1** (uint16_t) - The y-coordinate of the top-left corner. - **x2** (uint16_t) - The x-coordinate of the bottom-right corner. - **y2** (uint16_t) - The y-coordinate of the bottom-right corner. - **color** (uint16_t) - The 16-bit fill color of the rectangle. ### drawCircle #### Description Draws a circle outline. #### Method `void drawCircle(uint16_t x0, uint16_t y0, uint16_t radius, uint16_t color);` #### Parameters - **x0** (uint16_t) - The x-coordinate of the circle's center. - **y0** (uint16_t) - The y-coordinate of the circle's center. - **radius** (uint16_t) - The radius of the circle. - **color** (uint16_t) - The 16-bit color of the circle outline. ### fillCircle #### Description Draws a solid (filled) circle. #### Method `void fillCircle(uint8_t x0, uint8_t y0, uint8_t radius, uint16_t color);` #### Parameters - **x0** (uint8_t) - The x-coordinate of the circle's center. - **y0** (uint8_t) - The y-coordinate of the circle's center. - **radius** (uint8_t) - The radius of the circle. - **color** (uint16_t) - The 16-bit fill color of the circle. ### drawTriangle #### Description Draws a triangle outline. #### Method `void drawTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint16_t color);` #### Parameters - **x1, y1** (uint16_t) - Coordinates of the first vertex. - **x2, y2** (uint16_t) - Coordinates of the second vertex. - **x3, y3** (uint16_t) - Coordinates of the third vertex. - **color** (uint16_t) - The 16-bit color of the triangle outline. ### fillTriangle #### Description Draws a solid (filled) triangle. #### Method `void fillTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint16_t color);` #### Parameters - **x1, y1** (uint16_t) - Coordinates of the first vertex. - **x2, y2** (uint16_t) - Coordinates of the second vertex. - **x3, y3** (uint16_t) - Coordinates of the third vertex. - **color** (uint16_t) - The 16-bit fill color of the triangle. ``` -------------------------------- ### Bitmap Display Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home Functions for drawing bitmaps from memory. ```APIDOC ## drawBitmap ### Description Draws a 1-bit image (bitmap) from a buffer onto the display. ### Method `void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color)` ### Parameters #### Path Parameters - **x** (int16_t) - Required - The x-coordinate of the bitmap's top-left corner. - **y** (int16_t) - Required - The y-coordinate of the bitmap's top-left corner. - **bitmap** (const uint8_t*) - Required - Pointer to the bitmap data (PROGMEM recommended). - **w** (int16_t) - Required - The width of the bitmap in pixels. - **h** (int16_t) - Required - The height of the bitmap in pixels. - **color** (uint16_t) - Required - The foreground color for set bits. ### Response #### Success Response (200) - **void** - This function does not return a value. ### Method `void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg)` ### Parameters #### Path Parameters - **x** (int16_t) - Required - The x-coordinate of the bitmap's top-left corner. - **y** (int16_t) - Required - The y-coordinate of the bitmap's top-left corner. - **bitmap** (const uint8_t*) - Required - Pointer to the bitmap data (PROGMEM recommended). - **w** (int16_t) - Required - The width of the bitmap in pixels. - **h** (int16_t) - Required - The height of the bitmap in pixels. - **color** (uint16_t) - Required - The foreground color for set bits. - **bg** (uint16_t) - Required - The background color for unset bits. ### Response #### Success Response (200) - **void** - This function does not return a value. ### Method `void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color)` ### Parameters #### Path Parameters - **x** (int16_t) - Required - The x-coordinate of the bitmap's top-left corner. - **y** (int16_t) - Required - The y-coordinate of the bitmap's top-left corner. - **bitmap** (uint8_t*) - Required - Pointer to the bitmap data (RAM-resident). - **w** (int16_t) - Required - The width of the bitmap in pixels. - **h** (int16_t) - Required - The height of the bitmap in pixels. - **color** (uint16_t) - Required - The foreground color for set bits. ### Response #### Success Response (200) - **void** - This function does not return a value. ### Method `void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg)` ### Parameters #### Path Parameters - **x** (int16_t) - Required - The x-coordinate of the bitmap's top-left corner. - **y** (int16_t) - Required - The y-coordinate of the bitmap's top-left corner. - **bitmap** (uint8_t*) - Required - Pointer to the bitmap data (RAM-resident). - **w** (int16_t) - Required - The width of the bitmap in pixels. - **h** (int16_t) - Required - The height of the bitmap in pixels. - **color** (uint16_t) - Required - The foreground color for set bits. - **bg** (uint16_t) - Required - The background color for unset bits. ### Response #### Success Response (200) - **void** - This function does not return a value. ``` -------------------------------- ### Text Rendering Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home Functions for displaying text and characters on the display. ```APIDOC ## drawText ### Description Draws ASCII text at the specified coordinates. ### Method `void drawText(uint16_t x, uint16_t y, String s, uint16_t color = COLOR_WHITE)` ### Parameters #### Path Parameters - **x** (uint16_t) - Required - The x-coordinate of the text's starting point. - **y** (uint16_t) - Required - The y-coordinate of the text's starting point. - **s** (String) - Required - The text string to display. - **color** (uint16_t) - Optional - The 16-bit color of the text. Defaults to white. ### Response #### Success Response (200) - **void** - This function does not return a value. ## drawChar ### Description Draws a single ASCII character at the specified coordinates. ### Method `uint16_t drawChar(uint16_t x, uint16_t y, uint16_t ch, uint16_t color = COLOR_WHITE)` ### Parameters #### Path Parameters - **x** (uint16_t) - Required - The x-coordinate of the character's position. - **y** (uint16_t) - Required - The y-coordinate of the character's position. - **ch** (uint16_t) - Required - The ASCII value of the character to draw. - **color** (uint16_t) - Optional - The 16-bit color of the character. Defaults to white. ### Response #### Success Response (200) - **width** (uint16_t) - The width of the character in display pixels. ``` -------------------------------- ### Drawing Basic Shapes on TFT Display Source: https://context7.com/nkawu/tft_22_ili9225/llms.txt Demonstrates how to draw various geometric primitives including pixels, lines, rectangles, circles, and triangles. Colors are specified using the RGB565 format constants provided by the library. ```cpp void drawShapes() { // Draw single pixel tft.drawPixel(50, 50, COLOR_RED); // Draw line from (x1,y1) to (x2,y2) tft.drawLine(10, 10, 100, 100, COLOR_BLUE); // Draw hollow rectangle tft.drawRectangle(20, 30, 120, 80, COLOR_GREEN); // Draw filled rectangle tft.fillRectangle(30, 40, 110, 70, COLOR_YELLOW); // Draw hollow circle (center x, center y, radius) tft.drawCircle(88, 110, 40, COLOR_CYAN); // Draw filled circle tft.fillCircle(88, 110, 30, COLOR_MAGENTA); // Draw hollow triangle tft.drawTriangle(10, 150, 50, 180, 90, 150, COLOR_WHITE); // Draw filled triangle tft.fillTriangle(20, 160, 50, 180, 80, 160, COLOR_ORANGE); } ``` -------------------------------- ### TFT Text Rendering with Built-in Fonts Source: https://context7.com/nkawu/tft_22_ili9225/llms.txt Renders text on the TFT display using the library's integrated bitmap fonts. Allows setting foreground and background colors, selecting different font sizes, and calculating text width for alignment. ```cpp void displayText() { // Set built-in font tft.setFont(Terminal6x8); // Small font (6x8 pixels) tft.setBackgroundColor(COLOR_BLACK); // Draw text at position (x, y) tft.drawText(10, 10, "Hello World!", COLOR_WHITE); // Use larger font tft.setFont(Terminal12x16); // Larger font (12x16 pixels) tft.setBackgroundColor(COLOR_BLUE); tft.drawText(10, 30, "BIG TEXT", COLOR_RED); // Get text width for alignment tft.setFont(Terminal6x8); uint16_t textWidth = tft.getTextWidth("Center Me"); uint16_t centerX = (tft.maxX() - textWidth) / 2; tft.setBackgroundColor(COLOR_BLACK); tft.drawText(centerX, 50, "Center Me", COLOR_GREEN); // Available fonts: Terminal6x8, Terminal11x16, Terminal12x16, Trebuchet_MS16x21 } ``` -------------------------------- ### Draw Bitmap (C++) Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home Draws a bitmap image at the specified (x, y) coordinates. It supports both 1-bit images stored in PROGMEM or RAM. Overloads are provided for drawing with and without a background color, and for bitmaps with transparent or opaque pixels. ```C++ void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color); ``` ```C++ void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg); ``` ```C++ void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color); ``` ```C++ void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg); ``` -------------------------------- ### Color Manipulation Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home Functions for converting between RGB components and 16-bit color values. ```APIDOC ## setColor ### Description Calculates a 16-bit color value from 8-bit Red, Green, and Blue components. ### Method `uint16_t setColor(uint8_t red, uint8_t green, uint8_t blue)` ### Parameters #### Path Parameters - **red** (uint8_t) - Required - The red component (0x00 to 0xff). - **green** (uint8_t) - Required - The green component (0x00 to 0xff). - **blue** (uint8_t) - Required - The blue component (0x00 to 0xff). ### Response #### Success Response (200) - **color** (uint16_t) - The calculated 16-bit color value. ## splitColor ### Description Extracts 8-bit Red, Green, and Blue components from a 16-bit color value. ### Method `void splitColor(uint16_t rgb, uint8_t &red, uint8_t &green, uint8_t &blue)` ### Parameters #### Path Parameters - **rgb** (uint16_t) - Required - The 16-bit color value. - **red** (uint8_t&) - Output - The extracted red component (0x00 to 0xff). - **green** (uint8_t&) - Output - The extracted green component (0x00 to 0xff). - **blue** (uint8_t&) - Output - The extracted blue component (0x00 to 0xff). ### Response #### Success Response (200) - **void** - This function does not return a value. ``` -------------------------------- ### Draw XBitmap from GIMP Files Source: https://github.com/nkawu/tft_22_ili9225/wiki/Home This function allows drawing bitmap images exported from GIMP in *.xbm format. The C array representation of the bitmap can be directly used. Ensure the bitmap data, width, height, and color are correctly provided. ```c++ /// Draw XBitMap Files (*.xbm), exported from GIMP /// @param x point coordinate, x-axis /// @param y point coordinate, y-axis /// @param bitmap /// @param w width /// @param h height /// @param color 16-bit color, default=white void drawXBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color); ```