NetDeck M0: emulator sandbox + design doc
- src/netdeck: DESIGN.md (RTwP cyber-tactical ship crawler for T-Deck), STATUS.md milestone tracker, nd_layout.h shared constants - examples/NetDeck: M0 sandbox — 40x24 ship floorplan and 8-node sub-net in one shared coordinate space (door nodes pinned to bulkhead door tiles); Cameras/Network view modes via P (network overlaid as dotted outlines vs dimmed ship + full graph); trackball cursor (edge ISRs + momentum); Space tactical freeze with CRT scanlines; 20 Hz fixed-step sim (pause-verified frame-identical); keyboard @ I2C 0x55 groundwork for M2-M4 - emulator: APP=NetDeck support + Tab key host mapping - platformio.ini: NetDeck src_dir ready for firmware build
This commit is contained in:
@@ -0,0 +1,677 @@
|
||||
/**
|
||||
* @file NetDeck.ino
|
||||
* @brief NetDeck — Breach & Board: Milestone 0 emulator sandbox.
|
||||
*
|
||||
* Milestone 0 (DESIGN.md §7): the Milestone-1 hardware sandbox prepared and
|
||||
* tested locally, without hardware. This sketch exercises every tech piece
|
||||
* the real build needs, on the SDL2 emulator first:
|
||||
*
|
||||
* - 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 <Arduino.h>
|
||||
#include <SPI.h>
|
||||
#include <TFT_eSPI.h>
|
||||
#include <Wire.h>
|
||||
#include "utilities.h"
|
||||
#include "TouchDrvGT911.hpp"
|
||||
#include "../../src/netdeck/nd_layout.h"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Hardware
|
||||
// ---------------------------------------------------------------------------
|
||||
TFT_eSPI tft;
|
||||
TouchDrvGT911 touch;
|
||||
static bool touchAvail = false;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 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);
|
||||
tft.fillRect(tx * nd::TILE, ty * nd::TILE, nd::TILE, nd::TILE, c);
|
||||
if (row[tx] == 'A') { // airlock outline pops on the floor
|
||||
tft.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) {
|
||||
tft.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) tft.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;
|
||||
tft.fillCircle(px, py, full ? 2 : 1, nd::C_PACKET);
|
||||
if (full) tft.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) {
|
||||
tft.fillCircle(nx, ny, 6, flashing ? nd::C_CURSOR : NODES[i].color);
|
||||
tft.drawCircle(nx, ny, 6, TFT_BLACK);
|
||||
tft.setTextDatum(TC_DATUM);
|
||||
tft.setTextColor(nd::C_TEXTDIM);
|
||||
tft.drawString(NODES[i].label, nx, ny + 9);
|
||||
} else {
|
||||
tft.drawCircle(nx, ny, 6, flashing ? nd::C_CURSOR : nd::C_GHOST);
|
||||
}
|
||||
}
|
||||
|
||||
if (full) {
|
||||
tft.setTextDatum(TL_DATUM);
|
||||
tft.setTextColor(nd::C_TEXTDIM);
|
||||
tft.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;
|
||||
tft.fillTriangle(bx, by - 3, bx + 3, by, bx, by + 3, c);
|
||||
tft.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)
|
||||
{
|
||||
tft.drawRect(x, y, w, h, nd::C_BORDER);
|
||||
int fw = (w - 2) * pct / 100;
|
||||
if (fw > 0) tft.fillRect(x + 1, y + 1, fw, h - 2, c);
|
||||
}
|
||||
|
||||
static void renderHud()
|
||||
{
|
||||
tft.fillRect(nd::HUD_X, nd::HUD_Y, nd::HUD_W, nd::HUD_H, nd::C_HUDBG);
|
||||
tft.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;
|
||||
tft.setTextColor(tc);
|
||||
tft.setTextDatum(TL_DATUM);
|
||||
tft.drawString(String("[") + char('1' + i) + "]", x, y);
|
||||
tft.setTextColor(nd::C_TEXTDIM);
|
||||
tft.drawString(NAMES[i], x + 18, y);
|
||||
meterBar(x, y + 14, 48, 6, COOL[i], nd::C_GOOD);
|
||||
}
|
||||
|
||||
// RAM + thermal load
|
||||
tft.setTextColor(nd::C_TEXT);
|
||||
tft.drawString("RAM 4/6", 246, nd::HUD_Y + 6);
|
||||
meterBar(246, nd::HUD_Y + 16, 56, 7, 42, nd::C_WARN);
|
||||
tft.setTextColor(nd::C_TEXTDIM);
|
||||
tft.drawString("TEMP", 246, nd::HUD_Y + 25);
|
||||
|
||||
// pause hint / state
|
||||
tft.setTextDatum(TR_DATUM);
|
||||
tft.setTextColor(paused ? nd::C_FREEZE : nd::C_TEXT);
|
||||
tft.drawString("SPACE", nd::HUD_X + nd::HUD_W - 6, nd::HUD_Y + 6);
|
||||
tft.setTextColor(paused ? nd::C_FREEZE : nd::C_TEXTDIM);
|
||||
tft.drawString(paused ? "RESUME" : "PAUSE", nd::HUD_X + nd::HUD_W - 6, nd::HUD_Y + 16);
|
||||
tft.setTextDatum(TL_DATUM);
|
||||
|
||||
// view mode indicator
|
||||
tft.setTextColor(nd::C_FREEZE);
|
||||
tft.setTextDatum(TR_DATUM);
|
||||
tft.drawString(viewMode == VM_CAMERAS ? "VIEW: CAMERAS [P]" : "VIEW: NETWORK [P]",
|
||||
nd::HUD_X + nd::HUD_W - 6, nd::HUD_Y + 32);
|
||||
tft.setTextDatum(TL_DATUM);
|
||||
|
||||
// squad health pips
|
||||
tft.setTextColor(nd::C_TEXTDIM);
|
||||
tft.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;
|
||||
tft.fillRect(x + s * 8, nd::HUD_Y + 34, 6, 8, c);
|
||||
}
|
||||
tft.setTextColor(nd::C_TEXTDIM);
|
||||
tft.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;
|
||||
tft.drawRect(tx * nd::TILE, ty * nd::TILE, nd::TILE, nd::TILE, c);
|
||||
tft.drawCircle(curX, curY, 3, c);
|
||||
tft.drawPixel(curX, curY, c);
|
||||
} else {
|
||||
tft.drawCircle(curX, curY, 4, c);
|
||||
tft.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) {
|
||||
tft.drawFastHLine(0, y, nd::VIEW_W, 0x0000);
|
||||
}
|
||||
tft.setTextDatum(TR_DATUM);
|
||||
tft.setTextColor(nd::C_FREEZE, 0x0000);
|
||||
if ((millis() / 500) & 1) {
|
||||
tft.drawString("* TACTICAL FREEZE *", nd::VIEW_W - 4, 4);
|
||||
}
|
||||
tft.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;
|
||||
tft.drawFastHLine(nd::HUD_X, nd::HUD_Y - 1, nd::HUD_W, bc);
|
||||
if ((int32_t)(millis() - viewFlashUntil) < 0) {
|
||||
tft.drawRect(0, 0, nd::VIEW_W, nd::VIEW_H, nd::C_CURSOR);
|
||||
}
|
||||
|
||||
renderCursor();
|
||||
if (paused) renderFreezeTint();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Boot screen
|
||||
// ---------------------------------------------------------------------------
|
||||
static void drawBoot()
|
||||
{
|
||||
tft.fillScreen(TFT_BLACK);
|
||||
tft.setFreeFont(&FreeSansBold18pt7b);
|
||||
tft.setTextDatum(MC_DATUM);
|
||||
tft.setTextColor(nd::C_FREEZE);
|
||||
tft.drawString("NETDECK", 160, 74);
|
||||
tft.setTextColor(nd::C_CURSOR);
|
||||
tft.drawString("BREACH & BOARD", 160, 108);
|
||||
tft.setTextFont(2);
|
||||
tft.setTextColor(nd::C_TEXTDIM);
|
||||
tft.drawString("M0 // EMULATOR SANDBOX", 160, 136);
|
||||
tft.drawString("p: cameras/network space: freeze", 160, 158);
|
||||
tft.drawString("ball: cursor enter: select", 160, 174);
|
||||
tft.setTextDatum(TL_DATUM);
|
||||
tft.setTextFont(1);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Setup / loop
|
||||
// ---------------------------------------------------------------------------
|
||||
static void hardwareInit()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("NetDeck M0 — emulator 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();
|
||||
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;
|
||||
resetTrackball(); // swallow stale edges from floating pins
|
||||
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();
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* @file utilities.h
|
||||
* @author Lewis He (lewishe@outlook.com)
|
||||
* @license MIT
|
||||
* @copyright Copyright (c) 2023 Shenzhen Xin Yuan Electronic Technology Co., Ltd
|
||||
* @date 2023-04-11
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
|
||||
|
||||
//! The board peripheral power control pin needs to be set to HIGH when using the peripheral
|
||||
#define BOARD_POWERON 10
|
||||
|
||||
#define BOARD_I2S_WS 5
|
||||
#define BOARD_I2S_BCK 7
|
||||
#define BOARD_I2S_DOUT 6
|
||||
|
||||
#define BOARD_I2C_SDA 18
|
||||
#define BOARD_I2C_SCL 8
|
||||
|
||||
#define BOARD_BAT_ADC 4
|
||||
|
||||
#define BOARD_TOUCH_INT 16
|
||||
#define BOARD_KEYBOARD_INT 46
|
||||
|
||||
#define BOARD_SDCARD_CS 39
|
||||
#define BOARD_TFT_CS 12
|
||||
#define RADIO_CS_PIN 9
|
||||
|
||||
#define BOARD_TFT_DC 11
|
||||
#define BOARD_TFT_BACKLIGHT 42
|
||||
|
||||
#define BOARD_SPI_MOSI 41
|
||||
#define BOARD_SPI_MISO 38
|
||||
#define BOARD_SPI_SCK 40
|
||||
|
||||
#define BOARD_TBOX_G02 2
|
||||
#define BOARD_TBOX_G01 3
|
||||
#define BOARD_TBOX_G04 1
|
||||
#define BOARD_TBOX_G03 15
|
||||
|
||||
#define BOARD_ES7210_MCLK 48
|
||||
#define BOARD_ES7210_LRCK 21
|
||||
#define BOARD_ES7210_SCK 47
|
||||
#define BOARD_ES7210_DIN 14
|
||||
|
||||
#define RADIO_BUSY_PIN 13
|
||||
#define RADIO_RST_PIN 17
|
||||
#define RADIO_DIO1_PIN 45
|
||||
|
||||
#define BOARD_BOOT_PIN 0
|
||||
|
||||
#define BOARD_BL_PIN 42
|
||||
|
||||
|
||||
#define BOARD_GPS_TX_PIN 43
|
||||
#define BOARD_GPS_RX_PIN 44
|
||||
|
||||
|
||||
#ifndef RADIO_FREQ
|
||||
#define RADIO_FREQ 868.0
|
||||
#endif
|
||||
|
||||
#ifndef RADIO_BANDWIDTH
|
||||
#define RADIO_BANDWIDTH 125.0
|
||||
#endif
|
||||
|
||||
#ifndef RADIO_SF
|
||||
#define RADIO_SF 10
|
||||
#endif
|
||||
|
||||
#ifndef RADIO_CR
|
||||
#define RADIO_CR 6
|
||||
#endif
|
||||
|
||||
#ifndef RADIO_TX_POWER
|
||||
#define RADIO_TX_POWER 22
|
||||
#endif
|
||||
|
||||
#define DEFAULT_OPA 100
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user