Skip to content

Canvas Class

moononournation edited this page Jun 2, 2021 · 3 revisions

Arduino_GFX provide various canvas class, sometimes it call framebuffer, for draw buffering on complicated presentation. The display only require refresh once after canvas draw finish and call flush(). It can reduce the display flicking substantially but requires more RAM.

First declare a canvas output display:

Arduino_DataBus *bus = create_default_Arduino_DataBus();
Arduino_GFX *output_display = new Arduino_ST7789(bus, TFT_RST, 0 /* rotation */, true /* IPS */);

Then declare canvas:

16-bit color Canvas

240x320 resolution only works for ESP32 with PSRAM

Arduino_GFX *gfx = new Arduino_Canvas(240 /* width */, 320 /* height */, output_display);

Indexed color Canvas

  • mask_level: 0-2, larger mask level mean less color variation but can have faster index mapping
Arduino_GFX *gfx = new Arduino_Canvas_Indexed(240 /* width */, 320 /* height */, output_display,
    0 /* output_x */, 0 /* output_y */, MAXMASKLEVEL /* mask_level */);

3-bit color Canvas

R1G1B1, 8 colors

Arduino_GFX *gfx = new Arduino_Canvas_3bit(240 /* width */, 320 /* height */, output_display,
    0 /* output_x */, 0 /* output_y */);

Mono color Canvas

Arduino_GFX *gfx = new Arduino_Canvas_Mono(240 /* width */, 320 /* height */, output_display,
    0 /* output_x */, 0 /* output_y */);

Sample code layout

#include <Arduino_GFX_Library.h>

// 16-bit color Canvas (240x240 resolution requires 116 kB RAM)
Arduino_G *output_display = new Arduino_GC9A01(bus, TFT_RST, 0 /* rotation */, true /* IPS */);
Arduino_GFX *gfx = new Arduino_Canvas(240 /* width */, 240 /* height */, output_display);

void setup()
{
  gfx->begin();

#ifdef TFT_BL
  pinMode(TFT_BL, OUTPUT);
  digitalWrite(TFT_BL, HIGH);
#endif
}

void loop()
{
  // insert gfx complicated draw here
  gfx->flush();
}
Clone this wiki locally