/** * @file NetDeck.ino * @brief NetDeck — Breach & Board: Milestone 1 hardware sandbox. * * Milestone 1 (DESIGN.md §7): the same M0 sandbox sketch prepared for the * T-Deck hardware. Rendering goes into a full-frame RAM sprite and is pushed * to the ST7789 as one continuous SPI stream (anti-flicker, see present()); * the emulator draws straight into the SDL shim framebuffer instead. * * - shared tactical view (320x192) + HUD bar, ONE coordinate space: * bulkhead doors and their network door nodes occupy the same spot * - two view modes over that space (`P` to switch, `Tab` as legacy alias): * C) Cameras — full top-down ship view, network overlaid as outlines * N) Network — ship dimmed underneath, full network graph on top * - fixed-timestep engine loop (20 Hz sim decoupled from ~30 fps render) * - trackball cursor with edge-ISR counting + momentum * - keyboard device @ I2C 0x55: Space = tactical freeze, P = view switch * - demo floorplan tile map + demo sub-net graph with packet flow * * Run on the desktop: make APP=NetDeck run (from emulator/) * Headless smoke test: make smoke APP=NetDeck * * Controls (emulator / device): * Trackball move cursor (snaps to tiles in Cameras, nodes in Network) * Enter / click trackball press / screen tap = primary action * Space toggle tactical freeze (time-stop + CRT scanlines) * P (or Tab) switch view: Cameras <-> Network * 1-4 arm slotted exploit daemon (groundwork, flashes slot) * X / R / Q / E sever link / retreat / cycle (groundwork, logged) */ #include #include #include #include #include "utilities.h" #include "TouchDrvGT911.hpp" #include "../../src/netdeck/nd_layout.h" // --------------------------------------------------------------------------- // Hardware // --------------------------------------------------------------------------- TFT_eSPI tft; TouchDrvGT911 touch; static bool touchAvail = false; // --------------------------------------------------------------------------- // Full-frame rendering — draw into a RAM buffer, push to the panel once per // frame (the MiataDrive anti-flicker pattern: thousands of small draw calls // racing the panel's scan-out cause tearing; one continuous SPI stream writes // each pixel exactly once). Emulator: the SDL shim framebuffer IS the second // buffer, so we draw straight into it. // --------------------------------------------------------------------------- #ifndef EMULATOR static TFT_eSprite fb(&tft); // full-frame 320x240x16 buffer (PSRAM) static TFT_eSprite *const GFXP = &fb; #else static TFT_eSPI *const GFXP = &tft; #endif #ifndef EMULATOR // Push the finished frame to the ST7789 as one continuous SPI stream. static void present() { if (!fb.created()) return; tft.startWrite(); fb.pushSprite(0, 0); tft.endWrite(); } #else static void present() {} // shim blits the SDL framebuffer #endif // --------------------------------------------------------------------------- // Game state // --------------------------------------------------------------------------- enum GameState { ST_BOOT, ST_RUN }; static GameState gameState = ST_BOOT; enum ViewMode { VM_CAMERAS, VM_NETWORK }; static ViewMode viewMode = VM_CAMERAS; static uint32_t stateT0 = 0; static bool paused = false; static uint32_t simTick = 0; // 20 Hz deterministic ticks static uint32_t simAcc = 0; // fixed-timestep accumulator static uint32_t lastFrameMs = 0; // --------------------------------------------------------------------------- // Demo deck floorplan (40x24 tile grid; DESIGN.md §3 shared tactical view) // '#' wall '.' floor 'o' cover crate '+' bulkhead door 'A' airlock // Rooms: left hull (w/ Security), top-mid lab, bottom-mid hold (Power), // right server vault (Droid Hive). Bulkhead doors are network door nodes. // --------------------------------------------------------------------------- static const char *const DEMO_MAP[nd::TILE_ROWS] = { "########################################", "#...........#.............#............#", "#..o........#.............#...o........#", "#...........#....o........#............#", "#..........+.............#............#", "#...........#.........o...#............#", "#.......o...#.............#.........o..#", "#...........#.............#............#", "#...........######+#######............#", "#...........#.............#............#", "#.o.........#.............#............#", "#...........#.............+............#", "#...........#..o..........#............#", "#...........#.............#............#", "#........o..#.............#.o..........#", "#...........#.............#............#", "#...........#........o....#............#", "#...........#.............#............#", "#..........+.............#......o....#", "#...o......#.............#............#", "#...........#...........o#............#", "#.A.........#.............#....o......#", "#...........#.............#............#", "########################################", }; static const int AIRLOCK_TX = 2, AIRLOCK_TY = 21; // breach entry tile // --------------------------------------------------------------------------- // Demo sub-net graph — same coordinate space as the floorplan // (tile coords; door nodes sit exactly on their bulkhead door tiles) // --------------------------------------------------------------------------- struct NetNode { const char *label; int tx, ty; // tile coords (px center = tile*8+4) uint16_t color; uint8_t type; // 0 junction, 1 ICE, 2 hive, 3 door controller }; enum { NN_AIRLK, NN_SEC, NN_D1, NN_D2, NN_D3, NN_D4, NN_PWR, NN_HIVE }; static const NetNode NODES[8] = { { "AIRLK", 2, 21, nd::C_AIRLOCK, 0 }, { "SEC", 6, 6, nd::C_ICE, 1 }, { "DOOR1", 12, 4, nd::C_DOOR, 3 }, { "DOOR2", 12, 18, nd::C_DOOR, 3 }, { "DOOR3", 19, 8, nd::C_DOOR, 3 }, { "DOOR4", 26, 11, nd::C_DOOR, 3 }, { "PWR", 19, 14, nd::C_NODE, 0 }, { "HIVE", 33, 5, nd::C_HIVE, 2 }, }; static const uint8_t LINKS[7][2] = { { NN_AIRLK, NN_D2 }, { NN_D2, NN_PWR }, { NN_PWR, NN_D4 }, { NN_D4, NN_HIVE }, { NN_PWR, NN_D3 }, { NN_D3, NN_D1 }, { NN_D1, NN_SEC }, }; // packet flow path AIRLK -> DOOR2 -> PWR -> DOOR4 -> HIVE static const uint8_t PACKET_PATH[5] = { NN_AIRLK, NN_D2, NN_PWR, NN_D4, NN_HIVE }; static int nodePx(int i) { return NODES[i].tx * nd::TILE + nd::TILE / 2; } static int nodePy(int i) { return NODES[i].ty * nd::TILE + nd::TILE / 2; } // --------------------------------------------------------------------------- // Cursor (shared space; snaps to tiles in Cameras, nodes targeted in Network) // --------------------------------------------------------------------------- static int curX = 60, curY = 120; // Squad priority beacon (tile coords), starts at the airlock static int beaconTX = AIRLOCK_TX, beaconTY = AIRLOCK_TY; // transient UI feedback static uint32_t viewFlashUntil = 0; // frame flash after view switch static uint32_t slotFlash = 0xFF; // daemon slot flash id + expiry pair static uint32_t slotFlashUntil = 0; static int nodeFlash = -1; // clicked net node highlight static uint32_t nodeFlashUntil = 0; // --------------------------------------------------------------------------- // Trackball input — edge-counting ISRs + momentum model // Quadrant pins + center click, per UnitTest's mouse mapping: // G02 -> right, G01 -> up, G04 -> left, G03 -> down, BOOT -> click // --------------------------------------------------------------------------- static volatile uint32_t tbEdges[4]; static volatile uint32_t tbClicks; static void IRAM_ATTR tbIsrRight() { tbEdges[0]++; } static void IRAM_ATTR tbIsrUp() { tbEdges[1]++; } static void IRAM_ATTR tbIsrLeft() { tbEdges[2]++; } static void IRAM_ATTR tbIsrDown() { tbEdges[3]++; } static void IRAM_ATTR tbIsrClick() { tbClicks++; } static const int TB_FP = 8; // velocity fixed point #ifdef EMULATOR static const int TB_IMPULSE = 6; // emulated: rich edge rate static const int TB_FRICTION = 12; // velocity kept per frame (of 16) #else static const int TB_IMPULSE = 24; // device: fewer edges to move static const int TB_FRICTION = 15; #endif static const int TB_VMAX = 24 << TB_FP; // hard-flick cap static int32_t tbVelX, tbVelY; // momentum, 1/8 px per frame static int32_t tbRemX, tbRemY; // sub-pixel accumulators static void attachTrackball() { static const uint8_t tbPins[5] = { BOARD_TBOX_G02, BOARD_TBOX_G01, BOARD_TBOX_G04, BOARD_TBOX_G03, BOARD_BOOT_PIN }; for (int i = 0; i < 5; i++) pinMode(tbPins[i], INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(tbPins[0]), tbIsrRight, FALLING); attachInterrupt(digitalPinToInterrupt(tbPins[1]), tbIsrUp, FALLING); attachInterrupt(digitalPinToInterrupt(tbPins[2]), tbIsrLeft, FALLING); attachInterrupt(digitalPinToInterrupt(tbPins[3]), tbIsrDown, FALLING); attachInterrupt(digitalPinToInterrupt(tbPins[4]), tbIsrClick, FALLING); } static void resetTrackball() { for (int i = 0; i < 4; i++) tbEdges[i] = 0; tbClicks = 0; tbVelX = tbVelY = 0; tbRemX = tbRemY = 0; } static void clampCursor() { if (curX < 0) curX = 0; if (curX > nd::VIEW_W - 1) curX = nd::VIEW_W - 1; if (curY < 0) curY = 0; if (curY > nd::VIEW_H - 1) curY = nd::VIEW_H - 1; } // --------------------------------------------------------------------------- // View mode // --------------------------------------------------------------------------- static void toggleView() { viewMode = (viewMode == VM_CAMERAS) ? VM_NETWORK : VM_CAMERAS; viewFlashUntil = millis() + 350; Serial.printf("[key] view -> %s\n", viewMode == VM_CAMERAS ? "CAMERAS" : "NETWORK"); } // --------------------------------------------------------------------------- // Primary action (groundwork: M3 beacon placement, M2 node routing) // --------------------------------------------------------------------------- static void registerClick() { if (viewMode == VM_CAMERAS) { int tx = curX / nd::TILE, ty = curY / nd::TILE; char t = DEMO_MAP[ty][tx]; if (t != '#') { // beacons can't sit inside walls beaconTX = tx; beaconTY = ty; Serial.printf("[click] beacon -> tile %d,%d\n", tx, ty); } else { Serial.println("[click] wall tile: beacon ignored"); } } else { // nearest demo node flash (routing groundwork for Milestone 2) int best = -1, bestD = 1 << 30; for (int i = 0; i < 8; i++) { int dx = curX - nodePx(i); int dy = curY - nodePy(i); int d = dx * dx + dy * dy; if (d < bestD) { bestD = d; best = i; } } nodeFlash = best; nodeFlashUntil = millis() + 400; Serial.printf("[click] node %s targeted\n", NODES[best].label); } } // --------------------------------------------------------------------------- // Keyboard device @ I2C 0x55 // --------------------------------------------------------------------------- static void handleKey(char c) { switch (c) { case ' ': paused = !paused; Serial.printf("[key] tactical freeze %s\n", paused ? "ON" : "OFF"); break; case 'p': case '\t': // '\t' kept as legacy alias for the pane era toggleView(); break; case '1': case '2': case '3': case '4': slotFlash = c - '1'; slotFlashUntil = millis() + 400; Serial.printf("[key] arm daemon slot %d (M4 groundwork)\n", c - '1'); break; case 'x': Serial.println("[key] kill-switch sever (M2 groundwork)"); break; case 'r': Serial.println("[key] tactical retreat (M3 groundwork)"); beaconTX = AIRLOCK_TX; beaconTY = AIRLOCK_TY; break; case 'q': case 'e': Serial.printf("[key] cycle %s (M2 groundwork)\n", c == 'q' ? "back" : "fwd"); break; default: break; } } static void pollKeyboard() { static char lastKey = 0; Wire.requestFrom((uint8_t)0x55, (uint8_t)1); if (Wire.available() == 0) { lastKey = 0; // idle: arm the next press (same key can repeat) return; } char k = (char)Wire.read(); if (k == 0) { lastKey = 0; return; } if (k == lastKey) return; lastKey = k; handleKey((char)tolower(k)); } // --------------------------------------------------------------------------- // Touch (screen tap = move cursor + primary action) // --------------------------------------------------------------------------- static void pollTouch() { if (!touchAvail) return; static bool wasDown = false; bool down = touch.isPressed(); if (!down) { wasDown = false; return; } if (wasDown) return; // only act on new taps int16_t x[1], y[1]; if (touch.getPoint(x, y, 1) > 0) { wasDown = true; curX = x[0]; curY = y[0]; clampCursor(); registerClick(); } } // --------------------------------------------------------------------------- // Trackball -> cursor (momentum, capped; cursor stays live during the freeze // because planning happens while paused) // --------------------------------------------------------------------------- static void applyTrackball() { uint32_t e[4], clicks; for (int i = 0; i < 4; i++) { e[i] = tbEdges[i]; tbEdges[i] = 0; } clicks = tbClicks; tbClicks = 0; tbVelX += ((int32_t)e[0] - (int32_t)e[2]) * TB_IMPULSE; tbVelY += ((int32_t)e[3] - (int32_t)e[1]) * TB_IMPULSE; if (tbVelX > TB_VMAX) tbVelX = TB_VMAX; if (tbVelX < -TB_VMAX) tbVelX = -TB_VMAX; if (tbVelY > TB_VMAX) tbVelY = TB_VMAX; if (tbVelY < -TB_VMAX) tbVelY = -TB_VMAX; tbVelX = tbVelX * TB_FRICTION / 16; tbVelY = tbVelY * TB_FRICTION / 16; tbRemX += tbVelX; tbRemY += tbVelY; int pxX = tbRemX / TB_FP; int pxY = tbRemY / TB_FP; tbRemX -= pxX * TB_FP; tbRemY -= pxY * TB_FP; if (pxX || pxY) { curX += pxX; curY += pxY; clampCursor(); } if (clicks) registerClick(); } // --------------------------------------------------------------------------- // Simulation — deterministic 20 Hz ticks (grows into M2/M3 systems) // --------------------------------------------------------------------------- static void stepSim() { simTick++; } // --------------------------------------------------------------------------- // Rendering // --------------------------------------------------------------------------- // palette color of a map tile static uint16_t tileColor(char t) { switch (t) { case '#': return nd::C_WALL; case 'o': return nd::C_COVER; case '+': return nd::C_DOOR; case 'A': return nd::C_AIRLOCK; default: return nd::C_FLOOR; } } // ~35% brightness ghost of an RGB565 color (Network mode dims the ship view) static uint16_t dim565(uint16_t c) { uint32_t r = (((c >> 11) & 0x1F) * 22) >> 6; uint32_t g = (((c >> 5) & 0x3F) * 22) >> 6; uint32_t b = ((c & 0x1F) * 22) >> 6; return (uint16_t)((r << 11) | (g << 5) | b); } static void renderMap(bool dim) { for (int ty = 0; ty < nd::TILE_ROWS; ty++) { const char *row = DEMO_MAP[ty]; for (int tx = 0; tx < nd::TILE_COLS; tx++) { uint16_t c = tileColor(row[tx]); if (dim) c = dim565(c); GFXP->fillRect(tx * nd::TILE, ty * nd::TILE, nd::TILE, nd::TILE, c); if (row[tx] == 'A') { // airlock outline pops on the floor GFXP->drawRect(tx * nd::TILE, ty * nd::TILE, nd::TILE, nd::TILE, dim ? dim565(nd::C_FLOOR) : nd::C_FLOOR); } } } } static void renderNetwork(bool full) { // links: solid trace (Network) vs dotted ghost outline (Cameras) for (auto &l : LINKS) { int x0 = nodePx(l[0]), y0 = nodePy(l[0]); int x1 = nodePx(l[1]), y1 = nodePy(l[1]); if (full) { GFXP->drawLine(x0, y0, x1, y1, nd::C_LINK); } else { // dotted line: every other pixel of a Bresenham walk int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1; int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1; int err = dx + dy, step = 0; for (;;) { if ((step++ & 1) == 0) GFXP->drawPixel(x0, y0, nd::C_GHOST); if (x0 == x1 && y0 == y1) break; int e2 = 2 * err; if (e2 >= dy) { err += dy; x0 += sx; } if (e2 <= dx) { err += dx; y0 += sy; } } } } // data packets flowing along the packet path (freeze with the sim) int segs = 4, per = 36; int tt = (int)(simTick % (uint32_t)(segs * per)); int seg = tt / per, f = tt % per; int a = PACKET_PATH[seg], b = PACKET_PATH[seg + 1]; int px = nodePx(a) + (nodePx(b) - nodePx(a)) * f / per; int py = nodePy(a) + (nodePy(b) - nodePy(a)) * f / per; GFXP->fillCircle(px, py, full ? 2 : 1, nd::C_PACKET); if (full) GFXP->drawPixel(px, py, TFT_WHITE); // nodes: filled icons + labels (Network) vs hollow outlines (Cameras) for (int i = 0; i < 8; i++) { int nx = nodePx(i), ny = nodePy(i); bool flashing = (nodeFlash == i && (int32_t)(millis() - nodeFlashUntil) < 0); if (full) { GFXP->fillCircle(nx, ny, 6, flashing ? nd::C_CURSOR : NODES[i].color); GFXP->drawCircle(nx, ny, 6, TFT_BLACK); GFXP->setTextDatum(TC_DATUM); GFXP->setTextColor(nd::C_TEXTDIM); GFXP->drawString(NODES[i].label, nx, ny + 9); } else { GFXP->drawCircle(nx, ny, 6, flashing ? nd::C_CURSOR : nd::C_GHOST); } } if (full) { GFXP->setTextDatum(TL_DATUM); GFXP->setTextColor(nd::C_TEXTDIM); GFXP->drawString("NETWORK", 4, 4); } } static void renderBeacon() { // squad priority beacon (cyan diamond at the beacon tile center) int bx = beaconTX * nd::TILE + nd::TILE / 2; int by = beaconTY * nd::TILE + nd::TILE / 2; uint16_t c = (viewMode == VM_NETWORK) ? dim565(nd::C_BEACON) : nd::C_BEACON; GFXP->fillTriangle(bx, by - 3, bx + 3, by, bx, by + 3, c); GFXP->fillTriangle(bx, by - 3, bx - 3, by, bx, by + 3, c); } static void meterBar(int x, int y, int w, int h, int pct, uint16_t c) { GFXP->drawRect(x, y, w, h, nd::C_BORDER); int fw = (w - 2) * pct / 100; if (fw > 0) GFXP->fillRect(x + 1, y + 1, fw, h - 2, c); } static void renderHud() { GFXP->fillRect(nd::HUD_X, nd::HUD_Y, nd::HUD_W, nd::HUD_H, nd::C_HUDBG); GFXP->drawFastHLine(nd::HUD_X, nd::HUD_Y, nd::HUD_W, nd::C_BORDER); // 4 exploit daemon slots: keycap, name, cooldown bar static const char *const NAMES[4] = { "FRENZY", "BLIND", "OVRCLK", "PATCH" }; static const int COOL[4] = { 30, 55, 80, 45 }; for (int i = 0; i < 4; i++) { int x = 6 + i * 58, y = nd::HUD_Y + 6; bool flashing = (slotFlash == (uint32_t)i && (int32_t)(millis() - slotFlashUntil) < 0); uint16_t tc = flashing ? nd::C_CURSOR : nd::C_TEXT; GFXP->setTextColor(tc); GFXP->setTextDatum(TL_DATUM); GFXP->drawString(String("[") + char('1' + i) + "]", x, y); GFXP->setTextColor(nd::C_TEXTDIM); GFXP->drawString(NAMES[i], x + 18, y); meterBar(x, y + 14, 48, 6, COOL[i], nd::C_GOOD); } // RAM + thermal load GFXP->setTextColor(nd::C_TEXT); GFXP->drawString("RAM 4/6", 246, nd::HUD_Y + 6); meterBar(246, nd::HUD_Y + 16, 56, 7, 42, nd::C_WARN); GFXP->setTextColor(nd::C_TEXTDIM); GFXP->drawString("TEMP", 246, nd::HUD_Y + 25); // pause hint / state GFXP->setTextDatum(TR_DATUM); GFXP->setTextColor(paused ? nd::C_FREEZE : nd::C_TEXT); GFXP->drawString("SPACE", nd::HUD_X + nd::HUD_W - 6, nd::HUD_Y + 6); GFXP->setTextColor(paused ? nd::C_FREEZE : nd::C_TEXTDIM); GFXP->drawString(paused ? "RESUME" : "PAUSE", nd::HUD_X + nd::HUD_W - 6, nd::HUD_Y + 16); GFXP->setTextDatum(TL_DATUM); // view mode indicator GFXP->setTextColor(nd::C_FREEZE); GFXP->setTextDatum(TR_DATUM); GFXP->drawString(viewMode == VM_CAMERAS ? "VIEW: CAMERAS [P]" : "VIEW: NETWORK [P]", nd::HUD_X + nd::HUD_W - 6, nd::HUD_Y + 32); GFXP->setTextDatum(TL_DATUM); // squad health pips GFXP->setTextColor(nd::C_TEXTDIM); GFXP->drawString("SQUAD", 6, nd::HUD_Y + 30); static const int HP[3] = { 4, 5, 2 }; // of 5 (M3 groundwork) for (int i = 0; i < 3; i++) { int x = 48 + i * 58; for (int s = 0; s < 5; s++) { uint16_t c = s < HP[i] ? nd::C_GOOD : nd::C_BORDER; GFXP->fillRect(x + s * 8, nd::HUD_Y + 34, 6, 8, c); } GFXP->setTextColor(nd::C_TEXTDIM); GFXP->drawString(String("M") + String(i + 1), x - 10, nd::HUD_Y + 30); } } static void renderCursor() { uint16_t c = (viewMode == VM_NETWORK) ? nd::C_PACKET : nd::C_CURSOR; if (viewMode == VM_CAMERAS) { // tile snap highlight + crosshair int tx = curX / nd::TILE, ty = curY / nd::TILE; GFXP->drawRect(tx * nd::TILE, ty * nd::TILE, nd::TILE, nd::TILE, c); GFXP->drawCircle(curX, curY, 3, c); GFXP->drawPixel(curX, curY, c); } else { GFXP->drawCircle(curX, curY, 4, c); GFXP->drawCircle(curX, curY, 1, c); } } // CRT freeze tint: scanlines over the view + status text (DESIGN.md §4) static void renderFreezeTint() { for (int y = 0; y < nd::VIEW_H; y += 3) { GFXP->drawFastHLine(0, y, nd::VIEW_W, 0x0000); } GFXP->setTextDatum(TR_DATUM); GFXP->setTextColor(nd::C_FREEZE, 0x0000); if ((millis() / 500) & 1) { GFXP->drawString("* TACTICAL FREEZE *", nd::VIEW_W - 4, 4); } GFXP->setTextDatum(TL_DATUM); } static void render() { renderMap(viewMode == VM_NETWORK); renderNetwork(viewMode == VM_NETWORK); renderBeacon(); renderHud(); // HUD separator (flashes when the view mode switches) uint16_t bc = (int32_t)(millis() - viewFlashUntil) < 0 ? nd::C_CURSOR : nd::C_BORDER; GFXP->drawFastHLine(nd::HUD_X, nd::HUD_Y - 1, nd::HUD_W, bc); if ((int32_t)(millis() - viewFlashUntil) < 0) { GFXP->drawRect(0, 0, nd::VIEW_W, nd::VIEW_H, nd::C_CURSOR); } renderCursor(); if (paused) renderFreezeTint(); } // --------------------------------------------------------------------------- // Boot screen // --------------------------------------------------------------------------- static void drawBoot() { GFXP->fillScreen(TFT_BLACK); GFXP->setFreeFont(&FreeSansBold18pt7b); GFXP->setTextDatum(MC_DATUM); GFXP->setTextColor(nd::C_FREEZE); GFXP->drawString("NETDECK", 160, 74); GFXP->setTextColor(nd::C_CURSOR); GFXP->drawString("BREACH & BOARD", 160, 108); GFXP->setTextFont(2); GFXP->setTextColor(nd::C_TEXTDIM); GFXP->drawString("M1 // SANDBOX", 160, 136); GFXP->drawString("p: cameras/network space: freeze", 160, 158); GFXP->drawString("ball: cursor enter: select", 160, 174); GFXP->setTextDatum(TL_DATUM); GFXP->setTextFont(1); } // --------------------------------------------------------------------------- // Setup / loop // --------------------------------------------------------------------------- static void hardwareInit() { Serial.begin(115200); Serial.println("NetDeck M1 — hardware sandbox"); pinMode(BOARD_POWERON, OUTPUT); // power rail first (battery boot) digitalWrite(BOARD_POWERON, HIGH); // CS lines high during init 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); tft.fillScreen(TFT_BLACK); pinMode(BOARD_BL_PIN, OUTPUT); digitalWrite(BOARD_BL_PIN, HIGH); Wire.begin(BOARD_I2C_SDA, BOARD_I2C_SCL); touch.setPins(-1, BOARD_TOUCH_INT); touchAvail = touch.begin(Wire); if (touchAvail) { touch.setMaxCoordinates(tft.width(), tft.height()); touch.setSwapXY(true); touch.setMirrorXY(false, true); } attachTrackball(); randomSeed(esp_random()); } void setup() { hardwareInit(); #ifndef EMULATOR // second buffer for flicker-free full-frame pushes fb.setColorDepth(16); if (!fb.createSprite(tft.width(), tft.height())) Serial.println("NetDeck: framebuffer alloc failed (blank screen)"); #endif drawBoot(); stateT0 = millis(); lastFrameMs = stateT0; } void loop() { uint32_t now = millis(); if (gameState == ST_BOOT) { if (now - stateT0 >= nd::BOOT_MS) { gameState = ST_RUN; stateT0 = now; // swallow stale keyboard bytes + trackball edges from floating pins for (int i = 0; i < 8; i++) { if (Wire.requestFrom((uint8_t)0x55, (uint8_t)16) == 0) break; } resetTrackball(); Serial.println("[boot] entering sandbox (view: cameras)"); } delay(1); return; } // frame pacing ~30 fps static uint32_t nextFrame = 0; if ((int32_t)(now - nextFrame) < 0) { delay(1); return; } nextFrame = now + nd::FRAME_MS; uint32_t dt = now - lastFrameMs; lastFrameMs = now; if (dt > 200) dt = 200; // clamp hiccups (debug stalls, USB pauses) applyTrackball(); pollTouch(); pollKeyboard(); // fixed-timestep simulation (DESIGN.md §6) if (!paused) { simAcc += dt; while (simAcc >= nd::SIM_TICK_MS) { stepSim(); simAcc -= nd::SIM_TICK_MS; } } render(); present(); }