CHGame 0.3.0
Libraries for the CHGame handheld
Loading...
Searching...
No Matches
CHGfx_AdafruitGFX.h File Reference

Drop CHGfx in underneath Adafruit_GFX: existing sketches keep their drawing code and get the framebuffer and DMA transport. More...

#include "CHGfx.h"
#include <Adafruit_GFX.h>

Go to the source code of this file.

Classes

class  CHGfx_GFX
 An Adafruit_GFX display that draws into CHGfx's 4 bpp framebuffer and presents it by DMA. More...
 

Detailed Description

Drop CHGfx in underneath Adafruit_GFX: existing sketches keep their drawing code and get the framebuffer and DMA transport.

Why you might want this. Adafruit_GFX itself is not the slow part. The slow part is Adafruit_ST7735 underneath it, pushing every pixel down a polled SPI link with a fresh address window each time. Adafruit_GFX's own feature set - print(), setTextSize(), the custom GFXfont system, drawBitmap(), triangles, rounded rectangles - is perfectly good.

So: subclass Adafruit_GFX, point drawPixel() at the 4 bpp framebuffer, override the span primitives with CHGfx's fast paths, and present with display(). Existing sketches keep working and get the full DMA transport underneath.

#include <Adafruit_GFX.h> // see the note below - required
#include <CHGfx.h>
void setup() {
tft.begin(); // same as Gfx.begin()
tft.setPalette(myPalette, 16);
tft.setTextColor(WHITE_IDX);
tft.setCursor(4, 4);
tft.print("hello");
tft.display(); // one DMA burst
}
CHGfx: a maximum-throughput ST7735 driver for the CHGame board (CH32X035G8U6 @ 48 MHz,...
Drop CHGfx in underneath Adafruit_GFX: existing sketches keep their drawing code and get the framebuf...
An Adafruit_GFX display that draws into CHGfx's 4 bpp framebuffer and presents it by DMA.
Definition CHGfx_AdafruitGFX.h:106
void setPalette(const uint16_t *p, uint8_t n)
Sets the first n palette entries (gfx_setPalette()).
Definition CHGfx_AdafruitGFX.h:255
void begin(uint8_t spiDiv=GFX_DIV2, uint8_t colorMode=GFX_16BPP)
Starts the panel, as gfx_begin().
Definition CHGfx_AdafruitGFX.h:116
void display()
Sends the whole framebuffer to the panel and waits for it (gfx_flush()).
Definition CHGfx_AdafruitGFX.h:224

The one thing that changes. Adafruit_GFX passes colours as uint16_t. Here a colour is a PALETTE INDEX, 0..15 - there is no 16-colour framebuffer that can hold an arbitrary RGB565. Passing ST77XX_RED (0xF800) would be read as index 0, i.e. black, so map your colours once at setup:

const uint8_t RED = Gfx.nearest(0xF800);
uint8_t nearest(uint16_t rgb565) const
Nearest palette index for an RGB565 colour (slow: setup only).
Definition CHGfx.h:1531
@ RED
6: red (0xE12).
Definition Palette.h:48
CHGfx Gfx
The one CHGfx instance.

Set CHGFX_GFX_AUTOMAP if you would rather have colours mapped for you at every call. It makes old code work verbatim but costs a 16-entry search per drawing call, so it is off by default.

Fonts. Adafruit_GFX's own setFont() keeps working here, and CHGfx's bundled fonts are GFXfonts, so they go straight in:

#include <fonts/CHGfx_Sans12.h>
tft.setFont(&CHGfx_Sans12);

Note that tft.setFont() and Gfx.setFont() are SEPARATE state. The bridge routes drawing through Adafruit_GFX, which does its own glyph walking, so a font set on Gfx does not affect tft and vice versa. Pick one text path per sketch.

Requires the Adafruit GFX Library to be installed. This header is not included by CHGfx.h - include it yourself only if you want the bridge.

You must also write #include <Adafruit_GFX.h> in your .ino, above this include. That looks redundant, but the Arduino builder works out which library include paths to add by scanning the SKETCH file only. A library reached solely through another library's header never gets its path added, and the include below would fail to resolve.