// GraphicsDemo_P4.ino
// Graphics showcase for the ESP32-P4 Function EV Board V1.4 (7" 1024x600 MIPI DSI).
// Needs the "Adafruit GFX Library" (shapes and text). Scenes loop in this order:
// shapes -> plasma -> bouncing balls -> code scroll -> credit.
#include <Arduino.h>
#include <esp32-hal-psram.h>
#include <Adafruit_GFX.h>
#include <math.h>
#include <string.h>
#include "esp_lcd_panel_ops.h"
#define LCD_W 1024
#define LCD_H 600
#define PIN_LCD_BL 26
// Set by the board's display (BSP) init. Nothing is sent to the panel while this is nullptr.
esp_lcd_panel_handle_t panel_handle = nullptr;
// ---------- Colors ----------
constexpr uint16_t rgb565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
constexpr uint16_t C_NAVY = rgb565(12, 16, 30);
constexpr uint16_t C_PANEL = rgb565(34, 42, 68);
constexpr uint16_t C_GOLD = rgb565(212, 175, 55);
constexpr uint16_t C_WHITE = rgb565(240, 240, 245);
constexpr uint16_t C_RED = rgb565(255, 80, 60);
constexpr uint16_t C_TEAL = rgb565(60, 180, 140);
constexpr uint16_t C_DIM = rgb565(120, 128, 150);
// ---------- Canvas in PSRAM ----------
// Adafruit_GFX draws shapes and text into this buffer; flushFrame() sends it to the panel.
class PsramCanvas : public Adafruit_GFX {
public:
PsramCanvas(int16_t w, int16_t h) : Adafruit_GFX(w, h), buf(nullptr) {}
bool init() {
buf = (uint16_t *)ps_malloc((size_t)width() * height() * sizeof(uint16_t));
return buf != nullptr;
}
void drawPixel(int16_t x, int16_t y, uint16_t color) override {
if (!buf || x < 0 || y < 0 || x >= width() || y >= height()) return;
buf[(size_t)y * width() + x] = color;
}
void fillScreen(uint16_t color) {
if (!buf) return;
size_t n = (size_t)width() * height();
for (size_t i = 0; i < n; i++) buf[i] = color;
}
uint16_t *buffer() { return buf; }
bool ready() const { return buf != nullptr; }
private:
uint16_t *buf;
};
PsramCanvas canvas(LCD_W, LCD_H);
// ---------- Shared state ----------
static uint8_t sinTab[256]; // sine table for the plasma scene
struct Ball { float x, y, vx, vy; int r; uint16_t c; };
static Ball balls[8] = {
{120.0f, 120.0f, 4.0f, 3.0f, 36, C_RED},
{300.0f, 400.0f, -3.5f, 4.5f, 28, C_GOLD},
{520.0f, 200.0f, 5.0f, -2.5f, 44, C_TEAL},
{760.0f, 330.0f, -4.5f, -3.5f, 32, C_WHITE},
{200.0f, 500.0f, 2.5f, -4.0f, 24, C_RED},
{640.0f, 500.0f, -2.0f, 5.5f, 30, C_GOLD},
{900.0f, 80.0f, -5.0f, 2.0f, 26, C_TEAL},
{420.0f, 300.0f, 3.0f, 3.5f, 22, C_WHITE},
};
// Lines shown by the code-scroll scene
static const char *CODE_LINES[] = {
"// GraphicsDemo_P4.ino",
"canvas.fillRoundRect(60, 60, 280, 200, 24, C_PANEL);",
"canvas.drawRoundRect(60, 60, 280, 200, 24, C_GOLD);",
"canvas.fillCircle(560, 160, 90, C_RED);",
"canvas.fillTriangle(700, 300, 900, 300, 800, 480, C_TEAL);",
"canvas.setTextSize(3);",
"// plasma: three sine waves summed per pixel",
"uint8_t v = (sinTab[(x + p) & 255] + sinTab[(y + p) & 255] + sinTab[((x + y) / 2 + p) & 255]) / 3;",
"buf[y * LCD_W + x] = rgb565(v, 255 - v, (v * 3) & 255);",
"// balls: move, bounce off the edges, draw",
"b.x += b.vx; if (b.x - b.r < 0 || b.x + b.r > LCD_W) b.vx = -b.vx;",
"canvas.fillCircle((int)b.x, (int)b.y, b.r, b.c);",
"// code scroll: rows move up at a fixed speed",
"int y = LCD_H - scrollY + i * LINE_H;",
"canvas.print(CODE_LINES[i]);",
"esp_lcd_panel_draw_bitmap(panel_handle, 0, 0, LCD_W, LCD_H, canvas.buffer());",
"// credit",
"Created by Arvind Patil - AI Centre Nandurbar",
};
static const int CODE_COUNT = sizeof(CODE_LINES) / sizeof(CODE_LINES[0]);
// ---------- Helpers ----------
void centerText(uint8_t size, int y, const char *s, uint16_t color) {
int w = strlen(s) * 6 * size; // 5x7 glyph + 1px spacing, scaled
canvas.setTextSize(size);
canvas.setTextColor(color);
canvas.setCursor((LCD_W - w) / 2, y);
canvas.print(s);
}
// ---------- Scene 1: shapes, gradient and text ----------
void sceneShapes() {
for (int y = 0; y < LCD_H; y++) {
uint8_t g = 16 + (y * 60) / LCD_H;
canvas.drawFastHLine(0, y, LCD_W, rgb565(12, g, 30 + g / 2));
}
canvas.fillRoundRect(60, 60, 280, 200, 24, C_PANEL);
canvas.drawRoundRect(60, 60, 280, 200, 24, C_GOLD);
canvas.fillCircle(560, 160, 90, C_RED);
canvas.fillTriangle(700, 300, 900, 300, 800, 480, C_TEAL);
canvas.drawLine(60, 420, 960, 420, C_GOLD);
canvas.setTextSize(3);
canvas.setTextColor(C_WHITE);
canvas.setCursor(60, 450);
canvas.print("ESP32-P4 graphics");
canvas.setTextSize(2);
canvas.setTextColor(C_DIM);
canvas.setCursor(60, 500);
canvas.print("shapes, gradients, text");
}
// ---------- Scene 2: plasma (per-pixel sine waves) ----------
void scenePlasma(unsigned long frame) {
uint16_t *buf = canvas.buffer();
uint8_t p = (uint8_t)(frame * 4);
for (int y = 0; y < LCD_H; y++) {
for (int x = 0; x < LCD_W; x++) {
uint8_t v = (sinTab[(uint8_t)(x + p)] +
sinTab[(uint8_t)(y + p)] +
sinTab[(uint8_t)((x + y) / 2 + p)]) / 3;
buf[y * LCD_W + x] = rgb565(v, 255 - v, (uint8_t)(v * 3));
}
}
}
// ---------- Scene 3: bouncing balls ----------
void sceneBalls() {
canvas.fillScreen(C_NAVY);
for (auto &b : balls) {
b.x += b.vx;
b.y += b.vy;
if (b.x - b.r < 0) { b.x = b.r; b.vx = -b.vx; }
if (b.x + b.r > LCD_W) { b.x = LCD_W - b.r; b.vx = -b.vx; }
if (b.y - b.r < 0) { b.y = b.r; b.vy = -b.vy; }
if (b.y + b.r > LCD_H) { b.y = LCD_H - b.r; b.vy = -b.vy; }
canvas.fillCircle((int)b.x, (int)b.y, b.r, b.c);
}
canvas.setTextSize(2);
canvas.setTextColor(C_DIM);
canvas.setCursor(24, 20);
canvas.print("bouncing balls");
}
// ---------- Scene 4: code scroll (rows move up at 60 px/s) ----------
void sceneCode(unsigned long msIntoScene) {
const int LINE_H = 20;
canvas.fillScreen(C_NAVY);
canvas.setTextSize(2);
int scrollY = (int)(msIntoScene * 60UL / 1000UL);
for (int i = 0; i < CODE_COUNT; i++) {
int y = LCD_H - scrollY + i * LINE_H;
if (y < -LINE_H || y > LCD_H) continue;
bool comment = (CODE_LINES[i][0] == '/' && CODE_LINES[i][1] == '/');
canvas.setTextColor(comment ? C_GOLD : C_WHITE);
canvas.setCursor(40, y);
canvas.print(CODE_LINES[i]);
}
}
// ---------- Scene 5: credit ----------
//void sceneCredit() {
void sceneCredit() {
canvas.fillScreen(C_NAVY);
centerText(6, 170, "Arvind Sir", C_GOLD);
canvas.drawFastHLine(312, 330, 400, C_GOLD);
centerText(3, 360, "ESP32-P4 Graphics Demo", C_WHITE);
}
// Arduino entry points. The board's LCD must be initialized separately by its BSP.
void setup() {
Serial.begin(115200);
delay(200);
if (!psramFound()) {
Serial.println("PSRAM not found. Enable PSRAM in the board settings.");
while (true) delay(1000);
}
if (!canvas.init()) {
Serial.println("Could not allocate the RGB565 canvas in PSRAM.");
while (true) delay(1000);
}
// Fill the lookup table used by scenePlasma().
for (int i = 0; i < 256; i++) {
sinTab[i] = (uint8_t)(127.5f + 127.5f * sinf(i * 2.0f * PI / 256.0f));
}
Serial.println("Graphics demo ready.");
}
void loop() {
static unsigned long sceneStart = 0;
static unsigned long lastFrame = 0;
static int scene = 0;
const unsigned long now = millis();
if (sceneStart == 0) sceneStart = now;
// Each scene runs for eight seconds.
if (now - sceneStart >= 8000UL) {
scene = (scene + 1) % 5;
sceneStart = now;
}
// Aim for about 30 frames per second.
if (now - lastFrame < 33UL) return;
lastFrame = now;
const unsigned long elapsed = now - sceneStart;
switch (scene) {
case 0: sceneShapes(); break;
case 1: scenePlasma(elapsed / 33UL); break;
case 2: sceneBalls(); break;
case 3: sceneCode(elapsed); break;
case 4: sceneCredit(); break;
}
// panel_handle remains nullptr until initialized by a compatible LCD BSP.
if (panel_handle != nullptr) {
esp_lcd_panel_draw_bitmap(
panel_handle, 0, 0, LCD_W, LCD_H, canvas.buffer()
);
}
}
Loading
esp32-p4-function-ev
esp32-p4-function-ev