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).
This commit is contained in:
Dejvino
2026-11-16 22:51:44 +01:00
parent 23b3c88caa
commit c58f31d02c
8 changed files with 440 additions and 96 deletions
+117
View File
@@ -0,0 +1,117 @@
/**
* 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;
}
}
+37
View File
@@ -0,0 +1,37 @@
; PlatformIO project for running the template demo on the real T-Deck
; (ESP32-S3 + ST7789) as a free-running benchmark.
;
; Standalone on purpose (the repo's top-level platformio.ini keeps
; src_dir = src/netdeck for NetDeck): this points back at the shared
; boards/ and lib/ directories.
;
; pio run -d emulator-template/fw -t upload # flash the T-Deck
; pio device monitor # fps report every 2 s
;
; The loop free-runs (no pacing); the serial report's render/push split
; is the true throughput of the sketch core + SPI push on the hardware.
[platformio]
default_envs = DemoHW
src_dir = .
boards_dir = ../../boards
lib_dir = ../../lib
[env:DemoHW]
platform = espressif32@6.3.0
board = T-Deck
framework = arduino
upload_speed = 921600
monitor_speed = 115200
build_flags =
-DBOARD_HAS_PSRAM=1
-DCORE_DEBUG_LEVEL=1
-DARDUINO_USB_CDC_ON_BOOT=1
-DDISABLE_ALL_LIBRARY_WARNINGS
-DSPI_FREQUENCY=80000000
; -O2: PlatformIO's default -Os optimizes for size and can cost a
; large fraction of frame time — benchmarks deserve the honest number.
-O2
-I../examples/Demo
; T-Deck pin map (BOARD_POWERON, TFT pins...) from the firmware tree
-I../../src/netdeck