M314-style radar: dual-mic blips, sweep reveal, fade lifecycle

This commit is contained in:
Dejvino
2026-11-12 20:04:40 +01:00
parent 315cdbd177
commit 25ad8aa17c
3 changed files with 135 additions and 109 deletions
+88 -46
View File
@@ -37,6 +37,7 @@
// Hardware
// ---------------------------------------------------------------------------
TFT_eSPI tft;
TFT_eSprite spr(&tft);
TouchDrvGT911 touch;
SoundEngine sound;
SoundDetector soundDetector;
@@ -351,7 +352,7 @@ static void newline()
scrollUp();
}
cursorDirty = true;
sndBlip();
//sndBlip();
}
static void putChar(char ch)
@@ -362,7 +363,7 @@ static void putChar(char ch)
if (ch < 32 || ch > 126) ch = '?';
grid[curRow][curCol] = ch;
renderCell(curCol, curRow, 0, fxOn && bandRows[curRow]);
sndBlip();
//sndBlip();
curCol++;
if (curCol >= COLS) newline();
}
@@ -393,6 +394,7 @@ static void printPrompt()
// ---------------------------------------------------------------------------
static void sndBlip()
{
return;
// Keep the device's own radar ping out of the microphone detector.
soundDetector.suppress(200);
SndEvent e = { SND_TONE, 540, 0, 10, 8000 };
@@ -402,6 +404,7 @@ static void sndBlip()
static void sndClick()
{
return;
soundDetector.suppress(200);
SndEvent e = { SND_TONE, 660, 0, 152, 5000 };
sound.play(e);
@@ -611,15 +614,18 @@ struct RadarBlip {
float distance; // Normalized 0.0 (near) to 1.0 (far boundary)
uint8_t alpha; // Decay intensity (0 - 255)
bool active;
int cycle; // sweep cycle in which it was placed
bool revealed; // true once the sweep has revealed it
};
#define RADAR_CX 160
#define RADAR_CY 120
#define MAX_BLIPS 5
#define MAX_BLIPS 30
RadarBlip blips[MAX_BLIPS];
// Sweep line parameters (sweeps back and forth across the front wedge)
float sweepAngle = -M_PI / 4.0; // Start at -45 degrees (left)
int sweepCycle = 0;
int sweepDirection = 1; // 1 = sweeping right, -1 = sweeping left
const float SWEEP_SPEED = 0.04; // Adjust sweep rate
@@ -629,9 +635,9 @@ void initRadar() {
}
}
// Index of the blip currently driven by the microphone. Reused every frame so
// a continuous sound slides around instead of filling all MAX_BLIPS slots.
static int liveBlip = -1;
// Rotating index into the blip array: each sweep completion writes the next
// slot, keeping up to MAX_BLIPS recent positions.
static int nextBlip = 0;
static int allocBlip() {
for (int i = 0; i < MAX_BLIPS; i++) {
@@ -650,36 +656,40 @@ static void placeBlip(int i, float lrBalance, float loudness) {
// Loudness maps to distance: loudest is near origin, quiet is near edge
blips[i].distance = constrain(1.0f - loudness, 0.15f, 0.95f);
blips[i].alpha = 255;
blips[i].cycle = sweepCycle;
blips[i].revealed = false;
}
// One-shot blip (manual trigger / future events)
void triggerBlip(float lrBalance, float loudness) {
placeBlip(allocBlip(), lrBalance, loudness);
sndClick();
//sndClick();
}
// Continuous update from the dual-microphone detector.
static void updateSoundBlip(float lrBalance, float loudness) {
if (liveBlip < 0 || !blips[liveBlip].active) {
liveBlip = allocBlip();
}
placeBlip(liveBlip, lrBalance, loudness);
}
static void drawArc(float r, uint32_t color) {
for (float a = -M_PI / 4.0; a <= M_PI / 4.0; a += 0.05) {
int ax = RADAR_ORIGIN_X + (int)(r * sin(a));
int ay = RADAR_ORIGIN_Y - (int)(r * cos(a));
tft.drawPixel(ax, ay, color);
spr.drawPixel(ax, ay, color);
}
}
void radarUpdate() {
spr.fillSprite(C_BG);
spr.drawString("M314 MOTION TRACKER", 8, 2, 2);
// 1. Draw Forward-Facing Sector Grid (Distance Arcs)
// 50m, 100m, 150m, 200m equivalent scale lines
for (int r = 50; r <= MAX_RADIUS; r += 50) {
// Draw arc segments within the -45 to +45 degree wedge
drawArc(r, C_DIM_GRN);
// Range readout at the right edge of the arc.
int rx = RADAR_ORIGIN_X + (int)(r * sin(M_PI / 4.0f));
int ry = RADAR_ORIGIN_Y - (int)(r * cos(M_PI / 4.0f));
char buf[8];
snprintf(buf, sizeof(buf), "%d", r);
spr.drawString(buf, rx + 2, ry - 6, 1);
}
// 2. Draw Vector Boundary Lines (-45°, 0° Center, +45°)
@@ -688,62 +698,94 @@ void radarUpdate() {
int xRight = RADAR_ORIGIN_X + (int)(MAX_RADIUS * sin(M_PI / 4.0));
int yRight = RADAR_ORIGIN_Y - (int)(MAX_RADIUS * cos(M_PI / 4.0));
tft.drawLine(RADAR_ORIGIN_X, RADAR_ORIGIN_Y, xLeft, yLeft, C_DIM_GRN);
tft.drawLine(RADAR_ORIGIN_X, RADAR_ORIGIN_Y, xRight, yRight, C_DIM_GRN);
tft.drawLine(RADAR_ORIGIN_X, RADAR_ORIGIN_Y, RADAR_ORIGIN_X, RADAR_ORIGIN_Y - MAX_RADIUS, C_DIM_GRN); // Center line
spr.drawLine(RADAR_ORIGIN_X, RADAR_ORIGIN_Y, xLeft, yLeft, C_DIM_GRN);
spr.drawLine(RADAR_ORIGIN_X, RADAR_ORIGIN_Y, xRight, yRight, C_DIM_GRN);
spr.drawLine(RADAR_ORIGIN_X, RADAR_ORIGIN_Y, RADAR_ORIGIN_X, RADAR_ORIGIN_Y - MAX_RADIUS, C_DIM_GRN); // Center line
// 3. Update & Draw Oscillating Forward Sweep Line
/*sweepAngle += SWEEP_SPEED * sweepDirection;
if (sweepAngle >= M_PI / 4.0) {
sweepAngle = M_PI / 4.0;
sweepDirection = -1; // Reverse sweep direction
} else if (sweepAngle <= -M_PI / 4.0) {
sweepAngle = -M_PI / 4.0;
sweepDirection = 1;
// 3. Oscillating sweep across the wedge. Read the mics each frame to know
// the target position; place the blip when the sweep passes it.
SoundReading snd = soundDetector.read();
float lv = snd.slots[2];
float rv = snd.slots[3];
if (lv < 0.0f) lv = 0.0f;
if (rv < 0.0f) rv = 0.0f;
// Side bars showing each channel's level.
int barH = 120;
int lh = (int)(lv * 10.0f * barH);
if (lh > barH) lh = barH;
int rh = (int)(rv * 10.0f * barH);
if (rh > barH) rh = barH;
spr.fillRect(2, SCR_H - 10 - barH, 4, barH, C_BG);
spr.fillRect(2, SCR_H - 10 - lh, 4, lh, C_GLOW);
spr.fillRect(SCR_W - 6, SCR_H - 10 - barH, 4, barH, C_BG);
spr.fillRect(SCR_W - 6, SCR_H - 10 - rh, 4, rh, C_GLOW);
float total = lv + rv;
float threshold = 0.0005f;
float targetBalance = 0.0f;
float targetLoud = 0.0f;
bool hasTarget = (total > threshold);
if (hasTarget) {
targetBalance = (rv - lv) / total;
float stronger = (lv > rv) ? lv : rv;
targetLoud = stronger * 10.0f;
if (targetLoud > 1.0f) targetLoud = 1.0f;
}
int sx = RADAR_ORIGIN_X + (int)(MAX_RADIUS * sin(sweepAngle));
int sy = RADAR_ORIGIN_Y - (int)(MAX_RADIUS * cos(sweepAngle));
tft.drawLine(RADAR_ORIGIN_X, RADAR_ORIGIN_Y, sx, sy, C_GLOW);*/
sweepAngle += 10.0f;//SWEEP_SPEED;
// Place blips immediately when sound is detected, with a cooldown so a
// sustained sound doesn't flood all slots.
static uint32_t lastPlaceMs = 0;
if (hasTarget && (millis() - lastPlaceMs > 100)) {
placeBlip(nextBlip, targetBalance, targetLoud);
nextBlip = (nextBlip + 1) % MAX_BLIPS;
lastPlaceMs = millis();
}
// Arc sweep from center to outside. Place the blip when a full sweep
// completes (reaches MAX_RADIUS), using the latest detected target.
sweepAngle += 10.0f;
if (sweepAngle > MAX_RADIUS * 2) {
sweepAngle = 0;
sweepCycle++;
sndBlip();
} else if (sweepAngle < MAX_RADIUS) {
drawArc(sweepAngle, C_GLOW);
}
// Feed the live microphone measurement into the blip layer. Balance moves
// it left/right; loudness pulls it toward the centre (closer target).
SoundReading snd = soundDetector.read();
if (snd.active) {
updateSoundBlip(snd.balance, snd.loudness);
}
// 4. Render Active Target Blips with Phosphor Decay
for (int i = 0; i < MAX_BLIPS; i++) {
if (blips[i].active) {
float r = blips[i].distance * MAX_RADIUS;
// Reveal condition: placed in an earlier sweep and sweep passed it.
if (!blips[i].revealed) {
if (blips[i].cycle >= sweepCycle) continue;
if (r > sweepAngle) continue;
blips[i].revealed = true;
}
// 0 angle = straight up (forward), negative = left, positive = right
int bx = RADAR_ORIGIN_X + (int)(r * sin(blips[i].angle));
int by = RADAR_ORIGIN_Y - (int)(r * cos(blips[i].angle));
// Render blip core and outer glow
if (blips[i].alpha > 128) {
tft.fillCircle(bx, by, 4, C_WHITE);
tft.drawCircle(bx, by, 7, C_GLOW);
spr.fillCircle(bx, by, 4, C_WHITE);
spr.drawCircle(bx, by, 7, C_GLOW);
} else {
tft.drawCircle(bx, by, 4, C_GLOW);
spr.drawCircle(bx, by, 4, C_GLOW);
}
// Decay blip intensity over time
if (blips[i].alpha > 15) {
blips[i].alpha -= 15;
// Decay blip intensity over time (own lifecycle)
if (blips[i].alpha > 3) {
blips[i].alpha -= 3;
} else {
blips[i].active = false;
blips[i].revealed = false;
}
}
}
spr.pushSprite(0, 0);
}
// ---------------------------------------------------------------------------
@@ -1012,6 +1054,7 @@ static void hardwareInit()
tft.begin();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
spr.createSprite(SCR_W, SCR_H);
pinMode(BOARD_BL_PIN, OUTPUT);
for (int i = 0; i <= 16; ++i) {
@@ -1043,8 +1086,8 @@ static void hardwareInit()
static void bootSequence()
{
clearScreen();
SndEvent e = { SND_SWEEP, 300, 900, 320, 10000 };
sound.play(e);
//SndEvent e = { SND_SWEEP, 300, 900, 320, 10000 };
//sound.play(e);
bigPrintCentered("T-DECK", Y0 + 6 * CELL_H);
bigPrintCentered("MOTION SENSOR", Y0 + 11 * CELL_H);
delay(1600);
@@ -1106,7 +1149,6 @@ void loop()
pollTouch();
pollKeyboard();
clearScreen();
//fxUpdate();
radarUpdate();