Drive radar blips from the T-Deck Plus dual microphones
Add a SoundDetector that reads the ES7210 as a 4-slot TDM stream (slot 0 = MIC1/left, slot 2 = MIC3/right), tracks a per-channel RMS envelope with an adaptive noise floor, and publishes a stereo balance plus a dB-mapped loudness. The radar sketch now slides a single reused blip left/right from the balance and moves it nearer as the loudness rises, instead of firing a hardcoded demo blip. The device's own ping/click mutes the detector for 200 ms so it cannot hear itself. The trackball stays detached: GPIO0 is unavailable while the ES7210 microphones are running.
This commit is contained in:
@@ -31,6 +31,7 @@
|
||||
#include "utilities.h"
|
||||
#include "TouchDrvGT911.hpp"
|
||||
#include "sound_engine.h"
|
||||
#include "sound_detector.h"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Hardware
|
||||
@@ -38,6 +39,7 @@
|
||||
TFT_eSPI tft;
|
||||
TouchDrvGT911 touch;
|
||||
SoundEngine sound;
|
||||
SoundDetector soundDetector;
|
||||
Preferences prefs;
|
||||
|
||||
static bool touchAvail = false;
|
||||
@@ -391,6 +393,8 @@ static void printPrompt()
|
||||
// ---------------------------------------------------------------------------
|
||||
static void sndBlip()
|
||||
{
|
||||
// Keep the device's own radar ping out of the microphone detector.
|
||||
soundDetector.suppress(200);
|
||||
SndEvent e = { SND_TONE, 540, 0, 10, 8000 };
|
||||
sound.play(e);
|
||||
delay(15);
|
||||
@@ -398,6 +402,7 @@ static void sndBlip()
|
||||
|
||||
static void sndClick()
|
||||
{
|
||||
soundDetector.suppress(200);
|
||||
SndEvent e = { SND_TONE, 660, 0, 152, 5000 };
|
||||
sound.play(e);
|
||||
}
|
||||
@@ -624,24 +629,43 @@ void initRadar() {
|
||||
}
|
||||
}
|
||||
|
||||
// Call this when an audio trigger or target is detected
|
||||
// lrBalance: -1.0 (far left) to +1.0 (far right)
|
||||
// loudness: mapped to distance (higher volume = closer to origin)
|
||||
void triggerBlip(float lrBalance, float loudness) {
|
||||
// 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;
|
||||
|
||||
static int allocBlip() {
|
||||
for (int i = 0; i < MAX_BLIPS; i++) {
|
||||
if (!blips[i].active) {
|
||||
blips[i].active = true;
|
||||
// Map L/R balance to forward-facing wedge (-45 deg to +45 deg)
|
||||
blips[i].angle = constrain(lrBalance, -1.0f, 1.0f) * (M_PI / 4.0f);
|
||||
// Loudness maps to distance: loudest is near origin, quiet is near edge
|
||||
blips[i].distance = constrain(1.0f - (loudness / 100.0f), 0.15f, 0.95f);
|
||||
blips[i].alpha = 255;
|
||||
break;
|
||||
}
|
||||
if (!blips[i].active) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// lrBalance: -1.0 (far left) to +1.0 (far right)
|
||||
// loudness: 0.0 (quiet/far) to 1.0 (loud/near)
|
||||
static void placeBlip(int i, float lrBalance, float loudness) {
|
||||
if (i < 0) return;
|
||||
blips[i].active = true;
|
||||
// Map L/R balance to forward-facing wedge (-45 deg to +45 deg)
|
||||
blips[i].angle = constrain(lrBalance, -1.0f, 1.0f) * (M_PI / 4.0f);
|
||||
// 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;
|
||||
}
|
||||
|
||||
// One-shot blip (manual trigger / future events)
|
||||
void triggerBlip(float lrBalance, float loudness) {
|
||||
placeBlip(allocBlip(), lrBalance, loudness);
|
||||
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));
|
||||
@@ -687,10 +711,13 @@ void radarUpdate() {
|
||||
sndBlip();
|
||||
} else if (sweepAngle < MAX_RADIUS) {
|
||||
drawArc(sweepAngle, C_GLOW);
|
||||
}
|
||||
|
||||
if (sweepAngle > 109 && sweepAngle < 111) {
|
||||
triggerBlip(0.2, 50);
|
||||
}
|
||||
// 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
|
||||
@@ -1006,7 +1033,11 @@ static void hardwareInit()
|
||||
Serial.println("No GT911 touch found");
|
||||
}
|
||||
|
||||
attachTrackball();
|
||||
// NOTE: GPIO0 (trackball centre / BOOT) is unavailable while the ES7210
|
||||
// microphones are running, and leaving its interrupt enabled causes
|
||||
// spurious captures. Keep it detached; the microphone radar does not need
|
||||
// the trackball.
|
||||
// attachTrackball();
|
||||
}
|
||||
|
||||
static void bootSequence()
|
||||
@@ -1044,6 +1075,12 @@ void setup()
|
||||
bootSequence();
|
||||
|
||||
initRadar();
|
||||
|
||||
if (!soundDetector.begin(16000)) {
|
||||
Serial.println("Sound detector init failed (ES7210 mics)");
|
||||
} else {
|
||||
Serial.println("Sound detector ready (MIC1 left / MIC3 right)");
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
|
||||
Reference in New Issue
Block a user