Display — Utilities
M5GFX source:
lgfx/v1/LGFXBase.hpp
Back to: Display overview
Screen control, QR code generation, progress bar, state save/restore, and drawing sessions.
Screen Control
fillScreen()
void fillScreen(); // Fill with base color
void fillScreen(uint16_t color); // Fill with specified color
Fills the entire screen with a solid color.
M5Cardputer.Display.fillScreen(TFT_BLACK);
// Clear to default background
M5Cardputer.Display.setBaseColor(TFT_NAVY);
M5Cardputer.Display.fillScreen();
setBrightness() / getBrightness()
Controls the LCD backlight.
void setup() {
M5Cardputer.begin();
M5Cardputer.Display.setBrightness(128); // 50%
}
void onDimTimer() {
// Fade to 20% after idle
M5Cardputer.Display.setBrightness(51);
}
setRotation() / getRotation()
Rotates the screen coordinate system.
| Value | Orientation | Resolution |
|---|---|---|
| 0 | Portrait | 240 × 320 |
| 1 | Landscape | 320 × 240 |
| 2 | Portrait (inverted) | 240 × 320 |
| 3 | Landscape (inverted) | 320 × 240 |
M5Cardputer.Display.setRotation(1); // Default landscape
M5Cardputer.Display.printf("W=%d H=%d\n",
M5Cardputer.Display.width(),
M5Cardputer.Display.height()); // 320 x 240
width() / height()
Returns the current display dimensions, post-rotation.
int32_t centerX = M5Cardputer.Display.width() / 2;
int32_t centerY = M5Cardputer.Display.height() / 2;
M5Cardputer.Display.drawCircle(centerX, centerY, 30, TFT_RED);
sleep() / wakeup()
Power management for the LCD.
// Dim after inactivity
if (millis() - lastActivity > 30000) {
M5Cardputer.Display.sleep();
}
// Wake on button press
if (M5Cardputer.BtnA.wasPressed()) {
M5Cardputer.Display.wakeup();
lastActivity = millis();
}
invertDisplay()
Inverts all pixels on screen (useful for some panel configurations).
QR Code
qrcode()
void qrcode(const char* string, int32_t x = -1, int32_t y = -1,
int32_t width = -1, uint8_t version = 1);
Draws a QR code directly on the display.
| Parameter | Type | Default | Description |
|---|---|---|---|
string |
const char* |
— | Data to encode |
x, y |
int32_t |
-1 |
Position (-1 = centered) |
width |
int32_t |
-1 |
Size (-1 = maximum fit) |
version |
uint8_t |
1 |
QR version (1–40, determines data capacity) |
// Auto-centered
M5Cardputer.Display.qrcode("https://m5stack.com", -1, -1, -1);
// Custom position and size
M5Cardputer.Display.qrcode("hello123", 10, 10, 100);
Progress Bar
progressBar()
M5GFX-specific convenience widget. Draws a simple progress bar.
| Parameter | Type | Description |
|---|---|---|
x, y |
int |
Top-left |
w, h |
int |
Width and height |
val |
uint8_t |
Progress value 0–100 |
void loadingScreen() {
M5Cardputer.Display.fillScreen(TFT_BLACK);
M5Cardputer.Display.setTextColor(TFT_WHITE);
M5Cardputer.Display.drawString("Loading...", 10, 10);
for (int i = 0; i <= 100; i += 10) {
M5Cardputer.Display.progressBar(40, 80, 240, 20, i);
delay(200);
}
}
State Save / Restore
pushState() / popState()
LIFO stack for graphics state. Use when temporarily changing font/size/color inside a function.
void drawHeader(const char* title) {
M5Cardputer.Display.pushState();
M5Cardputer.Display.setTextSize(2);
M5Cardputer.Display.setTextColor(TFT_WHITE, TFT_NAVY);
M5Cardputer.Display.drawCenterString(title, 160, 15);
M5Cardputer.Display.popState(); // Original settings restored
}
void drawBodyLine(const char* text) {
M5Cardputer.Display.println(text); // Uses original settings
}
Write Sessions
startWrite() / endWrite()
Wraps multiple drawing operations in a single SPI transaction for better performance.