CHGame 0.4.1
Libraries for the CHGame handheld
Loading...
Searching...
No Matches
CHGfx.h
Go to the documentation of this file.
1/**
2 * @file CHGfx.h
3 * @brief CHGfx: a maximum-throughput ST7735 driver for the CHGame board
4 * (CH32X035G8U6 @ 48 MHz, QingKe V4C, 20 KB SRAM, 62 KB flash).
5 *
6 * **Why this exists.** Adafruit_GFX + Adafruit_ST7735 on this part is
7 * slow for three separate reasons, and they compound:
8 *
9 * 1. Every drawPixel() sends CASET + RASET + RAMWR (11 bytes of command
10 * traffic) to paint 2 bytes of pixel. ~85% of the bus is overhead.
11 * 2. The Arduino SPI class transfers one byte at a time in a polled loop
12 * (cores/.../libraries/SPI/src/utility/spi_com.c spins on TXE for
13 * every single byte). The CPU is the bottleneck, not the wire.
14 * 3. There is no framebuffer, so overdraw goes straight out over SPI.
15 *
16 * **What this does instead.**
17 *
18 * - Register-level SPI1 at HCLK/2 = 24 MHz. That is the CH32X035 hard
19 * ceiling: "Maximum clock frequency supports up to half of FHCLK"
20 * (Reference Manual ch.16.1), and HCLK cannot exceed 48 MHz because
21 * this part has no PLL, only the 48 MHz HSI RC (RM ch.3.3).
22 * - DMA1 channel 3 - the SPI1_TX request line (RM 9.2.3) - pushes whole
23 * chunks with zero CPU involvement.
24 * - 16-bit SPI data frames (DFF=1) so DMA does half as many bus cycles.
25 * - A 4 bpp (16-colour) framebuffer. 128*128 RGB565 is 32 KB and this
26 * chip has 20 KB, so a full-colour framebuffer is physically
27 * impossible. 4 bpp is 8 KB and leaves room to live.
28 * - A 256-entry uint32 lookup table expands one framebuffer byte
29 * (= 2 pixels) into one 32-bit store. Conversion costs ~3 cycles per
30 * pixel against a 32-cycle-per-pixel SPI budget, so the wire stays
31 * saturated. Free for the WIRE, not for the CPU: an async flush
32 * still spends its conversion time inside the DMA interrupt - see
33 * "What a flush costs the CPU" in @ref chgfx_present.
34 * - Optional 12 bpp (RGB444) output - ST7735 COLMOD 0x03. Three bytes
35 * per two pixels instead of four: 25% less traffic, and with a
36 * 16-colour palette you lose nothing you were actually using.
37 * - Ping-pong chunk buffers driven by the DMA transfer-complete ISR, so
38 * gfx_flushAsync() returns immediately and game logic overlaps the
39 * ~11 ms it takes to shift a frame out.
40 * - The conversion inner loops are linked into .data so they execute
41 * from SRAM. Flash on this part runs at 3 wait states above 24 MHz
42 * (RM 20.3.1, FLASH_ACTLR LATENCY = 10b); SRAM runs at zero.
43 *
44 * **Ceiling.** 24 MHz SPI = 3.0 MB/s.
45 *
46 * | Output | Bytes a frame | Frame time | Frame rate |
47 * |---|---:|---:|---:|
48 * | 16 bpp, 128*128*2 | 32768 | 10.9 ms | 91 fps |
49 * | 12 bpp, 128*128*1.5 | 24576 | 8.2 ms | 122 fps |
50 *
51 * Nothing on this chip beats that: there is no parallel LCD interface,
52 * no second SPI on these pins, and no PLL to go past 48 MHz HCLK.
53 *
54 * Everything is a plain C function (`gfx_*`); the CHGfx class and its one
55 * instance, Gfx, are inline forwarders for the `Gfx.clear(0)` spelling.
56 * The topics are listed under @ref lib_chgfx.
57 */
58#pragma once
59
60#include <Arduino.h>
61#include <stdint.h>
62#include "CHGfx_gfxfont.h"
63
64#if !defined(CH32X035)
65 #error "CHGfx targets the CH32X035. It drives SPI1 and DMA1 channel 3 directly and is not portable as-is. Select a CH32X035 board."
66#endif
67
68/**
69 * @defgroup chgfx_config Configuration
70 * @ingroup lib_chgfx
71 * @brief Screen geometry, the panel's wiring, colour modes and SPI
72 * dividers: the build options and the constants they define.
73 *
74 * The macros here can be overridden with `-D` build flags. They are
75 * compiled into the library's own files, so they have to be build flags
76 * (`--build-property build.extra_flags=-DGFX_CHUNK_ROWS=4` with
77 * `arduino-cli`), not `#define`s in the sketch. Three more switches are
78 * read only by the library's `.cpp` files:
79 *
80 * - `CHGFX_NO_SD_PARK`: do not drive the shared SD card's chip select
81 * (CHGFX_SDCS_PORT / CHGFX_SDCS_PIN) high at gfx_begin(). Define it if
82 * nothing else shares SPI1.
83 * - `CHGFX_ISR_IN_SRAM`: run the DMA interrupt handler from SRAM. An
84 * async flush costs 4-9% less CPU (0.1-0.25 ms a full frame), for about
85 * 330 bytes of SRAM.
86 * - `CHGFX_BL_PORT` / `CHGFX_BL_PIN`: a switched backlight, active high.
87 * Unset by default (CHGame Rev0's backlight is always on); the CHGame
88 * Rev1 board's variant sets PB9. With it, gfx_begin() holds the pin low
89 * from the start, sends the framebuffer as a first frame at the end
90 * (black until the sketch draws), and only then drives it high, so the
91 * panel's power-up noise is never lit.
92 *
93 * A board variant may define the pin macros below (CHGame Rev1's does:
94 * LCD_DC on PB4, SD_CS on PB3); the defaults here are CHGame Rev0's.
95 * @{
96 */
97
98/* ------------------------------------------------------------------ */
99/* Geometry */
100/* ------------------------------------------------------------------ */
101/* Override before including, or with a -D build flag. Tested at
102 * 128x128; the framebuffer is W*H/2 bytes, so keep an eye on the 20 KB
103 * SRAM budget if you raise these (160x128 = 10 KB, still fits). W must
104 * be even and W*H/2 must be a multiple of 32. */
105#ifndef GFX_W
106/**
107 * @brief Screen width in pixels (default 128).
108 *
109 * Override with a `-D` build flag. Must be a multiple of 8 (the row
110 * operations work in 32-bit words), and W*H/2 a multiple of 32. The
111 * framebuffer is W*H/2 bytes, so keep an eye on the 20 KB SRAM budget if
112 * you raise it (160x128 = 10 KB, still fits).
113 */
114#define GFX_W 128
115#endif
116#ifndef GFX_H
117/** @brief Screen height in pixels (default 128). Override with a `-D` build flag. */
118#define GFX_H 128
119#endif
120/** @brief Bytes per framebuffer row: two pixels a byte, 64 at 128 wide. */
121#define GFX_FB_STRIDE (GFX_W / 2)
122/** @brief Size of the framebuffer, gfx_fb, in bytes: 8192 at 128x128. */
123#define GFX_FB_BYTES (GFX_FB_STRIDE * GFX_H)
124
125/* Rows converted per DMA chunk. Two chunk buffers are allocated, so
126 * this directly costs GFX_W * GFX_CHUNK_ROWS * 4 bytes of SRAM. */
127#ifndef GFX_CHUNK_ROWS
128/**
129 * @brief Framebuffer rows converted per DMA chunk (default 2).
130 *
131 * Two chunk buffers are allocated, so this directly costs
132 * `GFX_W * GFX_CHUNK_ROWS * 4` bytes of SRAM. Override with
133 * `-DGFX_CHUNK_ROWS=n`; it does not buy much CPU back either (an async
134 * frame measured 2.49 / 2.33 / 2.29 ms of CPU at 2 / 4 / 8 rows, with
135 * `CHGFX_ISR_IN_SRAM`).
136 *
137 * 2 rows = 512 B per buffer, 1 KB total. Measured against the obvious
138 * 4 rows on real hardware: gfx_flush() went 11051 -> 11143 us, about
139 * 0.8%, which is the same order as run-to-run variation. That buys back
140 * a kilobyte, and on a part with 20 KB total a kilobyte is 5% of all
141 * the memory there is - the right way round for this chip. Raise it if
142 * you have RAM to spare and want the last percent.
143 */
144#define GFX_CHUNK_ROWS 2
145#endif
146/**
147 * @brief Size of one DMA chunk buffer in bytes (worst case, 16 bpp).
148 *
149 * There are two; together they are the 1 KB gfx_chunkScratch().
150 */
151#define GFX_CHUNK_BYTES (GFX_W * GFX_CHUNK_ROWS * 2)
152
153/* ------------------------------------------------------------------ */
154/* Wiring */
155/* ------------------------------------------------------------------ */
156/*
157 * SCK and MOSI are fixed: they are SPI1's pins on this package (PA5 and
158 * PA7), and SPI1 is the only peripheral with a DMA path to these lines.
159 * The three control pins are yours to move - redefine any of these
160 * before including CHGfx.h, or with -D build flags.
161 *
162 * Defaults are the CHGame board (see variant_CHGame_Rev0.h):
163 * PA4 = LCD_CS, PB0 = LCD_DC, PB12 = LCD_RST, PB11 = SD_CS
164 * SD_CS is driven high at begin() because the microSD slot shares SPI1.
165 * A board variant that defines these first wins: CHGame Rev1's
166 * (variant_CHGame_Rev1.h) has PA4 = LCD_CS, PB4 = LCD_DC, PB12 = LCD_RST,
167 * PB3 = SD_CS, and the backlight, PB9, as CHGFX_BL_PORT / CHGFX_BL_PIN
168 * (which has no default: a board without one leaves it undefined).
169 */
170#ifndef CHGFX_CS_PORT
171/**
172 * @brief GPIO port of the panel's chip select (default `GPIOA`, the
173 * CHGame board's LCD_CS on PA4).
174 *
175 * The three control pins (CS, DC, RST) can be moved with `-D` build
176 * flags; define the port and the pin together. SCK and MOSI are fixed:
177 * they are SPI1's pins on this package (PA5 and PA7), and SPI1 is the
178 * only peripheral with a DMA path to these lines.
179 */
180#define CHGFX_CS_PORT GPIOA
181/** @brief Pin number of the panel's chip select on CHGFX_CS_PORT (default 4). */
182#define CHGFX_CS_PIN 4
183#endif
184#ifndef CHGFX_DC_PORT
185/** @brief GPIO port of the panel's data/command line (default `GPIOB`, LCD_DC on PB0). */
186#define CHGFX_DC_PORT GPIOB
187/** @brief Pin number of the panel's data/command line on CHGFX_DC_PORT (default 0). */
188#define CHGFX_DC_PIN 0
189#endif
190#ifndef CHGFX_RST_PORT
191/** @brief GPIO port of the panel's reset line (default `GPIOB`, LCD_RST on PB12). */
192#define CHGFX_RST_PORT GPIOB
193/** @brief Pin number of the panel's reset line on CHGFX_RST_PORT (default 12). */
194#define CHGFX_RST_PIN 12
195#endif
196/* Define CHGFX_NO_SD_PARK to skip driving a shared SD chip select high. */
197#ifndef CHGFX_SDCS_PORT
198/**
199 * @brief GPIO port of the SD card's chip select, which shares SPI1
200 * (default `GPIOB`, SD_CS on PB11).
201 *
202 * gfx_begin() drives it high so the card does not answer the traffic
203 * meant for the panel. Define `CHGFX_NO_SD_PARK` to skip that.
204 */
205#define CHGFX_SDCS_PORT GPIOB
206/** @brief Pin number of the SD card's chip select on CHGFX_SDCS_PORT (default 11). */
207#define CHGFX_SDCS_PIN 11
208#endif
209
210/* ------------------------------------------------------------------ */
211/* Colour output modes */
212/* ------------------------------------------------------------------ */
213/**
214 * @brief The panel's colour output modes, for gfx_begin() and
215 * gfx_setColorMode().
216 *
217 * The framebuffer is always 4 bpp; this is what its palette colours are
218 * expanded to on the wire, so the frame rate is just the byte count.
219 * 12 bpp costs nothing visible behind a 16-colour palette and is a free
220 * 25%, so the CHGame games use it.
221 *
222 * A note on 18 bpp. Each pixel is three bytes and each byte carries its
223 * 6-bit component in bits 7:2, bits 1:0 don't-care (ST7735S DS 9.8.4).
224 * 128*128*3 = 49152 B, so a frame costs 16.4 ms instead of 10.9 - you
225 * are buying one extra bit of red and one of blue for a third of your
226 * frame rate. Worth it for smooth gradients, pointless for sprite work.
227 * Unlike 12 bpp it does not pass through the RGBSET conversion table:
228 * the datasheet only defines 4k->262k and 65k->262k tables, so 18-bit
229 * data reaches the frame memory directly.
230 */
231enum : uint8_t {
232 GFX_16BPP = 0, /**< ST7735 COLMOD 0x05, RGB565, 2 bytes/px -> 90 fps */
233 GFX_12BPP = 1, /**< ST7735 COLMOD 0x03, RGB444, 1.5 bytes/px -> 119 fps */
234 GFX_18BPP = 2 /**< ST7735 COLMOD 0x06, RGB666, 3 bytes/px -> 61 fps */
236
237/**
238 * @brief SPI baud dividers (HCLK / n) for gfx_begin() and gfx_setSpiDiv().
239 *
240 * @note The ST7735S datasheet specifies tSCYCW(min) = 66 ns, i.e. 15 MHz.
241 * 24 MHz is out of spec and only works on short flex. The Benchmark
242 * example's SPI sweep exists to find out whether YOUR panel tolerates it;
243 * GFX_DIV4 is comfortably in spec.
244 */
245enum : uint8_t { GFX_DIV2 = 2, GFX_DIV4 = 4, GFX_DIV8 = 8, GFX_DIV16 = 16 };
246/** @var GFX_DIV2
247 * @brief HCLK/2 = 24 MHz: the fastest the chip can clock SPI, and the default. */
248/** @var GFX_DIV4
249 * @brief HCLK/4 = 12 MHz: within the ST7735S datasheet's limit. */
250/** @var GFX_DIV8
251 * @brief HCLK/8 = 6 MHz. */
252/** @var GFX_DIV16
253 * @brief HCLK/16 = 3 MHz. */
254
255/** @} */
256
257/* ------------------------------------------------------------------ */
258/* Framebuffer + palette */
259/* ------------------------------------------------------------------ */
260/**
261 * @brief The framebuffer: GFX_FB_BYTES of 4 bpp pixels, two per byte.
262 * @ingroup chgfx_draw
263 *
264 * Row y starts at `gfx_fb + y * GFX_FB_STRIDE`. Even x is in the LOW
265 * nibble, odd x in the HIGH nibble - that ordering makes the expansion
266 * LUT a single uint32 store with no shuffling on a little-endian core.
267 * Each nibble is a palette index. Write it directly only for primitives
268 * of your own, and only when no flush is reading it (after gfx_wait()).
269 */
270extern uint8_t gfx_fb[GFX_FB_BYTES];
271/**
272 * @brief The 16 palette colours as set, in RGB565.
273 * @ingroup chgfx_palette
274 *
275 * Read it freely; change it with gfx_setPalette() or
276 * gfx_setPaletteEntry(), which stage the change for the next flush.
277 * The fade (gfx_setFade()) is not applied here: gfx_paletteOut() gives
278 * the colour the panel actually receives.
279 */
280extern uint16_t gfx_pal[16];
281
282/* ------------------------------------------------------------------ */
283/* Lifecycle */
284/* ------------------------------------------------------------------ */
285/**
286 * @defgroup chgfx_setup Setup
287 * @ingroup lib_chgfx
288 * @brief Starting the panel and changing its SPI clock, colour mode,
289 * orientation and scan rate.
290 * @{
291 */
292
293/* The two starts gfx_begin() picks between (see its documentation). */
294void gfx__begin(uint8_t spiDiv, uint8_t colorMode);
295void gfx__begin12(uint8_t spiDiv);
296/**
297 * @brief Starts the panel: pins, SPI1, DMA, a hardware reset and the
298 * ST7735 initialisation, then a default 16-colour palette.
299 *
300 * Call it once in `setup()`. It takes about 0.7 s (the panel's reset
301 * and wake-up delays). It parks the SD card's chip select high (unless
302 * `CHGFX_NO_SD_PARK`), selects the colour mode and, in 12 bpp, programs
303 * the panel's colour LUT (gfx_writeColorLut()). The framebuffer is not
304 * touched and nothing is sent to the glass until the first flush, except
305 * on a board with a switched backlight (`CHGFX_BL_PIN`, CHGame Rev1): there
306 * the backlight is held off throughout, the framebuffer is flushed once at
307 * the end (black, unless the sketch has drawn into it already; 8.2 ms in
308 * 12 bpp, and like any async flush it needs interrupts on), and then the
309 * backlight is turned on.
310 *
311 * gfx_begin() picks one of two starts. A sketch that names GFX_12BPP and
312 * never calls gfx_setColorMode() gets the 12 bpp-only start, and the 16
313 * and 18 bpp converters and tables stay out of its image (0.2-0.4 KB of
314 * flash, 176 B of it SRAM code). Any other mode, or one only known when
315 * it runs, gets the general start, which keeps all three.
316 *
317 * @param spiDiv SPI clock divider: GFX_DIV2 (24 MHz, the default),
318 * GFX_DIV4, GFX_DIV8 or GFX_DIV16.
319 * @param colorMode GFX_16BPP (the default), GFX_12BPP or GFX_18BPP.
320 *
321 * @code
322 * gfx_begin(GFX_DIV2, GFX_12BPP); // what the CHGame games use
323 * @endcode
324 * @see CHGfx::begin()
325 */
326static inline void gfx_begin(uint8_t spiDiv = GFX_DIV2, uint8_t colorMode = GFX_16BPP) {
327 if (colorMode == GFX_12BPP) gfx__begin12(spiDiv);
328 else gfx__begin(spiDiv, colorMode);
329}
330/**
331 * @brief Changes the SPI clock divider, and sets SPI1 up afresh for the panel.
332 *
333 * Waits for any flush in flight first, then programs all of SPI1 as
334 * gfx_begin() does (its clock on, master, mode 0, MSB first, this divider).
335 * That makes it the way to hand SPI1 back to CHGfx after anything else used
336 * it, such as the Arduino @ref core_spi "SPI" class:
337 * @code
338 * gfx_wait();
339 * SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
340 * // ... talk to your own device ...
341 * SPI.endTransaction();
342 * gfx_setSpiDiv(gfx_spiDiv()); // the panel's set-up back before the next flush
343 * gfx_flushAsync();
344 * @endcode
345 * @param div GFX_DIV2, GFX_DIV4, GFX_DIV8 or GFX_DIV16 (32 also works);
346 * any other value runs at HCLK/2.
347 */
348void gfx_setSpiDiv(uint8_t div);
349/**
350 * @brief Switches the panel's colour output mode.
351 *
352 * Waits for any flush in flight, sends the panel its new COLMOD (and the
353 * colour LUT when entering 12 bpp), and rebuilds the palette table for
354 * the next flush. Does nothing if the mode is already set.
355 * @param mode GFX_16BPP, GFX_12BPP or GFX_18BPP.
356 * @note Calling it anywhere in a sketch links the 16 and 18 bpp
357 * converters, even if gfx_begin() was given GFX_12BPP.
358 */
359void gfx_setColorMode(uint8_t mode);
360/**
361 * @brief The current colour output mode.
362 * @return GFX_16BPP, GFX_12BPP or GFX_18BPP.
363 */
364uint8_t gfx_colorMode(void);
365/**
366 * @brief The current SPI clock divider.
367 * @return The divider last given to gfx_begin() or gfx_setSpiDiv().
368 */
369uint8_t gfx_spiDiv(void);
370/**
371 * @brief The current SPI clock.
372 * @return The SPI clock in Hz: F_CPU divided by gfx_spiDiv(), 24000000 at
373 * GFX_DIV2.
374 */
375uint32_t gfx_spiHz(void);
376
377/**
378 * @brief Sets the panel's orientation (MADCTL) and the offsets of the
379 * visible window in its memory.
380 *
381 * MADCTL / window offsets differ between 1.44" panel batches. Defaults
382 * match Adafruit's INITR_144GREENTAB (madctl 0xC8, colstart 2, rowstart
383 * 3), which is what an Adafruit_ST7735 setup is already using. Change
384 * them if the image is offset or mirrored. Sends MADCTL at once, so call
385 * it after gfx_wait().
386 * @param madctl ST7735 MADCTL (36h) value: rotation, mirroring, RGB/BGR.
387 * @param colStart First visible column of the panel's memory.
388 * @param rowStart First visible row of the panel's memory.
389 */
390void gfx_setPanelOffsets(uint8_t madctl, uint8_t colStart, uint8_t rowStart);
391/**
392 * @brief Turns the panel's colour inversion (INVON / INVOFF) on or off.
393 *
394 * Sends the command at once, so call it after gfx_wait().
395 * @param on true to invert every colour, false for normal.
396 */
397void gfx_setInverted(bool on);
398
399/**
400 * @brief Sets the panel's own refresh rate (ST7735 FRMCTR1, 0xB1).
401 *
402 * Lower values scan the glass faster, which cuts the latency between a
403 * GRAM write and the pixel actually changing. The panel's rate is
404 * 850 kHz / ((RTNA*2 + 40) * (lines + FPA + BPA)). Defaults to the fast
405 * setting 0x05, 0x3A, 0x3A (Adafruit's is a lazy ~60 Hz 0x01, 0x2C,
406 * 0x2D). Waits for any flush in flight first.
407 * @param rtna RTNA, the line period.
408 * @param fpa Front porch, in lines.
409 * @param bpa Back porch, in lines.
410 */
411void gfx_setPanelFrameRate(uint8_t rtna, uint8_t fpa, uint8_t bpa);
412
413/** @} */
414
415/**
416 * @defgroup chgfx_palette Palette
417 * @ingroup lib_chgfx
418 * @brief The 16 colours the framebuffer's indices stand for, and a fade
419 * applied on top of them.
420 *
421 * Palette changes are STAGED: they take effect when the next flush
422 * starts, never part-way through one in flight, so these are safe to
423 * call at any time - including between gfx_flushAsync() and gfx_wait().
424 * Every frame is re-sent through the palette, so animating it (cycling a
425 * slot, pulsing a highlight, a fade) costs a 256-entry table rebuild per
426 * frame and no drawing at all.
427 * @{
428 */
429
430/**
431 * @brief Sets the first `count` palette entries.
432 * @param rgb565 The colours, in RGB565 (see gfx_rgb()). Passing gfx_pal
433 * itself just re-stages the palette.
434 * @param count How many entries to set, from index 0; at most 16 are used.
435 */
436void gfx_setPalette(const uint16_t *rgb565, uint8_t count);
437/**
438 * @brief Sets one palette entry.
439 * @param index Palette index, 0..15 (only the low 4 bits are used).
440 * @param rgb565 The colour, in RGB565.
441 */
442void gfx_setPaletteEntry(uint8_t index, uint16_t rgb565);
443
444/**
445 * @brief Fades every colour toward one colour (black by default).
446 *
447 * Applied while the expansion table is built, so gfx_pal[] keeps the
448 * true colours and gfx_nearest() still matches against them. Staged like
449 * the palette.
450 * @param amount 0 = the palette as set, 255 = solid `rgb565`.
451 * @param rgb565 The colour faded toward, in RGB565 (default black).
452 */
453void gfx_setFade(uint8_t amount, uint16_t rgb565 = 0x0000);
454/**
455 * @brief The current fade amount.
456 * @return 0 (no fade) to 255 (every colour is the fade colour).
457 */
458uint8_t gfx_fade(void);
459
460/**
461 * @brief The colour a palette index actually reaches the panel as, fade
462 * included.
463 * @param index Palette index, 0..15.
464 * @return The colour in RGB565.
465 */
466uint16_t gfx_paletteOut(uint8_t index);
467/**
468 * @brief Packs an 8-bit-per-channel colour into RGB565.
469 * @param r Red, 0..255 (the top 5 bits are kept).
470 * @param g Green, 0..255 (the top 6 bits are kept).
471 * @param b Blue, 0..255 (the top 5 bits are kept).
472 * @return The colour in RGB565, for gfx_setPalette() and the like.
473 */
474static inline uint16_t gfx_rgb(uint8_t r, uint8_t g, uint8_t b) {
475 return (uint16_t)(((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3));
476}
477
478/** @} */
479
480/* ------------------------------------------------------------------ */
481/* Presenting the framebuffer */
482/* ------------------------------------------------------------------ */
483/**
484 * @defgroup chgfx_present Presenting the framebuffer
485 * @ingroup lib_chgfx
486 * @brief Sending the framebuffer to the panel: whole or a rectangle,
487 * blocking or by DMA in the background, and waiting for it.
488 *
489 * A flush converts the 4 bpp framebuffer through the palette into the
490 * panel's format a chunk at a time and DMAs it out. The framebuffer is
491 * free to draw into again after gfx_wait() (or, row by row, after
492 * gfx_waitRow()). Every flush first waits for the one before it.
493 *
494 * **What a flush costs the CPU.** The wire time is fixed (11.1 ms a full
495 * frame at 16 bpp, 8.4 ms at 12 bpp) and an async flush hands it to the
496 * DMA. The pixel conversion still runs on the CPU, in the DMA interrupt,
497 * one chunk at a time, and the DMA shares the bus: measured on the board,
498 * an async full frame costs the main loop 2.5-3.2 ms of CPU (by -O2/-Os
499 * and 12/16 bpp). At 60 fps that is a sixth of the CPU, taken from
500 * whatever the main loop is doing - an AI search, for example. A partial
501 * flush costs in proportion to its area. (Before 1.3, at the board's
502 * default -Os, the converters were silently compiled into flash and the
503 * same flush cost 5.1 ms.) `-DCHGFX_ISR_IN_SRAM` trims it by 4-9% for
504 * ~330 bytes of SRAM.
505 *
506 * The cheapest frame is the one you do not draw: if nothing on screen
507 * changed, flush the buffer you already have. A palette animation still
508 * moves (the palette is applied during the flush), and the frame costs a
509 * flush and no drawing.
510 *
511 * **Flush progress - "racing the beam".** The flush converts the
512 * framebuffer top to bottom, and a row it has converted is never read
513 * again, so it is free to draw into while the rest of the frame is still
514 * going out. gfx_flushRow() is the first row the flush in flight may
515 * still read; gfx_waitRow() waits for a band to be free. Useful when the
516 * next frame is drawn top to bottom: wait for the top band, draw it, wait
517 * for the next. A clip rectangle keeps the drawing honest:
518 *
519 * @code
520 * gfx_waitRow(64); gfx_setClip(0, 0, 128, 64); drawTopHalf();
521 * gfx_wait(); gfx_setClip(0, 64, 128, 64); drawBottomHalf();
522 * gfx_resetClip(); gfx_flushAsync();
523 * @endcode
524 *
525 * **Partial updates.** The wire is the bottleneck, so sending a quarter
526 * of the screen costs a quarter of the time - gfx_flushRect() is the
527 * single biggest lever left once DMA is in place.
528 * @{
529 */
530
531/**
532 * @brief Sends the whole framebuffer to the panel and returns when it
533 * has gone out.
534 *
535 * About 11.1 ms at 16 bpp, 8.4 ms at 12 bpp, at 24 MHz.
536 * @see gfx_flushAsync(), gfx_flushRect()
537 */
538void gfx_flush(void); /* convert + DMA the whole frame, blocking */
539/**
540 * @brief Starts sending the whole framebuffer and returns at once; the
541 * DMA interrupt does the rest.
542 *
543 * Do not draw into the framebuffer until gfx_wait() (or gfx_waitRow())
544 * says the rows are free. The conversion costs the main loop 2.5-3.2 ms
545 * of CPU a full frame (see above).
546 */
547void gfx_flushAsync(void); /* same, but returns after the first chunk */
548/**
549 * @brief Whether an async flush is still in flight.
550 * @return true until the last byte of the flush has gone out.
551 */
552bool gfx_busy(void);
553/**
554 * @brief Waits until the flush in flight, if any, has finished.
555 *
556 * After it the framebuffer and gfx_chunkScratch() are free. Returns at
557 * once when nothing is being sent.
558 */
559void gfx_wait(void);
560
561/**
562 * @brief The first framebuffer row the flush in flight may still read.
563 * @return A row number; rows above it are free to draw into. GFX_H when
564 * no flush is in flight or the last chunk has been converted.
565 */
566int gfx_flushRow(void);
567/**
568 * @brief Waits until rows 0..y-1 are free to draw into.
569 * @param y One past the last row needed. gfx_waitRow(GFX_H) is gfx_wait()
570 * as far as drawing goes.
571 */
572void gfx_waitRow(int y);
573
574/** @} */
575
576/* ------------------------------------------------------------------ */
577/* Direct streaming - bypass the framebuffer entirely */
578/* ------------------------------------------------------------------ */
579/**
580 * @defgroup chgfx_stream Direct streaming
581 * @ingroup lib_chgfx
582 * @brief Full-colour procedural frames computed straight into the DMA's
583 * buffers, with no framebuffer and no palette.
584 *
585 * The 4 bpp framebuffer exists because 128*128 RGB565 will not fit in
586 * 20 KB. But a procedural effect - plasma, tunnel, rotozoomer, fire -
587 * does not need a framebuffer at all: it can compute pixels straight
588 * into the buffer the DMA is about to send. That gets you the panel's
589 * FULL colour depth, 65536 or 262144 colours, with no framebuffer and
590 * no palette.
591 *
592 * The callback (a gfx_streamFn) is called once per chunk while the
593 * previous chunk is still on the wire, so the transfer never stalls as
594 * long as you stay inside budget.
595 *
596 * **The budget, and it is the whole game:** 32 CPU cycles per pixel at
597 * 16 bpp, 48 at 18 bpp. Stay under it and you render at full wire speed.
598 * Go over and the frame rate degrades in proportion - nothing breaks, it
599 * just slows. Put your inner loop in SRAM; flash costs 3 wait states here
600 * and measured 2.2x slower on exactly this kind of loop.
601 *
602 * @code
603 * void plasma(uint8_t *dst, int y0, int rows, void *user) {
604 * for (int r = 0; r < rows; r++)
605 * for (int x = 0; x < GFX_W; x++)
606 * dst = gfx_px565(dst, someColour(x, y0 + r));
607 * }
608 * gfx_stream(plasma, nullptr);
609 * @endcode
610 * @{
611 */
612
613/**
614 * @brief A direct-streaming callback: fills `rows` rows starting at row
615 * `y0` into `dst`, in the panel's native format.
616 *
617 * `dst` takes `rows * GFX_W * gfx_bytesPerPixel()` bytes: GFX_16BPP is
618 * 2 bytes/px, RGB565 little-endian (write uint16_t, or gfx_px565());
619 * GFX_18BPP is 3 bytes/px, each component in bits 7:2 (gfx_px666()).
620 * `user` is the pointer given to gfx_stream().
621 */
622typedef void (*gfx_streamFn)(uint8_t *dst, int y0, int rows, void *user);
623/**
624 * @brief Draws one full-screen frame by calling `fn` for every chunk of
625 * rows and sending the result straight to the panel.
626 *
627 * Blocking: waits for any flush in flight, then returns when the frame
628 * has gone out. Requires GFX_16BPP or GFX_18BPP. In 12 bpp the packing
629 * is 1.5 bytes per pixel with pixels straddling bytes, which is no use to
630 * a per-pixel generator, so gfx_stream() switches to 16 bpp for you (and
631 * the mode stays 16 bpp afterwards).
632 * @param fn The callback that fills each chunk.
633 * @param user Passed to every call of `fn` (may be nullptr).
634 */
635void gfx_stream(gfx_streamFn fn, void *user);
636
637/**
638 * @brief Bytes per pixel a stream callback writes in the current mode.
639 * @return 2, or 3 for GFX_18BPP.
640 */
641uint8_t gfx_bytesPerPixel(void);
642
643/* Pack helpers for stream callbacks. */
644/**
645 * @brief Writes one 16 bpp pixel for a stream callback.
646 * @param p Where to write it.
647 * @param rgb565 The colour, in RGB565.
648 * @return `p` advanced past the pixel (2 bytes).
649 */
650static inline uint8_t *gfx_px565(uint8_t *p, uint16_t rgb565) {
651 *(uint16_t *)p = rgb565;
652 return p + 2;
653}
654/**
655 * @brief Writes one 18 bpp pixel for a stream callback.
656 * @param p Where to write it.
657 * @param r6 Red, 0..63.
658 * @param g6 Green, 0..63.
659 * @param b6 Blue, 0..63.
660 * @return `p` advanced past the pixel (3 bytes).
661 */
662static inline uint8_t *gfx_px666(uint8_t *p, uint8_t r6, uint8_t g6, uint8_t b6) {
663 p[0] = (uint8_t)(r6 << 2);
664 p[1] = (uint8_t)(g6 << 2);
665 p[2] = (uint8_t)(b6 << 2);
666 return p + 3;
667}
668
669/** @} */
670
671/** @addtogroup chgfx_present
672 * @{
673 */
674
675/* Partial update. The wire is the bottleneck, so sending a quarter of
676 * the screen costs a quarter of the time - this is the single biggest
677 * lever left once DMA is in place. Declare what your frame actually
678 * changed and a mostly-static scene runs several times faster. */
679/**
680 * @brief Sends one rectangle of the framebuffer to the panel and returns
681 * when it has gone out.
682 *
683 * Declare what your frame actually changed and a mostly-static scene
684 * runs several times faster: 64x64 goes out in 2.8 ms, 32x32 in 0.75 ms.
685 * The rectangle is clipped to the screen (an empty one sends nothing).
686 * x/w are rounded OUTWARD to a multiple of 2 (16 bpp) or 8 (12 bpp),
687 * because the 4 bpp source has to start and end on a byte. Rounding out
688 * is always safe - you just send a few more pixels than strictly needed.
689 * @param x Left edge, in pixels.
690 * @param y Top edge, in pixels.
691 * @param w Width, in pixels.
692 * @param h Height, in pixels.
693 * @see gfx_flushRectAsync()
694 */
695void gfx_flushRect(int x, int y, int w, int h);
696/**
697 * @brief Starts sending one rectangle of the framebuffer and returns at
698 * once, as gfx_flushAsync() does for the whole frame.
699 *
700 * Clipped and rounded as gfx_flushRect(). gfx_wait() before drawing
701 * again.
702 * @param x Left edge, in pixels.
703 * @param y Top edge, in pixels.
704 * @param w Width, in pixels.
705 * @param h Height, in pixels.
706 */
707void gfx_flushRectAsync(int x, int y, int w, int h);
708
709/** @} */
710
711/* ------------------------------------------------------------------ */
712/* Framebuffer drawing (all clipped, all operate on the 4 bpp buffer) */
713/* ------------------------------------------------------------------ */
714/**
715 * @defgroup chgfx_draw Drawing
716 * @ingroup lib_chgfx
717 * @brief The clip rectangle, pixels, lines, rectangles, circles and
718 * sprite blits, drawn into the 4 bpp framebuffer.
719 *
720 * Every colour is a palette index, 0..15 (only the low 4 bits are
721 * used). Every call is clipped to the clip rectangle, and nothing reaches
722 * the panel until a flush. Draw only after gfx_wait() (or gfx_waitRow()
723 * for the rows you touch).
724 *
725 * **The clip rectangle.** Every call that paints pixels - fills, lines,
726 * shapes, text, sprites, gfx_clear() - stays inside it. It is
727 * intersected with the screen; an empty one (w or h <= 0) stops all
728 * drawing. Defaults to the whole screen. gfx_getPixel(), gfx_scroll() and
729 * the flush ignore it. Changing it costs nothing, so set it around a
730 * panel or a HUD and put it back:
731 *
732 * @code
733 * gfx_setClip(0, 10, 128, 118); drawBoard(); gfx_resetClip();
734 * @endcode
735 * @{
736 */
737
738/**
739 * @brief Sets the clip rectangle every drawing call stays inside.
740 * @param x Left edge, in pixels.
741 * @param y Top edge, in pixels.
742 * @param w Width; 0 or less stops all drawing.
743 * @param h Height; 0 or less stops all drawing.
744 */
745void gfx_setClip(int x, int y, int w, int h);
746/** @brief Puts the clip rectangle back to the whole screen. */
747void gfx_resetClip(void);
748/**
749 * @brief Reads the clip rectangle, as intersected with the screen.
750 * @param[out] x Left edge (may be nullptr).
751 * @param[out] y Top edge (may be nullptr).
752 * @param[out] w Width (may be nullptr).
753 * @param[out] h Height (may be nullptr).
754 */
755void gfx_getClip(int *x, int *y, int *w, int *h);
756
757/**
758 * @brief Fills the clip rectangle (normally the whole screen) with one
759 * colour.
760 *
761 * The whole screen is one word store per 8 pixels, from SRAM.
762 * @param c Palette index.
763 */
764void gfx_clear(uint8_t c);
765/**
766 * @brief Sets one pixel.
767 * @param x Column.
768 * @param y Row.
769 * @param c Palette index.
770 */
771void gfx_pixel(int x, int y, uint8_t c);
772/**
773 * @brief Reads one pixel of the framebuffer (the clip is ignored).
774 * @param x Column.
775 * @param y Row.
776 * @return The palette index there, or 0 off the screen.
777 */
778uint8_t gfx_getPixel(int x, int y);
779/**
780 * @brief Draws a horizontal line from (x, y), `w` pixels to the right.
781 * @param x Left end.
782 * @param y Row.
783 * @param w Length in pixels; 0 or less draws nothing.
784 * @param c Palette index.
785 */
786void gfx_hline(int x, int y, int w, uint8_t c);
787/**
788 * @brief Draws a vertical line from (x, y), `h` pixels down.
789 * @param x Column.
790 * @param y Top end.
791 * @param h Length in pixels; 0 or less draws nothing.
792 * @param c Palette index.
793 */
794void gfx_vline(int x, int y, int h, uint8_t c);
795/**
796 * @brief Fills a rectangle.
797 * @param x Left edge.
798 * @param y Top edge.
799 * @param w Width; 0 or less draws nothing.
800 * @param h Height; 0 or less draws nothing.
801 * @param c Palette index.
802 */
803void gfx_fillRect(int x, int y, int w, int h, uint8_t c);
804/**
805 * @brief Draws a rectangle's 1-pixel outline.
806 * @param x Left edge.
807 * @param y Top edge.
808 * @param w Width; 0 or less draws nothing.
809 * @param h Height; 0 or less draws nothing.
810 * @param c Palette index.
811 */
812void gfx_rect(int x, int y, int w, int h, uint8_t c);
813/**
814 * @brief Draws a line between two points, both ends included.
815 * @param x0 Start column.
816 * @param y0 Start row.
817 * @param x1 End column.
818 * @param y1 End row.
819 * @param c Palette index.
820 */
821void gfx_line(int x0, int y0, int x1, int y1, uint8_t c);
822/**
823 * @brief Draws a circle's 1-pixel outline, 2*r+1 pixels across.
824 * @param cx Centre column.
825 * @param cy Centre row.
826 * @param r Radius in pixels.
827 * @param c Palette index.
828 */
829void gfx_circle(int cx, int cy, int r, uint8_t c);
830/**
831 * @brief Draws a filled circle, 2*r+1 pixels across.
832 * @param cx Centre column.
833 * @param cy Centre row.
834 * @param r Radius in pixels.
835 * @param c Palette index.
836 */
837void gfx_fillCircle(int cx, int cy, int r, uint8_t c);
838
839/**
840 * @brief Copies a 4 bpp bitmap into the framebuffer, optionally with one
841 * colour transparent.
842 *
843 * Source rows are packed the same way as the framebuffer (2 px/byte,
844 * even x low nibble), each row padded to a whole byte. The opaque even-x
845 * case runs at memcpy speed. For art with many runs of one colour, the
846 * compact @ref chgfx_sprites "span sprites" are smaller.
847 * @param spr The bitmap, `(w + 1) / 2` bytes a row.
848 * @param x Left edge on screen.
849 * @param y Top edge on screen.
850 * @param w Width in pixels.
851 * @param h Height in pixels.
852 * @param transparent Colour index skipped, or -1 for an opaque copy.
853 */
854void gfx_blit(const uint8_t *spr, int x, int y, int w, int h, int transparent);
855
856/** @} */
857
858/* ------------------------------------------------------------------ */
859/* Shapes */
860/* ------------------------------------------------------------------ */
861/**
862 * @defgroup chgfx_shapes Shapes
863 * @ingroup lib_chgfx
864 * @brief Rounded rectangles, ellipses, dithered fills and in-place
865 * recolouring.
866 *
867 * Clipped like everything in @ref chgfx_draw. Nothing here costs a
868 * sketch that does not call it: the linker drops what is unused.
869 * @{
870 */
871
872/**
873 * @brief Draws a rounded rectangle's 1-pixel outline.
874 *
875 * Corners are pixel-art arcs, not chamfers; r is clamped to half the
876 * shorter side, and r = 0 is a plain rectangle.
877 * @param x Left edge.
878 * @param y Top edge.
879 * @param w Width; 0 or less draws nothing.
880 * @param h Height; 0 or less draws nothing.
881 * @param r Corner radius in pixels.
882 * @param c Palette index.
883 */
884void gfx_roundRect(int x, int y, int w, int h, int r, uint8_t c);
885/**
886 * @brief Fills a rounded rectangle (corners as gfx_roundRect()).
887 * @param x Left edge.
888 * @param y Top edge.
889 * @param w Width; 0 or less draws nothing.
890 * @param h Height; 0 or less draws nothing.
891 * @param r Corner radius in pixels.
892 * @param c Palette index.
893 */
894void gfx_fillRoundRect(int x, int y, int w, int h, int r, uint8_t c);
895
896/**
897 * @brief Draws an axis-aligned ellipse's 1-pixel outline, 2*rx+1 by
898 * 2*ry+1 pixels, centred on (cx, cy).
899 *
900 * Integer only, no square roots.
901 * @param cx Centre column.
902 * @param cy Centre row.
903 * @param rx Horizontal radius in pixels.
904 * @param ry Vertical radius in pixels.
905 * @param c Palette index.
906 */
907void gfx_ellipse(int cx, int cy, int rx, int ry, uint8_t c);
908/**
909 * @brief Draws a filled axis-aligned ellipse, 2*rx+1 by 2*ry+1 pixels,
910 * centred on (cx, cy).
911 * @param cx Centre column.
912 * @param cy Centre row.
913 * @param rx Horizontal radius in pixels.
914 * @param ry Vertical radius in pixels.
915 * @param c Palette index.
916 */
917void gfx_fillEllipse(int cx, int cy, int rx, int ry, uint8_t c);
918
919/**
920 * @brief Paints a 50% checkerboard of one colour over a rectangle,
921 * leaving the pixels in between alone.
922 *
923 * Darkened backdrops behind menus, felt, shadows.
924 * @param x Left edge.
925 * @param y Top edge.
926 * @param w Width.
927 * @param h Height.
928 * @param c Palette index.
929 * @param phase 0 paints pixels where x + y is even, 1 where it is odd.
930 */
931void gfx_dither(int x, int y, int w, int h, uint8_t c, uint8_t phase);
932
933/**
934 * @brief Recolours a rectangle in place through a 16-entry table:
935 * pixel = remap[pixel].
936 *
937 * Dimming, tinting, a hit flash on what is already drawn. A rectangle of
938 * 512 pixels or more builds a 256-entry table first and then costs one
939 * lookup per two pixels.
940 * @param x Left edge.
941 * @param y Top edge.
942 * @param w Width.
943 * @param h Height.
944 * @param remap 16 palette indices: the new colour for each old one.
945 */
946void gfx_remapRect(int x, int y, int w, int h, const uint8_t *remap);
947
948/** @} */
949
950/* ------------------------------------------------------------------ */
951/* Span sprites (sprite4) */
952/* ------------------------------------------------------------------ */
953/**
954 * @defgroup chgfx_sprites Span sprites
955 * @ingroup lib_chgfx
956 * @brief The compact run-length sprite format (sprite4): plain, scaled,
957 * recoloured and rotated.
958 *
959 * The compact sprite format two CHGame titles arrived at independently.
960 * Each row is a list of runs of one colour, so the art is small and a
961 * run is a fill, not a pixel loop:
962 *
963 * w, h, then per row: n, then n bytes of (len - 1) << 4 | colour
964 *
965 * Runs are 1..16 px. Colour 15 is transparent (a skip), and trailing
966 * transparency is left out. `extras/sprite4.py` packs a PNG.
967 *
968 * Every pixel is drawn through remap[colour] (nullptr = as is), so ONE
969 * image serves many looks: a team colour, a red damage flash, a white
970 * hit flash, a solid silhouette for an outline. With 16 colours,
971 * palette swapping is the natural way to get variety.
972 *
973 * @code
974 * #include "slime.h" // python extras/sprite4.py slime.png SLIME > slime.h
975 * gfx_sprite4(SLIME, 40, 60); // as drawn
976 * gfx_sprite4(SLIME, 40, 60, redFlash); // through a 16-entry remap
977 * gfx_sprite4(SLIME, 40, 60, nullptr, 512); // double size
978 * @endcode
979 * @{
980 */
981
982/**
983 * @brief Draws a span sprite, optionally recoloured and scaled.
984 *
985 * Scaling is nearest neighbour, about the top-left corner. 1:1 has its
986 * own fast path, so leave it at 256 unless you mean it.
987 * @param spr The sprite data (w, h, then the rows of runs).
988 * @param x Left edge on screen.
989 * @param y Top edge on screen.
990 * @param remap 16 palette indices to draw each colour as, or nullptr
991 * (the default) for the colours as they are.
992 * @param scale Q8 scale: 256 = 1:1 (the default), 512 = double, 128 =
993 * half. 0 or less draws nothing.
994 */
995void gfx_sprite4(const uint8_t *spr, int x, int y,
996 const uint8_t *remap = nullptr, int scale = 256);
997/**
998 * @brief A span sprite's width.
999 * @param spr The sprite data.
1000 * @return Its width in pixels, at 1:1.
1001 */
1002static inline int gfx_sprite4W(const uint8_t *spr) { return spr[0]; }
1003/**
1004 * @brief A span sprite's height.
1005 * @param spr The sprite data.
1006 * @return Its height in pixels, at 1:1.
1007 */
1008static inline int gfx_sprite4H(const uint8_t *spr) { return spr[1]; }
1009
1010/**
1011 * @brief Draws a span sprite rotated and scaled about a pivot.
1012 *
1013 * The sprite's pixel (ax, ay) lands on screen at (px, py), turned by
1014 * `angle` and scaled by `scale`. The art is decoded into
1015 * gfx_chunkScratch(), so it must fit in 1 KB at 4 bpp (32x64, 45x45; a
1016 * larger sprite draws nothing) and this call waits for any flush in
1017 * flight first. Per pixel, so costlier than gfx_sprite4().
1018 * @param spr The sprite data.
1019 * @param ax Pivot column in the sprite.
1020 * @param ay Pivot row in the sprite.
1021 * @param px Screen column the pivot lands on.
1022 * @param py Screen row the pivot lands on.
1023 * @param angle Rotation, 256 = a full turn, clockwise on screen.
1024 * @param scale Q8 scale, 256 = 1:1 (the default); below 8 counts as 8,
1025 * 0 or less draws nothing.
1026 * @param remap 16 palette indices to draw each colour as, or nullptr.
1027 */
1028void gfx_sprite4Rot(const uint8_t *spr, int ax, int ay, int px, int py,
1029 uint8_t angle, int scale = 256, const uint8_t *remap = nullptr);
1030
1031/** @} */
1032
1033/* ------------------------------------------------------------------ */
1034/* Row operations */
1035/* ------------------------------------------------------------------ */
1036/**
1037 * @defgroup chgfx_rows Row operations
1038 * @ingroup lib_chgfx
1039 * @brief Scrolling and stamping whole framebuffer rows at word speed.
1040 *
1041 * Word copies in SRAM. newlib-nano's memmove/memcpy are byte loops in
1042 * flash here: a screen shake built on memmove cost ~10 ms a frame.
1043 * @{
1044 */
1045
1046/**
1047 * @brief Moves a band of rows by (dx, dy) pixels inside the band.
1048 *
1049 * Moves the band of rows y..y+h-1 by dx pixels right and dy rows down
1050 * (negative = left/up). Odd dx is fine (nibble shifts). It works on whole
1051 * rows and ignores the clip rectangle - it is a post-process: shake,
1052 * scrolling backgrounds.
1053 * @param y First row of the band.
1054 * @param h Rows in the band (clipped to the screen).
1055 * @param dx Pixels to move right; negative moves left.
1056 * @param dy Rows to move down; negative moves up.
1057 * @param fill Palette index for the pixels uncovered by the move, or -1
1058 * (the default) to leave them as they were.
1059 */
1060void gfx_scroll(int y, int h, int dx, int dy, int fill = -1);
1061
1062/**
1063 * @brief Copies pixels [x0, x1) of a full-width row buffer into
1064 * framebuffer row y, clipped.
1065 *
1066 * Build a pattern row once, stamp it into many rows at word speed: tiled
1067 * floors, checkerboards, gradients.
1068 * @param y Framebuffer row.
1069 * @param src A row of GFX_FB_STRIDE bytes, packed like the framebuffer.
1070 * @param x0 First pixel copied.
1071 * @param x1 One past the last pixel copied.
1072 */
1073void gfx_copyRow(int y, const uint8_t *src, int x0, int x1);
1074
1075/** @} */
1076
1077/* ------------------------------------------------------------------ */
1078/* Text */
1079/* ------------------------------------------------------------------ */
1080/**
1081 * @defgroup chgfx_text Text
1082 * @ingroup lib_chgfx
1083 * @brief Text in the built-in 5x7 font or any GFXfont: plain, scaled,
1084 * measured, and outlined/shadowed banners.
1085 *
1086 * The default font is the built-in 5x7, ASCII 32..126, 475 bytes, no
1087 * setup. gfx_setFont() swaps in a proportional GFXfont;
1088 * gfx_setFont(nullptr) puts the built-in one back.
1089 *
1090 * @code
1091 * #include <fonts/CHGfx_Sans12.h>
1092 * Gfx.setFont(&CHGfx_Sans12);
1093 * Gfx.print(4, 20, "Hello", WHITE);
1094 * @endcode
1095 *
1096 * GFXfont is byte-for-byte Adafruit's format (see CHGfx_gfxfont.h), so
1097 * anything from Adafruit_GFX's Fonts/ directory, or produced by either
1098 * font converter, works here as-is:
1099 *
1100 * @code
1101 * #include <Fonts/FreeSans9pt7b.h>
1102 * Gfx.setFont(&FreeSans9pt7b);
1103 * @endcode
1104 *
1105 * **The origin changes with the font.** Built-in font: y is the TOP of
1106 * the glyph box. Custom font: y is the BASELINE, with ascenders above it
1107 * and descenders below. Adafruit_GFX behaves exactly the same way, which
1108 * is why it is worth living with. gfx_fontBaseline() converts:
1109 *
1110 * @code
1111 * gfx_text(x, y + gfx_fontBaseline(), s, c); // y = top, either font
1112 * @endcode
1113 *
1114 * A newline starts a new line at the original x; a carriage return is
1115 * ignored. Characters outside the font's range are drawn as '?', or
1116 * dropped if the font has no '?'.
1117 *
1118 * Bundled fonts live in src/fonts/ - see @ref chgfx_fonts for the list,
1119 * their flash cost, and how to convert your own with
1120 * `extras/fontconvert.py`.
1121 * @{
1122 */
1123
1124/**
1125 * @brief Selects the font every text call uses from now on.
1126 *
1127 * Scans the glyphs once to find the font's ascent (for
1128 * gfx_fontBaseline()).
1129 * @param f A GFXfont, or nullptr for the built-in 5x7.
1130 */
1131void gfx_setFont(const GFXfont *f);
1132/**
1133 * @brief The current font.
1134 * @return The GFXfont last given to gfx_setFont(), or nullptr for the
1135 * built-in 5x7.
1136 */
1137const GFXfont *gfx_font(void);
1138
1139/**
1140 * @brief Baseline-to-baseline line spacing of the current font, at scale 1.
1141 * @return Pixels: the font's yAdvance, or 8 for the built-in font.
1142 * Multiply by your scale.
1143 */
1145/**
1146 * @brief The current font's ascent: how far its tallest ink reaches above
1147 * the baseline, at scale 1.
1148 * @return Pixels; 0 for the built-in font, since that one is already
1149 * top-anchored. Add it to a top y to get the y to draw at.
1150 */
1152
1153/**
1154 * @brief Draws one character.
1155 * @param x Left edge (pen position).
1156 * @param y Top of the glyph box (built-in font) or baseline (GFXfont).
1157 * @param ch The character.
1158 * @param c Palette index.
1159 */
1160void gfx_char(int x, int y, char ch, uint8_t c);
1161/**
1162 * @brief Draws one character, scaled up by a whole number.
1163 * @param x Left edge (pen position).
1164 * @param y Top of the glyph box (built-in font) or baseline (GFXfont).
1165 * @param ch The character.
1166 * @param c Palette index.
1167 * @param scale Integer scale, 1 or more (0 counts as 1).
1168 */
1169void gfx_charScaled(int x, int y, char ch, uint8_t c, uint8_t scale);
1170/**
1171 * @brief Draws a string in the current font.
1172 *
1173 * The built-in font advances 6 pixels a character.
1174 * @param x Left edge of the first character.
1175 * @param y Top of the glyph box (built-in font) or baseline (GFXfont).
1176 * @param s Null-terminated string; a newline starts a new line at x.
1177 * @param c Palette index.
1178 */
1179void gfx_text(int x, int y, const char *s, uint8_t c);
1180/**
1181 * @brief Draws a string in the current font, scaled up by a whole number.
1182 * @param x Left edge of the first character.
1183 * @param y Top of the glyph box (built-in font) or baseline (GFXfont).
1184 * @param s Null-terminated string; a newline starts a new line at x.
1185 * @param c Palette index.
1186 * @param scale Integer scale, 1 or more (0 counts as 1).
1187 */
1188void gfx_textScaled(int x, int y, const char *s, uint8_t c, uint8_t scale);
1189
1190/**
1191 * @brief Advance width of a string in the current font - the number to
1192 * use for centring and right-alignment.
1193 * @param s Null-terminated string.
1194 * @return Width in pixels; for a multi-line string, the widest line.
1195 */
1196int gfx_textWidth(const char *s);
1197/**
1198 * @brief Advance width of a string in the current font at a scale.
1199 * @param s Null-terminated string.
1200 * @param scale Integer scale, 1 or more (0 counts as 1).
1201 * @return Width in pixels; for a multi-line string, the widest line.
1202 */
1203int gfx_textWidthScaled(const char *s, uint8_t scale);
1204
1205/**
1206 * @brief Bounding box of a string drawn at (x, y).
1207 *
1208 * Same units and origin convention as gfx_text() - use it to frame or
1209 * erase text without guessing. With a custom font this is the tight ink
1210 * box; with the built-in font it is the 5x7 cell box, so a leading space
1211 * still counts. A string with no ink gives a 0x0 box at (x, y).
1212 * @param s Null-terminated string.
1213 * @param x Where the string would be drawn.
1214 * @param y Where the string would be drawn (top or baseline, as
1215 * gfx_text()).
1216 * @param scale Integer scale, 1 or more (0 counts as 1).
1217 * @param[out] bx Left edge of the box (may be nullptr).
1218 * @param[out] by Top edge of the box (may be nullptr).
1219 * @param[out] bw Width of the box (may be nullptr).
1220 * @param[out] bh Height of the box (may be nullptr).
1221 */
1222void gfx_textBounds(const char *s, int x, int y, uint8_t scale,
1223 int *bx, int *by, int *bw, int *bh);
1224
1225/**
1226 * @brief Draws outlined, shadowed or gradient-filled text - titles and
1227 * banners.
1228 *
1229 * Same font, origin and scale rules as gfx_textScaled(). Drawing the
1230 * glyphs nine times over would cost ~9x a plain print. This renders the
1231 * text once into a 1 bpp mask in gfx_chunkScratch(), grows the outline
1232 * out of it with word-wide ORs and paints each row as spans, so a 3x
1233 * outlined, shadowed nine-letter banner costs about 2.9 ms. The mask -
1234 * the text's ink box plus a 1 px margin - must fit in 1 KB: text up to
1235 * 126x62 px, or 254x30. Waits for any flush in flight first.
1236 *
1237 * @param x Left edge, as gfx_text().
1238 * @param y Top (built-in font) or baseline (GFXfont), as gfx_text().
1239 * @param s Null-terminated string.
1240 * @param scale Integer scale, 1 or more.
1241 * @param fill Palette index of the letters.
1242 * @param outline Palette index of a 1 px ring around them (all 8
1243 * directions), or -1 (the default) for none.
1244 * @param shadow Palette index of the outlined shape again, 1 px
1245 * down-right, underneath, or -1 (the default) for none.
1246 * @param ramp Optional: a fill colour per pixel row of the text, top
1247 * to bottom (as many entries as the text is tall),
1248 * replacing `fill`; nullptr for none.
1249 * @param dy Optional: a vertical offset per character, for wavy
1250 * text; nullptr for none.
1251 * @return false, having drawn nothing, if the text is too big for the
1252 * mask; true otherwise (also when nothing was inside the clip).
1253 */
1254bool gfx_textFx(int x, int y, const char *s, uint8_t scale, uint8_t fill,
1255 int outline = -1, int shadow = -1,
1256 const uint8_t *ramp = nullptr, const int8_t *dy = nullptr);
1257
1258/** @} */
1259
1260/* ------------------------------------------------------------------ */
1261/* Direct-to-panel paths (bypass the framebuffer entirely) */
1262/* ------------------------------------------------------------------ */
1263/**
1264 * @defgroup chgfx_direct Direct to the panel
1265 * @ingroup lib_chgfx
1266 * @brief Talking to the ST7735 without the framebuffer: chip select,
1267 * commands, address windows, DMA fills and raw blits.
1268 *
1269 * These write to the panel at once, so call them only when no flush is
1270 * in flight (after gfx_wait()); the SD card shares the bus too. Raw
1271 * pixel data must already be in the panel's current format.
1272 *
1273 * @code
1274 * gfx_wait();
1275 * gfx_select();
1276 * gfx_setWindow(0, 0, 32, 32);
1277 * gfx_directBlit(pixels565, 32 * 32 * 2, true);
1278 * gfx_deselect();
1279 * @endcode
1280 * @{
1281 */
1282
1283/**
1284 * @brief Asserts the panel's chip select.
1285 *
1286 * Assert once, stream a whole frame, deassert - that is the entire
1287 * performance thesis of this driver in two functions. The panel latches
1288 * nothing while CS is high.
1289 */
1290void gfx_select(void);
1291/** @brief Waits for the last byte to leave SPI1, then releases the panel's chip select. */
1292void gfx_deselect(void);
1293
1294/**
1295 * @brief Sets the panel's address window and starts a memory write
1296 * (CASET, RASET, RAMWR).
1297 *
1298 * Pixel data sent after it fills the window left to right, top to
1299 * bottom. The panel offsets (gfx_setPanelOffsets()) are added; nothing
1300 * is clipped. Needs gfx_select() first.
1301 * @param x Left edge, in screen pixels.
1302 * @param y Top edge, in screen pixels.
1303 * @param w Width in pixels, 1 or more.
1304 * @param h Height in pixels, 1 or more.
1305 */
1306void gfx_setWindow(uint8_t x, uint8_t y, uint8_t w, uint8_t h);
1307/**
1308 * @brief Sends one command byte (D/C low), then sets D/C back to data.
1309 * @param c ST7735 command.
1310 */
1311void gfx_cmd(uint8_t c);
1312/**
1313 * @brief Sends one data byte (D/C high).
1314 * @param d The byte.
1315 */
1316void gfx_data8(uint8_t d);
1317
1318/**
1319 * @brief Programs the ST7735 RGBSET (2Dh) colour-depth conversion LUT for
1320 * the current mode.
1321 *
1322 * Done automatically for 12 bpp because the datasheet says the table
1323 * powers up "Random"; call gfx_setWriteColorLut(false) before
1324 * gfx_setColorMode() if you would rather trust the factory contents.
1325 */
1327/**
1328 * @brief Chooses whether entering 12 bpp writes the colour LUT
1329 * (gfx_writeColorLut()).
1330 * @param on true (the default) to write it, false to trust the panel's
1331 * factory contents.
1332 */
1334
1335/**
1336 * @brief Fills a rectangle of the panel with one colour by DMA, bypassing
1337 * the framebuffer.
1338 *
1339 * DMA with memory-increment DISABLED: the DMA engine re-reads one
1340 * halfword N times. Zero RAM, zero CPU, full wire speed. Waits for any
1341 * flush in flight; handles chip select itself; not clipped.
1342 * @param x Left edge, in screen pixels.
1343 * @param y Top edge, in screen pixels.
1344 * @param w Width in pixels.
1345 * @param h Height in pixels.
1346 * @param rgb565 The colour, in RGB565.
1347 * @warning 16 bpp only - a 12 bpp solid colour has a 3-byte repeat that
1348 * does not fit in a single halfword.
1349 */
1350void gfx_directFillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t rgb565);
1351
1352/**
1353 * @brief Sends a caller-owned buffer, already in panel format, by DMA and
1354 * waits for it.
1355 *
1356 * Needs gfx_select() and gfx_setWindow() first.
1357 * @param data The pixel data.
1358 * @param bytes Its length in bytes (even when `halfword`).
1359 * @param halfword true to send 16-bit SPI frames (RGB565 as uint16_t,
1360 * 16 bpp), false for bytes (12 and 18 bpp).
1361 */
1362void gfx_directBlit(const void *data, uint32_t bytes, bool halfword);
1363
1364/**
1365 * @brief Sends bytes one at a time in a polled loop, for baseline
1366 * comparisons.
1367 * @param data The bytes.
1368 * @param bytes How many.
1369 */
1370void gfx_blockingWrite(const uint8_t *data, uint32_t bytes);
1371
1372/** @} */
1373
1374/* ------------------------------------------------------------------ */
1375/* Internals exposed for the benchmark */
1376/* ------------------------------------------------------------------ */
1377/**
1378 * @defgroup chgfx_internals Internals and scratch
1379 * @ingroup lib_chgfx
1380 * @brief The converters the Benchmark example measures, the flush's
1381 * chunk buffers as scratch memory, and the frame size.
1382 * @{
1383 */
1384
1385/**
1386 * @brief Converts framebuffer rows into the panel's format, with the
1387 * converter in SRAM.
1388 *
1389 * _ram lives in SRAM, _flash lives in flash. Benchmarking both is how
1390 * you measure the 3-wait-state penalty.
1391 * @param dst Where the panel-format bytes go.
1392 * @param row First framebuffer row.
1393 * @param rows How many rows.
1394 * @return Bytes written to `dst`.
1395 */
1396uint32_t gfx_convertRows_ram(uint8_t *dst, uint16_t row, uint16_t rows);
1397/**
1398 * @brief Converts framebuffer rows into the panel's format, with the
1399 * converter in flash (for comparison with gfx_convertRows_ram()).
1400 * @param dst Where the panel-format bytes go.
1401 * @param row First framebuffer row.
1402 * @param rows How many rows.
1403 * @return Bytes written to `dst`.
1404 */
1405uint32_t gfx_convertRows_flash(uint8_t *dst, uint16_t row, uint16_t rows);
1406/**
1407 * @brief Converts a run of framebuffer bytes into the panel's format (the
1408 * SRAM converter every flush uses).
1409 * @param dst Where the panel-format bytes go.
1410 * @param src 4 bpp source bytes, two pixels each.
1411 * @param srcBytes How many source bytes.
1412 * @return Bytes written to `dst`.
1413 */
1414uint32_t gfx_convertSpan_ram(uint8_t *dst, const uint8_t *src, uint32_t srcBytes);
1415
1416/**
1417 * @brief 1 KB of word-aligned scratch memory: the flush's chunk buffers.
1418 *
1419 * 2 * GFX_CHUNK_BYTES, idle from gfx_wait() until the next flush starts.
1420 * Handy for decoding, masks, building a flash page, an SD card block -
1421 * never across a flush. gfx_sprite4Rot() and gfx_textFx() use it, so it
1422 * does not survive them either.
1423 * @return The start of the scratch.
1424 */
1425uint8_t *gfx_chunkScratch(void);
1426
1427/**
1428 * @brief Bytes the panel receives for a full frame in the current mode.
1429 * @return 32768 (16 bpp), 24576 (12 bpp) or 49152 (18 bpp) at 128x128.
1430 */
1431uint32_t gfx_frameBytes(void);
1432
1433/** @} */
1434
1435/* ------------------------------------------------------------------ */
1436/* Palette helper */
1437/* ------------------------------------------------------------------ */
1438/**
1439 * @brief Nearest palette index for a full RGB565 colour.
1440 * @ingroup chgfx_palette
1441 *
1442 * Linear search over 16 entries, by weighted distance, against the
1443 * palette as set (not as faded) - fine at setup time to build named
1444 * constants, far too slow to call per pixel.
1445 * @param rgb565 The colour, in RGB565.
1446 * @return The palette index, 0..15, closest to it.
1447 */
1448uint8_t gfx_nearest(uint16_t rgb565);
1449
1450/* ------------------------------------------------------------------ */
1451/* Idiomatic Arduino wrapper */
1452/* ------------------------------------------------------------------ */
1453/**
1454 * @defgroup chgfx_class The Gfx object
1455 * @ingroup lib_chgfx
1456 * @brief The CHGfx class and its one instance, Gfx: the same API with
1457 * Arduino-style names.
1458 * @{
1459 */
1460
1461/**
1462 * @brief Arduino-style access to CHGfx: every method is a zero-cost
1463 * inline forwarder to a `gfx_*` function.
1464 *
1465 * There is exactly one panel, one SPI peripheral and one DMA channel, so
1466 * the driver state is inherently global - the class exists for the
1467 * familiar `Gfx.clear(0)` spelling, not to allow two instances. Use the
1468 * one instance, Gfx. Use whichever style you prefer; they are the same
1469 * code. Each method's full description is on the function it forwards to.
1470 *
1471 * @code
1472 * Gfx.begin(GFX_DIV2, GFX_12BPP);
1473 * Gfx.clear(0);
1474 * Gfx.print(4, 4, "Hello", 1);
1475 * Gfx.display();
1476 * @endcode
1477 */
1478class CHGfx {
1479public:
1480 /**
1481 * @brief Starts the panel.
1482 * @param spiDiv SPI clock divider (default GFX_DIV2, 24 MHz).
1483 * @param colorMode GFX_16BPP (the default), GFX_12BPP or GFX_18BPP.
1484 * @see gfx_begin()
1485 */
1486 void begin(uint8_t spiDiv = GFX_DIV2, uint8_t colorMode = GFX_16BPP) { gfx_begin(spiDiv, colorMode); }
1487
1488 /** @name Configuration */
1489 /** @{ */
1490 /** @brief Changes the SPI clock divider. @param d GFX_DIV2..GFX_DIV16. @see gfx_setSpiDiv() */
1491 void setSpiDiv(uint8_t d) { gfx_setSpiDiv(d); }
1492 /** @brief Switches the colour output mode. @param m GFX_16BPP, GFX_12BPP or GFX_18BPP. @see gfx_setColorMode() */
1493 void setColorMode(uint8_t m) { gfx_setColorMode(m); }
1494 /** @brief The current colour output mode. @return GFX_16BPP, GFX_12BPP or GFX_18BPP. @see gfx_colorMode() */
1495 uint8_t colorMode() const { return gfx_colorMode(); }
1496 /** @brief The current SPI clock. @return Hz. @see gfx_spiHz() */
1497 uint32_t spiHz() const { return gfx_spiHz(); }
1498 /**
1499 * @brief Sets the panel's MADCTL and window offsets.
1500 * @param m MADCTL value.
1501 * @param cs First visible column.
1502 * @param rs First visible row.
1503 * @see gfx_setPanelOffsets()
1504 */
1505 void setPanelOffsets(uint8_t m, uint8_t cs, uint8_t rs) { gfx_setPanelOffsets(m, cs, rs); }
1506 /** @brief Turns colour inversion on or off. @param on true to invert. @see gfx_setInverted() */
1507 void setInverted(bool on) { gfx_setInverted(on); }
1508 /**
1509 * @brief Sets the panel's own refresh rate (FRMCTR1).
1510 * @param r RTNA.
1511 * @param f Front porch.
1512 * @param b Back porch.
1513 * @see gfx_setPanelFrameRate()
1514 */
1515 void setPanelFrameRate(uint8_t r, uint8_t f, uint8_t b) { gfx_setPanelFrameRate(r, f, b); }
1516 /** @} */
1517
1518 /** @name Palette */
1519 /** @{ */
1520 /**
1521 * @brief Sets the first `n` palette entries.
1522 * @param p RGB565 colours.
1523 * @param n How many, at most 16.
1524 * @see gfx_setPalette()
1525 */
1526 void setPalette(const uint16_t *p, uint8_t n) { gfx_setPalette(p, n); }
1527 /**
1528 * @brief Sets one palette entry.
1529 * @param i Palette index, 0..15.
1530 * @param c RGB565 colour.
1531 * @see gfx_setPaletteEntry()
1532 */
1533 void setPaletteEntry(uint8_t i, uint16_t c) { gfx_setPaletteEntry(i, c); }
1534 /**
1535 * @brief Fades every colour toward one colour.
1536 * @param amount 0 = none, 255 = solid `rgb565`.
1537 * @param rgb565 The colour faded toward (default black).
1538 * @see gfx_setFade()
1539 */
1540 void setFade(uint8_t amount, uint16_t rgb565 = 0) { gfx_setFade(amount, rgb565); }
1541 /** @brief The current fade amount. @return 0..255. @see gfx_fade() */
1542 uint8_t fade() const { return gfx_fade(); }
1543 /**
1544 * @brief Nearest palette index for an RGB565 colour (slow: setup only).
1545 * @param rgb565 The colour.
1546 * @return Palette index, 0..15.
1547 * @see gfx_nearest()
1548 */
1549 uint8_t nearest(uint16_t rgb565) const { return gfx_nearest(rgb565); }
1550 /**
1551 * @brief Packs an 8-bit-per-channel colour into RGB565.
1552 * @param r Red, 0..255.
1553 * @param g Green, 0..255.
1554 * @param b Blue, 0..255.
1555 * @return RGB565.
1556 * @see gfx_rgb()
1557 */
1558 static uint16_t rgb(uint8_t r, uint8_t g, uint8_t b) { return gfx_rgb(r, g, b); }
1559 /** @} */
1560
1561 /** @name Present */
1562 /** @{ */
1563 /** @brief Sends the whole framebuffer and waits for it. @see gfx_flush() */
1564 void display() { gfx_flush(); }
1565 /**
1566 * @brief Draws one full-colour frame from a callback, bypassing the
1567 * framebuffer.
1568 * @param fn Fills each chunk of rows.
1569 * @param user Passed to `fn` (default nullptr).
1570 * @see gfx_stream()
1571 */
1572 void stream(gfx_streamFn fn, void *user = nullptr) { gfx_stream(fn, user); }
1573 /** @brief Starts sending the whole framebuffer by DMA and returns at once. @see gfx_flushAsync() */
1575 /**
1576 * @brief Sends one rectangle of the framebuffer and waits for it.
1577 * @param x Left edge.
1578 * @param y Top edge.
1579 * @param w Width.
1580 * @param h Height.
1581 * @see gfx_flushRect()
1582 */
1583 void displayRect(int x, int y, int w, int h) { gfx_flushRect(x, y, w, h); }
1584 /**
1585 * @brief Starts sending one rectangle of the framebuffer and returns at once.
1586 * @param x Left edge.
1587 * @param y Top edge.
1588 * @param w Width.
1589 * @param h Height.
1590 * @see gfx_flushRectAsync()
1591 */
1592 void displayRectAsync(int x, int y, int w, int h){ gfx_flushRectAsync(x, y, w, h); }
1593 /** @brief Whether an async flush is in flight. @return true while sending. @see gfx_busy() */
1594 bool busy() const { return gfx_busy(); }
1595 /** @brief Waits for the flush in flight to finish. @see gfx_wait() */
1596 void wait() { gfx_wait(); }
1597 /** @brief The first row the flush in flight may still read. @return A row, or GFX_H when idle. @see gfx_flushRow() */
1598 int flushRow() const { return gfx_flushRow(); }
1599 /** @brief Waits until rows 0..y-1 are free to draw into. @param y One past the last row needed. @see gfx_waitRow() */
1600 void waitRow(int y) { gfx_waitRow(y); }
1601 /** @} */
1602
1603 /** @name Clip */
1604 /** @{ */
1605 /**
1606 * @brief Sets the clip rectangle.
1607 * @param x Left edge.
1608 * @param y Top edge.
1609 * @param w Width.
1610 * @param h Height.
1611 * @see gfx_setClip()
1612 */
1613 void setClip(int x, int y, int w, int h) { gfx_setClip(x, y, w, h); }
1614 /** @brief Puts the clip rectangle back to the whole screen. @see gfx_resetClip() */
1616 /**
1617 * @brief Reads the clip rectangle.
1618 * @param[out] x Left edge (may be nullptr).
1619 * @param[out] y Top edge (may be nullptr).
1620 * @param[out] w Width (may be nullptr).
1621 * @param[out] h Height (may be nullptr).
1622 * @see gfx_getClip()
1623 */
1624 void getClip(int *x, int *y, int *w, int *h) const { gfx_getClip(x, y, w, h); }
1625 /** @} */
1626
1627 /** @name Draw */
1628 /** @{ */
1629 /** @brief Fills the clip rectangle with one colour. @param c Palette index. @see gfx_clear() */
1630 void clear(uint8_t c) { gfx_clear(c); }
1631 /**
1632 * @brief Sets one pixel.
1633 * @param x Column.
1634 * @param y Row.
1635 * @param c Palette index.
1636 * @see gfx_pixel()
1637 */
1638 void drawPixel(int x, int y, uint8_t c) { gfx_pixel(x, y, c); }
1639 /**
1640 * @brief Reads one pixel.
1641 * @param x Column.
1642 * @param y Row.
1643 * @return Palette index, or 0 off the screen.
1644 * @see gfx_getPixel()
1645 */
1646 uint8_t getPixel(int x, int y) const { return gfx_getPixel(x, y); }
1647 /**
1648 * @brief Draws a horizontal line.
1649 * @param x Left end.
1650 * @param y Row.
1651 * @param w Length.
1652 * @param c Palette index.
1653 * @see gfx_hline()
1654 */
1655 void drawFastHLine(int x, int y, int w, uint8_t c) { gfx_hline(x, y, w, c); }
1656 /**
1657 * @brief Draws a vertical line.
1658 * @param x Column.
1659 * @param y Top end.
1660 * @param h Length.
1661 * @param c Palette index.
1662 * @see gfx_vline()
1663 */
1664 void drawFastVLine(int x, int y, int h, uint8_t c) { gfx_vline(x, y, h, c); }
1665 /**
1666 * @brief Fills a rectangle.
1667 * @param x Left edge.
1668 * @param y Top edge.
1669 * @param w Width.
1670 * @param h Height.
1671 * @param c Palette index.
1672 * @see gfx_fillRect()
1673 */
1674 void fillRect(int x, int y, int w, int h, uint8_t c) { gfx_fillRect(x, y, w, h, c); }
1675 /**
1676 * @brief Draws a rectangle's outline.
1677 * @param x Left edge.
1678 * @param y Top edge.
1679 * @param w Width.
1680 * @param h Height.
1681 * @param c Palette index.
1682 * @see gfx_rect()
1683 */
1684 void drawRect(int x, int y, int w, int h, uint8_t c) { gfx_rect(x, y, w, h, c); }
1685 /**
1686 * @brief Draws a line, both ends included.
1687 * @param x0 Start column.
1688 * @param y0 Start row.
1689 * @param x1 End column.
1690 * @param y1 End row.
1691 * @param c Palette index.
1692 * @see gfx_line()
1693 */
1694 void drawLine(int x0, int y0, int x1, int y1, uint8_t c) { gfx_line(x0, y0, x1, y1, c); }
1695 /**
1696 * @brief Draws a circle's outline.
1697 * @param cx Centre column.
1698 * @param cy Centre row.
1699 * @param r Radius.
1700 * @param c Palette index.
1701 * @see gfx_circle()
1702 */
1703 void drawCircle(int cx, int cy, int r, uint8_t c) { gfx_circle(cx, cy, r, c); }
1704 /**
1705 * @brief Draws a filled circle.
1706 * @param cx Centre column.
1707 * @param cy Centre row.
1708 * @param r Radius.
1709 * @param c Palette index.
1710 * @see gfx_fillCircle()
1711 */
1712 void fillCircle(int cx, int cy, int r, uint8_t c) { gfx_fillCircle(cx, cy, r, c); }
1713 /**
1714 * @brief Copies a 4 bpp bitmap into the framebuffer.
1715 * @param s The bitmap, packed like the framebuffer.
1716 * @param x Left edge.
1717 * @param y Top edge.
1718 * @param w Width in pixels.
1719 * @param h Height in pixels.
1720 * @param transparent Colour index skipped, or -1 (the default) for opaque.
1721 * @see gfx_blit()
1722 */
1723 void drawSprite(const uint8_t *s, int x, int y, int w, int h, int transparent = -1)
1724 { gfx_blit(s, x, y, w, h, transparent); }
1725 /**
1726 * @brief Draws a rounded rectangle's outline.
1727 * @param x Left edge.
1728 * @param y Top edge.
1729 * @param w Width.
1730 * @param h Height.
1731 * @param r Corner radius.
1732 * @param c Palette index.
1733 * @see gfx_roundRect()
1734 */
1735 void drawRoundRect(int x, int y, int w, int h, int r, uint8_t c) { gfx_roundRect(x, y, w, h, r, c); }
1736 /**
1737 * @brief Fills a rounded rectangle.
1738 * @param x Left edge.
1739 * @param y Top edge.
1740 * @param w Width.
1741 * @param h Height.
1742 * @param r Corner radius.
1743 * @param c Palette index.
1744 * @see gfx_fillRoundRect()
1745 */
1746 void fillRoundRect(int x, int y, int w, int h, int r, uint8_t c) { gfx_fillRoundRect(x, y, w, h, r, c); }
1747 /**
1748 * @brief Draws an ellipse's outline.
1749 * @param cx Centre column.
1750 * @param cy Centre row.
1751 * @param rx Horizontal radius.
1752 * @param ry Vertical radius.
1753 * @param c Palette index.
1754 * @see gfx_ellipse()
1755 */
1756 void drawEllipse(int cx, int cy, int rx, int ry, uint8_t c) { gfx_ellipse(cx, cy, rx, ry, c); }
1757 /**
1758 * @brief Draws a filled ellipse.
1759 * @param cx Centre column.
1760 * @param cy Centre row.
1761 * @param rx Horizontal radius.
1762 * @param ry Vertical radius.
1763 * @param c Palette index.
1764 * @see gfx_fillEllipse()
1765 */
1766 void fillEllipse(int cx, int cy, int rx, int ry, uint8_t c) { gfx_fillEllipse(cx, cy, rx, ry, c); }
1767 /**
1768 * @brief Paints a 50% checkerboard of one colour over a rectangle.
1769 * @param x Left edge.
1770 * @param y Top edge.
1771 * @param w Width.
1772 * @param h Height.
1773 * @param c Palette index.
1774 * @param phase 0 (the default) paints where x + y is even, 1 where odd.
1775 * @see gfx_dither()
1776 */
1777 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); }
1778 /**
1779 * @brief Recolours a rectangle in place through a 16-entry table.
1780 * @param x Left edge.
1781 * @param y Top edge.
1782 * @param w Width.
1783 * @param h Height.
1784 * @param remap 16 palette indices.
1785 * @see gfx_remapRect()
1786 */
1787 void remapRect(int x, int y, int w, int h, const uint8_t *remap) { gfx_remapRect(x, y, w, h, remap); }
1788 /**
1789 * @brief Draws a span sprite.
1790 * @param s The sprite data.
1791 * @param x Left edge.
1792 * @param y Top edge.
1793 * @param remap 16 palette indices, or nullptr (the default).
1794 * @param scale Q8 scale, 256 = 1:1 (the default).
1795 * @see gfx_sprite4()
1796 */
1797 void drawSprite4(const uint8_t *s, int x, int y, const uint8_t *remap = nullptr, int scale = 256)
1798 { gfx_sprite4(s, x, y, remap, scale); }
1799 /**
1800 * @brief Draws a span sprite rotated and scaled about a pivot.
1801 * @param s The sprite data (at most 1 KB decoded).
1802 * @param ax Pivot column in the sprite.
1803 * @param ay Pivot row in the sprite.
1804 * @param px Screen column of the pivot.
1805 * @param py Screen row of the pivot.
1806 * @param angle 256 = a full turn, clockwise.
1807 * @param scale Q8 scale, 256 = 1:1 (the default).
1808 * @param remap 16 palette indices, or nullptr (the default).
1809 * @see gfx_sprite4Rot()
1810 */
1811 void drawSprite4Rot(const uint8_t *s, int ax, int ay, int px, int py, uint8_t angle,
1812 int scale = 256, const uint8_t *remap = nullptr)
1813 { gfx_sprite4Rot(s, ax, ay, px, py, angle, scale, remap); }
1814 /**
1815 * @brief Moves a band of rows by (dx, dy) inside the band.
1816 * @param y First row.
1817 * @param h Rows.
1818 * @param dx Pixels right (negative: left).
1819 * @param dy Rows down (negative: up).
1820 * @param fill Palette index for uncovered pixels, or -1 (the default) to keep them.
1821 * @see gfx_scroll()
1822 */
1823 void scroll(int y, int h, int dx, int dy, int fill = -1) { gfx_scroll(y, h, dx, dy, fill); }
1824 /**
1825 * @brief Copies pixels [x0, x1) of a row buffer into a framebuffer row.
1826 * @param y Framebuffer row.
1827 * @param src GFX_FB_STRIDE bytes, packed like the framebuffer.
1828 * @param x0 First pixel.
1829 * @param x1 One past the last pixel.
1830 * @see gfx_copyRow()
1831 */
1832 void copyRow(int y, const uint8_t *src, int x0, int x1) { gfx_copyRow(y, src, x0, x1); }
1833 /**
1834 * @brief Draws one character.
1835 * @param x Left edge.
1836 * @param y Top (built-in font) or baseline (GFXfont).
1837 * @param ch The character.
1838 * @param c Palette index.
1839 * @see gfx_char()
1840 */
1841 void drawChar(int x, int y, char ch, uint8_t c) { gfx_char(x, y, ch, c); }
1842 /**
1843 * @brief Draws one character, scaled.
1844 * @param x Left edge.
1845 * @param y Top (built-in font) or baseline (GFXfont).
1846 * @param ch The character.
1847 * @param c Palette index.
1848 * @param scale Integer scale.
1849 * @see gfx_charScaled()
1850 */
1851 void drawChar(int x, int y, char ch, uint8_t c, uint8_t scale)
1852 { gfx_charScaled(x, y, ch, c, scale); }
1853 /**
1854 * @brief Draws a string.
1855 * @param x Left edge.
1856 * @param y Top (built-in font) or baseline (GFXfont).
1857 * @param s Null-terminated string.
1858 * @param c Palette index.
1859 * @see gfx_text()
1860 */
1861 void print(int x, int y, const char *s, uint8_t c) { gfx_text(x, y, s, c); }
1862 /**
1863 * @brief Draws a string, scaled.
1864 * @param x Left edge.
1865 * @param y Top (built-in font) or baseline (GFXfont).
1866 * @param s Null-terminated string.
1867 * @param c Palette index.
1868 * @param scale Integer scale.
1869 * @see gfx_textScaled()
1870 */
1871 void print(int x, int y, const char *s, uint8_t c, uint8_t scale)
1872 { gfx_textScaled(x, y, s, c, scale); }
1873 /**
1874 * @brief Draws outlined, shadowed or gradient-filled text.
1875 * @param x Left edge.
1876 * @param y Top (built-in font) or baseline (GFXfont).
1877 * @param s Null-terminated string.
1878 * @param scale Integer scale.
1879 * @param fill Palette index of the letters.
1880 * @param outline Palette index of the outline, or -1 (the default).
1881 * @param shadow Palette index of the shadow, or -1 (the default).
1882 * @param ramp A fill colour per pixel row, or nullptr (the default).
1883 * @param dy A vertical offset per character, or nullptr (the default).
1884 * @return false if the text is too big for the 1 KB mask.
1885 * @see gfx_textFx()
1886 */
1887 bool printFx(int x, int y, const char *s, uint8_t scale, uint8_t fill, int outline = -1,
1888 int shadow = -1, const uint8_t *ramp = nullptr, const int8_t *dy = nullptr)
1889 { return gfx_textFx(x, y, s, scale, fill, outline, shadow, ramp, dy); }
1890 /** @} */
1891
1892 /** @name Fonts
1893 * setFont(nullptr) returns to the built-in 5x7. Note that a custom
1894 * font takes y as the baseline - see @ref chgfx_text.
1895 */
1896 /** @{ */
1897 /** @brief Selects the font. @param f A GFXfont, or nullptr (the default) for the built-in 5x7. @see gfx_setFont() */
1898 void setFont(const GFXfont *f = nullptr) { gfx_setFont(f); }
1899 /** @brief The current font. @return The GFXfont, or nullptr for the built-in one. @see gfx_font() */
1900 const GFXfont *font() const { return gfx_font(); }
1901 /** @brief Baseline-to-baseline line spacing. @return Pixels at scale 1. @see gfx_fontLineHeight() */
1902 int fontLineHeight() const { return gfx_fontLineHeight(); }
1903 /** @brief The font's ascent above the baseline. @return Pixels at scale 1; 0 for the built-in font. @see gfx_fontBaseline() */
1904 int fontBaseline() const { return gfx_fontBaseline(); }
1905 /** @brief Advance width of a string. @param s Null-terminated string. @return Pixels (the widest line). @see gfx_textWidth() */
1906 int textWidth(const char *s) const { return gfx_textWidth(s); }
1907 /**
1908 * @brief Advance width of a string at a scale.
1909 * @param s Null-terminated string.
1910 * @param scale Integer scale.
1911 * @return Pixels (the widest line).
1912 * @see gfx_textWidthScaled()
1913 */
1914 int textWidth(const char *s, uint8_t scale) const { return gfx_textWidthScaled(s, scale); }
1915 /**
1916 * @brief Bounding box of a string drawn at (x, y).
1917 * @param s Null-terminated string.
1918 * @param x Where it would be drawn.
1919 * @param y Where it would be drawn.
1920 * @param scale Integer scale.
1921 * @param[out] bx Left edge (may be nullptr).
1922 * @param[out] by Top edge (may be nullptr).
1923 * @param[out] bw Width (may be nullptr).
1924 * @param[out] bh Height (may be nullptr).
1925 * @see gfx_textBounds()
1926 */
1927 void textBounds(const char *s, int x, int y, uint8_t scale,
1928 int *bx, int *by, int *bw, int *bh) const
1929 { gfx_textBounds(s, x, y, scale, bx, by, bw, bh); }
1930 /** @} */
1931
1932 /** @name Direct to the panel */
1933 /** @{ */
1934 /**
1935 * @brief Fills a rectangle of the panel by DMA, bypassing the
1936 * framebuffer (16 bpp only).
1937 * @param x Left edge.
1938 * @param y Top edge.
1939 * @param w Width.
1940 * @param h Height.
1941 * @param rgb565 The colour.
1942 * @see gfx_directFillRect()
1943 */
1944 void fillRectDirect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t rgb565)
1945 { gfx_directFillRect(x, y, w, h, rgb565); }
1946 /** @} */
1947
1948 /** @name The raw framebuffer */
1949 /** @{ */
1950 /** @brief The framebuffer, for primitives of your own. @return gfx_fb. */
1951 uint8_t *buffer() const { return gfx_fb; }
1952 /** @brief Screen width. @return GFX_W. */
1953 static constexpr int width() { return GFX_W; }
1954 /** @brief Screen height. @return GFX_H. */
1955 static constexpr int height() { return GFX_H; }
1956 /** @} */
1957};
1958
1959/**
1960 * @brief The one CHGfx instance. Declared here, defined in CHGfx.cpp.
1961 *
1962 * `Gfx.clear(0)` is the same code as `gfx_clear(0)`.
1963 */
1964extern CHGfx Gfx;
1965
1966/** @} */
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:1478
void stream(gfx_streamFn fn, void *user=nullptr)
Draws one full-colour frame from a callback, bypassing the framebuffer.
Definition CHGfx.h:1572
void setInverted(bool on)
Turns colour inversion on or off.
Definition CHGfx.h:1507
void setFade(uint8_t amount, uint16_t rgb565=0)
Fades every colour toward one colour.
Definition CHGfx.h:1540
void drawChar(int x, int y, char ch, uint8_t c)
Draws one character.
Definition CHGfx.h:1841
uint8_t getPixel(int x, int y) const
Reads one pixel.
Definition CHGfx.h:1646
void setPaletteEntry(uint8_t i, uint16_t c)
Sets one palette entry.
Definition CHGfx.h:1533
void drawRect(int x, int y, int w, int h, uint8_t c)
Draws a rectangle's outline.
Definition CHGfx.h:1684
void wait()
Waits for the flush in flight to finish.
Definition CHGfx.h:1596
int flushRow() const
The first row the flush in flight may still read.
Definition CHGfx.h:1598
uint8_t * buffer() const
The framebuffer, for primitives of your own.
Definition CHGfx.h:1951
int fontLineHeight() const
Baseline-to-baseline line spacing.
Definition CHGfx.h:1902
int textWidth(const char *s, uint8_t scale) const
Advance width of a string at a scale.
Definition CHGfx.h:1914
int textWidth(const char *s) const
Advance width of a string.
Definition CHGfx.h:1906
uint8_t nearest(uint16_t rgb565) const
Nearest palette index for an RGB565 colour (slow: setup only).
Definition CHGfx.h:1549
static constexpr int width()
Screen width.
Definition CHGfx.h:1953
void displayAsync()
Starts sending the whole framebuffer by DMA and returns at once.
Definition CHGfx.h:1574
void begin(uint8_t spiDiv=GFX_DIV2, uint8_t colorMode=GFX_16BPP)
Starts the panel.
Definition CHGfx.h:1486
void setPanelOffsets(uint8_t m, uint8_t cs, uint8_t rs)
Sets the panel's MADCTL and window offsets.
Definition CHGfx.h:1505
void fillCircle(int cx, int cy, int r, uint8_t c)
Draws a filled circle.
Definition CHGfx.h:1712
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:1797
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:1777
void drawChar(int x, int y, char ch, uint8_t c, uint8_t scale)
Draws one character, scaled.
Definition CHGfx.h:1851
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:1832
void drawFastHLine(int x, int y, int w, uint8_t c)
Draws a horizontal line.
Definition CHGfx.h:1655
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:1823
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:1787
void setColorMode(uint8_t m)
Switches the colour output mode.
Definition CHGfx.h:1493
void setPalette(const uint16_t *p, uint8_t n)
Sets the first n palette entries.
Definition CHGfx.h:1526
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:1558
void setPanelFrameRate(uint8_t r, uint8_t f, uint8_t b)
Sets the panel's own refresh rate (FRMCTR1).
Definition CHGfx.h:1515
uint32_t spiHz() const
The current SPI clock.
Definition CHGfx.h:1497
void setSpiDiv(uint8_t d)
Changes the SPI clock divider.
Definition CHGfx.h:1491
void displayRect(int x, int y, int w, int h)
Sends one rectangle of the framebuffer and waits for it.
Definition CHGfx.h:1583
const GFXfont * font() const
The current font.
Definition CHGfx.h:1900
void print(int x, int y, const char *s, uint8_t c, uint8_t scale)
Draws a string, scaled.
Definition CHGfx.h:1871
void displayRectAsync(int x, int y, int w, int h)
Starts sending one rectangle of the framebuffer and returns at once.
Definition CHGfx.h:1592
void drawLine(int x0, int y0, int x1, int y1, uint8_t c)
Draws a line, both ends included.
Definition CHGfx.h:1694
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:1927
int fontBaseline() const
The font's ascent above the baseline.
Definition CHGfx.h:1904
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:1887
void fillRect(int x, int y, int w, int h, uint8_t c)
Fills a rectangle.
Definition CHGfx.h:1674
void drawPixel(int x, int y, uint8_t c)
Sets one pixel.
Definition CHGfx.h:1638
void drawCircle(int cx, int cy, int r, uint8_t c)
Draws a circle's outline.
Definition CHGfx.h:1703
void clear(uint8_t c)
Fills the clip rectangle with one colour.
Definition CHGfx.h:1630
void waitRow(int y)
Waits until rows 0..y-1 are free to draw into.
Definition CHGfx.h:1600
void resetClip()
Puts the clip rectangle back to the whole screen.
Definition CHGfx.h:1615
uint8_t colorMode() const
The current colour output mode.
Definition CHGfx.h:1495
void fillEllipse(int cx, int cy, int rx, int ry, uint8_t c)
Draws a filled ellipse.
Definition CHGfx.h:1766
void setFont(const GFXfont *f=nullptr)
Selects the font.
Definition CHGfx.h:1898
void drawRoundRect(int x, int y, int w, int h, int r, uint8_t c)
Draws a rounded rectangle's outline.
Definition CHGfx.h:1735
void drawEllipse(int cx, int cy, int rx, int ry, uint8_t c)
Draws an ellipse's outline.
Definition CHGfx.h:1756
void display()
Sends the whole framebuffer and waits for it.
Definition CHGfx.h:1564
void drawFastVLine(int x, int y, int h, uint8_t c)
Draws a vertical line.
Definition CHGfx.h:1664
uint8_t fade() const
The current fade amount.
Definition CHGfx.h:1542
void fillRoundRect(int x, int y, int w, int h, int r, uint8_t c)
Fills a rounded rectangle.
Definition CHGfx.h:1746
void print(int x, int y, const char *s, uint8_t c)
Draws a string.
Definition CHGfx.h:1861
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:1811
static constexpr int height()
Screen height.
Definition CHGfx.h:1955
void getClip(int *x, int *y, int *w, int *h) const
Reads the clip rectangle.
Definition CHGfx.h:1624
bool busy() const
Whether an async flush is in flight.
Definition CHGfx.h:1594
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:1944
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:1723
void setClip(int x, int y, int w, int h)
Sets the clip rectangle.
Definition CHGfx.h:1613
CHGfx Gfx
The one CHGfx instance.
#define GFX_H
Screen height in pixels (default 128).
Definition CHGfx.h:118
#define GFX_W
Screen width in pixels (default 128).
Definition CHGfx.h:114
#define GFX_FB_BYTES
Size of the framebuffer, gfx_fb, in bytes: 8192 at 128x128.
Definition CHGfx.h:123
@ GFX_DIV2
HCLK/2 = 24 MHz: the fastest the chip can clock SPI, and the default.
Definition CHGfx.h:245
@ GFX_DIV16
HCLK/16 = 3 MHz.
Definition CHGfx.h:245
@ GFX_DIV8
HCLK/8 = 6 MHz.
Definition CHGfx.h:245
@ GFX_DIV4
HCLK/4 = 12 MHz: within the ST7735S datasheet's limit.
Definition CHGfx.h:245
@ GFX_12BPP
ST7735 COLMOD 0x03, RGB444, 1.5 bytes/px -> 119 fps.
Definition CHGfx.h:233
@ GFX_18BPP
ST7735 COLMOD 0x06, RGB666, 3 bytes/px -> 61 fps.
Definition CHGfx.h:234
@ GFX_16BPP
ST7735 COLMOD 0x05, RGB565, 2 bytes/px -> 90 fps.
Definition CHGfx.h:232
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:474
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:326
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:1008
static int gfx_sprite4W(const uint8_t *spr)
A span sprite's width.
Definition CHGfx.h:1002
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:622
static uint8_t * gfx_px565(uint8_t *p, uint16_t rgb565)
Writes one 16 bpp pixel for a stream callback.
Definition CHGfx.h:650
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:662
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