Introduce the Speaker class to play audio from the SD card when detecting tags. The only audio supported is WAV. Audio playback is handled via interrupts, so it might try to read from the SD card at the same time as the LCD class is trying to read new frames. Update the LCD animation code to temporarily disable audio interrupts while reading from the SD card.
49 lines
1.1 KiB
C++
49 lines
1.1 KiB
C++
//
|
|
// Created by Teo-CD on 22/10/23.
|
|
//
|
|
|
|
#include "Speaker.h"
|
|
|
|
DMAMEM audio_block_t Speaker::audioMemory[audioMemReservation];
|
|
bool Speaker::memoryInitialized = false;
|
|
|
|
void Speaker::init() {
|
|
if (memoryInitialized)
|
|
return;
|
|
AudioStream::initialize_memory(audioMemory, audioMemReservation);
|
|
memoryInitialized = true;
|
|
}
|
|
|
|
void Speaker::playNewSound(int8_t ID) {
|
|
char filePath[20];
|
|
uint8_t rawID = getRawId(ID);
|
|
|
|
if (!SD.mediaPresent()) {
|
|
Com::sendComment("SD Card not present, cannot play sound");
|
|
return;
|
|
}
|
|
if (!isValidId(ID)) {
|
|
Com::sendComment("Unknown ID for sound : %d", rawID);
|
|
return;
|
|
}
|
|
|
|
if (sdWavSource.isPlaying())
|
|
sdWavSource.stop();
|
|
|
|
snprintf(filePath, 20, "%s/ENTRY.WAV",
|
|
(isIdCharacter(ID) ? charDirs : promoDirs)[rawID]);
|
|
|
|
if (!sd.exists(filePath)) {
|
|
Com::sendComment("File %s not present, cannot play sound.", filePath);
|
|
return;
|
|
}
|
|
digitalWrite(pin_Audio_Amp_enable, HIGH);
|
|
sdWavSource.play(filePath);
|
|
}
|
|
|
|
bool Speaker::checkPlayingAndDisable() {
|
|
bool isPlaying = sdWavSource.isPlaying();
|
|
if (!isPlaying)
|
|
digitalWrite(pin_Audio_Amp_enable, LOW);
|
|
return isPlaying;
|
|
}
|