CHGame 0.3.0
Libraries for the CHGame handheld
Loading...
Searching...
No Matches

Starting the panel and changing its SPI clock, colour mode, orientation and scan rate. More...

Functions

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, then a default 16-colour palette.
 
void gfx_setSpiDiv (uint8_t div)
 Changes the SPI clock divider, and sets SPI1 up afresh for the panel.
 
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.
 
uint32_t gfx_spiHz (void)
 The current SPI clock.
 
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_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).
 

Detailed Description

Starting the panel and changing its SPI clock, colour mode, orientation and scan rate.

Function Documentation

◆ gfx_begin()

static void gfx_begin ( uint8_t  spiDiv = GFX_DIV2,
uint8_t  colorMode = GFX_16BPP 
)
inlinestatic

Starts the panel: pins, SPI1, DMA, a hardware reset and the ST7735 initialisation, then a default 16-colour palette.

Call it once in setup(). It takes about 0.7 s (the panel's reset and wake-up delays). It parks the SD card's chip select high (unless CHGFX_NO_SD_PARK), selects the colour mode and, in 12 bpp, programs the panel's colour LUT (gfx_writeColorLut()). The framebuffer is not touched and nothing is sent to the glass until the first flush.

gfx_begin() picks one of two starts. A sketch that names GFX_12BPP and never calls gfx_setColorMode() gets the 12 bpp-only start, and the 16 and 18 bpp converters and tables stay out of its image (0.2-0.4 KB of flash, 176 B of it SRAM code). Any other mode, or one only known when it runs, gets the general start, which keeps all three.

Parameters
spiDivSPI clock divider: GFX_DIV2 (24 MHz, the default), GFX_DIV4, GFX_DIV8 or GFX_DIV16.
colorModeGFX_16BPP (the default), GFX_12BPP or GFX_18BPP.
gfx_begin(GFX_DIV2, GFX_12BPP); // what the CHGame games use
@ GFX_DIV2
HCLK/2 = 24 MHz: the fastest the chip can clock SPI, and the default.
Definition CHGfx.h:232
@ GFX_12BPP
ST7735 COLMOD 0x03, RGB444, 1.5 bytes/px -> 119 fps.
Definition CHGfx.h:220
static void gfx_begin(uint8_t spiDiv=GFX_DIV2, uint8_t colorMode=GFX_16BPP)
Starts the panel: pins, SPI1, DMA, a hardware reset and the ST7735 initialisation,...
Definition CHGfx.h:308
See also
CHGfx::begin()

◆ gfx_setSpiDiv()

void gfx_setSpiDiv ( uint8_t  div)

Changes the SPI clock divider, and sets SPI1 up afresh for the panel.

Waits for any flush in flight first, then programs all of SPI1 as gfx_begin() does (its clock on, master, mode 0, MSB first, this divider). That makes it the way to hand SPI1 back to CHGfx after anything else used it, such as the Arduino SPI class:

// ... talk to your own device ...
gfx_setSpiDiv(gfx_spiDiv()); // the panel's set-up back before the next flush
void endTransaction(uint8_t pin)
Forgets the pin's settings: transfers for it do nothing until the next begin() or beginTransaction() ...
virtual void beginTransaction(uint8_t pin, SPISettings settings)
Stores a device's settings for a CS pin and programs SPI1 with them.
A device's SPI settings: its maximum clock, bit order and mode, for SPIClass::beginTransaction().
Definition SPI.h:156
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_setSpiDiv(uint8_t div)
Changes the SPI clock divider, and sets SPI1 up afresh for the panel.
uint8_t gfx_spiDiv(void)
The current SPI clock divider.
SPIClass SPI
The SPI: SPI1 on PA5 (SCK), PA6 (MISO) and PA7 (MOSI), the bus the board's panel and SD card are on.
#define SPI_MODE3
clock idles high, data sampled on the rising edge (CPOL 1, CPHA 1)
Definition SPI.h:105
Parameters
divGFX_DIV2, GFX_DIV4, GFX_DIV8 or GFX_DIV16 (32 also works); any other value runs at HCLK/2.

◆ gfx_setColorMode()

void gfx_setColorMode ( uint8_t  mode)

Switches the panel's colour output mode.

Waits for any flush in flight, sends the panel its new COLMOD (and the colour LUT when entering 12 bpp), and rebuilds the palette table for the next flush. Does nothing if the mode is already set.

Parameters
modeGFX_16BPP, GFX_12BPP or GFX_18BPP.
Note
Calling it anywhere in a sketch links the 16 and 18 bpp converters, even if gfx_begin() was given GFX_12BPP.

◆ gfx_colorMode()

uint8_t gfx_colorMode ( void  )

The current colour output mode.

Returns
GFX_16BPP, GFX_12BPP or GFX_18BPP.

◆ gfx_spiDiv()

uint8_t gfx_spiDiv ( void  )

The current SPI clock divider.

Returns
The divider last given to gfx_begin() or gfx_setSpiDiv().

◆ gfx_spiHz()

uint32_t gfx_spiHz ( void  )

The current SPI clock.

Returns
The SPI clock in Hz: F_CPU divided by gfx_spiDiv(), 24000000 at GFX_DIV2.

◆ gfx_setPanelOffsets()

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.

MADCTL / window offsets differ between 1.44" panel batches. Defaults match Adafruit's INITR_144GREENTAB (madctl 0xC8, colstart 2, rowstart 3), which is what an Adafruit_ST7735 setup is already using. Change them if the image is offset or mirrored. Sends MADCTL at once, so call it after gfx_wait().

Parameters
madctlST7735 MADCTL (36h) value: rotation, mirroring, RGB/BGR.
colStartFirst visible column of the panel's memory.
rowStartFirst visible row of the panel's memory.

◆ gfx_setInverted()

void gfx_setInverted ( bool  on)

Turns the panel's colour inversion (INVON / INVOFF) on or off.

Sends the command at once, so call it after gfx_wait().

Parameters
ontrue to invert every colour, false for normal.

◆ gfx_setPanelFrameRate()

void gfx_setPanelFrameRate ( uint8_t  rtna,
uint8_t  fpa,
uint8_t  bpa 
)

Sets the panel's own refresh rate (ST7735 FRMCTR1, 0xB1).

Lower values scan the glass faster, which cuts the latency between a GRAM write and the pixel actually changing. The panel's rate is 850 kHz / ((RTNA*2 + 40) * (lines + FPA + BPA)). Defaults to the fast setting 0x05, 0x3A, 0x3A (Adafruit's is a lazy ~60 Hz 0x01, 0x2C, 0x2D). Waits for any flush in flight first.

Parameters
rtnaRTNA, the line period.
fpaFront porch, in lines.
bpaBack porch, in lines.