65 #error "CHGfx targets the CH32X035. It drives SPI1 and DMA1 channel 3 directly and is not portable as-is. Select a CH32X035 board."
112#define GFX_FB_STRIDE (GFX_W / 2)
114#define GFX_FB_BYTES (GFX_FB_STRIDE * GFX_H)
118#ifndef GFX_CHUNK_ROWS
135#define GFX_CHUNK_ROWS 2
142#define GFX_CHUNK_BYTES (GFX_W * GFX_CHUNK_ROWS * 2)
167#define CHGFX_CS_PORT GPIOA
169#define CHGFX_CS_PIN 4
173#define CHGFX_DC_PORT GPIOB
175#define CHGFX_DC_PIN 0
177#ifndef CHGFX_RST_PORT
179#define CHGFX_RST_PORT GPIOB
181#define CHGFX_RST_PIN 12
184#ifndef CHGFX_SDCS_PORT
192#define CHGFX_SDCS_PORT GPIOB
194#define CHGFX_SDCS_PIN 11
281void gfx__begin(uint8_t spiDiv, uint8_t colorMode);
282void gfx__begin12(uint8_t spiDiv);
309 if (colorMode ==
GFX_12BPP) gfx__begin12(spiDiv);
310 else gfx__begin(spiDiv, colorMode);
456static inline uint16_t
gfx_rgb(uint8_t r, uint8_t g, uint8_t b) {
457 return (uint16_t)(((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3));
604typedef void (*
gfx_streamFn)(uint8_t *dst,
int y0,
int rows,
void *user);
632static inline uint8_t *
gfx_px565(uint8_t *p, uint16_t rgb565) {
633 *(uint16_t *)p = rgb565;
644static inline uint8_t *
gfx_px666(uint8_t *p, uint8_t r6, uint8_t g6, uint8_t b6) {
645 p[0] = (uint8_t)(r6 << 2);
646 p[1] = (uint8_t)(g6 << 2);
647 p[2] = (uint8_t)(b6 << 2);
794void gfx_rect(
int x,
int y,
int w,
int h, uint8_t c);
803void gfx_line(
int x0,
int y0,
int x1,
int y1, uint8_t c);
836void gfx_blit(
const uint8_t *spr,
int x,
int y,
int w,
int h,
int transparent);
913void gfx_dither(
int x,
int y,
int w,
int h, uint8_t c, uint8_t phase);
978 const uint8_t *remap =
nullptr,
int scale = 256);
1011 uint8_t angle,
int scale = 256,
const uint8_t *remap =
nullptr);
1205 int *bx,
int *by,
int *bw,
int *bh);
1236bool gfx_textFx(
int x,
int y,
const char *s, uint8_t scale, uint8_t fill,
1237 int outline = -1,
int shadow = -1,
1238 const uint8_t *ramp =
nullptr,
const int8_t *dy =
nullptr);
1540 static uint16_t
rgb(uint8_t r, uint8_t g, uint8_t b) {
return gfx_rgb(r, g, b); }
1705 void drawSprite(
const uint8_t *s,
int x,
int y,
int w,
int h,
int transparent = -1)
1706 {
gfx_blit(s, x, y, w, h, transparent); }
1717 void drawRoundRect(
int x,
int y,
int w,
int h,
int r, uint8_t c) {
gfx_roundRect(x, y, w, h, r, c); }
1728 void fillRoundRect(
int x,
int y,
int w,
int h,
int r, uint8_t c) {
gfx_fillRoundRect(x, y, w, h, r, c); }
1759 void dither(
int x,
int y,
int w,
int h, uint8_t c, uint8_t phase = 0) {
gfx_dither(x, y, w, h, c, phase); }
1779 void drawSprite4(
const uint8_t *s,
int x,
int y,
const uint8_t *remap =
nullptr,
int scale = 256)
1794 int scale = 256,
const uint8_t *remap =
nullptr)
1833 void drawChar(
int x,
int y,
char ch, uint8_t c, uint8_t scale)
1853 void print(
int x,
int y,
const char *s, uint8_t c, uint8_t scale)
1869 bool printFx(
int x,
int y,
const char *s, uint8_t scale, uint8_t fill,
int outline = -1,
1870 int shadow = -1,
const uint8_t *ramp =
nullptr,
const int8_t *dy =
nullptr)
1871 {
return gfx_textFx(x, y, s, scale, fill, outline, shadow, ramp, dy); }
1910 int *bx,
int *by,
int *bw,
int *bh)
const
The bitmap font container, GFXfont, byte-for-byte Adafruit_GFX's.
Arduino-style access to CHGfx: every method is a zero-cost inline forwarder to a gfx_* function.
Definition CHGfx.h:1460
void stream(gfx_streamFn fn, void *user=nullptr)
Draws one full-colour frame from a callback, bypassing the framebuffer.
Definition CHGfx.h:1554
void setInverted(bool on)
Turns colour inversion on or off.
Definition CHGfx.h:1489
void setFade(uint8_t amount, uint16_t rgb565=0)
Fades every colour toward one colour.
Definition CHGfx.h:1522
void drawChar(int x, int y, char ch, uint8_t c)
Draws one character.
Definition CHGfx.h:1823
uint8_t getPixel(int x, int y) const
Reads one pixel.
Definition CHGfx.h:1628
void setPaletteEntry(uint8_t i, uint16_t c)
Sets one palette entry.
Definition CHGfx.h:1515
void drawRect(int x, int y, int w, int h, uint8_t c)
Draws a rectangle's outline.
Definition CHGfx.h:1666
void wait()
Waits for the flush in flight to finish.
Definition CHGfx.h:1578
int flushRow() const
The first row the flush in flight may still read.
Definition CHGfx.h:1580
uint8_t * buffer() const
The framebuffer, for primitives of your own.
Definition CHGfx.h:1933
int fontLineHeight() const
Baseline-to-baseline line spacing.
Definition CHGfx.h:1884
int textWidth(const char *s, uint8_t scale) const
Advance width of a string at a scale.
Definition CHGfx.h:1896
int textWidth(const char *s) const
Advance width of a string.
Definition CHGfx.h:1888
uint8_t nearest(uint16_t rgb565) const
Nearest palette index for an RGB565 colour (slow: setup only).
Definition CHGfx.h:1531
static constexpr int width()
Screen width.
Definition CHGfx.h:1935
void displayAsync()
Starts sending the whole framebuffer by DMA and returns at once.
Definition CHGfx.h:1556
void begin(uint8_t spiDiv=GFX_DIV2, uint8_t colorMode=GFX_16BPP)
Starts the panel.
Definition CHGfx.h:1468
void setPanelOffsets(uint8_t m, uint8_t cs, uint8_t rs)
Sets the panel's MADCTL and window offsets.
Definition CHGfx.h:1487
void fillCircle(int cx, int cy, int r, uint8_t c)
Draws a filled circle.
Definition CHGfx.h:1694
void drawSprite4(const uint8_t *s, int x, int y, const uint8_t *remap=nullptr, int scale=256)
Draws a span sprite.
Definition CHGfx.h:1779
void dither(int x, int y, int w, int h, uint8_t c, uint8_t phase=0)
Paints a 50% checkerboard of one colour over a rectangle.
Definition CHGfx.h:1759
void drawChar(int x, int y, char ch, uint8_t c, uint8_t scale)
Draws one character, scaled.
Definition CHGfx.h:1833
void copyRow(int y, const uint8_t *src, int x0, int x1)
Copies pixels [x0, x1) of a row buffer into a framebuffer row.
Definition CHGfx.h:1814
void drawFastHLine(int x, int y, int w, uint8_t c)
Draws a horizontal line.
Definition CHGfx.h:1637
void scroll(int y, int h, int dx, int dy, int fill=-1)
Moves a band of rows by (dx, dy) inside the band.
Definition CHGfx.h:1805
void remapRect(int x, int y, int w, int h, const uint8_t *remap)
Recolours a rectangle in place through a 16-entry table.
Definition CHGfx.h:1769
void setColorMode(uint8_t m)
Switches the colour output mode.
Definition CHGfx.h:1475
void setPalette(const uint16_t *p, uint8_t n)
Sets the first n palette entries.
Definition CHGfx.h:1508
static uint16_t rgb(uint8_t r, uint8_t g, uint8_t b)
Packs an 8-bit-per-channel colour into RGB565.
Definition CHGfx.h:1540
void setPanelFrameRate(uint8_t r, uint8_t f, uint8_t b)
Sets the panel's own refresh rate (FRMCTR1).
Definition CHGfx.h:1497
uint32_t spiHz() const
The current SPI clock.
Definition CHGfx.h:1479
void setSpiDiv(uint8_t d)
Changes the SPI clock divider.
Definition CHGfx.h:1473
void displayRect(int x, int y, int w, int h)
Sends one rectangle of the framebuffer and waits for it.
Definition CHGfx.h:1565
const GFXfont * font() const
The current font.
Definition CHGfx.h:1882
void print(int x, int y, const char *s, uint8_t c, uint8_t scale)
Draws a string, scaled.
Definition CHGfx.h:1853
void displayRectAsync(int x, int y, int w, int h)
Starts sending one rectangle of the framebuffer and returns at once.
Definition CHGfx.h:1574
void drawLine(int x0, int y0, int x1, int y1, uint8_t c)
Draws a line, both ends included.
Definition CHGfx.h:1676
void textBounds(const char *s, int x, int y, uint8_t scale, int *bx, int *by, int *bw, int *bh) const
Bounding box of a string drawn at (x, y).
Definition CHGfx.h:1909
int fontBaseline() const
The font's ascent above the baseline.
Definition CHGfx.h:1886
bool printFx(int x, int y, const char *s, uint8_t scale, uint8_t fill, int outline=-1, int shadow=-1, const uint8_t *ramp=nullptr, const int8_t *dy=nullptr)
Draws outlined, shadowed or gradient-filled text.
Definition CHGfx.h:1869
void fillRect(int x, int y, int w, int h, uint8_t c)
Fills a rectangle.
Definition CHGfx.h:1656
void drawPixel(int x, int y, uint8_t c)
Sets one pixel.
Definition CHGfx.h:1620
void drawCircle(int cx, int cy, int r, uint8_t c)
Draws a circle's outline.
Definition CHGfx.h:1685
void clear(uint8_t c)
Fills the clip rectangle with one colour.
Definition CHGfx.h:1612
void waitRow(int y)
Waits until rows 0..y-1 are free to draw into.
Definition CHGfx.h:1582
void resetClip()
Puts the clip rectangle back to the whole screen.
Definition CHGfx.h:1597
uint8_t colorMode() const
The current colour output mode.
Definition CHGfx.h:1477
void fillEllipse(int cx, int cy, int rx, int ry, uint8_t c)
Draws a filled ellipse.
Definition CHGfx.h:1748
void setFont(const GFXfont *f=nullptr)
Selects the font.
Definition CHGfx.h:1880
void drawRoundRect(int x, int y, int w, int h, int r, uint8_t c)
Draws a rounded rectangle's outline.
Definition CHGfx.h:1717
void drawEllipse(int cx, int cy, int rx, int ry, uint8_t c)
Draws an ellipse's outline.
Definition CHGfx.h:1738
void display()
Sends the whole framebuffer and waits for it.
Definition CHGfx.h:1546
void drawFastVLine(int x, int y, int h, uint8_t c)
Draws a vertical line.
Definition CHGfx.h:1646
uint8_t fade() const
The current fade amount.
Definition CHGfx.h:1524
void fillRoundRect(int x, int y, int w, int h, int r, uint8_t c)
Fills a rounded rectangle.
Definition CHGfx.h:1728
void print(int x, int y, const char *s, uint8_t c)
Draws a string.
Definition CHGfx.h:1843
void drawSprite4Rot(const uint8_t *s, int ax, int ay, int px, int py, uint8_t angle, int scale=256, const uint8_t *remap=nullptr)
Draws a span sprite rotated and scaled about a pivot.
Definition CHGfx.h:1793
static constexpr int height()
Screen height.
Definition CHGfx.h:1937
void getClip(int *x, int *y, int *w, int *h) const
Reads the clip rectangle.
Definition CHGfx.h:1606
bool busy() const
Whether an async flush is in flight.
Definition CHGfx.h:1576
void fillRectDirect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t rgb565)
Fills a rectangle of the panel by DMA, bypassing the framebuffer (16 bpp only).
Definition CHGfx.h:1926
void drawSprite(const uint8_t *s, int x, int y, int w, int h, int transparent=-1)
Copies a 4 bpp bitmap into the framebuffer.
Definition CHGfx.h:1705
void setClip(int x, int y, int w, int h)
Sets the clip rectangle.
Definition CHGfx.h:1595
CHGfx Gfx
The one CHGfx instance.
#define GFX_H
Screen height in pixels (default 128).
Definition CHGfx.h:109
#define GFX_W
Screen width in pixels (default 128).
Definition CHGfx.h:105
#define GFX_FB_BYTES
Size of the framebuffer, gfx_fb, in bytes: 8192 at 128x128.
Definition CHGfx.h:114
@ GFX_DIV2
HCLK/2 = 24 MHz: the fastest the chip can clock SPI, and the default.
Definition CHGfx.h:232
@ GFX_DIV16
HCLK/16 = 3 MHz.
Definition CHGfx.h:232
@ GFX_DIV8
HCLK/8 = 6 MHz.
Definition CHGfx.h:232
@ GFX_DIV4
HCLK/4 = 12 MHz: within the ST7735S datasheet's limit.
Definition CHGfx.h:232
@ GFX_12BPP
ST7735 COLMOD 0x03, RGB444, 1.5 bytes/px -> 119 fps.
Definition CHGfx.h:220
@ GFX_18BPP
ST7735 COLMOD 0x06, RGB666, 3 bytes/px -> 61 fps.
Definition CHGfx.h:221
@ GFX_16BPP
ST7735 COLMOD 0x05, RGB565, 2 bytes/px -> 90 fps.
Definition CHGfx.h:219
void gfx_select(void)
Asserts the panel's chip select.
void gfx_blockingWrite(const uint8_t *data, uint32_t bytes)
Sends bytes one at a time in a polled loop, for baseline comparisons.
void gfx_setWriteColorLut(bool on)
Chooses whether entering 12 bpp writes the colour LUT (gfx_writeColorLut()).
void gfx_writeColorLut(void)
Programs the ST7735 RGBSET (2Dh) colour-depth conversion LUT for the current mode.
void gfx_deselect(void)
Waits for the last byte to leave SPI1, then releases the panel's chip select.
void gfx_directFillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t rgb565)
Fills a rectangle of the panel with one colour by DMA, bypassing the framebuffer.
void gfx_directBlit(const void *data, uint32_t bytes, bool halfword)
Sends a caller-owned buffer, already in panel format, by DMA and waits for it.
void gfx_cmd(uint8_t c)
Sends one command byte (D/C low), then sets D/C back to data.
void gfx_setWindow(uint8_t x, uint8_t y, uint8_t w, uint8_t h)
Sets the panel's address window and starts a memory write (CASET, RASET, RAMWR).
void gfx_data8(uint8_t d)
Sends one data byte (D/C high).
void gfx_getClip(int *x, int *y, int *w, int *h)
Reads the clip rectangle, as intersected with the screen.
void gfx_blit(const uint8_t *spr, int x, int y, int w, int h, int transparent)
Copies a 4 bpp bitmap into the framebuffer, optionally with one colour transparent.
uint8_t gfx_getPixel(int x, int y)
Reads one pixel of the framebuffer (the clip is ignored).
void gfx_circle(int cx, int cy, int r, uint8_t c)
Draws a circle's 1-pixel outline, 2*r+1 pixels across.
void gfx_rect(int x, int y, int w, int h, uint8_t c)
Draws a rectangle's 1-pixel outline.
void gfx_hline(int x, int y, int w, uint8_t c)
Draws a horizontal line from (x, y), w pixels to the right.
void gfx_clear(uint8_t c)
Fills the clip rectangle (normally the whole screen) with one colour.
void gfx_pixel(int x, int y, uint8_t c)
Sets one pixel.
void gfx_fillRect(int x, int y, int w, int h, uint8_t c)
Fills a rectangle.
void gfx_setClip(int x, int y, int w, int h)
Sets the clip rectangle every drawing call stays inside.
uint8_t gfx_fb[GFX_FB_BYTES]
The framebuffer: GFX_FB_BYTES of 4 bpp pixels, two per byte.
void gfx_fillCircle(int cx, int cy, int r, uint8_t c)
Draws a filled circle, 2*r+1 pixels across.
void gfx_resetClip(void)
Puts the clip rectangle back to the whole screen.
void gfx_vline(int x, int y, int h, uint8_t c)
Draws a vertical line from (x, y), h pixels down.
void gfx_line(int x0, int y0, int x1, int y1, uint8_t c)
Draws a line between two points, both ends included.
uint32_t gfx_convertRows_flash(uint8_t *dst, uint16_t row, uint16_t rows)
Converts framebuffer rows into the panel's format, with the converter in flash (for comparison with g...
uint8_t * gfx_chunkScratch(void)
1 KB of word-aligned scratch memory: the flush's chunk buffers.
uint32_t gfx_convertRows_ram(uint8_t *dst, uint16_t row, uint16_t rows)
Converts framebuffer rows into the panel's format, with the converter in SRAM.
uint32_t gfx_convertSpan_ram(uint8_t *dst, const uint8_t *src, uint32_t srcBytes)
Converts a run of framebuffer bytes into the panel's format (the SRAM converter every flush uses).
uint32_t gfx_frameBytes(void)
Bytes the panel receives for a full frame in the current mode.
uint16_t gfx_pal[16]
The 16 palette colours as set, in RGB565.
uint16_t gfx_paletteOut(uint8_t index)
The colour a palette index actually reaches the panel as, fade included.
void gfx_setPaletteEntry(uint8_t index, uint16_t rgb565)
Sets one palette entry.
void gfx_setFade(uint8_t amount, uint16_t rgb565=0x0000)
Fades every colour toward one colour (black by default).
uint8_t gfx_fade(void)
The current fade amount.
void gfx_setPalette(const uint16_t *rgb565, uint8_t count)
Sets the first count palette entries.
static uint16_t gfx_rgb(uint8_t r, uint8_t g, uint8_t b)
Packs an 8-bit-per-channel colour into RGB565.
Definition CHGfx.h:456
uint8_t gfx_nearest(uint16_t rgb565)
Nearest palette index for a full RGB565 colour.
void gfx_flushRectAsync(int x, int y, int w, int h)
Starts sending one rectangle of the framebuffer and returns at once, as gfx_flushAsync() does for the...
void gfx_flushAsync(void)
Starts sending the whole framebuffer and returns at once; the DMA interrupt does the rest.
void gfx_wait(void)
Waits until the flush in flight, if any, has finished.
void gfx_waitRow(int y)
Waits until rows 0..y-1 are free to draw into.
bool gfx_busy(void)
Whether an async flush is still in flight.
void gfx_flushRect(int x, int y, int w, int h)
Sends one rectangle of the framebuffer to the panel and returns when it has gone out.
void gfx_flush(void)
Sends the whole framebuffer to the panel and returns when it has gone out.
int gfx_flushRow(void)
The first framebuffer row the flush in flight may still read.
void gfx_copyRow(int y, const uint8_t *src, int x0, int x1)
Copies pixels [x0, x1) of a full-width row buffer into framebuffer row y, clipped.
void gfx_scroll(int y, int h, int dx, int dy, int fill=-1)
Moves a band of rows by (dx, dy) pixels inside the band.
uint32_t gfx_spiHz(void)
The current SPI clock.
static void gfx_begin(uint8_t spiDiv=GFX_DIV2, uint8_t colorMode=GFX_16BPP)
Starts the panel: pins, SPI1, DMA, a hardware reset and the ST7735 initialisation,...
Definition CHGfx.h:308
void gfx_setPanelOffsets(uint8_t madctl, uint8_t colStart, uint8_t rowStart)
Sets the panel's orientation (MADCTL) and the offsets of the visible window in its memory.
void gfx_setSpiDiv(uint8_t div)
Changes the SPI clock divider, and sets SPI1 up afresh for the panel.
void gfx_setInverted(bool on)
Turns the panel's colour inversion (INVON / INVOFF) on or off.
void gfx_setPanelFrameRate(uint8_t rtna, uint8_t fpa, uint8_t bpa)
Sets the panel's own refresh rate (ST7735 FRMCTR1, 0xB1).
void gfx_setColorMode(uint8_t mode)
Switches the panel's colour output mode.
uint8_t gfx_colorMode(void)
The current colour output mode.
uint8_t gfx_spiDiv(void)
The current SPI clock divider.
void gfx_fillEllipse(int cx, int cy, int rx, int ry, uint8_t c)
Draws a filled axis-aligned ellipse, 2*rx+1 by 2*ry+1 pixels, centred on (cx, cy).
void gfx_remapRect(int x, int y, int w, int h, const uint8_t *remap)
Recolours a rectangle in place through a 16-entry table: pixel = remap[pixel].
void gfx_dither(int x, int y, int w, int h, uint8_t c, uint8_t phase)
Paints a 50% checkerboard of one colour over a rectangle, leaving the pixels in between alone.
void gfx_ellipse(int cx, int cy, int rx, int ry, uint8_t c)
Draws an axis-aligned ellipse's 1-pixel outline, 2*rx+1 by 2*ry+1 pixels, centred on (cx,...
void gfx_fillRoundRect(int x, int y, int w, int h, int r, uint8_t c)
Fills a rounded rectangle (corners as gfx_roundRect()).
void gfx_roundRect(int x, int y, int w, int h, int r, uint8_t c)
Draws a rounded rectangle's 1-pixel outline.
void gfx_sprite4(const uint8_t *spr, int x, int y, const uint8_t *remap=nullptr, int scale=256)
Draws a span sprite, optionally recoloured and scaled.
void gfx_sprite4Rot(const uint8_t *spr, int ax, int ay, int px, int py, uint8_t angle, int scale=256, const uint8_t *remap=nullptr)
Draws a span sprite rotated and scaled about a pivot.
static int gfx_sprite4H(const uint8_t *spr)
A span sprite's height.
Definition CHGfx.h:990
static int gfx_sprite4W(const uint8_t *spr)
A span sprite's width.
Definition CHGfx.h:984
uint8_t gfx_bytesPerPixel(void)
Bytes per pixel a stream callback writes in the current mode.
void(* gfx_streamFn)(uint8_t *dst, int y0, int rows, void *user)
A direct-streaming callback: fills rows rows starting at row y0 into dst, in the panel's native forma...
Definition CHGfx.h:604
static uint8_t * gfx_px565(uint8_t *p, uint16_t rgb565)
Writes one 16 bpp pixel for a stream callback.
Definition CHGfx.h:632
static uint8_t * gfx_px666(uint8_t *p, uint8_t r6, uint8_t g6, uint8_t b6)
Writes one 18 bpp pixel for a stream callback.
Definition CHGfx.h:644
void gfx_stream(gfx_streamFn fn, void *user)
Draws one full-screen frame by calling fn for every chunk of rows and sending the result straight to ...
void gfx_charScaled(int x, int y, char ch, uint8_t c, uint8_t scale)
Draws one character, scaled up by a whole number.
int gfx_fontLineHeight(void)
Baseline-to-baseline line spacing of the current font, at scale 1.
void gfx_textBounds(const char *s, int x, int y, uint8_t scale, int *bx, int *by, int *bw, int *bh)
Bounding box of a string drawn at (x, y).
int gfx_textWidth(const char *s)
Advance width of a string in the current font - the number to use for centring and right-alignment.
void gfx_textScaled(int x, int y, const char *s, uint8_t c, uint8_t scale)
Draws a string in the current font, scaled up by a whole number.
bool gfx_textFx(int x, int y, const char *s, uint8_t scale, uint8_t fill, int outline=-1, int shadow=-1, const uint8_t *ramp=nullptr, const int8_t *dy=nullptr)
Draws outlined, shadowed or gradient-filled text - titles and banners.
int gfx_textWidthScaled(const char *s, uint8_t scale)
Advance width of a string in the current font at a scale.
void gfx_char(int x, int y, char ch, uint8_t c)
Draws one character.
const GFXfont * gfx_font(void)
The current font.
void gfx_text(int x, int y, const char *s, uint8_t c)
Draws a string in the current font.
void gfx_setFont(const GFXfont *f)
Selects the font every text call uses from now on.
int gfx_fontBaseline(void)
The current font's ascent: how far its tallest ink reaches above the baseline, at scale 1.
A whole bitmap font, for gfx_setFont(): glyph bitmaps, glyph records, the character range and the lin...
Definition CHGfx_gfxfont.h:65