emulator-template: Neutralize demo slot, add 10s auto-exit safety hatch
Make the template foolproof to copy-and-fill for a new demo: - examples/Conway/ replaced by a neutral examples/Demo/Demo.cpp sample (bouncing box) documenting the full sketch contract (setup/loop, emuGfx::fb, emuGfx::dirty, emu::keyboard) - Makefile passes the sketch path as -DAPP_SRC; main.cpp now includes APP_SRC instead of hardcoding Conway.cpp, window title uses APP_NAME - Safety hatch: emulator auto-exits after 10 seconds by default so a runaway demo can't hang and eat resources; --user-run disables the cap, --timeout MS adjusts it (default 10000) - README rewritten as a step-by-step recipe: replace the Demo sketch, run 'make run'; documents all CLI flags, drops the unimplemented --dump-interval flag Verified: fresh copy + sketch swap builds and renders; hatch fires at 10s (and at --timeout 2000); --user-run runs until --frames cap.
This commit is contained in:
@@ -1,141 +0,0 @@
|
||||
/**
|
||||
* @file Conway.cpp
|
||||
* @brief Game of Life — minimal SDL3 emulator prototype
|
||||
*
|
||||
* A minimal Game of Life implementation using the T-Deck emulator canvas.
|
||||
* Press Space to toggle pause. No hardware dependencies.
|
||||
*
|
||||
* Build: make -C /var/home/dejvino/workspace/T-Deck/emulator-template run
|
||||
* or: make -C /var/home/dejvino/workspace/T-Deck/emulator APP=Conway run
|
||||
* Smoke test: make smoke -C /var/home/dejvino/workspace/T-Deck/emulator-template APP=Conway
|
||||
*/
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Minimal emulator shim — provides millis(), random(), randomSeed(), etc.
|
||||
// ---------------------------------------------------------------------------
|
||||
#include "shim.h"
|
||||
|
||||
// Declare emulated state provided by the emulator main.cpp
|
||||
extern std::deque<uint8_t> emu::keyboard;
|
||||
extern uint32_t emuGfx::fb[320 * 240];
|
||||
extern bool emuGfx::dirty;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Canvas configuration (320x240, same as T-Deck display)
|
||||
// ---------------------------------------------------------------------------
|
||||
static constexpr int W = 320;
|
||||
static constexpr int H = 240;
|
||||
static constexpr int SCALE = 8; // 40 × 30 tile grid
|
||||
using Grid = uint8_t[W / SCALE][H / SCALE];
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Game of Life state
|
||||
// ---------------------------------------------------------------------------
|
||||
static Grid grid{};
|
||||
static Grid next{};
|
||||
static bool paused = true;
|
||||
static int tick = 0;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Input tracking — read from the emu::keyboard queue (populated by the
|
||||
// emulator main.cpp from SDL key events).
|
||||
// ---------------------------------------------------------------------------
|
||||
static bool justPressedSpace = false;
|
||||
static bool justPressedP = false;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// setup() — run once at launch
|
||||
// ---------------------------------------------------------------------------
|
||||
void setup()
|
||||
{
|
||||
randomSeed(millis());
|
||||
|
||||
for (int y = 0; y < H / SCALE; y++) {
|
||||
for (int x = 0; x < W / SCALE; x++) {
|
||||
grid[x][y] = random(2);
|
||||
}
|
||||
}
|
||||
|
||||
tick = 0;
|
||||
paused = false;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// tickOnce() — advance the simulation by one logic tick
|
||||
// ---------------------------------------------------------------------------
|
||||
void tickOnce()
|
||||
{
|
||||
for (int y = 0; y < H / SCALE; y++) {
|
||||
for (int x = 0; x < W / SCALE; x++) {
|
||||
int neighbors = 0;
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
if (dx == 0 && dy == 0) continue;
|
||||
int nx = (x + dx + W / SCALE) % (W / SCALE);
|
||||
int ny = (y + dy + H / SCALE) % (H / SCALE);
|
||||
neighbors += grid[nx][ny];
|
||||
}
|
||||
}
|
||||
if (grid[x][y]) {
|
||||
next[x][y] = (neighbors == 2 || neighbors == 3) ? 1 : 0;
|
||||
} else {
|
||||
next[x][y] = (neighbors == 3) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < (W / SCALE) * (H / SCALE); i++) {
|
||||
int ix = i / (W / SCALE);
|
||||
int iy = i % (W / SCALE);
|
||||
grid[ix][iy] = next[ix][iy];
|
||||
}
|
||||
tick++;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// render() — draw the current grid to the emulator framebuffer
|
||||
// ---------------------------------------------------------------------------
|
||||
void render()
|
||||
{
|
||||
for (int y = 0; y < H / SCALE; y++) {
|
||||
for (int x = 0; x < W / SCALE; x++) {
|
||||
uint32_t c = grid[x][y] ? 0xFFFFFFFF : 0xFF000000;
|
||||
emuGfx::fb[y * W + x] = c;
|
||||
}
|
||||
}
|
||||
emuGfx::dirty = true;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Main loop — called once per frame by the emulator main.cpp
|
||||
// ---------------------------------------------------------------------------
|
||||
void loop()
|
||||
{
|
||||
while (!emu::keyboard.empty()) {
|
||||
char k = (char)emu::keyboard.front();
|
||||
emu::keyboard.pop_front();
|
||||
|
||||
if (k == ' ') {
|
||||
justPressedSpace = true;
|
||||
}
|
||||
if (k == 'p' || k == '\t') {
|
||||
justPressedP = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (justPressedSpace) {
|
||||
paused = !paused;
|
||||
justPressedSpace = false;
|
||||
}
|
||||
|
||||
if (justPressedP) {
|
||||
justPressedP = false;
|
||||
}
|
||||
|
||||
static uint32_t lastStep = 0;
|
||||
uint32_t now = millis();
|
||||
if (!paused && (now - lastStep > 150)) {
|
||||
tickOnce();
|
||||
render();
|
||||
lastStep = now;
|
||||
}
|
||||
}
|
||||
@@ -1,165 +0,0 @@
|
||||
/**
|
||||
* @file Conway.ino
|
||||
* @brief Game of Life — minimal SDL3 emulator prototype
|
||||
*
|
||||
* A minimal Game of Life implementation using the T-Deck emulator canvas.
|
||||
* Press Space to toggle pause. No hardware dependencies.
|
||||
*
|
||||
* Build: make -C /var/home/dejvino/workspace/T-Deck/emulator-template run
|
||||
* or: make -C /var/home/dejvino/workspace/T-Deck/emulator APP=Conway run
|
||||
* Smoke test: make smoke -C /var/home/dejvino/workspace/T-Deck/emulator-template APP=Conway
|
||||
*/
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Minimal emulator shim — provides millis(), random(), randomSeed(), etc.
|
||||
// Pull in the definitions that the emulator main.cpp provides.
|
||||
// ---------------------------------------------------------------------------
|
||||
#include "shim.h"
|
||||
|
||||
// Emulated state provided by the emulator main.cpp
|
||||
extern uint32_t emuGfx::fb[320 * 240];
|
||||
extern bool emuGfx::dirty;
|
||||
extern std::deque<uint8_t> emu::keyboard;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Canvas configuration (320x240, same as T-Deck display)
|
||||
// ---------------------------------------------------------------------------
|
||||
static constexpr int W = 320;
|
||||
static constexpr int H = 240;
|
||||
static constexpr int SCALE = 8; // 40 × 30 tile grid
|
||||
using Grid = uint8_t[W / SCALE][H / SCALE];
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Game of Life state
|
||||
// ---------------------------------------------------------------------------
|
||||
static Grid grid{};
|
||||
static Grid next{};
|
||||
static bool paused = true;
|
||||
static int tick = 0;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Input tracking — read from the emu::keyboard queue (populated by the
|
||||
// emulator main.cpp from SDL key events). Each keypress becomes an ASCII
|
||||
// byte queued at I2C address 0x55.
|
||||
// ---------------------------------------------------------------------------
|
||||
static bool justPressedSpace = false;
|
||||
static bool justPressedP = false;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// setup() — run once at launch
|
||||
// ---------------------------------------------------------------------------
|
||||
void setup()
|
||||
{
|
||||
// Fast random seed
|
||||
randomSeed(millis());
|
||||
|
||||
// Populate a random initial state
|
||||
for (int y = 0; y < H / SCALE; y++) {
|
||||
for (int x = 0; x < W / SCALE; x++) {
|
||||
grid[x][y] = random(2);
|
||||
}
|
||||
}
|
||||
|
||||
tick = 0;
|
||||
paused = true;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// tickOnce() — advance the simulation by one logic tick
|
||||
// ---------------------------------------------------------------------------
|
||||
void tickOnce()
|
||||
{
|
||||
for (int y = 0; y < H / SCALE; y++) {
|
||||
for (int x = 0; x < W / SCALE; x++) {
|
||||
int neighbors = 0;
|
||||
// wrap-around torus (simplest: no edges)
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
if (dx == 0 && dy == 0) continue;
|
||||
int nx = (x + dx + W / SCALE) % (W / SCALE);
|
||||
int ny = (y + dy + H / SCALE) % (H / SCALE);
|
||||
neighbors += grid[nx][ny];
|
||||
}
|
||||
}
|
||||
// Conway's rules
|
||||
if (grid[x][y]) {
|
||||
next[x][y] = (neighbors == 2 || neighbors == 3) ? 1 : 0;
|
||||
} else {
|
||||
next[x][y] = (neighbors == 3) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Swap buffers
|
||||
for (int i = 0; i < (W / SCALE) * (H / SCALE); i++) {
|
||||
int ix = i / (W / SCALE);
|
||||
int iy = i % (W / SCALE);
|
||||
grid[ix][iy] = next[ix][iy];
|
||||
}
|
||||
tick++;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// render() — draw the current grid to the emulator framebuffer
|
||||
// The emulator main.cpp maps emuGfx::fb[y * W + x] to an SDL texture
|
||||
// each frame. ABGR8888 pixel format.
|
||||
// ---------------------------------------------------------------------------
|
||||
void render()
|
||||
{
|
||||
for (int y = 0; y < H / SCALE; y++) {
|
||||
for (int x = 0; x < W / SCALE; x++) {
|
||||
// White living cell / black dead cell
|
||||
uint32_t c = grid[x][y] ? 0xFFFFFFFF : 0xFF000000;
|
||||
emuGfx::fb[y * W + x] = c;
|
||||
}
|
||||
}
|
||||
emuGfx::dirty = true;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Main loop — called once per frame by the emulator main.cpp.
|
||||
// Handles input and advances simulation when unpaused.
|
||||
// ---------------------------------------------------------------------------
|
||||
void loop()
|
||||
{
|
||||
// Poll keyboard queue (filled by emulator main.cpp from SDL events)
|
||||
while (!emu::keyboard.empty()) {
|
||||
char k = (char)emu::keyboard.front();
|
||||
emu::keyboard.pop_front();
|
||||
|
||||
if (k == ' ') {
|
||||
justPressedSpace = true;
|
||||
}
|
||||
if (k == 'p' || k == '\t') {
|
||||
justPressedP = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Toggle pause with Space
|
||||
if (justPressedSpace) {
|
||||
paused = !paused;
|
||||
justPressedSpace = false;
|
||||
}
|
||||
|
||||
// Switch view mode with P / Tab (in this simple template both modes
|
||||
// render the same grid; the flag is available for future expansion)
|
||||
if (justPressedP) {
|
||||
justPressedP = false;
|
||||
}
|
||||
|
||||
// Simulation step (paced ~6.6 Hz when unpaused)
|
||||
static uint32_t lastStep = 0;
|
||||
uint32_t now = millis();
|
||||
if (!paused && (now - lastStep > 150)) {
|
||||
tickOnce();
|
||||
render();
|
||||
lastStep = now;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// The emulator main.cpp provides the full SDL3 loop, event pump, and
|
||||
// frame presentation. This sketch only supplies setup(), loop(), tickOnce(),
|
||||
// render(), and the framebuffer writes above. No hardware dependencies,
|
||||
// no TFT_eSPI, no SPI, no I2S — pure emulator-side C++ for rapid
|
||||
// graphics/engine prototyping.
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Demo sketch — replace this file with your own experiment.
|
||||
*
|
||||
* Contract with the emulator host (src/main.cpp):
|
||||
* - implement setup() — called once at launch
|
||||
* - implement loop() — called once per frame (~60 fps)
|
||||
* - write pixels into emuGfx::fb[] (320x240, ABGR8888 uint32)
|
||||
* - set emuGfx::dirty = true each frame you want presented
|
||||
* - read keys from emu::keyboard (ASCII queue, filled from SDL events)
|
||||
*
|
||||
* This sample just bounces a colored box around — enough to prove the
|
||||
* pipeline works. Delete everything and start from the structure below.
|
||||
*/
|
||||
|
||||
#include "shim.h"
|
||||
|
||||
// Emulator state provided by the host (src/main.cpp)
|
||||
extern uint32_t emuGfx::fb[320 * 240];
|
||||
extern bool emuGfx::dirty;
|
||||
extern std::deque<uint8_t> emu::keyboard;
|
||||
|
||||
static constexpr int W = 320;
|
||||
static constexpr int H = 240;
|
||||
|
||||
// --- Demo state -----------------------------------------------------------
|
||||
static float boxX = 40, boxY = 30;
|
||||
static float velX = 1.7f, velY = 1.1f;
|
||||
static constexpr float BOX = 24.0f;
|
||||
|
||||
void setup()
|
||||
{
|
||||
randomSeed(millis());
|
||||
velX = 1.0f + random(100) / 50.0f;
|
||||
velY = 1.0f + random(100) / 50.0f;
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Move and bounce
|
||||
boxX += velX;
|
||||
boxY += velY;
|
||||
if (boxX < 0 || boxX + BOX > W) { velX = -velX; boxX += velX; }
|
||||
if (boxY < 0 || boxY + BOX > H) { velY = -velY; boxY += velY; }
|
||||
|
||||
// Clear the framebuffer (opaque black, alpha = 0xFF)
|
||||
for (int i = 0; i < W * H; i++) emuGfx::fb[i] = 0xFF000000;
|
||||
|
||||
// Draw a filled box with a color that drifts over time
|
||||
uint8_t r = (uint8_t)(128 + 127 * sinf(millis() / 900.0f));
|
||||
uint32_t color = 0xFF000000u | ((uint32_t)r << 16) | 0x0000CC66u;
|
||||
for (int y = (int)boxY; y < (int)(boxY + BOX); y++) {
|
||||
if (y < 0 || y >= H) continue;
|
||||
for (int x = (int)boxX; x < (int)(boxX + BOX); x++) {
|
||||
if (x < 0 || x >= W) continue;
|
||||
emuGfx::fb[y * W + x] = color;
|
||||
}
|
||||
}
|
||||
|
||||
// Present the frame
|
||||
emuGfx::dirty = true;
|
||||
}
|
||||
Reference in New Issue
Block a user