Files
lilygo-t-deck/emulator-template/fw/DemoHW.ino
T
Dejvino c58f31d02c emulator-template: RGB565 wire-order contract + ready-to-build HW twin
Propagate the lessons from tron-room-demo's hardware bring-up back into
the canonical template, so demos are hardware-testable from day one:

- Contract change: the sketch renders into emuGfx::fb565 (320x240
  uint16_t, RGB565 in PANEL WIRE ORDER - big-endian, the byte sequence
  the ST7789 receives over SPI, the same convention TFT_eSprite's
  primitives use). The host expands 565 to 8888 for SDL only. Native
  little-endian writes look right in the emulator but show byte-swapped
  on the panel (seen on hardware: BG 0x0002 -> green, fogged floor
  0x0882 -> pink) - the new contract makes that bug impossible.
- Recommended sketch structure: examples/APP/Core.h holds all
  rendering/logic device-agnostically (demoInit/demoInput/demoFrame);
  the .cpp is a thin emulator shell. Bundled sample demonstrates it.
- New fw/ scaffold: standalone PlatformIO project (shared boards/lib,
  T-Deck pin map) with DemoHW.ino driving the same core through a
  TFT_eSprite frameBuffer() and reporting true fps + render/push split
  over USB serial every 2 s. -O2 explicitly (pio default -Os skews
  benchmarks).
- shim.h gains rgb565()/wire565() helpers; Makefile deps now include
  the sketch's headers; --dump now also fires on safety-hatch exit (a
  long --frames run was silently losing its dump before).

Verified: template builds + smoke renders correct 565 colors; DemoHW
builds clean for ESP32-S3 (328 KB flash).
2026-11-16 22:51:44 +01:00

118 lines
3.8 KiB
Arduino

/**
* DemoHW — the template's bundled demo on the real T-Deck
* (ESP32-S3 + ST7789), with frame-time instrumentation.
*
* Firmware twin of the emulator sketch (../examples/Demo/Demo.cpp).
* Both drive the same device-agnostic DemoCore.h, which renders the
* whole scene in RGB565 (panel wire order) into a single 320x240
* buffer. Here that buffer IS a TFT_eSprite's frameBuffer() — the core
* writes straight into the sprite, and the sprite is pushed with the
* proven NetDeck M1 anti-flicker pattern.
*
* Build & flash (from the repo root):
* pio run -d emulator-template/fw -t upload
* pio device monitor # 115200 baud
*
* The loop is deliberately free-running: the serial report every 2 s is
* the true throughput of the core + SPI push on this hardware.
*/
#include <Arduino.h>
#include <SPI.h>
#include <TFT_eSPI.h>
#include "utilities.h"
#include "DemoCore.h"
TFT_eSPI tft;
static TFT_eSprite fb(&tft); // full-frame 320x240x16 buffer (PSRAM)
// --- frame-time accounting ---------------------------------------------------
static uint32_t accRenderUs = 0;
static uint32_t accPushUs = 0;
static uint32_t accFrames = 0;
static uint32_t reportT0 = 0;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println("DemoHW — emulator-template demo on real T-Deck (RGB565)");
Serial.println("Free-running benchmark: report every 2 s over USB serial.");
// Proven T-Deck bring-up (same sequence as NetDeck.ino hardwareInit)
pinMode(BOARD_POWERON, OUTPUT);
digitalWrite(BOARD_POWERON, HIGH);
pinMode(BOARD_SDCARD_CS, OUTPUT);
pinMode(RADIO_CS_PIN, OUTPUT);
pinMode(BOARD_TFT_CS, OUTPUT);
digitalWrite(BOARD_SDCARD_CS, HIGH);
digitalWrite(RADIO_CS_PIN, HIGH);
digitalWrite(BOARD_TFT_CS, HIGH);
pinMode(BOARD_SPI_MISO, INPUT_PULLUP);
SPI.begin(BOARD_SPI_SCK, BOARD_SPI_MISO, BOARD_SPI_MOSI);
tft.begin();
tft.setRotation(1); // 320x240 landscape
tft.fillScreen(TFT_BLACK);
pinMode(BOARD_BL_PIN, OUTPUT);
digitalWrite(BOARD_BL_PIN, HIGH);
// Full-frame 16-bit sprite; the core draws directly into its buffer.
fb.setColorDepth(16);
if (!fb.createSprite(320, 240)) {
Serial.println("DemoHW: framebuffer alloc failed (stopping)");
while (true) delay(1000);
}
uint16_t *buf = (uint16_t *)fb.frameBuffer(0);
if (!buf) {
Serial.println("DemoHW: frameBuffer() unavailable (stopping)");
while (true) delay(1000);
}
demo::demoInit(buf);
reportT0 = millis();
}
void loop()
{
static uint32_t lastUs = 0;
uint32_t t0 = micros();
// Delta time from real elapsed time — motion speed stays correct no
// matter the achieved fps.
float dt = lastUs ? (t0 - lastUs) / 1e6f : 1.0f / 60.0f;
lastUs = t0;
if (dt > 0.1f) dt = 0.1f;
demo::demoFrame(dt, millis());
uint32_t renderUs = micros() - t0;
// Panel push: the M1 anti-flicker pattern (NetDeck.ino present()).
uint32_t p0 = micros();
tft.startWrite();
fb.pushSprite(0, 0);
tft.endWrite();
uint32_t pushUs = micros() - p0;
// --- accounting ---
accRenderUs += renderUs;
accPushUs += pushUs;
accFrames++;
uint32_t elapsed = millis() - reportT0;
if (elapsed >= 2000) {
float fps = accFrames * 1000.0f / elapsed;
float avgRender = accFrames ? accRenderUs / (float)accFrames / 1000.0f : 0.0f;
float avgPush = accFrames ? accPushUs / (float)accFrames / 1000.0f : 0.0f;
Serial.printf("DemoHW: %lu frames in %lu ms = %.1f fps | "
"render %.1f ms, push %.1f ms\n",
(unsigned long)accFrames, (unsigned long)elapsed, fps,
avgRender, avgPush);
reportT0 = millis();
accRenderUs = accPushUs = accFrames = 0;
}
}