Files
Dejvino 7dfdc06e6f Add NoiseSensor radar example and fix short sound playback
The sound task used to zero the I2S TX DMA ring as soon as the event
queue drained. i2s_write() only queues samples, so anything shorter than
the ~64 ms ring was erased before it reached the speaker. Enable
tx_desc_auto_clear instead, which clears each descriptor after the DMA
has actually transmitted it.

Also add the NoiseSensor radar example and point the default PlatformIO
environment at it.
2026-11-12 08:14:19 +01:00

56 lines
1.7 KiB
C++

/**
* @file sound_engine.h
* @brief Tiny non-blocking I2S sound engine for the T-Deck speaker.
*
* A FreeRTOS task owns the I2S peripheral (speaker, I2S_NUM_0) and renders
* queued sound events one after another. The game only ever pushes events,
* so it never blocks. Event types:
*
* SND_TONE fixed square-wave beep
* SND_SWEEP square wave gliding from freq down/up to freq2 (sirens, booms)
* SND_NOISE white-noise burst with linear decay (explosions)
*/
#pragma once
#include <Arduino.h>
enum SndType : uint8_t {
SND_TONE = 0, // fixed square wave
SND_SWEEP, // square wave gliding freq -> freq2
SND_NOISE, // white noise burst
};
struct SndEvent {
SndType type;
uint16_t freq; // Hz (tone, or sweep start)
uint16_t freq2; // Hz (sweep end; ignored for tone/noise)
uint16_t dur; // duration in ms
uint16_t amp; // peak amplitude 0..32767
};
class SoundEngine {
public:
static const uint8_t VOL_MAX = 8;
bool begin();
void play(const SndEvent &e);
// Master volume, applied to every rendered sound
void volumeUp();
void volumeDown();
void toggleMute();
void setVolume(uint8_t v); // restore a saved level (0 = muted)
uint8_t volume() const { return vol_; }
private:
static void trampoline(void *arg);
void task();
void renderTone(uint16_t freq, uint16_t durMs, uint16_t amp);
void renderSweep(uint16_t freq0, uint16_t freq1, uint16_t durMs, uint16_t amp);
void renderNoise(uint16_t durMs, uint16_t amp);
QueueHandle_t q_ = nullptr;
uint8_t vol_ = 5; // default: moderate
uint8_t lastVol_ = 5; // remembered for mute toggle
};