API

Install and include our kgfx library to access these APIs.

In platformio.ini:

lib_deps = 
  kublet/KGFX@^0.0.22

Check the latest version of kgfx here.

In your source or header files:

#include <kgfx.h>

KGFX ui;

Index

TFT_eSprite createSprite

TFT_eSprite createSprite(int width, int height)

createSprite creates the sprite of the size of width*height in pixels, for drawing text or numbers on it so that the text or numbers can be regularly updated without causing flicker on the screen.

Example
TFT_eSprite textSpr = ui.createSprite(240, 30);
ui.drawText(textSpr, "hello", Arial_12, TFT_YELLOW, 0, 30);

TFT_eSPI_ext tft

TFT_eSPI_ext tft()

tft returns an instance of the tft library (extended), which was initialized in KGFX. With this instance, you can use any of the TFT_eSPI functions directly, on top of the helper functions KGFX provides.

Example
ui.init();
ui.clear();
ui.drawText("hello", Arial_28, TFT_YELLOW, 0, 0);
ui.tft().drawCircle(50, 50, 10, TFT_YELLOW);

void clear

void clear()

clear clears the screen by painting it black.

Example
ui.clear()

void createChartSprite

void createChartSprite()

createChartSprite enforces strict dimensions and creates a sprite of the size of 240*80 in pixels and stores that object as a private variable with the UI object so there is no need to manage the chart sprite object.

Example
ui.createChartSprite();
ui.drawChart(arr, K_GREEN, 10);

void deleteChartSprite

void deleteChartSprite()

deleteChartSprite deletes the chart sprite and frees up memory. Call this when you no longer need to use the chart sprite.

Example
ui.createChartSprite();
// do many things 
ui.deleteChartSprite();

void deleteSprite

void deleteSprite(TFT_eSprite &spr)

deleteSprite deletes the given sprite and frees up memory. Call this when you no longer need to use the sprite.

Example
TFT_eSprite textSpr = ui.createSprite(240, 30);
// do many things 
ui.deleteSprite(textSpr);

void drawChart

drawChart(std::vector<float> arr, int color, int y)

drawChart plots a graph with the given array. An array of maximum length of 30 will be plotted onto the chart sprite with a default size of 240 by 80 pixels. You must create the chart sprite first.

Example
float a[30] = {1, 3, 4, 12, 2, 1, 5, 4, 3, 12, 4, 5, 3, 2, 4, 4, 5, 6, 3, 2, 2, 4, 5, 2, 3, 4, 5, 6, 3, 4};
std::vector<float> arr;
for (int i=0; i<30; i++) {
  arr.push_back(a[i]);
}
  
ui.initChart();
ui.drawChart(arr, K_GREEN, 10);

void drawText

drawText(const char *txt, const tftfont_t &f, int color, int x, int y)

drawText draws text or numbers on the screen. Use this if text will be not updated at regular intervals. Numbers will be have to converted to const char format.

Example
drawText("hello", Arial_12, TFT_YELLOW, 0, 30);

To convert a float or int type variable to a string for printing on screen, use std::to_string(v).

Example
float v = 0.1; 
std::string strv = std::to_string(v);
drawText(strv.c_str(), Arial_12, TFT_YELLOW, 0, 30);

void drawText (sprite)

drawText(TFT_eSprite &spr, const char *txt, const tftfont_t &f, int color, int x, int y)

drawText (sprite) requires an additional argument, a sprite object type. Use this if your text or numbers will be updated at regular intervals. Numbers will be have to converted to const char format.

Use createSprite to create the sprite object.

Example
TFT_eSprite textSpr = ui.createSprite(240, 30);
drawText(textSpr, "23,445", Arial_12, TFT_YELLOW, 0, 30);
delay(1000);
drawText(textSpr, "23,454", Arial_12, TFT_YELLOW, 0, 30);

void init

void init()

init initializes the display library. Call this before you use any of the KFGX library.

Example
ui.init()